annotate src/goodjava/xml/XmlParser.java @ 1823:36dfc31fd607

install fix
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 07 Aug 2024 18:29:58 -0600
parents 9daae8cf4bef
children
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 import java.util.AbstractMap;
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 import java.util.LinkedHashMap;
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6 import java.util.List;
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 import java.util.ArrayList;
1790
a8c685a894b4 start xml work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1723
diff changeset
8 import java.util.regex.Pattern;
a8c685a894b4 start xml work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1723
diff changeset
9 import java.util.regex.Matcher;
1466
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 import goodjava.parser.Parser;
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 import goodjava.parser.ParseException;
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 public final class XmlParser {
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15
1790
a8c685a894b4 start xml work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1723
diff changeset
16 public static Xml parse(String text) throws ParseException {
1466
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 return new XmlParser(text).parse();
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 }
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 private final Parser parser;
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 private XmlParser(String text) {
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 this.parser = new Parser(text);
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 }
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 private ParseException exception(String msg) {
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 return new ParseException(parser,msg);
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 }
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29
1790
a8c685a894b4 start xml work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1723
diff changeset
30 private Xml parse() throws ParseException {
a8c685a894b4 start xml work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1723
diff changeset
31 Xml xml = new Xml();
1466
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 spaces();
1790
a8c685a894b4 start xml work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1723
diff changeset
33 xml.declaration = declaration();
1466
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 spaces();
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 XmlElement element = element();
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 spaces();
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 if( !parser.endOfInput() )
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38 throw exception("unexpected text");
1723
0246add03d11 dont allow empty xml
Franklin Schmidt <fschmidt@gmail.com>
parents: 1468
diff changeset
39 if( element==null )
0246add03d11 dont allow empty xml
Franklin Schmidt <fschmidt@gmail.com>
parents: 1468
diff changeset
40 throw exception("empty document not allowed");
1790
a8c685a894b4 start xml work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1723
diff changeset
41 xml.setElement(element);
a8c685a894b4 start xml work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1723
diff changeset
42 return xml;
1466
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: 1723
diff changeset
45 private String declaration() throws ParseException {
a8c685a894b4 start xml work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1723
diff changeset
46 int start = parser.begin();
1466
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 if( !parser.match("<?xml") )
1790
a8c685a894b4 start xml work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1723
diff changeset
48 return parser.failure(null);
1466
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49 while( attribute() != null );
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
50 spaces();
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
51 required("?>");
1790
a8c685a894b4 start xml work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1723
diff changeset
52 String s = parser.textFrom(start);
a8c685a894b4 start xml work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1723
diff changeset
53 return parser.success(s);
1466
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
54 }
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
55
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
56 private XmlElement element() throws ParseException {
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
57 parser.begin();
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
58 if( !parser.match('<') || parser.test('/') )
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
59 return parser.failure(null);
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
60 //spaces();
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
61 String name = name();
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
62 if( name==null )
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
63 throw exception("element name not found");
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
64 Map<String,String> attributes = new LinkedHashMap<String,String>();
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
65 Map.Entry<String,String> attribute;
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
66 while( (attribute=attribute()) != null ) {
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
67 attributes.put(attribute.getKey(),attribute.getValue());
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
68 }
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
69 spaces();
1468
Franklin Schmidt <fschmidt@gmail.com>
parents: 1466
diff changeset
70 if( parser.match("/>") ) {
Franklin Schmidt <fschmidt@gmail.com>
parents: 1466
diff changeset
71 XmlElement element = new XmlElement(name,attributes);
Franklin Schmidt <fschmidt@gmail.com>
parents: 1466
diff changeset
72 return parser.success(element);
Franklin Schmidt <fschmidt@gmail.com>
parents: 1466
diff changeset
73 }
1466
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
74 required(">");
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
75 String s = string(name);
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
76 if( s != null ) {
1790
a8c685a894b4 start xml work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1723
diff changeset
77 XmlElement element = new XmlElement(name,attributes);
a8c685a894b4 start xml work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1723
diff changeset
78 element.setContent(s);
1466
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
79 return parser.success(element);
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
80 }
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
81 List<XmlElement> elements = elements(name);
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
82 if( elements != null ) {
1790
a8c685a894b4 start xml work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1723
diff changeset
83 XmlElement element = new XmlElement(name,attributes);
a8c685a894b4 start xml work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1723
diff changeset
84 element.setContent( elements.toArray(new XmlElement[0]) );
1466
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
85 return parser.success(element);
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
86 }
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
87 throw exception("bad element");
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
88 }
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
89
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
90 private String string(String name) throws ParseException {
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
91 int start = parser.begin();
1819
9daae8cf4bef xml minor
Franklin Schmidt <fschmidt@gmail.com>
parents: 1815
diff changeset
92 while( parser.noneOf("<") );
1466
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
93 String s = parser.textFrom(start);
1468
Franklin Schmidt <fschmidt@gmail.com>
parents: 1466
diff changeset
94 s = decode(s);
1466
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
95 if( !endTag(name) )
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
96 return parser.failure(null);
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
97 return parser.success(s);
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
98 }
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
99
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
100 private List<XmlElement> elements(String name) throws ParseException {
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
101 parser.begin();
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
102 List<XmlElement> elements = new ArrayList<XmlElement>();
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
103 spaces();
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
104 XmlElement element;
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
105 while( (element=element()) != null ) {
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
106 elements.add(element);
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
107 spaces();
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
108 }
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
109 if( !endTag(name) )
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
110 return parser.failure(null);
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
111 return parser.success(elements);
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
112 }
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
113
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
114 private boolean endTag(String name) throws ParseException {
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
115 parser.begin();
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
116 if( !parser.match("</") || !parser.match(name) )
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
117 return parser.failure();
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
118 spaces();
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
119 if( !parser.match('>') )
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
120 return parser.failure();
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
121 return parser.success();
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
122 }
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
123
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
124 private Map.Entry<String,String> attribute() throws ParseException {
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
125 parser.begin();
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
126 if( !matchSpace() )
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
127 return parser.failure(null);
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
128 spaces();
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
129 String name = name();
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
130 if( name==null )
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
131 return parser.failure(null);
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
132 spaces();
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
133 required("=");
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
134 spaces();
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
135 if( !parser.anyOf("\"'") )
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
136 throw exception("quote expected");
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
137 char quote = parser.lastChar();
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
138 int start = parser.currentIndex();
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
139 while( !parser.test(quote) ) {
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
140 if( !parser.anyChar() )
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
141 throw exception("unclosed attribute value");
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
142 }
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
143 String value = parser.textFrom(start);
1468
Franklin Schmidt <fschmidt@gmail.com>
parents: 1466
diff changeset
144 value = decode(value);
1466
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
145 parser.match(quote);
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
146 Map.Entry<String,String> attribute = new AbstractMap.SimpleImmutableEntry<String,String>(name,value);
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
147 return parser.success(attribute);
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
148 }
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
149
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
150 private String name() {
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
151 int start = parser.currentIndex();
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
152 if( !matchNameChar() )
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
153 return null;
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
154 while( matchNameChar() );
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
155 return parser.textFrom(start);
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
156 }
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
157
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
158 private boolean matchNameChar() {
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
159 return parser.inCharRange('a','z')
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
160 || parser.inCharRange('A','Z')
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
161 || parser.inCharRange('0','9')
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
162 || parser.anyOf("_.-:")
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
163 ;
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
164 }
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
165
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
166 private void required(String s) throws ParseException {
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
167 if( !parser.match(s) )
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
168 exception("'"+s+"' expected");
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
169 }
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
170
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
171 private void spaces() throws ParseException {
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
172 while( matchSpace() || matchComment() );
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
173 }
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
174
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
175 private boolean matchComment() throws ParseException {
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
176 if( !parser.match("<!--") )
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
177 return false;
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
178 while( !parser.match("-->") ) {
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
179 if( !parser.anyChar() )
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
180 throw exception("unclosed comment");
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
181 }
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
182 return true;
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
183 }
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
184
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
185 private boolean matchSpace() {
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
186 return parser.anyOf(" \t\r\n");
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
187 }
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
188
1790
a8c685a894b4 start xml work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1723
diff changeset
189 private static final Pattern dec = Pattern.compile("&#(\\d+);");
a8c685a894b4 start xml work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1723
diff changeset
190
1468
Franklin Schmidt <fschmidt@gmail.com>
parents: 1466
diff changeset
191 private static String decode(String s) {
1790
a8c685a894b4 start xml work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1723
diff changeset
192 Matcher m = dec.matcher(s);
a8c685a894b4 start xml work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1723
diff changeset
193 StringBuffer sb = new StringBuffer();
a8c685a894b4 start xml work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1723
diff changeset
194 while( m.find() ) {
a8c685a894b4 start xml work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1723
diff changeset
195 String d = m.group(1);
a8c685a894b4 start xml work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1723
diff changeset
196 int i = Integer.parseInt(d);
a8c685a894b4 start xml work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1723
diff changeset
197 d = String.valueOf((char)i);
a8c685a894b4 start xml work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1723
diff changeset
198 m.appendReplacement(sb, d);
a8c685a894b4 start xml work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1723
diff changeset
199 }
a8c685a894b4 start xml work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1723
diff changeset
200 m.appendTail(sb);
a8c685a894b4 start xml work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1723
diff changeset
201 s = sb.toString();
1468
Franklin Schmidt <fschmidt@gmail.com>
parents: 1466
diff changeset
202 s = s.replace("&lt;","<");
Franklin Schmidt <fschmidt@gmail.com>
parents: 1466
diff changeset
203 s = s.replace("&gt;",">");
Franklin Schmidt <fschmidt@gmail.com>
parents: 1466
diff changeset
204 s = s.replace("&quot;","\"");
Franklin Schmidt <fschmidt@gmail.com>
parents: 1466
diff changeset
205 s = s.replace("&apos;","'");
Franklin Schmidt <fschmidt@gmail.com>
parents: 1466
diff changeset
206 s = s.replace("&amp;","&");
Franklin Schmidt <fschmidt@gmail.com>
parents: 1466
diff changeset
207 return s;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1466
diff changeset
208 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 1466
diff changeset
209
1466
670b7d089699 xml support
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
210 }