001: /**
002: OctopusValidator - Class used for validating XML files.
003:
004: Copyright (C) 2002-2003 Together
005:
006: This library is free software; you can redistribute it and/or
007: modify it under the terms of the GNU Lesser General Public
008: License as published by the Free Software Foundation; either
009: version 2.1 of the License, or (at your option) any later version.
010:
011: This library is distributed in the hope that it will be useful,
012: but WITHOUT ANY WARRANTY; without even the implied warranty of
013: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: Lesser General Public License for more details.
015:
016: You should have received a copy of the GNU Lesser General Public
017: License along with this library; if not, write to the Free Software
018: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019:
020: OctopusValidator.java
021: Date: 20.5.2003.
022: @version 1.0.0
023: @author: Zoran Milakovic zoran@prozone.co.yu
024: */package org.webdocwf.util.loader;
025:
026: import java.io.ByteArrayInputStream;
027: import java.io.IOException;
028:
029: import javax.xml.parsers.ParserConfigurationException;
030:
031: import org.apache.xerces.parsers.DOMParser;
032: import org.xml.sax.ErrorHandler;
033: import org.xml.sax.InputSource;
034: import org.xml.sax.SAXException;
035: import org.xml.sax.SAXParseException;
036: import org.xml.sax.helpers.DefaultHandler;
037:
038: /**
039: * Class used for validation of XML files.
040: *
041: * @author Zoran Milakovic
042: * @version 1.1
043: */
044: public class OctopusValidator extends DefaultHandler {
045:
046: /**
047: * Validate xml document which is passed in inStream parameter.
048: * @param inStream xml document to validate
049: * @throws SAXException
050: * @throws SAXParseException
051: * @throws ParserConfigurationException
052: * @throws IOException
053: **/
054: public void validate(ByteArrayInputStream inStream)
055: throws Exception {
056:
057: final String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
058: final String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema";
059:
060: DOMParser parser = new DOMParser();
061:
062: try {
063: parser.setFeature("http://xml.org/sax/features/validation",
064: true);
065: parser.setFeature(
066: "http://apache.org/xml/features/validation/schema",
067: true);
068: parser.setErrorHandler(new OctopusErrorHandler());
069: parser.setEntityResolver(new OctopusEntityResolver());
070:
071: parser.parse(new InputSource(inStream));
072:
073: } catch (Exception e) {
074: throw e;
075: }
076: }
077:
078: /**
079: *
080: * @param ex SAXParseException
081: * @return line and column where error occured in input stream created from loaderJob.xml and ALL
082: * included .xml files.
083: */
084: private String getLocationString(SAXParseException ex) {
085: StringBuffer str = new StringBuffer();
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: return str.toString();
098: }
099:
100: class OctopusErrorHandler implements ErrorHandler {
101:
102: // throw SAXException for fatal errors
103: public void fatalError(SAXParseException exception)
104: throws SAXException {
105: //System.err.println("[Validation : FatalError ] URI = " + getLocationString(exception) + ": " + exception.getMessage());
106: LocationOfException
107: .getLineNumber(exception.getLineNumber());
108: System.err.println("[Validation : FatalError ] URI : "
109: + LocationOfException.getFileName());
110: System.err
111: .println("[Validation : FatalError ] Line number : "
112: + LocationOfException
113: .getLineNumber(exception
114: .getLineNumber()));
115: throw new SAXException(exception);
116: }
117:
118: public void error(SAXParseException errorException)
119: throws SAXException {
120: // System.err.println("[Validation : Error ] URI = " + getLocationString(errorException) + ": " + errorException.getMessage());
121: LocationOfException.getLineNumber(errorException
122: .getLineNumber());
123: System.err.println("[Validation : Error ] URI : "
124: + LocationOfException.getFileName());
125: System.err.println("[Validation : Error ] Line number : "
126: + LocationOfException.getLineNumber(errorException
127: .getLineNumber()));
128: throw new SAXException(errorException);
129: }
130:
131: // print any warnings
132: public void warning(SAXParseException warningError)
133: throws SAXException {
134: System.err.println("[Validation : Warning] URI : "
135: + getLocationString(warningError) + ": "
136: + warningError.getMessage());
137: }
138: }
139:
140: }
|