annotate src/goodjava/xml/XmlElement.java @ 1474:13cbce740e1e

LowercaseAnalyzer
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 17 Apr 2020 21:53:09 -0600
parents 21f5edab1fbf
children a8c685a894b4
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;
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 public final Object content;
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
1466
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 public XmlElement(String name,Map<String,String> attributes,String content) {
1468
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
18 if( content == null )
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
19 throw new IllegalArgumentException("content can't be null");
1466
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 this.name = name;
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 this.attributes = attributes;
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 this.content = content;
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 }
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 public XmlElement(String name,Map<String,String> attributes,XmlElement[] 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");
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
28 if( content.length == 0 )
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
29 throw new IllegalArgumentException("content can't be empty");
1466
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 this.name = name;
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 this.attributes = attributes;
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 this.content = content;
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 }
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 public String toString() {
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 StringBuilder sb = new StringBuilder();
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 toString(sb,0);
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38 return sb.toString();
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39 }
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
41 private void toString(StringBuilder sb,int indented) {
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
42 indent(sb,indented);
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
43 sb.append( '<' );
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44 sb.append( name );
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45 for( Map.Entry<String,String> attribute : attributes.entrySet() ) {
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46 sb.append( ' ' );
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 sb.append( attribute.getKey() );
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48 sb.append( "=\"" );
1468
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
49 sb.append( encode(attribute.getValue()) );
1466
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 }
1468
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
52 if( content == null ) {
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
53 sb.append( "/>\n" );
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
54 } else if( content instanceof String ) {
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
55 sb.append( '>' );
1466
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
56 String s = (String)content;
1468
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
57 sb.append( encode(s) );
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
58 closeTag(sb,name);
1467
Franklin Schmidt <fschmidt@gmail.com>
parents: 1466
diff changeset
59 } else {
1468
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
60 sb.append( '>' );
1466
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
61 XmlElement[] elements = (XmlElement[])content;
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
62 sb.append( '\n' );
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
63 for( XmlElement element : elements ) {
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
64 element.toString(sb,indented+1);
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
65 }
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
66 indent(sb,indented);
1468
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
67 closeTag(sb,name);
1467
Franklin Schmidt <fschmidt@gmail.com>
parents: 1466
diff changeset
68 }
1468
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
69 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
70
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
71 private static void closeTag(StringBuilder sb,String name) {
1466
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
72 sb.append( "</" );
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
73 sb.append( name );
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 }
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
76
1467
Franklin Schmidt <fschmidt@gmail.com>
parents: 1466
diff changeset
77 private static void indent(StringBuilder sb,int indented) {
1466
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
78 for( int i=0; i<indented; i++ ) {
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
79 sb.append('\t');
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
80 }
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
81 }
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
82
1469
Franklin Schmidt <fschmidt@gmail.com>
parents: 1468
diff changeset
83 public static String encode(String s) {
1468
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
84 final char[] a = s.toCharArray();
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
85 StringBuilder buf = new StringBuilder();
1469
Franklin Schmidt <fschmidt@gmail.com>
parents: 1468
diff changeset
86 for( char c : a ) {
1468
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
87 switch(c) {
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
88 case '&':
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
89 buf.append("&amp;");
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
90 break;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
91 case '<':
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
92 buf.append("&lt;");
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
93 break;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
94 case '>':
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
95 buf.append("&gt;");
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
96 break;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
97 case '"':
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
98 buf.append("&quot;");
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
99 break;
1469
Franklin Schmidt <fschmidt@gmail.com>
parents: 1468
diff changeset
100 case '\'':
Franklin Schmidt <fschmidt@gmail.com>
parents: 1468
diff changeset
101 buf.append("&apos;");
Franklin Schmidt <fschmidt@gmail.com>
parents: 1468
diff changeset
102 break;
1468
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
103 default:
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
104 buf.append(c);
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
105 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
106 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
107 return buf.toString();
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
108 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 1467
diff changeset
109
1466
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
110 }