1 package dolda.jsvc.next;
9 public final Document doc = DomUtil.document(null, "dummy");
10 public final PeekReader in;
12 private State(Reader in) {
13 this.in = new PeekReader(in);
17 private static boolean namechar(char c) {
18 return((c == ':') || (c == '_') || (c == '$') || (c == '.') || (c == '-') || ((c >= '0') && (c <= '9')) || ((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z')));
21 protected String entity(String name) {
22 if(name.equals("amp"))
28 if(name.equals("apos"))
30 if(name.equals("quot"))
35 protected Element makenode(Document doc, String name) {
36 return(doc.createElementNS(null, name));
39 protected Attr makeattr(Document doc, Element el, String name, String val) {
40 Attr a = doc.createAttributeNS(el.getNamespaceURI(), name);
45 protected Attr makeattr(Document doc, Element el, String name) {
46 return(doc.createAttributeNS(el.getNamespaceURI(), name));
49 protected String name(State s) throws IOException {
50 StringBuilder buf = new StringBuilder();
55 } else if(namechar((char)c)) {
56 buf.append((char)s.in.read());
62 throw(new ParseException("Expected name, got `" + printable(s.in.peek()) + "'"));
63 return(buf.toString());
66 protected String entity(State s) throws IOException {
69 throw(new ParseException("Expected `&' while reading entity, got `" + printable(c) + "'"));
73 throw(new ParseException("Expected `;' while reading entity, got `" + printable(c) + "'"));
77 protected Attr attribute(State s, Element el) throws IOException {
82 throw(new ParseException("Expected `=' while reading attribute, got `" + printable(c) + "'"));
85 if((qt != '"') && (qt != '\''))
86 throw(new ParseException("Expected double or single quote while reading attribute, got `" + printable(qt) + "'"));
87 StringBuilder buf = new StringBuilder();
91 throw(new ParseException("Unexpected end-of-file while reading attribute value"));
96 buf.append(entity(s));
98 buf.append((char)s.in.read());
101 return(makeattr(s.doc, el, nm, buf.toString()));
104 protected Element element(State s) throws IOException {
105 Element n = makenode(s.doc, name(s));
107 int c = s.in.peek(true);
109 throw(new ParseException("Unexpected end-of-file while parsing start tag"));
110 } else if(c == '>') {
113 } else if(c == '/') {
118 throw(new ParseException("Unexpected character `" + printable(c) + "' encountered in end of empty tag"));
120 } else if(namechar((char)c)) {
121 n.setAttributeNodeNS(attribute(s, n));
123 throw(new ParseException("Unexpected character `" + printable(c) + "' encountered in start tag"));
130 } else if(c == '<') {
137 if(!nm.equals(n.getTagName()))
138 throw(new ParseException("Unexpected end tag for `" + nm + "' while parsing `" + n.getTagName() + "'"));
139 if(s.in.peek(true) != '>')
140 throw(new ParseException("Expected `>' while reading end tag, got `" + printable(c) + "'"));
144 n.appendChild(stag(s));
147 n.appendChild(text(s));
153 protected Comment comment(State s) throws IOException {
154 if((s.in.read() != '!') ||
155 (s.in.read() != '-') ||
156 (s.in.read() != '-'))
157 throw(new ParseException("Illegal start of comment"));
158 StringBuilder buf = new StringBuilder();
162 throw(new ParseException("Unexpected end-of-file while parsing comment"));
163 } else if(c == '-') {
165 if(s.in.peek() == '-') {
167 if(s.in.peek() == '>') {
177 buf.append((char)s.in.read());
180 return(s.doc.createComment(buf.toString()));
183 protected Node stag(State s) throws IOException {
184 int c = s.in.peek(true);
186 throw(new ParseException("Unexpected end-of-file while parsing tag type"));
187 } else if(c == '!') {
194 protected Text text(State s) throws IOException {
195 StringBuilder buf = new StringBuilder();
200 } else if(c == '<') {
202 } else if(c == '&') {
203 buf.append(entity(s));
205 buf.append((char)s.in.read());
208 return(s.doc.createTextNode(buf.toString()));
211 public DocumentFragment parse(Reader in) throws IOException {
212 State s = new State(in);
213 DocumentFragment frag = s.doc.createDocumentFragment();
218 } else if(c == '<') {
220 frag.appendChild(stag(s));
222 frag.appendChild(text(s));
227 private static String printable(int c) {
231 return(String.format("\\%03o", (int)c));
232 return(Character.toString((char)c));
235 public static void main(String[] args) throws Exception {
236 Parser p = new Parser();
237 DocumentFragment f = p.parse(new FileReader(args[0]));
238 javax.xml.transform.TransformerFactory fac = javax.xml.transform.TransformerFactory.newInstance();
239 fac.setAttribute("indent-number", 2);
240 javax.xml.transform.Transformer t = fac.newTransformer();
241 t.setOutputProperty(javax.xml.transform.OutputKeys.INDENT, "yes");
242 t.transform(new javax.xml.transform.dom.DOMSource(f), new javax.xml.transform.stream.StreamResult(System.out));
243 System.out.println(t.getClass());