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
|
1469
|
83 public static String encode(String s) {
|
1468
|
84 final char[] a = s.toCharArray();
|
|
85 StringBuilder buf = new StringBuilder();
|
1469
|
86 for( char c : a ) {
|
1468
|
87 switch(c) {
|
|
88 case '&':
|
|
89 buf.append("&");
|
|
90 break;
|
|
91 case '<':
|
|
92 buf.append("<");
|
|
93 break;
|
|
94 case '>':
|
|
95 buf.append(">");
|
|
96 break;
|
|
97 case '"':
|
|
98 buf.append(""");
|
|
99 break;
|
1469
|
100 case '\'':
|
|
101 buf.append("'");
|
|
102 break;
|
1468
|
103 default:
|
|
104 buf.append(c);
|
|
105 }
|
|
106 }
|
|
107 return buf.toString();
|
|
108 }
|
|
109
|
1466
|
110 }
|