KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > webdocwf > util > loader > OctopusValidator


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 JavaDoc;
29 import java.io.IOException JavaDoc;
30
31 import javax.xml.parsers.ParserConfigurationException JavaDoc;
32
33 import org.apache.xerces.parsers.DOMParser;
34 import org.xml.sax.ErrorHandler JavaDoc;
35 import org.xml.sax.InputSource JavaDoc;
36 import org.xml.sax.SAXException JavaDoc;
37 import org.xml.sax.SAXParseException JavaDoc;
38 import org.xml.sax.helpers.DefaultHandler JavaDoc;
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 JavaDoc {
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 JavaDoc inStream) throws Exception JavaDoc
58   {
59     
60         final String JavaDoc JAXP_SCHEMA_LANGUAGE =
61                 "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
62         final String JavaDoc 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 JavaDoc(inStream));
74     
75         } catch (Exception JavaDoc 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 JavaDoc getLocationString (SAXParseException JavaDoc ex) {
87     StringBuffer JavaDoc str = new StringBuffer JavaDoc();
88     String JavaDoc 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 JavaDoc
103   {
104
105     // throw SAXException for fatal errors
106
public void fatalError( SAXParseException JavaDoc exception ) throws SAXException JavaDoc
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 JavaDoc(exception);
113     }
114
115     public void error( SAXParseException JavaDoc errorException ) throws SAXException JavaDoc
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 JavaDoc(errorException);
122     }
123
124     // print any warnings
125
public void warning( SAXParseException JavaDoc warningError ) throws SAXException JavaDoc
126     {
127       System.err.println("[Validation : Warning] URI : " + getLocationString(warningError) + ": " + warningError.getMessage());
128     }
129   }
130
131
132 }
Popular Tags