1 /*
2 * Created on Feb 24, 2004
3 *
4 */
5 package jsdsi.xml;
6
7 import jsdsi.sexp.*;
8 import java.io.*;
9 import java.util.*;
10 import javax.xml.parsers.*;
11 import org.xml.sax.*;
12 import org.xml.sax.helpers.*;
13
14 /***
15 * @author Sameer Ajmani
16 **/
17 public class XmlReader extends FilterReader
18 implements SexpInput
19 {
20 private static class Handler extends DefaultHandler
21 {
22 private static class ListMaker {
23 private String type;
24 private List l = new ArrayList();
25 public ListMaker(final String t) {
26 type = t;
27 }
28 public void add(Sexp s) {
29 l.add(s);
30 }
31 public Sexp toSexp()
32 {
33 if (type.startsWith(XmlWriter.starTagPrefix)) {
34 if (type.substring(XmlWriter.starTagPrefix.length()).length()
35 == 0) {
36 return new SexpList(new SexpString("*"),
37 (Sexp[])l.toArray(new Sexp[0]));
38 }
39 ArrayList l2 = new ArrayList();
40 l2.add(new SexpString
41 (type.substring(XmlWriter.starTagPrefix.length())));
42 l2.addAll(l);
43 return new SexpList(new SexpString("*"),
44 (Sexp[])l2.toArray(new Sexp[0]));
45 }
46 return new SexpList(new SexpString(type),
47 (Sexp[])l.toArray(new Sexp[0]));
48 }
49 }
50 StringBuffer buf = new StringBuffer();
51 Stack stack = new Stack();
52 Sexp sexp = null;
53 public Handler()
54 {
55 }
56 public Sexp toSexp()
57 {
58 return sexp;
59 }
60 public void characters(char[] ch,
61 int start,
62 int length)
63 throws SAXException
64 {
65 buf.append(ch, start, length);
66 }
67 private void consumeBuffer()
68 throws SAXException
69 {
70 if (buf.length() == 0 ) return;
71 if (stack.empty()) return;
72 ListMaker top = (ListMaker)stack.peek();
73 SexpInputStream in = new SexpInputStream
74 (new ByteArrayInputStream
75 (Sexp.encodeString(buf.toString())));
76 try {
77 while (true) {
78 top.add(in.readSexp());
79 }
80 } catch (EOFException e) {
81 // ok
82 } catch (IOException e) {
83 throw new SAXException(e);
84 } catch (SexpException e) {
85 throw new SAXException(e);
86 } finally {
87 buf = new StringBuffer();
88 }
89 }
90 public void startElement(String uri,
91 String localName,
92 String qName,
93 Attributes attributes)
94 throws SAXException
95 {
96 consumeBuffer();
97 ListMaker top = new ListMaker(qName);
98 stack.push(top);
99 }
100 public void endElement(String uri,
101 String localName,
102 String qName)
103 throws SAXException
104 {
105 consumeBuffer();
106 ListMaker top = (ListMaker)stack.pop();
107 if (stack.empty()) {
108 sexp = top.toSexp();
109 } else {
110 ((ListMaker)stack.peek()).add(top.toSexp());
111 }
112 }
113 }
114 private InputSource is;
115 private SAXParser p;
116 /***
117 * @param in
118 */
119 public XmlReader(Reader in)
120 {
121 super(in);
122 is = new InputSource(in);
123 try {
124 p = SAXParserFactory.newInstance().newSAXParser();
125 } catch (SAXException e) {
126 throw new Error(e);
127 } catch (ParserConfigurationException e) {
128 throw new Error(e);
129 }
130 }
131 public Sexp readSexp() throws IOException
132 {
133 try {
134 Handler handler = new Handler();
135 p.parse(is, handler);
136 return handler.toSexp();
137 } catch (SAXException e) {
138 throw (IOException)new IOException().initCause(e);
139 }
140 }
141 }
This page was automatically generated by Maven