001: // XMLReader.java
002: // $Id: XMLReader.java,v 1.10 2007/04/04 12:22:30 ylafon Exp $
003: // (c) COPYRIGHT MIT, INRIA and Keio, 1999.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005: package org.w3c.tools.resources.serialization.xml;
006:
007: import java.io.IOException;
008: import java.io.Reader;
009:
010: import java.util.Vector;
011: import java.util.Hashtable;
012: import java.util.Stack;
013:
014: import org.xml.sax.AttributeList;
015: import org.xml.sax.HandlerBase;
016: import org.xml.sax.InputSource;
017: import org.xml.sax.Parser;
018: import org.xml.sax.SAXException;
019: import org.xml.sax.SAXParseException;
020:
021: import org.w3c.tools.resources.Resource;
022: import org.w3c.tools.resources.AttributeHolder;
023: import org.w3c.tools.resources.UnknownResource;
024: import org.w3c.tools.resources.ResourceFrame;
025: import org.w3c.tools.resources.Attribute;
026: import org.w3c.tools.resources.SimpleAttribute;
027: import org.w3c.tools.resources.ArrayAttribute;
028: import org.w3c.tools.resources.serialization.SerializationException;
029:
030: /**
031: * @version $Revision: 1.10 $
032: * @author Benoît Mahé (bmahe@w3.org)
033: */
034: public class XMLReader extends HandlerBase implements JigXML {
035:
036: Parser parser = null;
037: AttributeHolder holders[] = null;
038: Reader reader = null;
039: Stack resourceSetStack = null;
040: Stack resourceStack = null;
041: Stack defsStack = null;
042: SimpleAttribute currentS = null;
043: ArrayAttribute currentA = null;
044: Stack faNameStack = null;
045: int length = -1;
046: boolean isavalue = false;
047: String array[] = null;
048: int arrayidx = -1;
049: StringBuffer characters = null;
050: String charvalue = null;
051:
052: public void startElement(String name, AttributeList attributes)
053: throws SAXException {
054: endCharacters();
055: String iname = name.intern();
056: if (iname == iRESOURCE_TAG) {
057: String resourceclass = attributes.getValue(CLASS_ATTR);
058: try {
059: Class c = Class.forName(resourceclass);
060: resourceStack.push(c.newInstance());
061: } catch (Exception ex) {
062: UnknownResource unknown = new UnknownResource();
063: resourceStack.push(unknown);
064: }
065: Hashtable defs = new Hashtable(5);
066: defsStack.push(defs);
067: } else if (iname == iATTRIBUTE_TAG) {
068: String attrclass = attributes.getValue(CLASS_ATTR);
069: try {
070: Class c = Class.forName(attrclass);
071: currentS = (SimpleAttribute) c.newInstance();
072: currentS.setName(attributes.getValue(NAME_ATTR));
073: currentS.setFlag(attributes.getValue(FLAG_ATTR));
074: } catch (Exception ex) {
075: ex.printStackTrace();
076: currentS = null;
077: }
078: } else if (iname == iARRAY_TAG) {
079: String attrclass = attributes.getValue(CLASS_ATTR);
080: try {
081: Class c = Class.forName(attrclass);
082: currentA = (ArrayAttribute) c.newInstance();
083: currentA.setName(attributes.getValue(NAME_ATTR));
084: currentA.setFlag(attributes.getValue(FLAG_ATTR));
085: length = Integer.parseInt(attributes
086: .getValue(LENGTH_ATTR));
087: array = new String[length];
088: arrayidx = 0;
089: } catch (Exception ex) {
090: ex.printStackTrace();
091: currentA = null;
092: }
093: } else if (iname == iRESARRAY_TAG) {
094: resourceSetStack.push(new Vector(10));
095: faNameStack.push(attributes.getValue(NAME_ATTR));
096: } else if (iname == iVALUE_TAG) {
097: isavalue = true;
098: }
099: }
100:
101: public void endElement(String name) throws SAXException {
102: endCharacters();
103: String iname = name.intern();
104: if (iname == iRESOURCE_TAG) {
105: AttributeHolder res = (AttributeHolder) resourceStack.pop();
106: res.pickleValues((Hashtable) defsStack.pop());
107: Vector vresources = (Vector) resourceSetStack.peek();
108: vresources.addElement(res);
109: } else if (iname == iATTRIBUTE_TAG) {
110: currentS = null;
111: } else if (iname == iARRAY_TAG) {
112: Hashtable defs = (Hashtable) defsStack.peek();
113: Object value = currentA.unpickle(array);
114: if (value != null)
115: defs.put(currentA.getName(), value);
116: currentA = null;
117: } else if (iname == iRESARRAY_TAG) {
118: Hashtable defs = (Hashtable) defsStack.peek();
119: Vector vframes = (Vector) resourceSetStack.pop();
120: ResourceFrame frames[] = new ResourceFrame[vframes.size()];
121: vframes.copyInto(frames);
122: defs.put((String) faNameStack.pop(), frames);
123: } else if (iname == iVALUE_TAG) {
124: isavalue = false;
125: }
126: }
127:
128: public void startDocument() throws SAXException {
129: defsStack = new Stack();
130: faNameStack = new Stack();
131: resourceStack = new Stack();
132: resourceSetStack = new Stack();
133: resourceSetStack.push(new Vector(10));
134: }
135:
136: public void endDocument() throws SAXException {
137: Vector vresources = (Vector) resourceSetStack.pop();
138: holders = new AttributeHolder[vresources.size()];
139: vresources.copyInto(holders);
140: try {
141: reader.close();
142: } catch (IOException ex) {
143: ex.printStackTrace();
144: }
145: }
146:
147: public void characters(char ch[], int start, int length)
148: throws SAXException {
149: if (charvalue == null) {
150: charvalue = new String(ch, start, length);
151: } else if (length > 0) {
152: if (characters == null) {
153: characters = new StringBuffer(charvalue);
154: }
155: characters.append(ch, start, length);
156: }
157: }
158:
159: private void endCharacters() {
160: String value;
161: if (charvalue == null) {
162: return;
163: }
164: if (characters == null) {
165: value = charvalue;
166: } else {
167: value = characters.toString();
168: }
169: characters = null;
170: charvalue = null;
171: if (currentS != null) {
172: Hashtable defs = (Hashtable) defsStack.peek();
173: if (!value.equals(NULL)) {
174: defs.put(currentS.getName(), currentS.unpickle(value));
175: }
176: } else if ((currentA != null) && (isavalue)) {
177: array[arrayidx++] = value;
178: }
179: }
180:
181: public void warning(SAXParseException e) throws SAXException {
182: System.out.println("WARNING in element " + e.getPublicId());
183: System.out.println("Sys : " + e.getSystemId());
184: System.out.println("Line : " + e.getLineNumber());
185: System.out.println("Col : " + e.getColumnNumber());
186: e.printStackTrace();
187: }
188:
189: public void error(SAXParseException e) throws SAXException {
190: System.out.println("ERROR in element " + e.getPublicId());
191: System.out.println("Sys : " + e.getSystemId());
192: System.out.println("Line : " + e.getLineNumber());
193: System.out.println("Col : " + e.getColumnNumber());
194: e.printStackTrace();
195: }
196:
197: public void fatalError(SAXParseException e) throws SAXException {
198: System.out.println("FATAL ERROR in element " + e.getPublicId());
199: System.out.println("Sys : " + e.getSystemId());
200: System.out.println("Line : " + e.getLineNumber());
201: System.out.println("Col : " + e.getColumnNumber());
202: e.printStackTrace();
203: }
204:
205: protected void parse() throws SAXException, IOException {
206: try {
207: parser.setDocumentHandler(this );
208: parser.setErrorHandler(this );
209: parser.parse(new InputSource(reader));
210: // } catch (IOException ex) {
211: // try { reader.close(); } catch (IOException ioex) {}
212: // throw ex;
213: // } catch (SAXException sex) {
214: // try { reader.close(); } catch (IOException ioex) {}
215: // throw sex;
216: } finally {
217: try {
218: reader.close();
219: } catch (IOException ioex) {
220: }
221: }
222: }
223:
224: public Resource[] readResources() throws IOException,
225: SerializationException {
226: try {
227: parse();
228: } catch (SAXException ex) {
229: ex.printStackTrace();
230: return new Resource[0];
231: }
232:
233: int len = holders.length;
234: Resource crs[] = new Resource[len];
235: for (int i = 0; i < len; i++)
236: crs[i] = (Resource) holders[i];
237: return crs;
238: }
239:
240: public AttributeHolder[] readAttributeHolders() throws IOException,
241: SerializationException {
242: try {
243: parse();
244: } catch (SAXException ex) {
245: ex.printStackTrace();
246: return new AttributeHolder[0];
247: }
248:
249: return holders;
250: }
251:
252: public XMLReader(Reader reader, Parser parser) {
253: this.reader = reader;
254: this.parser = parser;
255: }
256:
257: }
|