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