001: // XMLSubsetReader.java
002: // $Id: XMLSubsetReader.java,v 1.3 2002/06/12 09:40:12 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.w3c.util.LookupTable;
015:
016: import org.xml.sax.AttributeList;
017: import org.xml.sax.DocumentHandler;
018: import org.xml.sax.ErrorHandler;
019: import org.xml.sax.HandlerBase;
020: import org.xml.sax.InputSource;
021: import org.xml.sax.Parser;
022: import org.xml.sax.SAXException;
023: import org.xml.sax.SAXParseException;
024:
025: import org.w3c.tools.resources.Resource;
026: import org.w3c.tools.resources.AttributeHolder;
027: import org.w3c.tools.resources.UnknownResource;
028: import org.w3c.tools.resources.ResourceFrame;
029: import org.w3c.tools.resources.Attribute;
030: import org.w3c.tools.resources.SimpleAttribute;
031: import org.w3c.tools.resources.ArrayAttribute;
032: import org.w3c.tools.resources.serialization.SerializationException;
033:
034: /**
035: * @version $Revision: 1.3 $
036: * @author Benoît Mahé (bmahe@w3.org)
037: */
038: public class XMLSubsetReader extends HandlerBase implements JigXML {
039:
040: Vector tables = null;
041: LookupTable table = null;
042: LookupTable lookuptables[] = null;
043: String attributes[] = null;
044: String attribute = null;
045: boolean readAttr = false;
046: int level = 0;
047: int len = 0;
048:
049: Parser parser = null;
050: Reader reader = null;
051: boolean isavalue = false;
052:
053: public void startElement(String name, AttributeList attributes)
054: throws SAXException {
055: String iname = name.intern();
056: if (iname == iRESOURCE_TAG) {
057: if (level == 0) {
058: table = new LookupTable(len);
059: }
060: } else if (iname == iATTRIBUTE_TAG) {
061: if (level == 0) {
062: String attribute = attributes.getValue(NAME_ATTR);
063: readAttr = load(attribute);
064: }
065: } else if (iname == iARRAY_TAG) {
066: level++;
067: } else if (iname == iRESARRAY_TAG) {
068: level++;
069: } else if (iname == iVALUE_TAG) {
070: isavalue = true;
071: }
072: }
073:
074: public void endElement(String name) throws SAXException {
075: String iname = name.intern();
076: if (iname == iRESOURCE_TAG) {
077: if (level == 0) {
078: tables.addElement(table);
079: }
080: } else if (iname == iATTRIBUTE_TAG) {
081: if (level == 0) {
082: readAttr = false;
083: }
084: } else if (iname == iARRAY_TAG) {
085: level--;
086: } else if (iname == iRESARRAY_TAG) {
087: level--;
088: } else if (iname == iVALUE_TAG) {
089: isavalue = false;
090: }
091: }
092:
093: public void startDocument() throws SAXException {
094: tables = new Vector(10);
095: }
096:
097: public void endDocument() throws SAXException {
098: lookuptables = new LookupTable[tables.size()];
099: tables.copyInto(lookuptables);
100: try {
101: reader.close();
102: } catch (IOException ex) {
103: ex.printStackTrace();
104: }
105: }
106:
107: public void characters(char ch[], int start, int length)
108: throws SAXException {
109: if ((level == 0) && readAttr) {
110: String value = new String(ch, start, length);
111: if (value.equals(NULL)) {
112: table.put(attribute, value);
113: }
114: }
115: }
116:
117: public void warning(SAXParseException e) throws SAXException {
118: System.out.println("WARNING in element " + e.getPublicId());
119: System.out.println("Sys : " + e.getSystemId());
120: System.out.println("Line : " + e.getLineNumber());
121: System.out.println("Col : " + e.getColumnNumber());
122: e.printStackTrace();
123: }
124:
125: public void error(SAXParseException e) throws SAXException {
126: System.out.println("ERROR in element " + e.getPublicId());
127: System.out.println("Sys : " + e.getSystemId());
128: System.out.println("Line : " + e.getLineNumber());
129: System.out.println("Col : " + e.getColumnNumber());
130: e.printStackTrace();
131: }
132:
133: public void fatalError(SAXParseException e) throws SAXException {
134: System.out.println("FATAL ERROR in element " + e.getPublicId());
135: System.out.println("Sys : " + e.getSystemId());
136: System.out.println("Line : " + e.getLineNumber());
137: System.out.println("Col : " + e.getColumnNumber());
138: e.printStackTrace();
139: }
140:
141: protected void parse() throws SAXException, IOException {
142: parser.setDocumentHandler(this );
143: parser.setErrorHandler(this );
144: parser.parse(new InputSource(reader));
145: }
146:
147: /**
148: * Read a subset of the resource attributes. (excluding array and frames)
149: * @return a LookupTable
150: * <attribute_name (String), attribute_value (String)>
151: */
152: public LookupTable[] readAttributeTables() throws IOException,
153: SerializationException {
154: try {
155: parse();
156: } catch (SAXException ex) {
157: ex.printStackTrace();
158: return new LookupTable[0];
159: }
160: return lookuptables;
161: }
162:
163: private boolean load(String attribute) {
164: for (int i = 0; i < len; i++) {
165: if (attributes[i].equals(attribute))
166: return true;
167: }
168: return false;
169: }
170:
171: public XMLSubsetReader(Reader reader, Parser parser,
172: String attributes[]) {
173: this.reader = reader;
174: this.parser = parser;
175: this.attributes = attributes;
176: this.len = attributes.length;
177: }
178:
179: }
|