View Javadoc
1 /*** 2 OctopusValidator - Class used for validating XML files. 3 4 Copyright (C) 2002-2003 Together 5 6 This library is free software; you can redistribute it and/or 7 modify it under the terms of the GNU Lesser General Public 8 License as published by the Free Software Foundation; either 9 version 2.1 of the License, or (at your option) any later version. 10 11 This library is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 Lesser General Public License for more details. 15 16 You should have received a copy of the GNU Lesser General Public 17 License along with this library; if not, write to the Free Software 18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 20 OctopusValidator.java 21 Date: 20.5.2003. 22 @version 1.0.0 23 @author: Zoran Milakovic zoran@prozone.co.yu 24 */ 25 26 package org.webdocwf.util.loader; 27 28 import java.io.ByteArrayInputStream; 29 import java.io.IOException; 30 31 import javax.xml.parsers.ParserConfigurationException; 32 33 import org.apache.xerces.parsers.DOMParser; 34 import org.xml.sax.ErrorHandler; 35 import org.xml.sax.InputSource; 36 import org.xml.sax.SAXException; 37 import org.xml.sax.SAXParseException; 38 import org.xml.sax.helpers.DefaultHandler; 39 40 /*** 41 * Class used for validation of XML files. 42 * 43 * @author Zoran Milakovic 44 * @version 1.1 45 */ 46 public class OctopusValidator extends DefaultHandler { 47 48 49 /*** 50 * Validate xml document which is passed in inStream parameter. 51 * @param inStream xml document to validate 52 * @throws SAXException 53 * @throws SAXParseException 54 * @throws ParserConfigurationException 55 * @throws IOException 56 **/ 57 public void validate(ByteArrayInputStream inStream) throws Exception 58 { 59 60 final String JAXP_SCHEMA_LANGUAGE = 61 "http://java.sun.com/xml/jaxp/properties/schemaLanguage"; 62 final String W3C_XML_SCHEMA = 63 "http://www.w3.org/2001/XMLSchema"; 64 65 DOMParser parser = new DOMParser(); 66 67 try { 68 parser.setFeature("http://xml.org/sax/features/validation",true); 69 parser.setFeature("http://apache.org/xml/features/validation/schema",true); 70 parser.setErrorHandler(new OctopusErrorHandler()); 71 parser.setEntityResolver(new OctopusEntityResolver()); 72 73 parser.parse(new InputSource(inStream)); 74 75 } catch (Exception e) { 76 throw e; 77 } 78 } 79 80 /*** 81 * 82 * @param ex SAXParseException 83 * @return line and column where error occured in input stream created from loaderJob.xml and ALL 84 * included .xml files. 85 */ 86 private String getLocationString (SAXParseException ex) { 87 StringBuffer str = new StringBuffer(); 88 String systemId = ex.getSystemId(); 89 if (systemId != null) { 90 int index = systemId.lastIndexOf('/'); 91 if (index != -1) 92 systemId = systemId.substring(index + 1); 93 str.append(systemId); 94 } 95 str.append(':'); 96 str.append(ex.getLineNumber()); 97 str.append(':'); 98 str.append(ex.getColumnNumber()); 99 return str.toString(); 100 } 101 102 class OctopusErrorHandler implements ErrorHandler 103 { 104 105 // throw SAXException for fatal errors 106 public void fatalError( SAXParseException exception ) throws SAXException 107 { 108 //System.err.println("[Validation : FatalError ] URI = " + getLocationString(exception) + ": " + exception.getMessage()); 109 LocationOfException.getLineNumber(exception.getLineNumber()); 110 System.err.println("[Validation : FatalError ] URI : "+LocationOfException.getFileName()); 111 System.err.println("[Validation : FatalError ] Line number : "+LocationOfException.getLineNumber(exception.getLineNumber())); 112 throw new SAXException(exception); 113 } 114 115 public void error( SAXParseException errorException ) throws SAXException 116 { 117 // System.err.println("[Validation : Error ] URI = " + getLocationString(errorException) + ": " + errorException.getMessage()); 118 LocationOfException.getLineNumber(errorException.getLineNumber()); 119 System.err.println("[Validation : Error ] URI : "+LocationOfException.getFileName()); 120 System.err.println("[Validation : Error ] Line number : "+LocationOfException.getLineNumber(errorException.getLineNumber())); 121 throw new SAXException(errorException); 122 } 123 124 // print any warnings 125 public void warning( SAXParseException warningError ) throws SAXException 126 { 127 System.err.println("[Validation : Warning] URI : " + getLocationString(warningError) + ": " + warningError.getMessage()); 128 } 129 } 130 131 132 }

This page was automatically generated by Maven