001: /**
002: * Redistribution and use of this software and associated documentation
003: * ("Software"), with or without modification, are permitted provided
004: * that the following conditions are met:
005: *
006: * 1. Redistributions of source code must retain copyright
007: * statements and notices. Redistributions must also contain a
008: * copy of this document.
009: *
010: * 2. Redistributions in binary form must reproduce the
011: * above copyright notice, this list of conditions and the
012: * following disclaimer in the documentation and/or other
013: * materials provided with the distribution.
014: *
015: * 3. The name "Exolab" must not be used to endorse or promote
016: * products derived from this Software without prior written
017: * permission of Intalio, Inc. For written permission,
018: * please contact info@exolab.org.
019: *
020: * 4. Products derived from this Software may not be called "Exolab"
021: * nor may "Exolab" appear in their names without prior written
022: * permission of Intalio, Inc. Exolab is a registered
023: * trademark of Intalio, Inc.
024: *
025: * 5. Due credit should be given to the Exolab Project
026: * (http://www.exolab.org/).
027: *
028: * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS
029: * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
030: * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
031: * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
032: * INTALIO, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
033: * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
034: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
035: * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
036: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
037: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
038: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
039: * OF THE POSSIBILITY OF SUCH DAMAGE.
040: *
041: * Copyright 1999-2002 (C) Intalio, Inc. All Rights Reserved.
042: *
043: * $Id: Sax2ComponentReader.java 5951 2006-05-30 22:18:48Z bsnyder $
044: */package org.exolab.castor.xml.schema.reader;
045:
046: //-- imported classes and packages
047: import org.exolab.castor.xml.AttributeSet;
048: import org.exolab.castor.xml.Namespaces;
049: import org.exolab.castor.xml.XMLException;
050: import org.exolab.castor.xml.util.AttributeSetImpl;
051: import org.xml.sax.AttributeList;
052: import org.xml.sax.DocumentHandler;
053: import org.xml.sax.Locator;
054: import org.xml.sax.SAXException;
055: import org.xml.sax.SAXParseException;
056:
057: /**
058: * A SAX adapter class for the ComponentReader.
059: *
060: * @author <a href="mailto:kvisco@intalio.com">Keith Visco</a>
061: * @version $Revision: 5951 $ $Date: 2006-04-14 04:14:43 -0600 (Fri, 14 Apr 2006) $
062: **/
063: public final class Sax2ComponentReader implements DocumentHandler,
064: org.xml.sax.ErrorHandler {
065: //-------------------/
066: //- Class Variables -/
067: //-------------------/
068:
069: private static final String XMLNS = "xmlns";
070: private static final String XMLNS_PREFIX = "xmlns:";
071: private static final String XML_PREFIX = "xml";
072:
073: //----------------------/
074: //- Instance Variables -/
075: //----------------------/
076:
077: private ComponentReader _compReader = null;
078:
079: private Namespaces _namespaces = null;
080:
081: //----------------/
082: //- Constructors -/
083: //----------------/
084:
085: public Sax2ComponentReader(ComponentReader compReader) {
086: super ();
087: _compReader = compReader;
088: _namespaces = new Namespaces();
089: } //-- Sax2ComponentReader
090:
091: /**
092: * Processes the attributes and namespace declarations found
093: * in the given SAX AttributeList. The global AttributeSet
094: * is cleared and updated with the attributes. Namespace
095: * declarations are added to the set of namespaces in scope.
096: *
097: * @param atts the AttributeList to process.
098: **/
099: private AttributeSet processAttributeList(AttributeList atts)
100: throws SAXException {
101:
102: if (atts == null)
103: return new AttributeSetImpl(0);
104:
105: //-- process all namespaces first
106: int attCount = 0;
107: boolean[] validAtts = new boolean[atts.getLength()];
108: for (int i = 0; i < validAtts.length; i++) {
109: String attName = atts.getName(i);
110: if (attName.equals(XMLNS)) {
111: _namespaces.addNamespace("", atts.getValue(i));
112: } else if (attName.startsWith(XMLNS_PREFIX)) {
113: String prefix = attName
114: .substring(XMLNS_PREFIX.length());
115: _namespaces.addNamespace(prefix, atts.getValue(i));
116: } else {
117: validAtts[i] = true;
118: ++attCount;
119: }
120: }
121: //-- process validAtts...if any exist
122: AttributeSetImpl attSet = null;
123: if (attCount > 0) {
124: attSet = new AttributeSetImpl(attCount);
125: for (int i = 0; i < validAtts.length; i++) {
126: if (!validAtts[i])
127: continue;
128: String namespace = null;
129: String attName = atts.getName(i);
130: int idx = attName.indexOf(':');
131: if (idx > 0) {
132: String prefix = attName.substring(0, idx);
133: if (!prefix.equals(XML_PREFIX)) {
134: attName = attName.substring(idx + 1);
135: namespace = _namespaces.getNamespaceURI(prefix);
136: if (namespace == null) {
137: String error = "The namespace associated with "
138: + "the prefix '"
139: + prefix
140: + "' could not be resolved.";
141: throw new SAXException(error);
142:
143: }
144: }
145: }
146: attSet.setAttribute(attName, atts.getValue(i),
147: namespace);
148: }
149: } else
150: attSet = new AttributeSetImpl(0);
151:
152: return attSet;
153:
154: } //-- method: processAttributeList
155:
156: //---------------------------------------/
157: //- org.xml.sax.DocumentHandler methods -/
158: //---------------------------------------/
159:
160: public void characters(char[] ch, int start, int length)
161: throws org.xml.sax.SAXException {
162: try {
163: _compReader.characters(ch, start, length);
164: } catch (XMLException ex) {
165: throw new SAXException(ex);
166: }
167:
168: } //-- characters
169:
170: public void endDocument() throws org.xml.sax.SAXException {
171: //-- do nothing
172:
173: } //-- endDocument
174:
175: public void endElement(String name) throws org.xml.sax.SAXException {
176: String namespace = null;
177: int idx = name.indexOf(':');
178: if (idx >= 0) {
179: String prefix = name.substring(0, idx);
180: name = name.substring(idx + 1);
181: namespace = _namespaces.getNamespaceURI(prefix);
182: } else
183: namespace = _namespaces.getNamespaceURI("");
184:
185: //-- remove namespaces
186: if (_namespaces.getParent() != null) {
187: _namespaces = _namespaces.getParent();
188: }
189:
190: try {
191: _compReader.endElement(name, namespace);
192: } catch (XMLException ex) {
193: throw new SAXException(ex);
194: }
195:
196: } //-- endElement
197:
198: public void ignorableWhitespace(char[] ch, int start, int length)
199: throws org.xml.sax.SAXException {
200: //-- do nothing
201:
202: } //-- ignorableWhitespace
203:
204: public void processingInstruction(String target, String data)
205: throws org.xml.sax.SAXException {
206: //-- do nothing
207:
208: } //-- processingInstruction
209:
210: public void setDocumentLocator(Locator locator) {
211: _compReader.setDocumentLocator(locator);
212: } //-- setDocumentLocator
213:
214: public void startDocument() throws org.xml.sax.SAXException {
215: //-- do nothing
216:
217: } //-- startDocument
218:
219: public void startElement(String name, AttributeList atts)
220: throws org.xml.sax.SAXException {
221: //-- create new Namespace scope
222: Namespaces nsDecls = _namespaces.createNamespaces();
223: _namespaces = nsDecls;
224:
225: //-- handle namespaces
226: AttributeSet attSet = processAttributeList(atts);
227:
228: String namespace = null;
229: int idx = name.indexOf(':');
230: if (idx >= 0) {
231: String prefix = name.substring(0, idx);
232: name = name.substring(idx + 1);
233: namespace = _namespaces.getNamespaceURI(prefix);
234: } else
235: namespace = _namespaces.getNamespaceURI("");
236:
237: try {
238: _compReader.startElement(name, namespace, attSet, nsDecls);
239: } catch (XMLException ex) {
240: throw new SAXException(ex);
241: }
242:
243: } //-- startElement
244:
245: //------------------------------------/
246: //- org.xml.sax.ErrorHandler methods -/
247: //------------------------------------/
248:
249: //------------------------------------/
250: //- org.xml.sax.ErrorHandler methods -/
251: //------------------------------------/
252:
253: public void error(SAXParseException exception)
254: throws org.xml.sax.SAXException {
255: String systemId = exception.getSystemId();
256: String err = "Parsing Error : " + exception.getMessage() + '\n'
257: + "Line : " + exception.getLineNumber() + '\n'
258: + "Column : " + exception.getColumnNumber() + '\n';
259: if (systemId != null) {
260: err = "In document: '" + systemId + "'\n" + err;
261: }
262:
263: throw new SAXException(err);
264: } //-- error
265:
266: public void fatalError(SAXParseException exception)
267: throws org.xml.sax.SAXException {
268: String systemId = exception.getSystemId();
269: String err = "Parsing Error : " + exception.getMessage() + '\n'
270: + "Line : " + exception.getLineNumber() + '\n'
271: + "Column : " + exception.getColumnNumber() + '\n';
272: if (systemId != null) {
273: err = "In document: '" + systemId + "'\n" + err;
274: }
275: throw new SAXException(err);
276:
277: } //-- fatalError
278:
279: public void warning(SAXParseException exception)
280: throws org.xml.sax.SAXException {
281: String systemId = exception.getSystemId();
282: String err = "Parsing Error : " + exception.getMessage() + '\n'
283: + "Line : " + exception.getLineNumber() + '\n'
284: + "Column : " + exception.getColumnNumber() + '\n';
285: if (systemId != null) {
286: err = "In document: '" + systemId + "'\n" + err;
287: }
288: throw new SAXException(err);
289:
290: } //-- warning
291:
292: } //-- Sax2ComponentReader
|