comparison 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
comparison
equal deleted inserted replaced
1789:bac27b119ff2 1790:a8c685a894b4
4 4
5 5
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 private Object content = null;
10 10
11 public XmlElement(String name,Map<String,String> attributes) { 11 public XmlElement(String name,Map<String,String> attributes) {
12 this.name = name; 12 this.name = name;
13 this.attributes = attributes; 13 this.attributes = attributes;
14 this.content = null; 14 this.content = null;
15 } 15 }
16 16
17 public XmlElement(String name,Map<String,String> attributes,String content) { 17 public Object getContent() {
18 return content;
19 }
20
21 public void removeContent() {
22 content = null;
23 }
24
25 public void setContent(String content) {
18 if( content == null ) 26 if( content == null )
19 throw new IllegalArgumentException("content can't be null"); 27 throw new IllegalArgumentException("content can't be null");
20 this.name = name;
21 this.attributes = attributes;
22 this.content = content; 28 this.content = content;
23 } 29 }
24 30
25 public XmlElement(String name,Map<String,String> attributes,XmlElement[] content) { 31 public void setContent(XmlElement[] content) {
26 if( content == null ) 32 if( content == null )
27 throw new IllegalArgumentException("content can't be null"); 33 throw new IllegalArgumentException("content can't be null");
28 if( content.length == 0 ) 34 if( content.length == 0 )
29 throw new IllegalArgumentException("content can't be empty"); 35 throw new IllegalArgumentException("content can't be empty");
30 this.name = name;
31 this.attributes = attributes;
32 this.content = content; 36 this.content = content;
33 } 37 }
34 38
35 public String toString() { 39 @Override public String toString() {
36 StringBuilder sb = new StringBuilder(); 40 StringBuilder sb = new StringBuilder();
37 toString(sb,0); 41 toString(sb,0);
38 return sb.toString(); 42 return sb.toString();
39 } 43 }
40 44
41 private void toString(StringBuilder sb,int indented) { 45 void toString(StringBuilder sb,int indented) {
42 indent(sb,indented); 46 indent(sb,indented);
43 sb.append( '<' ); 47 sb.append( '<' );
44 sb.append( name ); 48 sb.append( name );
45 for( Map.Entry<String,String> attribute : attributes.entrySet() ) { 49 for( Map.Entry<String,String> attribute : attributes.entrySet() ) {
46 sb.append( ' ' ); 50 sb.append( ' ' );
98 buf.append("&quot;"); 102 buf.append("&quot;");
99 break; 103 break;
100 case '\'': 104 case '\'':
101 buf.append("&apos;"); 105 buf.append("&apos;");
102 break; 106 break;
107 case '\n':
108 buf.append("&#10;");
109 break;
110 case '\r':
111 buf.append("&#13;");
112 break;
103 default: 113 default:
104 buf.append(c); 114 buf.append(c);
105 } 115 }
106 } 116 }
107 return buf.toString(); 117 return buf.toString();