Advertisement
Guest User

Untitled

a guest
Nov 27th, 2014
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.00 KB | None | 0 0
  1. public class Runner1 {
  2.  
  3.         public static void main(String argv[]) {
  4.                 Tag t = new Tag("HTML");
  5.                 Tag b = new Tag("BODY");
  6.                 t.add(b);
  7.  
  8.                 Tag h = new Tag("H1");
  9.                 b.add(h);
  10.        
  11.                 Tag p = new Tag("p");
  12.                 b.add(p);
  13.                 TextTag a1= new TextTag("alamakota");
  14.                 TextTag a2 = new TextTag("hello");
  15.                
  16.                 h.add(a2);
  17.                 p.add(a1);
  18.                 t.prettyPrint();
  19.  
  20.         }      
  21.  
  22.  
  23.  
  24. }
  25. public class Tag {
  26.         public static final String TAB = "    ";              
  27.  
  28.         private Tag [] children = new Tag[5];
  29.         private int countChildren = 0;
  30.  
  31.         protected String name;
  32.  
  33.         public Tag(String name) {
  34.                 this.name=name;
  35.         }
  36.  
  37.         protected String wciecie(int glebokosc) {
  38.                 String wciecie="";
  39.                 for (int i = 0; i < glebokosc; i++)
  40.                         wciecie+=TAB;
  41.                 return wciecie;
  42.         }
  43.  
  44.         public void add(Tag t) {
  45.                 children[countChildren++]=t;
  46.         }
  47.  
  48.         public void prettyPrint() {
  49.                 this.prettyPrint(0);
  50.         }
  51.  
  52.         public void prettyPrint(int glebokosc) {
  53.                 String wciecie = this.wciecie(glebokosc);
  54.                 System.out.println(wciecie+"<"+name+">");
  55.                 for (int i =0 ; i < countChildren; i++)
  56.                         children[i].prettyPrint(glebokosc+1);
  57.                 System.out.println(wciecie+"</"+name+">");
  58.         }
  59.  
  60.        
  61. }
  62.  
  63.  
  64. public class TextTag extends Tag {
  65.         public TextTag(String name) {
  66.                 super(name);
  67.         }
  68.  
  69.         public void prettyPrint() {
  70.                 this.prettyPrint(0);
  71.         }
  72.  
  73.         public void prettyPrint(int glebokosc) {
  74.                 String wciecie = this.wciecie(glebokosc);
  75.                 System.out.println(wciecie+name);
  76.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement