annotate src/goodjava/xml/XmlElement.java @ 1827:50e570b598b2

security
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 15 Sep 2024 10:36:46 -0600
parents 9daae8cf4bef
children
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 )
1810
Franklin Schmidt <fschmidt@gmail.com>
parents: 1793
diff changeset
32 throw new IllegalArgumentException("content of '"+name+"' 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 )
1810
Franklin Schmidt <fschmidt@gmail.com>
parents: 1793
diff changeset
38 throw new IllegalArgumentException("content of '"+name+"' can't be null");
1466
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39 this.content = content;
1791
f8f5c51f5b36 xml work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1790
diff changeset
40 for( XmlElement el : content ) {
f8f5c51f5b36 xml work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1790
diff changeset
41 el.parent = this;
f8f5c51f5b36 xml work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1790
diff changeset
42 }
1466
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
43 }
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44
1790
a8c685a894b4 start xml work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1469
diff changeset
45 @Override public String toString() {
1466
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46 StringBuilder sb = new StringBuilder();
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 toString(sb,0);
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48 return sb.toString();
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49 }
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
50
1790
a8c685a894b4 start xml work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1469
diff changeset
51 void toString(StringBuilder sb,int indented) {
1466
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
52 indent(sb,indented);
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
53 sb.append( '<' );
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
54 sb.append( name );
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
55 for( Map.Entry<String,String> attribute : attributes.entrySet() ) {
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
56 sb.append( ' ' );
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
57 sb.append( attribute.getKey() );
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
58 sb.append( "=\"" );
1793
a55a891a4f67 xml minor
Franklin Schmidt <fschmidt@gmail.com>
parents: 1791
diff changeset
59 sb.append( encodeAttr(attribute.getValue()) );
1466
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
60 sb.append( '"' );
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
61 }
1468
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
62 if( content == null ) {
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
63 sb.append( "/>\n" );
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
64 } else if( content instanceof String ) {
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
65 sb.append( '>' );
1466
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
66 String s = (String)content;
1468
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
67 sb.append( encode(s) );
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
68 closeTag(sb,name);
1467
Franklin Schmidt <fschmidt@gmail.com>
parents: 1466
diff changeset
69 } else {
1468
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
70 sb.append( '>' );
1466
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
71 XmlElement[] elements = (XmlElement[])content;
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
72 sb.append( '\n' );
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
73 for( XmlElement element : elements ) {
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
74 element.toString(sb,indented+1);
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
75 }
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
76 indent(sb,indented);
1468
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
77 closeTag(sb,name);
1467
Franklin Schmidt <fschmidt@gmail.com>
parents: 1466
diff changeset
78 }
1468
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
79 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
80
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
81 private static void closeTag(StringBuilder sb,String name) {
1466
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
82 sb.append( "</" );
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
83 sb.append( name );
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
84 sb.append( ">\n" );
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
85 }
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
86
1467
Franklin Schmidt <fschmidt@gmail.com>
parents: 1466
diff changeset
87 private static void indent(StringBuilder sb,int indented) {
1466
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
88 for( int i=0; i<indented; i++ ) {
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
89 sb.append('\t');
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
90 }
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
91 }
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
92
1469
Franklin Schmidt <fschmidt@gmail.com>
parents: 1468
diff changeset
93 public static String encode(String s) {
1468
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
94 final char[] a = s.toCharArray();
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
95 StringBuilder buf = new StringBuilder();
1469
Franklin Schmidt <fschmidt@gmail.com>
parents: 1468
diff changeset
96 for( char c : a ) {
1468
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
97 switch(c) {
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
98 case '&':
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
99 buf.append("&amp;");
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
100 break;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
101 case '<':
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
102 buf.append("&lt;");
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
103 break;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
104 case '>':
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
105 buf.append("&gt;");
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
106 break;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
107 case '"':
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
108 buf.append("&quot;");
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
109 break;
1469
Franklin Schmidt <fschmidt@gmail.com>
parents: 1468
diff changeset
110 case '\'':
Franklin Schmidt <fschmidt@gmail.com>
parents: 1468
diff changeset
111 buf.append("&apos;");
Franklin Schmidt <fschmidt@gmail.com>
parents: 1468
diff changeset
112 break;
1793
a55a891a4f67 xml minor
Franklin Schmidt <fschmidt@gmail.com>
parents: 1791
diff changeset
113 default:
a55a891a4f67 xml minor
Franklin Schmidt <fschmidt@gmail.com>
parents: 1791
diff changeset
114 buf.append(c);
a55a891a4f67 xml minor
Franklin Schmidt <fschmidt@gmail.com>
parents: 1791
diff changeset
115 }
a55a891a4f67 xml minor
Franklin Schmidt <fschmidt@gmail.com>
parents: 1791
diff changeset
116 }
a55a891a4f67 xml minor
Franklin Schmidt <fschmidt@gmail.com>
parents: 1791
diff changeset
117 return buf.toString();
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
a55a891a4f67 xml minor
Franklin Schmidt <fschmidt@gmail.com>
parents: 1791
diff changeset
120 public static String encodeAttr(String s) {
a55a891a4f67 xml minor
Franklin Schmidt <fschmidt@gmail.com>
parents: 1791
diff changeset
121 final char[] a = s.toCharArray();
a55a891a4f67 xml minor
Franklin Schmidt <fschmidt@gmail.com>
parents: 1791
diff changeset
122 StringBuilder buf = new StringBuilder();
a55a891a4f67 xml minor
Franklin Schmidt <fschmidt@gmail.com>
parents: 1791
diff changeset
123 for( char c : a ) {
a55a891a4f67 xml minor
Franklin Schmidt <fschmidt@gmail.com>
parents: 1791
diff changeset
124 switch(c) {
a55a891a4f67 xml minor
Franklin Schmidt <fschmidt@gmail.com>
parents: 1791
diff changeset
125 case '&':
a55a891a4f67 xml minor
Franklin Schmidt <fschmidt@gmail.com>
parents: 1791
diff changeset
126 buf.append("&amp;");
a55a891a4f67 xml minor
Franklin Schmidt <fschmidt@gmail.com>
parents: 1791
diff changeset
127 break;
a55a891a4f67 xml minor
Franklin Schmidt <fschmidt@gmail.com>
parents: 1791
diff changeset
128 case '<':
a55a891a4f67 xml minor
Franklin Schmidt <fschmidt@gmail.com>
parents: 1791
diff changeset
129 buf.append("&lt;");
a55a891a4f67 xml minor
Franklin Schmidt <fschmidt@gmail.com>
parents: 1791
diff changeset
130 break;
a55a891a4f67 xml minor
Franklin Schmidt <fschmidt@gmail.com>
parents: 1791
diff changeset
131 case '>':
a55a891a4f67 xml minor
Franklin Schmidt <fschmidt@gmail.com>
parents: 1791
diff changeset
132 buf.append("&gt;");
a55a891a4f67 xml minor
Franklin Schmidt <fschmidt@gmail.com>
parents: 1791
diff changeset
133 break;
a55a891a4f67 xml minor
Franklin Schmidt <fschmidt@gmail.com>
parents: 1791
diff changeset
134 case '"':
a55a891a4f67 xml minor
Franklin Schmidt <fschmidt@gmail.com>
parents: 1791
diff changeset
135 buf.append("&quot;");
a55a891a4f67 xml minor
Franklin Schmidt <fschmidt@gmail.com>
parents: 1791
diff changeset
136 break;
a55a891a4f67 xml minor
Franklin Schmidt <fschmidt@gmail.com>
parents: 1791
diff changeset
137 case '\'':
a55a891a4f67 xml minor
Franklin Schmidt <fschmidt@gmail.com>
parents: 1791
diff changeset
138 buf.append("&apos;");
a55a891a4f67 xml minor
Franklin Schmidt <fschmidt@gmail.com>
parents: 1791
diff changeset
139 break;
1790
a8c685a894b4 start xml work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1469
diff changeset
140 case '\n':
a8c685a894b4 start xml work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1469
diff changeset
141 buf.append("&#10;");
a8c685a894b4 start xml work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1469
diff changeset
142 break;
a8c685a894b4 start xml work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1469
diff changeset
143 case '\r':
a8c685a894b4 start xml work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1469
diff changeset
144 buf.append("&#13;");
a8c685a894b4 start xml work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1469
diff changeset
145 break;
1468
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
146 default:
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
147 buf.append(c);
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
148 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
149 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
150 return buf.toString();
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
151 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
152
1466
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
153 }