annotate src/goodjava/xml/XmlElement.java @ 1793:a55a891a4f67

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