comparison 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
comparison
equal deleted inserted replaced
1792:a5f62fe28b3e 1793:a55a891a4f67
56 sb.append( name ); 56 sb.append( name );
57 for( Map.Entry<String,String> attribute : attributes.entrySet() ) { 57 for( Map.Entry<String,String> attribute : attributes.entrySet() ) {
58 sb.append( ' ' ); 58 sb.append( ' ' );
59 sb.append( attribute.getKey() ); 59 sb.append( attribute.getKey() );
60 sb.append( "=\"" ); 60 sb.append( "=\"" );
61 sb.append( encode(attribute.getValue()) ); 61 sb.append( encodeAttr(attribute.getValue()) );
62 sb.append( '"' ); 62 sb.append( '"' );
63 } 63 }
64 if( content == null ) { 64 if( content == null ) {
65 sb.append( "/>\n" ); 65 sb.append( "/>\n" );
66 } else if( content instanceof String ) { 66 } else if( content instanceof String ) {
110 buf.append("&quot;"); 110 buf.append("&quot;");
111 break; 111 break;
112 case '\'': 112 case '\'':
113 buf.append("&apos;"); 113 buf.append("&apos;");
114 break; 114 break;
115 default:
116 buf.append(c);
117 }
118 }
119 return buf.toString();
120 }
121
122 public static String encodeAttr(String s) {
123 final char[] a = s.toCharArray();
124 StringBuilder buf = new StringBuilder();
125 for( char c : a ) {
126 switch(c) {
127 case '&':
128 buf.append("&amp;");
129 break;
130 case '<':
131 buf.append("&lt;");
132 break;
133 case '>':
134 buf.append("&gt;");
135 break;
136 case '"':
137 buf.append("&quot;");
138 break;
139 case '\'':
140 buf.append("&apos;");
141 break;
115 case '\n': 142 case '\n':
116 buf.append("&#10;"); 143 buf.append("&#10;");
117 break; 144 break;
118 case '\r': 145 case '\r':
119 buf.append("&#13;"); 146 buf.append("&#13;");