001: /*
002: * The Apache Software License, Version 1.1
003: *
004: *
005: * Copyright (c) 1999, 2000 The Apache Software Foundation. All rights
006: * reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * 1. Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * 2. Redistributions in binary form must reproduce the above copyright
016: * notice, this list of conditions and the following disclaimer in
017: * the documentation and/or other materials provided with the
018: * distribution.
019: *
020: * 3. The end-user documentation included with the redistribution,
021: * if any, must include the following acknowledgment:
022: * "This product includes software developed by the
023: * Apache Software Foundation (http://www.apache.org/)."
024: * Alternately, this acknowledgment may appear in the software itself,
025: * if and wherever such third-party acknowledgments normally appear.
026: *
027: * 4. The names "Xerces" and "Apache Software Foundation" must
028: * not be used to endorse or promote products derived from this
029: * software without prior written permission. For written
030: * permission, please contact apache@apache.org.
031: *
032: * 5. Products derived from this software may not be called "Apache",
033: * nor may "Apache" appear in their name, without prior written
034: * permission of the Apache Software Foundation.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
040: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: * ====================================================================
049: *
050: * This software consists of voluntary contributions made by many
051: * individuals on behalf of the Apache Software Foundation and was
052: * originally based on software copyright (c) 1999, International
053: * Business Machines, Inc., http://www.apache.org. For more
054: * information on the Apache Software Foundation, please see
055: * <http://www.apache.org/>.
056: */
057:
058: package dom.wrappers;
059:
060: import java.io.IOException;
061:
062: import dom.DOMParserWrapper;
063:
064: import org.w3c.dom.Document;
065:
066: import org.xml.sax.ErrorHandler;
067: import org.xml.sax.SAXException;
068: import org.xml.sax.SAXParseException;
069: import org.xml.sax.SAXNotRecognizedException;
070: import org.xml.sax.SAXNotSupportedException;
071:
072: import com.knowgate.debug.DebugFile;
073:
074: /**
075: * Wraps the Xerces DOM parser and extends NonValidatingDOMParser
076: * @version 2.1
077: */
078: public class DOMParser implements DOMParserWrapper, ErrorHandler {
079:
080: /** Parser. */
081: org.apache.xerces.parsers.DOMParser parser = new org.apache.xerces.parsers.DOMParser();
082:
083: //
084: // Constructors
085: //
086:
087: /** Default constructor. */
088: public DOMParser() {
089: try {
090: parser.setFeature("http://xml.org/sax/features/validation",
091: true);
092:
093: } catch (SAXException e) {
094: if (DebugFile.trace)
095: DebugFile
096: .writeln("DOMParser.<init> error in setting up parser feature http://xml.org/sax/features/validation");
097: }
098:
099: parser.setErrorHandler(this );
100: }
101:
102: //
103: // DOMParserWrapper methods
104: //
105:
106: /**
107: * Parses the specified URI and returns the document.
108: * @param uri File uri
109: * @throws IOException
110: * @throws SAXException
111: */
112: public Document parse(String uri) throws IOException, SAXException {
113: parser.parse(uri);
114: return parser.getDocument();
115:
116: } // parse(String):Document
117:
118: /**
119: * Parses the specified InputSource and returns the document.
120: * @param saxsource org.xml.sax.InputSource
121: * @throws IOException
122: * @throws SAXException
123: */
124: public Document parse(org.xml.sax.InputSource saxsource)
125: throws IOException, SAXException {
126: parser.parse(saxsource);
127: return parser.getDocument();
128: } // parse(String):Document
129:
130: /**
131: *
132: * @param featureId
133: * @param state
134: * @throws SAXNotRecognizedException
135: * @throws SAXNotSupportedException
136: */
137: public void setFeature(String featureId, boolean state)
138: throws SAXNotRecognizedException, SAXNotSupportedException {
139: parser.setFeature(featureId, state);
140: }
141:
142: //
143: // ErrorHandler methods
144: //
145:
146: /** Warning. */
147: public void warning(SAXParseException ex) {
148: DebugFile.writeln("[Warning] " + getLocationString(ex) + ": "
149: + ex.getMessage());
150: }
151:
152: /** Error. */
153: public void error(SAXParseException ex) {
154: DebugFile.writeln("[Error] " + getLocationString(ex) + ": "
155: + ex.getMessage());
156: }
157:
158: /** Fatal error. */
159: public void fatalError(SAXParseException ex) throws SAXException {
160: DebugFile.writeln("[Fatal Error] " + getLocationString(ex)
161: + ": " + ex.getMessage());
162: throw ex;
163: }
164:
165: //
166: // Private methods
167: //
168:
169: /** Returns a string of the location. */
170: private String getLocationString(SAXParseException ex) {
171: StringBuffer str = new StringBuffer();
172:
173: String systemId = ex.getSystemId();
174: if (systemId != null) {
175: int index = systemId.lastIndexOf('/');
176: if (index != -1)
177: systemId = systemId.substring(index + 1);
178: str.append(systemId);
179: }
180: str.append(':');
181: str.append(ex.getLineNumber());
182: str.append(':');
183: str.append(ex.getColumnNumber());
184:
185: return str.toString();
186:
187: } // getLocationString(SAXParseException):String
188:
189: public DocumentInfo getDocumentInfo() {
190: return null;
191: }
192: } // class DOMParser
|