1466
|
1 package goodjava.xml;
|
|
2
|
|
3 import java.util.Map;
|
|
4
|
|
5
|
|
6 public final class XmlElement {
|
|
7 public final String name;
|
|
8 public final Map<String,String> attributes;
|
1790
|
9 private Object content = null;
|
1791
|
10 private XmlElement parent = null;
|
1466
|
11
|
1468
|
12 public XmlElement(String name,Map<String,String> attributes) {
|
|
13 this.name = name;
|
|
14 this.attributes = attributes;
|
|
15 this.content = null;
|
|
16 }
|
|
17
|
1791
|
18 public XmlElement getParent() {
|
|
19 return parent;
|
|
20 }
|
|
21
|
1790
|
22 public Object getContent() {
|
|
23 return content;
|
|
24 }
|
|
25
|
|
26 public void removeContent() {
|
|
27 content = null;
|
|
28 }
|
|
29
|
|
30 public void setContent(String content) {
|
1468
|
31 if( content == null )
|
1810
|
32 throw new IllegalArgumentException("content of '"+name+"' can't be null");
|
1466
|
33 this.content = content;
|
|
34 }
|
|
35
|
1790
|
36 public void setContent(XmlElement[] content) {
|
1468
|
37 if( content == null )
|
1810
|
38 throw new IllegalArgumentException("content of '"+name+"' can't be null");
|
1468
|
39 if( content.length == 0 )
|
1810
|
40 throw new IllegalArgumentException("content of '"+name+"' can't be empty");
|
1466
|
41 this.content = content;
|
1791
|
42 for( XmlElement el : content ) {
|
|
43 el.parent = this;
|
|
44 }
|
1466
|
45 }
|
|
46
|
1790
|
47 @Override public String toString() {
|
1466
|
48 StringBuilder sb = new StringBuilder();
|
|
49 toString(sb,0);
|
|
50 return sb.toString();
|
|
51 }
|
|
52
|
1790
|
53 void toString(StringBuilder sb,int indented) {
|
1466
|
54 indent(sb,indented);
|
|
55 sb.append( '<' );
|
|
56 sb.append( name );
|
|
57 for( Map.Entry<String,String> attribute : attributes.entrySet() ) {
|
|
58 sb.append( ' ' );
|
|
59 sb.append( attribute.getKey() );
|
|
60 sb.append( "=\"" );
|
1793
|
61 sb.append( encodeAttr(attribute.getValue()) );
|
1466
|
62 sb.append( '"' );
|
|
63 }
|
1468
|
64 if( content == null ) {
|
|
65 sb.append( "/>\n" );
|
|
66 } else if( content instanceof String ) {
|
|
67 sb.append( '>' );
|
1466
|
68 String s = (String)content;
|
1468
|
69 sb.append( encode(s) );
|
|
70 closeTag(sb,name);
|
1467
|
71 } else {
|
1468
|
72 sb.append( '>' );
|
1466
|
73 XmlElement[] elements = (XmlElement[])content;
|
|
74 sb.append( '\n' );
|
|
75 for( XmlElement element : elements ) {
|
|
76 element.toString(sb,indented+1);
|
|
77 }
|
|
78 indent(sb,indented);
|
1468
|
79 closeTag(sb,name);
|
1467
|
80 }
|
1468
|
81 }
|
|
82
|
|
83 private static void closeTag(StringBuilder sb,String name) {
|
1466
|
84 sb.append( "</" );
|
|
85 sb.append( name );
|
|
86 sb.append( ">\n" );
|
|
87 }
|
|
88
|
1467
|
89 private static void indent(StringBuilder sb,int indented) {
|
1466
|
90 for( int i=0; i<indented; i++ ) {
|
|
91 sb.append('\t');
|
|
92 }
|
|
93 }
|
|
94
|
1469
|
95 public static String encode(String s) {
|
1468
|
96 final char[] a = s.toCharArray();
|
|
97 StringBuilder buf = new StringBuilder();
|
1469
|
98 for( char c : a ) {
|
1468
|
99 switch(c) {
|
|
100 case '&':
|
|
101 buf.append("&");
|
|
102 break;
|
|
103 case '<':
|
|
104 buf.append("<");
|
|
105 break;
|
|
106 case '>':
|
|
107 buf.append(">");
|
|
108 break;
|
|
109 case '"':
|
|
110 buf.append(""");
|
|
111 break;
|
1469
|
112 case '\'':
|
|
113 buf.append("'");
|
|
114 break;
|
1793
|
115 default:
|
|
116 buf.append(c);
|
|
117 }
|
|
118 }
|
|
119 return buf.toString();
|
|
120 }
|
|
121
|
|
122 public static String encodeAttr(String s) {
|
|
123 final char[] a = s.toCharArray();
|
|
124 StringBuilder buf = new StringBuilder();
|
|
125 for( char c : a ) {
|
|
126 switch(c) {
|
|
127 case '&':
|
|
128 buf.append("&");
|
|
129 break;
|
|
130 case '<':
|
|
131 buf.append("<");
|
|
132 break;
|
|
133 case '>':
|
|
134 buf.append(">");
|
|
135 break;
|
|
136 case '"':
|
|
137 buf.append(""");
|
|
138 break;
|
|
139 case '\'':
|
|
140 buf.append("'");
|
|
141 break;
|
1790
|
142 case '\n':
|
|
143 buf.append(" ");
|
|
144 break;
|
|
145 case '\r':
|
|
146 buf.append(" ");
|
|
147 break;
|
1468
|
148 default:
|
|
149 buf.append(c);
|
|
150 }
|
|
151 }
|
|
152 return buf.toString();
|
|
153 }
|
|
154
|
1466
|
155 }
|