annotate src/goodjava/xml/XmlElement.java @ 1790:a8c685a894b4

start xml work
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 25 Dec 2023 23:07:59 -0700
parents 21f5edab1fbf
children f8f5c51f5b36
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;
1466
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10
1468
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
11 public XmlElement(String name,Map<String,String> attributes) {
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
12 this.name = name;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
13 this.attributes = attributes;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
14 this.content = null;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
15 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
16
1790
a8c685a894b4 start xml work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1469
diff changeset
17 public Object getContent() {
a8c685a894b4 start xml work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1469
diff changeset
18 return content;
a8c685a894b4 start xml work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1469
diff changeset
19 }
a8c685a894b4 start xml work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1469
diff changeset
20
a8c685a894b4 start xml work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1469
diff changeset
21 public void removeContent() {
a8c685a894b4 start xml work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1469
diff changeset
22 content = null;
a8c685a894b4 start xml work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1469
diff changeset
23 }
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 public void setContent(String content) {
1468
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
26 if( content == null )
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
27 throw new IllegalArgumentException("content can't be null");
1466
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 this.content = content;
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 }
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30
1790
a8c685a894b4 start xml work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1469
diff changeset
31 public void setContent(XmlElement[] content) {
1468
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
32 if( content == null )
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
33 throw new IllegalArgumentException("content can't be null");
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
34 if( content.length == 0 )
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
35 throw new IllegalArgumentException("content can't be empty");
1466
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 this.content = content;
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 }
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38
1790
a8c685a894b4 start xml work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1469
diff changeset
39 @Override public String toString() {
1466
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40 StringBuilder sb = new StringBuilder();
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
41 toString(sb,0);
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
42 return sb.toString();
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 void toString(StringBuilder sb,int indented) {
1466
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46 indent(sb,indented);
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 sb.append( '<' );
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48 sb.append( name );
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49 for( Map.Entry<String,String> attribute : attributes.entrySet() ) {
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
50 sb.append( ' ' );
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
51 sb.append( attribute.getKey() );
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
52 sb.append( "=\"" );
1468
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
53 sb.append( encode(attribute.getValue()) );
1466
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
54 sb.append( '"' );
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
55 }
1468
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
56 if( content == null ) {
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
57 sb.append( "/>\n" );
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
58 } else if( content instanceof String ) {
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
59 sb.append( '>' );
1466
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
60 String s = (String)content;
1468
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
61 sb.append( encode(s) );
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
62 closeTag(sb,name);
1467
Franklin Schmidt <fschmidt@gmail.com>
parents: 1466
diff changeset
63 } else {
1468
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
64 sb.append( '>' );
1466
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
65 XmlElement[] elements = (XmlElement[])content;
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
66 sb.append( '\n' );
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
67 for( XmlElement element : elements ) {
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
68 element.toString(sb,indented+1);
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
69 }
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
70 indent(sb,indented);
1468
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
71 closeTag(sb,name);
1467
Franklin Schmidt <fschmidt@gmail.com>
parents: 1466
diff changeset
72 }
1468
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
73 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
74
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
75 private static void closeTag(StringBuilder sb,String name) {
1466
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
76 sb.append( "</" );
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
77 sb.append( name );
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
78 sb.append( ">\n" );
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
79 }
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
80
1467
Franklin Schmidt <fschmidt@gmail.com>
parents: 1466
diff changeset
81 private static void indent(StringBuilder sb,int indented) {
1466
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
82 for( int i=0; i<indented; i++ ) {
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
83 sb.append('\t');
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
84 }
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
85 }
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
86
1469
Franklin Schmidt <fschmidt@gmail.com>
parents: 1468
diff changeset
87 public static String encode(String s) {
1468
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
88 final char[] a = s.toCharArray();
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
89 StringBuilder buf = new StringBuilder();
1469
Franklin Schmidt <fschmidt@gmail.com>
parents: 1468
diff changeset
90 for( char c : a ) {
1468
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
91 switch(c) {
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
92 case '&':
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
93 buf.append("&amp;");
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
94 break;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
95 case '<':
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
96 buf.append("&lt;");
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
97 break;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
98 case '>':
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
99 buf.append("&gt;");
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("&quot;");
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
103 break;
1469
Franklin Schmidt <fschmidt@gmail.com>
parents: 1468
diff changeset
104 case '\'':
Franklin Schmidt <fschmidt@gmail.com>
parents: 1468
diff changeset
105 buf.append("&apos;");
Franklin Schmidt <fschmidt@gmail.com>
parents: 1468
diff changeset
106 break;
1790
a8c685a894b4 start xml work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1469
diff changeset
107 case '\n':
a8c685a894b4 start xml work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1469
diff changeset
108 buf.append("&#10;");
a8c685a894b4 start xml work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1469
diff changeset
109 break;
a8c685a894b4 start xml work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1469
diff changeset
110 case '\r':
a8c685a894b4 start xml work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1469
diff changeset
111 buf.append("&#13;");
a8c685a894b4 start xml work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1469
diff changeset
112 break;
1468
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
113 default:
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
114 buf.append(c);
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
115 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
116 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
117 return buf.toString();
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
118 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
119
1466
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
120 }