comparison src/goodjava/xml/XmlElement.java @ 1468:35f3bfd4f51d

xml
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 14 Apr 2020 08:44:33 -0600
parents 509d49c493c0
children 21f5edab1fbf
comparison
equal deleted inserted replaced
1467:509d49c493c0 1468:35f3bfd4f51d
6 public final class XmlElement { 6 public final class XmlElement {
7 public final String name; 7 public final String name;
8 public final Map<String,String> attributes; 8 public final Map<String,String> attributes;
9 public final Object content; 9 public final Object content;
10 10
11 public XmlElement(String name,Map<String,String> attributes) {
12 this.name = name;
13 this.attributes = attributes;
14 this.content = null;
15 }
16
11 public XmlElement(String name,Map<String,String> attributes,String content) { 17 public XmlElement(String name,Map<String,String> attributes,String content) {
18 if( content == null )
19 throw new IllegalArgumentException("content can't be null");
12 this.name = name; 20 this.name = name;
13 this.attributes = attributes; 21 this.attributes = attributes;
14 this.content = content; 22 this.content = content;
15 } 23 }
16 24
17 public XmlElement(String name,Map<String,String> attributes,XmlElement[] content) { 25 public XmlElement(String name,Map<String,String> attributes,XmlElement[] content) {
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");
18 this.name = name; 30 this.name = name;
19 this.attributes = attributes; 31 this.attributes = attributes;
20 this.content = content; 32 this.content = content;
21 } 33 }
22 34
32 sb.append( name ); 44 sb.append( name );
33 for( Map.Entry<String,String> attribute : attributes.entrySet() ) { 45 for( Map.Entry<String,String> attribute : attributes.entrySet() ) {
34 sb.append( ' ' ); 46 sb.append( ' ' );
35 sb.append( attribute.getKey() ); 47 sb.append( attribute.getKey() );
36 sb.append( "=\"" ); 48 sb.append( "=\"" );
37 sb.append( attribute.getValue() ); 49 sb.append( encode(attribute.getValue()) );
38 sb.append( '"' ); 50 sb.append( '"' );
39 } 51 }
40 sb.append( '>' ); 52 if( content == null ) {
41 if( content instanceof String ) { 53 sb.append( "/>\n" );
54 } else if( content instanceof String ) {
55 sb.append( '>' );
42 String s = (String)content; 56 String s = (String)content;
43 sb.append(s); 57 sb.append( encode(s) );
58 closeTag(sb,name);
44 } else { 59 } else {
60 sb.append( '>' );
45 XmlElement[] elements = (XmlElement[])content; 61 XmlElement[] elements = (XmlElement[])content;
46 sb.append( '\n' ); 62 sb.append( '\n' );
47 for( XmlElement element : elements ) { 63 for( XmlElement element : elements ) {
48 element.toString(sb,indented+1); 64 element.toString(sb,indented+1);
49 } 65 }
50 indent(sb,indented); 66 indent(sb,indented);
67 closeTag(sb,name);
51 } 68 }
69 }
70
71 private static void closeTag(StringBuilder sb,String name) {
52 sb.append( "</" ); 72 sb.append( "</" );
53 sb.append( name ); 73 sb.append( name );
54 sb.append( ">\n" ); 74 sb.append( ">\n" );
55 } 75 }
56 76
58 for( int i=0; i<indented; i++ ) { 78 for( int i=0; i<indented; i++ ) {
59 sb.append('\t'); 79 sb.append('\t');
60 } 80 }
61 } 81 }
62 82
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("&amp;");
91 break;
92 case '<':
93 buf.append("&lt;");
94 break;
95 case '>':
96 buf.append("&gt;");
97 break;
98 case '"':
99 buf.append("&quot;");
100 break;
101 default:
102 buf.append(c);
103 }
104 }
105 return buf.toString();
106 }
107
63 } 108 }