comparison src/goodjava/xml/XmlParser.java @ 1468:35f3bfd4f51d

xml
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 14 Apr 2020 08:44:33 -0600
parents 670b7d089699
children 0246add03d11
comparison
equal deleted inserted replaced
1467:509d49c493c0 1468:35f3bfd4f51d
56 Map.Entry<String,String> attribute; 56 Map.Entry<String,String> attribute;
57 while( (attribute=attribute()) != null ) { 57 while( (attribute=attribute()) != null ) {
58 attributes.put(attribute.getKey(),attribute.getValue()); 58 attributes.put(attribute.getKey(),attribute.getValue());
59 } 59 }
60 spaces(); 60 spaces();
61 if( parser.match("/>") ) {
62 XmlElement element = new XmlElement(name,attributes);
63 return parser.success(element);
64 }
61 required(">"); 65 required(">");
62 String s = string(name); 66 String s = string(name);
63 if( s != null ) { 67 if( s != null ) {
64 XmlElement element = new XmlElement(name,attributes,s); 68 XmlElement element = new XmlElement(name,attributes,s);
65 return parser.success(element); 69 return parser.success(element);
74 78
75 private String string(String name) throws ParseException { 79 private String string(String name) throws ParseException {
76 int start = parser.begin(); 80 int start = parser.begin();
77 while( parser.noneOf("<") ); 81 while( parser.noneOf("<") );
78 String s = parser.textFrom(start); 82 String s = parser.textFrom(start);
83 s = decode(s);
79 if( !endTag(name) ) 84 if( !endTag(name) )
80 return parser.failure(null); 85 return parser.failure(null);
81 return parser.success(s); 86 return parser.success(s);
82 } 87 }
83 88
123 while( !parser.test(quote) ) { 128 while( !parser.test(quote) ) {
124 if( !parser.anyChar() ) 129 if( !parser.anyChar() )
125 throw exception("unclosed attribute value"); 130 throw exception("unclosed attribute value");
126 } 131 }
127 String value = parser.textFrom(start); 132 String value = parser.textFrom(start);
133 value = decode(value);
128 parser.match(quote); 134 parser.match(quote);
129 Map.Entry<String,String> attribute = new AbstractMap.SimpleImmutableEntry<String,String>(name,value); 135 Map.Entry<String,String> attribute = new AbstractMap.SimpleImmutableEntry<String,String>(name,value);
130 return parser.success(attribute); 136 return parser.success(attribute);
131 } 137 }
132 138
167 173
168 private boolean matchSpace() { 174 private boolean matchSpace() {
169 return parser.anyOf(" \t\r\n"); 175 return parser.anyOf(" \t\r\n");
170 } 176 }
171 177
178 private static String decode(String s) {
179 s = s.replace("&lt;","<");
180 s = s.replace("&gt;",">");
181 s = s.replace("&quot;","\"");
182 s = s.replace("&apos;","'");
183 s = s.replace("&amp;","&");
184 return s;
185 }
186
172 } 187 }