001: package com.salmonllc.xml;
002:
003: /////////////////////////
004: //$Archive: /JADE/SourceCode/com/salmonllc/xml/DOMParser.java $
005: //$Author: Srufle $
006: //$Revision: 10 $
007: //$Modtime: 7/31/02 6:11p $
008: /////////////////////////
009: /*
010: * (C) Copyright IBM Corp. 1999 All rights reserved.
011: *
012: * US Government Users Restricted Rights Use, duplication or
013: * disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
014: *
015: * The program is provided "as is" without any warranty express or
016: * implied, including the warranty of non-infringement and the implied
017: * warranties of merchantibility and fitness for a particular purpose.
018: * IBM will not be liable for any damages suffered by you as a result
019: * of using the Program. In no event will IBM be liable for any
020: * special, indirect or consequential damages or lost profits even if
021: * IBM has been advised of the possibility of their occurrence. IBM
022: * will not be liable for any third party claims against you.
023: */
024:
025: import org.w3c.dom.Document;
026:
027: import org.xml.sax.ErrorHandler;
028: import org.xml.sax.SAXException;
029: import org.xml.sax.SAXParseException;
030:
031: import javax.xml.parsers.DocumentBuilderFactory;
032: import javax.xml.parsers.DocumentBuilder;
033:
034: /**
035: * Licensed Material - Property of Salmon LLC
036: * (C) Copyright Salmon LLC. 1999 - All Rights Reserved
037: * For more information go to www.salmonllc.com
038: *
039: * *************************************************************************
040: * DISCLAIMER:
041: * The following code has been created by Salmon LLC. The code is provided
042: * 'AS IS' , without warranty of any kind unless covered in another agreement
043: * between your corporation and Salmon LLC. Salmon LLC shall not be liable
044: * for any damages arising out of your use of this, even if they have been
045: * advised of the possibility of such damages.
046: * *************************************************************************
047: *
048: * Wraps the JAXP DOM parser.
049: *
050: * @version Revision: 12 1.2 samples/dom/wrappers/DOMParser.java, samples, xml4j2, xml4j2_0_15
051: */
052: public class DOMParser implements DOMParserWrapper, ErrorHandler {
053:
054: //
055: // Data
056: //
057: private boolean _bErrorFlagSet = false;
058: private boolean _bWarningFlagSet = false;
059:
060: //
061: // Constructors
062: //
063:
064: /** Error. */
065: public void error(SAXParseException ex) {
066: System.err.println("[Error] " + getLocationString(ex) + ": "
067: + ex.getMessage());
068: _bErrorFlagSet = true;
069: }
070:
071: /** Fatal error. */
072: public void fatalError(SAXParseException ex) throws SAXException {
073: System.err.println("[Fatal Error] " + getLocationString(ex)
074: + ": " + ex.getMessage());
075: throw ex;
076: }
077:
078: //
079: // Private methods
080: //
081:
082: /** Returns a string of the location. */
083: private String getLocationString(SAXParseException ex) {
084: StringBuffer str = new StringBuffer();
085:
086: String systemId = ex.getSystemId();
087: if (systemId != null) {
088: int index = systemId.lastIndexOf('/');
089: if (index != -1)
090: systemId = systemId.substring(index + 1);
091: str.append(systemId);
092: }
093: str.append(':');
094: str.append(ex.getLineNumber());
095: str.append(':');
096: str.append(ex.getColumnNumber());
097:
098: return str.toString();
099:
100: } // getLocationString(SAXParseException):String
101:
102: /**
103: * Creation date: (3/22/02 1:52:00 PM)
104: * @return boolean
105: */
106: public boolean isErrorFlagSet() {
107: return _bErrorFlagSet;
108: }
109:
110: /**
111: * Creation date: (3/22/02 1:52:00 PM)
112: * @return boolean
113: */
114: public boolean isWarningFlagSet() {
115: return _bWarningFlagSet;
116: }
117:
118: //
119: // DOMParserWrapper methods
120: //
121:
122: /** Parses the specified URI and returns the document. */
123: public Document parse(String uri) throws Exception {
124: DocumentBuilderFactory dbf = DocumentBuilderFactory
125: .newInstance();
126: DocumentBuilder db = dbf.newDocumentBuilder();
127: db.setErrorHandler(this );
128: return db.parse(uri);
129:
130: } // parse(String):Document
131:
132: /**
133: * Creation date: (3/22/02 1:52:00 PM)
134: * @param newErrorFlagSet boolean
135: */
136: public void setErrorFlag(boolean newErrorFlagSet) {
137: _bErrorFlagSet = newErrorFlagSet;
138: }
139:
140: /**
141: * Creation date: (3/22/02 1:52:00 PM)
142: * @param newWarningFlagSet boolean
143: */
144: public void setWarningFlag(boolean newWarningFlagSet) {
145: _bWarningFlagSet = newWarningFlagSet;
146: }
147:
148: //
149: // ErrorHandler methods
150: //
151:
152: /** Warning. */
153: public void warning(SAXParseException ex) {
154: System.err.println("[Warning] " + getLocationString(ex) + ": "
155: + ex.getMessage());
156: _bWarningFlagSet = true;
157: }
158: } // interface DOMParser
|