Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- class UnusedContentException extends Exception {
- public UnusedContentException(String message){
- super(message);
- }
- }
- class TagFormatter {
- public String prepareTagSingle(Tag tag){
- if (tag.getContent() != ""){
- return prepareTagBlock(tag, tag.getContent());
- } else {
- return "<" + tag.getName() + prepareAttributes(tag) + " />";
- }
- }
- public String prepareTagBlock(Tag tag, String mid){
- if (tag.getName().length() > 0){
- String str = "";
- str += ("<" + tag.getName() + prepareAttributes(tag) + ">");
- str += mid;
- str += ("</" + tag.getName() + ">");
- return str;
- } else {
- return mid;
- }
- }
- private String prepareAttributes(Tag tag){
- String str = "";
- HashMap<String, String> hm = tag.getAttributes();
- for(String key : hm.keySet()){
- str += " " + key + "=\"" + hm.get(key) + "\"";
- }
- str += new StyleFormatter().prepareStyleSingle(tag.getStyle());
- return str;
- }
- }
- class StyleFormatter {
- public String prepareStyleSingle(Style style){
- HashMap<String, String> hm = style.getProperties();
- if(hm.size() > 0){
- String str = " style=\"";
- for(String key : hm.keySet()){
- str += key + ": " + hm.get(key) + "; ";
- }
- return str.substring(0, str.length()-2) + "\"";
- } else {
- return "";
- }
- }
- public String prepareStyleBlock(ArrayList<Style> styles){
- String str = "";
- for(int i = 0; i < styles.size(); ++i){
- HashMap<String, String> hm = styles.get(i).getProperties();
- if(hm.size() > 0){
- str += " " + styles.get(i).getTarget() + " {\n";
- for(String key : hm.keySet()){
- str += " " + key + ": " + hm.get(key) + ";\n";
- }
- str += " " + "}\n";
- }
- }
- return str;
- }
- }
- interface Aligner extends Cloneable {
- abstract public String alignTagSingle(String mid);
- abstract public String alignTagBlock(String mid);
- abstract public Aligner inc();
- public Aligner clone();
- }
- class LazyAligner implements Aligner {
- public LazyAligner(){
- }
- public Aligner clone(){
- try {
- return (Aligner)super.clone();
- } catch(CloneNotSupportedException cnse){
- return null;
- }
- }
- public LazyAligner inc(){
- return this;
- }
- public String alignTagSingle(String mid){
- return mid;
- }
- public String alignTagBlock(String mid){
- return mid;
- }
- }
- class BusyAligner implements Aligner {
- private int depth;
- private boolean isBlock;
- public BusyAligner(int depth, boolean isBlock){
- this.depth = depth;
- this.isBlock = isBlock;
- }
- public Aligner clone(){
- try {
- return (Aligner)super.clone();
- } catch(CloneNotSupportedException cnse){
- return null;
- }
- }
- public BusyAligner inc(){
- depth = depth + 1;
- return this;
- }
- public int getDepth(){
- return depth;
- }
- public boolean getIsBlock(){
- return isBlock;
- }
- private String getPrefix(){
- String prefix = "";
- for(int j = depth; j > 0; --j){
- prefix += " ";
- }
- return prefix;
- }
- public String alignTagSingle(String mid){
- isBlock = false;
- return alignTagBlock(mid);
- }
- public String alignTagBlock(String mid){
- String str = "\n";
- String prefix = getPrefix();
- str += prefix;
- int index = mid.lastIndexOf("</");
- if ((index > -1) && (isBlock))
- str += mid.substring(0, index) + prefix + mid.substring(index);
- else
- str += mid;
- str += "\n";
- return str.replace("\n\n", "\n");
- }
- }
- final class Tag {
- private String name;
- private String content;
- private ArrayList<Tag> elements = new ArrayList<>();
- private HashMap<String, String> attributes = new HashMap<>();
- private ArrayList<Style> styles = new ArrayList<>();
- public Tag(String name){
- this(name, "");
- }
- public Tag(String name, String content){
- this.name = name;
- this.content = content;
- styles.add(new Style(""));
- }
- public Tag(Tag other){
- this(other.name, other.content);
- this.elements = new ArrayList<>(other.elements);
- this.attributes = new HashMap<>(other.attributes);
- this.styles = new ArrayList<Style>(other.styles);
- }
- public String getName(){
- return name;
- }
- public String getContent(){
- return content;
- }
- public Style getStyle(){
- return styles.get(0);
- }
- public Tag getElement(int i){
- return elements.get(i);
- }
- public ArrayList<Tag> getElements(){
- return elements;
- }
- public String getAttribute(String key){
- return attributes.get(key);
- }
- public HashMap<String, String> getAttributes(){
- return attributes;
- }
- public Tag add(String name){
- Tag temp = new Tag(name);
- elements.add(temp);
- return temp;
- }
- public Tag add(Tag inner){
- Tag temp = new Tag(inner);
- elements.add(temp);
- return temp;
- }
- public void put(String key, String value){
- attributes.put(key, value);
- }
- }
- final class Style {
- private String target;
- private HashMap<String, String> properties = new HashMap<>();
- public Style(String target){
- this.target = target;
- }
- public Style(Style other){
- this(other.target);
- this.properties = new HashMap<>(other.properties);
- }
- public String getTarget(){
- return target;
- }
- public String getProperty(String key){
- return properties.get(key);
- }
- public HashMap<String, String> getProperties(){
- return properties;
- }
- public void put(String key, String value){
- properties.put(key, value);
- }
- }
- class TagExporter {
- private String export(Page.Head head, int depth) throws UnusedContentException {
- String str = " <head>";
- for(Tag tag : head.getElements()){
- str += export(tag, 2);
- }
- if (head.getStyles().size() > 0){
- str += " <style>\n";
- str += new StyleFormatter().prepareStyleBlock(head.getStyles());
- str += " </style>\n";
- }
- return str + " </head>";
- }
- public String export(Page page) throws UnusedContentException {
- String str = "<!DOCTYPE html>\n";
- str += "<html>\n";
- str += export(page.getHead(), 1);
- str += export(page.getBody(), 1);
- str += "</html>\n";
- return str;
- }
- public String export(Tag tag) throws UnusedContentException {
- return export(tag, new LazyAligner());
- }
- public String export(Tag tag, int depth) throws UnusedContentException {
- return export(tag, new BusyAligner(depth, true));
- }
- private String export(Tag tag, Aligner a) throws UnusedContentException {
- if (tag.getElements().size() == 0){
- return a.alignTagSingle(new TagFormatter().prepareTagSingle(tag));
- } else if (tag.getContent() == ""){
- String mid = "";
- for(int i = 0; i < tag.getElements().size(); ++i){
- mid += export(tag.getElement(i), a.clone().inc());
- }
- return a.alignTagBlock(new TagFormatter().prepareTagBlock(tag, mid));
- } else {
- throw new UnusedContentException("Unused content!");
- }
- }
- }
- class Page {
- private Head head = new Head();
- private Tag body = new Tag("body");
- private HashMap<String, String> attributes = new HashMap<>();
- public String getAttribute(String key){
- return attributes.get(key);
- }
- public HashMap<String, String> getAttributes(){
- return attributes;
- }
- public void put(String key, String value){
- attributes.put(key, value);
- }
- public Tag getBody(){
- return body;
- }
- public Head getHead(){
- return head;
- }
- class Head {
- private ArrayList<Tag> elements = new ArrayList<>();
- private ArrayList<Style> styles = new ArrayList<>();
- public ArrayList<Style> getStyles(){
- return styles;
- }
- public Tag getElement(int i){
- return elements.get(i);
- }
- public ArrayList<Tag> getElements(){
- return elements;
- }
- public Tag add(String name){
- Tag temp = new Tag(name);
- elements.add(temp);
- return temp;
- }
- public Tag add(Tag inner){
- Tag temp = new Tag(inner);
- elements.add(temp);
- return temp;
- }
- public Style add(Style inner){
- Style temp = new Style(inner);
- styles.add(temp);
- return temp;
- }
- }
- }
- public class Sandbox {
- public static void main(String args[]) {
- try {
- TagExporter te = new TagExporter();
- Page page = new Page();
- page.put("lang", "en");
- Page.Head myHead = page.getHead();
- myHead.add(new Tag("title", "Hello world!"));
- Style s1 = new Style("a:link, a:visited");
- s1.put("text-decoration", "none");
- s1.put("color", "black");
- myHead.add(s1);
- Style s2 = new Style("a:hover, a:active");
- s2.put("text-decoration", "underline");
- s2.put("color", "black");
- myHead.add(s2);
- Tag myBody = page.getBody();
- Tag myH1 = myBody.add(new Tag("h1", "My heading"));
- myH1.getStyle().put("color", "red");
- myH1.put("class", "heading");
- Tag myP = myBody.add(new Tag("p", "Lorem ipsum"));
- myP.put("id", "my-p");
- myP.getStyle().put("border", "1px solid black");
- myP.getStyle().put("background-color", "green");
- Tag myPar = myBody.add(new Tag("p", ""));
- myPar.add(new Tag("", "My paragraph"));
- Tag myLink = new Tag("a");
- myLink.put("id", "my-link");
- myLink.put("style", "font-weight: bold; text-decoration: none");
- Tag myImg = myLink.add(new Tag("img", ""));
- myImg.put("id", "my-img");
- myImg.put("class", "my-images");
- Tag myTxt = myLink.add(new Tag("", "My link"));
- Tag myDiv1 = new Tag("div");
- myDiv1.put("class", "my-div");
- Tag myDiv2 = myDiv1.add(new Tag("div"));
- Tag myDiv3 = myDiv2.add(new Tag("div"));
- Tag myDiv4 = myDiv3.add(new Tag("div", "Tekstas tekstas " + te.export(myLink) + " tekstas tekstas"));
- Tag myDiv5 = myDiv3.add(new Tag("div", ""));
- myDiv5.add(new Tag("", "Tekstas tekstas "));
- myDiv5.add(myLink);
- myDiv5.add(new Tag("", "tekstas tekstas"));
- myBody.add(myDiv1);
- System.out.println(te.export(page));
- FileWriter fw = new FileWriter("index.htm");
- fw.write(te.export(page));
- fw.close();
- } catch(Exception e){
- e.printStackTrace();
- System.out.println("Unexpected error, sorry!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement