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;
|
1466
|
10
|
1468
|
11 public XmlElement(String name,Map<String,String> attributes) {
|
|
12 this.name = name;
|
|
13 this.attributes = attributes;
|
|
14 this.content = null;
|
|
15 }
|
|
16
|
1790
|
17 public Object getContent() {
|
|
18 return content;
|
|
19 }
|
|
20
|
|
21 public void removeContent() {
|
|
22 content = null;
|
|
23 }
|
|
24
|
|
25 public void setContent(String content) {
|
1468
|
26 if( content == null )
|
|
27 throw new IllegalArgumentException("content can't be null");
|
1466
|
28 this.content = content;
|
|
29 }
|
|
30
|
1790
|
31 public void setContent(XmlElement[] content) {
|
1468
|
32 if( content == null )
|
|
33 throw new IllegalArgumentException("content can't be null");
|
|
34 if( content.length == 0 )
|
|
35 throw new IllegalArgumentException("content can't be empty");
|
1466
|
36 this.content = content;
|
|
37 }
|
|
38
|
1790
|
39 @Override public String toString() {
|
1466
|
40 StringBuilder sb = new StringBuilder();
|
|
41 toString(sb,0);
|
|
42 return sb.toString();
|
|
43 }
|
|
44
|
1790
|
45 void toString(StringBuilder sb,int indented) {
|
1466
|
46 indent(sb,indented);
|
|
47 sb.append( '<' );
|
|
48 sb.append( name );
|
|
49 for( Map.Entry<String,String> attribute : attributes.entrySet() ) {
|
|
50 sb.append( ' ' );
|
|
51 sb.append( attribute.getKey() );
|
|
52 sb.append( "=\"" );
|
1468
|
53 sb.append( encode(attribute.getValue()) );
|
1466
|
54 sb.append( '"' );
|
|
55 }
|
1468
|
56 if( content == null ) {
|
|
57 sb.append( "/>\n" );
|
|
58 } else if( content instanceof String ) {
|
|
59 sb.append( '>' );
|
1466
|
60 String s = (String)content;
|
1468
|
61 sb.append( encode(s) );
|
|
62 closeTag(sb,name);
|
1467
|
63 } else {
|
1468
|
64 sb.append( '>' );
|
1466
|
65 XmlElement[] elements = (XmlElement[])content;
|
|
66 sb.append( '\n' );
|
|
67 for( XmlElement element : elements ) {
|
|
68 element.toString(sb,indented+1);
|
|
69 }
|
|
70 indent(sb,indented);
|
1468
|
71 closeTag(sb,name);
|
1467
|
72 }
|
1468
|
73 }
|
|
74
|
|
75 private static void closeTag(StringBuilder sb,String name) {
|
1466
|
76 sb.append( "</" );
|
|
77 sb.append( name );
|
|
78 sb.append( ">\n" );
|
|
79 }
|
|
80
|
1467
|
81 private static void indent(StringBuilder sb,int indented) {
|
1466
|
82 for( int i=0; i<indented; i++ ) {
|
|
83 sb.append('\t');
|
|
84 }
|
|
85 }
|
|
86
|
1469
|
87 public static String encode(String s) {
|
1468
|
88 final char[] a = s.toCharArray();
|
|
89 StringBuilder buf = new StringBuilder();
|
1469
|
90 for( char c : a ) {
|
1468
|
91 switch(c) {
|
|
92 case '&':
|
|
93 buf.append("&");
|
|
94 break;
|
|
95 case '<':
|
|
96 buf.append("<");
|
|
97 break;
|
|
98 case '>':
|
|
99 buf.append(">");
|
|
100 break;
|
|
101 case '"':
|
|
102 buf.append(""");
|
|
103 break;
|
1469
|
104 case '\'':
|
|
105 buf.append("'");
|
|
106 break;
|
1790
|
107 case '\n':
|
|
108 buf.append(" ");
|
|
109 break;
|
|
110 case '\r':
|
|
111 buf.append(" ");
|
|
112 break;
|
1468
|
113 default:
|
|
114 buf.append(c);
|
|
115 }
|
|
116 }
|
|
117 return buf.toString();
|
|
118 }
|
|
119
|
1466
|
120 }
|