Advertisement
irmantas_radavicius

Untitled

Apr 9th, 2022
625
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.01 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. class UnusedContentException extends Exception {
  5.     public UnusedContentException(String message){
  6.         super(message);
  7.     }
  8. }
  9.  
  10. class TagFormatter {
  11.     public String prepareTagSingle(Tag tag){
  12.         if (tag.getContent() != ""){
  13.             return prepareTagBlock(tag, tag.getContent());
  14.         } else {
  15.             return "<" + tag.getName() + prepareAttributes(tag) + " />";
  16.         }      
  17.     }  
  18.     public String prepareTagBlock(Tag tag, String mid){
  19.         if (tag.getName().length() > 0){
  20.             String str = "";
  21.             str += ("<" + tag.getName() + prepareAttributes(tag) + ">");
  22.             str += mid;
  23.             str += ("</" + tag.getName() + ">");
  24.             return str;        
  25.         } else {
  26.             return mid;
  27.         }
  28.     }  
  29.    
  30.     private String prepareAttributes(Tag tag){
  31.         String str = "";               
  32.         HashMap<String, String> hm = tag.getAttributes();
  33.         for(String key : hm.keySet()){
  34.             str += " " + key + "=\"" + hm.get(key) + "\"";
  35.         }
  36.         str += new StyleFormatter().prepareStyleSingle(tag.getStyle());
  37.         return str;
  38.     }
  39. }
  40.  
  41. class StyleFormatter { 
  42.     public String prepareStyleSingle(Style style){         
  43.         HashMap<String, String> hm = style.getProperties();
  44.         if(hm.size() > 0){
  45.             String str = " style=\"";
  46.             for(String key : hm.keySet()){
  47.                 str += key + ": " + hm.get(key) + "; ";
  48.             }
  49.             return str.substring(0, str.length()-2) + "\"";
  50.         } else {
  51.             return "";
  52.         }
  53.     }
  54.    
  55.     public String prepareStyleBlock(ArrayList<Style> styles){
  56.         String str = "";
  57.         for(int i = 0; i < styles.size(); ++i){            
  58.             HashMap<String, String> hm = styles.get(i).getProperties();
  59.             if(hm.size() > 0){
  60.                 str += "            " + styles.get(i).getTarget() + " {\n";
  61.                 for(String key : hm.keySet()){
  62.                     str += "                " + key + ": " + hm.get(key) + ";\n";
  63.                 }
  64.                 str += "            " + "}\n";
  65.             }
  66.         }      
  67.         return str;
  68.     }
  69. }
  70.  
  71. interface Aligner extends Cloneable {      
  72.     abstract public String alignTagSingle(String mid);
  73.     abstract public String alignTagBlock(String mid);
  74.     abstract public Aligner inc();
  75.     public Aligner clone();
  76.    
  77. }
  78.  
  79. class LazyAligner implements Aligner {
  80.     public LazyAligner(){      
  81.     }
  82.     public Aligner clone(){
  83.         try {
  84.             return (Aligner)super.clone();
  85.         } catch(CloneNotSupportedException cnse){
  86.             return null;
  87.         }      
  88.     }  
  89.     public LazyAligner inc(){
  90.         return this;
  91.     }
  92.     public String alignTagSingle(String mid){
  93.         return mid;
  94.     }
  95.     public String alignTagBlock(String mid){
  96.         return mid;
  97.     }
  98. }
  99.  
  100. class BusyAligner implements Aligner {
  101.     private int depth;
  102.     private boolean isBlock;   
  103.    
  104.     public BusyAligner(int depth, boolean isBlock){
  105.         this.depth = depth;
  106.         this.isBlock = isBlock;
  107.     }
  108.     public Aligner clone(){
  109.         try {
  110.             return (Aligner)super.clone();
  111.         } catch(CloneNotSupportedException cnse){
  112.             return null;
  113.         }      
  114.     }
  115.     public BusyAligner inc(){
  116.         depth = depth + 1;
  117.         return this;
  118.     }
  119.     public int getDepth(){
  120.         return depth;
  121.     }  
  122.     public boolean getIsBlock(){
  123.         return isBlock;
  124.     }      
  125.     private String getPrefix(){
  126.         String prefix = "";
  127.         for(int j = depth; j > 0; --j){
  128.             prefix += "    ";
  129.         }      
  130.         return prefix;
  131.     }      
  132.     public String alignTagSingle(String mid){
  133.         isBlock = false;
  134.         return alignTagBlock(mid);
  135.     }
  136.     public String alignTagBlock(String mid){       
  137.         String str = "\n";     
  138.         String prefix = getPrefix();
  139.         str += prefix;
  140.         int index = mid.lastIndexOf("</");
  141.         if ((index > -1) && (isBlock))
  142.             str += mid.substring(0, index) + prefix + mid.substring(index);
  143.         else
  144.             str += mid;
  145.         str += "\n";
  146.         return str.replace("\n\n", "\n");
  147.     }
  148. }
  149.  
  150. final class Tag {  
  151.     private String name;
  152.     private String content;
  153.     private ArrayList<Tag> elements = new ArrayList<>();
  154.     private HashMap<String, String> attributes = new HashMap<>();
  155.     private ArrayList<Style> styles = new ArrayList<>();
  156.    
  157.     public Tag(String name){
  158.         this(name, "");
  159.     }
  160.     public Tag(String name, String content){
  161.         this.name = name;
  162.         this.content = content;
  163.         styles.add(new Style(""));
  164.     }
  165.     public Tag(Tag other){
  166.         this(other.name, other.content);       
  167.         this.elements = new ArrayList<>(other.elements);
  168.         this.attributes = new HashMap<>(other.attributes);
  169.         this.styles = new ArrayList<Style>(other.styles);
  170.     }  
  171.     public String getName(){
  172.         return name;
  173.     }
  174.     public String getContent(){
  175.         return content;
  176.     }      
  177.     public Style getStyle(){
  178.         return styles.get(0);
  179.     }
  180.  
  181.     public Tag getElement(int i){
  182.         return elements.get(i);
  183.     }
  184.     public ArrayList<Tag> getElements(){
  185.         return elements;
  186.     }
  187.     public String getAttribute(String key){
  188.         return attributes.get(key);
  189.     }
  190.     public HashMap<String, String> getAttributes(){
  191.         return attributes;
  192.     }      
  193.     public Tag add(String name){
  194.         Tag temp = new Tag(name);
  195.         elements.add(temp);
  196.         return temp;
  197.     }
  198.     public Tag add(Tag inner){     
  199.         Tag temp = new Tag(inner);
  200.         elements.add(temp);
  201.         return temp;
  202.     }
  203.     public void put(String key, String value){     
  204.         attributes.put(key, value);    
  205.     }
  206.  
  207. }
  208.  
  209.  
  210. final class Style {
  211.     private String target; 
  212.     private HashMap<String, String> properties = new HashMap<>();
  213.    
  214.     public Style(String target){
  215.         this.target = target;
  216.     }
  217.     public Style(Style other){
  218.         this(other.target);    
  219.         this.properties = new HashMap<>(other.properties);
  220.     }  
  221.     public String getTarget(){
  222.         return target;
  223.     }  
  224.     public String getProperty(String key){
  225.         return properties.get(key);
  226.     }
  227.     public HashMap<String, String> getProperties(){
  228.         return properties;
  229.     }
  230.  
  231.     public void put(String key, String value){     
  232.         properties.put(key, value);    
  233.     }
  234.    
  235.        
  236. }
  237.  
  238. class TagExporter {
  239.     private String export(Page.Head head, int depth) throws UnusedContentException {
  240.         String str = "    <head>";
  241.         for(Tag tag : head.getElements()){
  242.             str += export(tag, 2);
  243.         }      
  244.         if (head.getStyles().size() > 0){
  245.             str += "        <style>\n";
  246.             str += new StyleFormatter().prepareStyleBlock(head.getStyles());           
  247.             str += "        </style>\n";
  248.         }      
  249.         return str + "    </head>";
  250.     }
  251.     public String export(Page page) throws UnusedContentException {
  252.         String str = "<!DOCTYPE html>\n";
  253.         str += "<html>\n";
  254.         str += export(page.getHead(), 1);
  255.         str += export(page.getBody(), 1);
  256.         str += "</html>\n";
  257.         return str;
  258.     }
  259.     public String export(Tag tag) throws UnusedContentException {
  260.         return export(tag, new LazyAligner());
  261.     }
  262.     public String export(Tag tag, int depth) throws UnusedContentException {
  263.         return export(tag, new BusyAligner(depth, true));
  264.     }  
  265.     private String export(Tag tag, Aligner a) throws UnusedContentException {
  266.         if (tag.getElements().size() == 0){    
  267.             return a.alignTagSingle(new TagFormatter().prepareTagSingle(tag));
  268.         } else if (tag.getContent() == ""){
  269.             String mid = "";
  270.             for(int i = 0; i < tag.getElements().size(); ++i){                             
  271.                 mid += export(tag.getElement(i), a.clone().inc());
  272.             }      
  273.             return a.alignTagBlock(new TagFormatter().prepareTagBlock(tag, mid));
  274.         } else {
  275.             throw new UnusedContentException("Unused content!");
  276.         }      
  277.     }  
  278. }
  279.  
  280. class Page {   
  281.     private Head head = new Head();
  282.     private Tag body = new Tag("body");
  283.     private HashMap<String, String> attributes = new HashMap<>();
  284.        
  285.     public String getAttribute(String key){
  286.         return attributes.get(key);
  287.     }
  288.     public HashMap<String, String> getAttributes(){
  289.         return attributes;
  290.     }          
  291.     public void put(String key, String value){     
  292.         attributes.put(key, value);    
  293.     }
  294.     public Tag getBody(){
  295.         return body;
  296.     }
  297.     public Head getHead(){
  298.         return head;
  299.     }
  300.  
  301.     class Head {               
  302.         private ArrayList<Tag> elements = new ArrayList<>();
  303.         private ArrayList<Style> styles = new ArrayList<>();
  304.        
  305.         public ArrayList<Style> getStyles(){
  306.             return styles;
  307.         }
  308.         public Tag getElement(int i){
  309.             return elements.get(i);
  310.         }
  311.         public ArrayList<Tag> getElements(){
  312.             return elements;
  313.         }              
  314.         public Tag add(String name){
  315.             Tag temp = new Tag(name);
  316.             elements.add(temp);
  317.             return temp;
  318.         }
  319.         public Tag add(Tag inner){     
  320.             Tag temp = new Tag(inner);
  321.             elements.add(temp);
  322.             return temp;
  323.         }              
  324.         public Style add(Style inner){     
  325.             Style temp = new Style(inner);
  326.             styles.add(temp);
  327.             return temp;
  328.         }  
  329.     }
  330.    
  331.  
  332.    
  333. }
  334.  
  335.  
  336. public class Sandbox {     
  337.     public static void main(String args[]) {           
  338.         try {                      
  339.             TagExporter te = new TagExporter();
  340.            
  341.            
  342.             Page page = new Page();
  343.             page.put("lang", "en");
  344.            
  345.             Page.Head myHead = page.getHead();
  346.             myHead.add(new Tag("title", "Hello world!"));
  347.            
  348.             Style s1 = new Style("a:link, a:visited");
  349.             s1.put("text-decoration", "none");
  350.             s1.put("color", "black");
  351.             myHead.add(s1);
  352.             Style s2 = new Style("a:hover, a:active");
  353.             s2.put("text-decoration", "underline");
  354.             s2.put("color", "black");          
  355.             myHead.add(s2);
  356.            
  357.            
  358.             Tag myBody = page.getBody();
  359.             Tag myH1 = myBody.add(new Tag("h1", "My heading"));
  360.             myH1.getStyle().put("color", "red");
  361.             myH1.put("class", "heading");
  362.            
  363.             Tag myP = myBody.add(new Tag("p", "Lorem ipsum")); 
  364.             myP.put("id", "my-p");
  365.             myP.getStyle().put("border", "1px solid black");
  366.             myP.getStyle().put("background-color", "green");           
  367.            
  368.             Tag myPar = myBody.add(new Tag("p", ""));
  369.             myPar.add(new Tag("", "My paragraph"));
  370.            
  371.             Tag myLink = new Tag("a");
  372.             myLink.put("id", "my-link");
  373.             myLink.put("style", "font-weight: bold; text-decoration: none");
  374.             Tag myImg = myLink.add(new Tag("img", ""));
  375.             myImg.put("id", "my-img");
  376.             myImg.put("class", "my-images");
  377.             Tag myTxt = myLink.add(new Tag("", "My link"));
  378.            
  379.             Tag myDiv1 = new Tag("div");
  380.             myDiv1.put("class", "my-div");
  381.             Tag myDiv2 = myDiv1.add(new Tag("div"));
  382.             Tag myDiv3 = myDiv2.add(new Tag("div"));
  383.             Tag myDiv4 = myDiv3.add(new Tag("div", "Tekstas tekstas " + te.export(myLink) + " tekstas tekstas"));                                  
  384.            
  385.             Tag myDiv5 = myDiv3.add(new Tag("div", ""));
  386.             myDiv5.add(new Tag("", "Tekstas tekstas "));
  387.             myDiv5.add(myLink);
  388.             myDiv5.add(new Tag("", "tekstas tekstas"));
  389.            
  390.             myBody.add(myDiv1);        
  391.            
  392.             System.out.println(te.export(page));
  393.            
  394.            
  395.            
  396.             FileWriter fw = new FileWriter("index.htm");
  397.             fw.write(te.export(page));
  398.             fw.close();
  399.            
  400.            
  401.         } catch(Exception e){
  402.             e.printStackTrace();           
  403.             System.out.println("Unexpected error, sorry!");
  404.         }          
  405.     }  
  406. }
  407.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement