001: // XMLDescrReader.java
002: // $Id: XMLDescrReader.java,v 1.9 2007/04/04 12:22:10 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.UnknownResource;
023: import org.w3c.tools.resources.ResourceFrame;
024: import org.w3c.tools.resources.Attribute;
025: import org.w3c.tools.resources.SimpleAttribute;
026: import org.w3c.tools.resources.ArrayAttribute;
027: import org.w3c.tools.resources.serialization.SerializationException;
028: import org.w3c.tools.resources.serialization.AttributeDescription;
029: import org.w3c.tools.resources.serialization.ResourceDescription;
030: import org.w3c.tools.resources.serialization.EmptyDescription;
031:
032: /**
033: * @version $Revision: 1.9 $
034: * @author Benoît Mahé (bmahe@w3.org)
035: */
036: public class XMLDescrReader extends HandlerBase implements JigXML {
037:
038: Parser parser = null;
039: ResourceDescription resources[] = null;
040:
041: Reader reader = null;
042: Stack resourceSetStack = null;
043: Stack resourceStack = null;
044: Stack defsStack = null;
045: Stack classes = null;
046: Stack interfaces = null;
047: Stack FAStack = null;
048: Vector children = null;
049: SimpleAttribute currentS = null;
050: ArrayAttribute currentA = null;
051: int length = -1;
052: boolean isavalue = false;
053: String array[] = null;
054: int arrayidx = -1;
055: StringBuffer characters = null;
056: String charvalue = null;
057:
058: public void startElement(String name, AttributeList attributes)
059: throws SAXException {
060: endCharacters();
061: String iname = name.intern();
062: if (iname == iINHERIT_TAG) {
063: Vector vclasses = (Vector) classes.peek();
064: vclasses.addElement(attributes.getValue(CLASS_ATTR));
065: } else if (iname == iIMPLEMENTS_TAG) {
066: Vector vinterfaces = (Vector) interfaces.peek();
067: vinterfaces.addElement(attributes.getValue(CLASS_ATTR));
068: } else if (iname == iCHILDREN_TAG) {
069: length = Integer.parseInt(attributes.getValue(LENGTH_ATTR));
070: array = new String[length];
071: arrayidx = 0;
072: } else if (iname == iDESCR_TAG) {
073: String resourceclass = attributes.getValue(CLASS_ATTR);
074: String identifier = attributes.getValue(NAME_ATTR);
075: if (identifier.equals(NULL))
076: identifier = null;
077: resourceStack.push(new EmptyDescription(resourceclass,
078: identifier));
079:
080: } else if (iname == iRESOURCE_TAG) {
081: String resourceclass = attributes.getValue(CLASS_ATTR);
082: resourceStack.push(new ResourceDescription(resourceclass));
083: defsStack.push(new Vector(10));
084: Vector vcls = new Vector(8);
085: vcls.addElement(resourceclass);
086: classes.push(vcls);
087: interfaces.push(new Vector(8));
088: } else if (iname == iATTRIBUTE_TAG) {
089: String attrclass = attributes.getValue(CLASS_ATTR);
090: try {
091: Class c = Class.forName(attrclass);
092: currentS = (SimpleAttribute) c.newInstance();
093: currentS.setName(attributes.getValue(NAME_ATTR));
094: currentS.setFlag(attributes.getValue(FLAG_ATTR));
095: } catch (Exception ex) {
096: ex.printStackTrace();
097: currentS = null;
098: }
099: } else if (iname == iARRAY_TAG) {
100: String attrclass = attributes.getValue(CLASS_ATTR);
101: try {
102: Class c = Class.forName(attrclass);
103: currentA = (ArrayAttribute) c.newInstance();
104: currentA.setName(attributes.getValue(NAME_ATTR));
105: currentA.setFlag(attributes.getValue(FLAG_ATTR));
106: length = Integer.parseInt(attributes
107: .getValue(LENGTH_ATTR));
108: array = new String[length];
109: arrayidx = 0;
110: } catch (Exception ex) {
111: ex.printStackTrace();
112: currentA = null;
113: }
114: } else if (iname == iRESARRAY_TAG) {
115: resourceSetStack.push(new Vector(10));
116: String attrclass = attributes.getValue(CLASS_ATTR);
117: try {
118: Class c = Class.forName(attrclass);
119: Attribute attr = (Attribute) c.newInstance();
120: attr.setName(attributes.getValue(NAME_ATTR));
121: FAStack.push(attr);
122: } catch (Exception ex) {
123: ex.printStackTrace();
124: throw new SAXException(
125: "Unable to create an instance of " + attrclass);
126: }
127: } else if ((iname == iCHILD_TAG) || (iname == iVALUE_TAG)) {
128: isavalue = true;
129: }
130: }
131:
132: public void endElement(String name) throws SAXException {
133: endCharacters();
134: String iname = name.intern();
135: if (iname == iRESOURCE_TAG) {
136: ResourceDescription res = (ResourceDescription) resourceStack
137: .pop();
138: res.setAttributeDescriptions((Vector) defsStack.pop());
139: Vector vresources = (Vector) resourceSetStack.peek();
140: vresources.addElement(res);
141: Vector vclasses = (Vector) classes.pop();
142: res.setClassHierarchy(vclasses);
143: Vector vinterfaces = (Vector) interfaces.pop();
144: res.setInterfaces(vinterfaces);
145: } else if (iname == iCHILDREN_TAG) {
146: ResourceDescription res = (ResourceDescription) resourceStack
147: .peek();
148: res.setChildren(array);
149: } else if (iname == iDESCR_TAG) {
150: ResourceDescription res = (ResourceDescription) resourceStack
151: .pop();
152: Vector vresources = (Vector) resourceSetStack.peek();
153: vresources.addElement(res);
154: } else if (iname == iATTRIBUTE_TAG) {
155: currentS = null;
156: } else if (iname == iARRAY_TAG) {
157: AttributeDescription ad = new AttributeDescription(
158: currentA, currentA.unpickle(array));
159: Vector attrs = (Vector) defsStack.peek();
160: attrs.addElement(ad);
161: currentA = null;
162: } else if (iname == iRESARRAY_TAG) {
163: Vector vframes = (Vector) resourceSetStack.pop();
164: ResourceDescription frames[] = new ResourceDescription[vframes
165: .size()];
166: vframes.copyInto(frames);
167: AttributeDescription ad = new AttributeDescription(
168: (Attribute) FAStack.pop(), frames);
169: Vector attrs = (Vector) defsStack.peek();
170: attrs.addElement(ad);
171: } else if ((iname == iCHILD_TAG) || (iname == iVALUE_TAG)) {
172: isavalue = false;
173: }
174: }
175:
176: public void startDocument() throws SAXException {
177: defsStack = new Stack();
178: classes = new Stack();
179: interfaces = new Stack();
180: FAStack = new Stack();
181: resourceStack = new Stack();
182: resourceSetStack = new Stack();
183: resourceSetStack.push(new Vector(10));
184: }
185:
186: public void endDocument() throws SAXException {
187: Vector vresources = (Vector) resourceSetStack.pop();
188: resources = new ResourceDescription[vresources.size()];
189: vresources.copyInto(resources);
190: try {
191: reader.close();
192: } catch (IOException ex) {
193: ex.printStackTrace();
194: }
195: }
196:
197: public void characters(char ch[], int start, int length)
198: throws SAXException {
199: if (charvalue == null) {
200: charvalue = new String(ch, start, length);
201: } else if (length > 0) {
202: if (characters == null) {
203: characters = new StringBuffer(charvalue);
204: }
205: characters.append(ch, start, length);
206: }
207: }
208:
209: private void endCharacters() {
210: String value;
211: if (charvalue == null) {
212: return;
213: }
214: if (characters == null) {
215: value = charvalue;
216: } else {
217: value = characters.toString();
218: }
219: characters = null;
220: charvalue = null;
221: if (currentS != null) {
222: AttributeDescription ad = null;
223: if (value.equals(NULL))
224: ad = new AttributeDescription(currentS, null);
225: else
226: ad = new AttributeDescription(currentS, currentS
227: .unpickle(value));
228: Vector attrs = (Vector) defsStack.peek();
229: attrs.addElement(ad);
230: } else if (isavalue) {
231: array[arrayidx++] = value;
232: }
233: }
234:
235: public void warning(SAXParseException e) throws SAXException {
236: System.out.println("WARNING in element " + e.getPublicId());
237: System.out.println("Sys : " + e.getSystemId());
238: System.out.println("Line : " + e.getLineNumber());
239: System.out.println("Col : " + e.getColumnNumber());
240: e.printStackTrace();
241: }
242:
243: public void error(SAXParseException e) throws SAXException {
244: System.out.println("ERROR in element " + e.getPublicId());
245: System.out.println("Sys : " + e.getSystemId());
246: System.out.println("Line : " + e.getLineNumber());
247: System.out.println("Col : " + e.getColumnNumber());
248: e.printStackTrace();
249: }
250:
251: public void fatalError(SAXParseException e) throws SAXException {
252: System.out.println("FATAL ERROR in element " + e.getPublicId());
253: System.out.println("Sys : " + e.getSystemId());
254: System.out.println("Line : " + e.getLineNumber());
255: System.out.println("Col : " + e.getColumnNumber());
256: e.printStackTrace();
257: }
258:
259: protected void parse() throws SAXException, IOException {
260: try {
261: parser.setDocumentHandler(this );
262: parser.setErrorHandler(this );
263: parser.parse(new InputSource(reader));
264: } catch (IOException ex) {
265: try {
266: reader.close();
267: } catch (IOException ioex) {
268: }
269: throw ex;
270: } catch (SAXException sex) {
271: try {
272: reader.close();
273: } catch (IOException ioex) {
274: }
275: throw sex;
276: }
277: }
278:
279: public ResourceDescription[] readResourceDescriptions()
280: throws IOException, SerializationException {
281: try {
282: parse();
283: } catch (SAXException ex) {
284: ex.printStackTrace();
285: return new ResourceDescription[0];
286: }
287:
288: return resources;
289: }
290:
291: public XMLDescrReader(Reader reader, Parser parser) {
292: this.reader = reader;
293: this.parser = parser;
294: }
295:
296: }
|