1 | /****************************************************************************** |
---|
2 | * (c) Copyright 2002,2003, 1060 Research Ltd |
---|
3 | * |
---|
4 | * This Software is licensed to You, the licensee, for use under the terms of |
---|
5 | * the 1060 Public License v1.0. Please read and agree to the 1060 Public |
---|
6 | * License v1.0 [www.1060research.com/license] before using or redistributing |
---|
7 | * this software. |
---|
8 | * |
---|
9 | * In summary the 1060 Public license has the following conditions. |
---|
10 | * A. You may use the Software free of charge provided you agree to the terms |
---|
11 | * laid out in the 1060 Public License v1.0 |
---|
12 | * B. You are only permitted to use the Software with components or applications |
---|
13 | * that provide you with OSI Certified Open Source Code [www.opensource.org], or |
---|
14 | * for which licensing has been approved by 1060 Research Limited. |
---|
15 | * You may write your own software for execution by this Software provided any |
---|
16 | * distribution of your software with this Software complies with terms set out |
---|
17 | * in section 2 of the 1060 Public License v1.0 |
---|
18 | * C. You may redistribute the Software provided you comply with the terms of |
---|
19 | * the 1060 Public License v1.0 and that no warranty is implied or given. |
---|
20 | * D. If you find you are unable to comply with this license you may seek to |
---|
21 | * obtain an alternative license from 1060 Research Limited by contacting |
---|
22 | * license@1060research.com or by visiting www.1060research.com |
---|
23 | * |
---|
24 | * NO WARRANTY: THIS SOFTWARE IS NOT COVERED BY ANY WARRANTY. SEE 1060 PUBLIC |
---|
25 | * LICENSE V1.0 FOR DETAILS |
---|
26 | * |
---|
27 | * THIS COPYRIGHT NOTICE IS *NOT* THE 1060 PUBLIC LICENSE v1.0. PLEASE READ |
---|
28 | * THE DISTRIBUTED 1060_Public_License.txt OR www.1060research.com/license |
---|
29 | * |
---|
30 | * File: $RCSfile: XSDValidatorHandler.java,v $ |
---|
31 | * Version: $Name: $ $Revision: 1.3 $ |
---|
32 | * Last Modified: $Date: 2004/06/21 10:25:42 $ |
---|
33 | * |
---|
34 | * CHANGELOG |
---|
35 | * [30/9/2011 maw] Added the use of a CatalogResolver. |
---|
36 | *****************************************************************************/ |
---|
37 | package org.ten60.netkernel.xml.accessor; |
---|
38 | |
---|
39 | import org.ten60.netkernel.xml.xda.*; |
---|
40 | import org.ten60.netkernel.xml.xahelper.*; |
---|
41 | import org.ten60.netkernel.xml.util.*; |
---|
42 | import com.ten60.netkernel.util.SysLogger; |
---|
43 | |
---|
44 | |
---|
45 | import org.xml.sax.helpers.*; |
---|
46 | import org.xml.sax.*; |
---|
47 | import org.w3c.dom.*; |
---|
48 | import java.io.*; |
---|
49 | import org.apache.xml.resolver.tools.CatalogResolver; |
---|
50 | |
---|
51 | /** |
---|
52 | * An XSD Validator Handler for catching errors and warnings from XSD validation |
---|
53 | * @author pjrhplb |
---|
54 | */ |
---|
55 | public class XSDValidatorHandler extends DefaultHandler |
---|
56 | { |
---|
57 | private int errorcount; |
---|
58 | private int warningcount; |
---|
59 | private Document results=org.ten60.netkernel.xml.util.XMLUtils.getInstance().newDocument(); |
---|
60 | private DOMXDA wrt=new DOMXDA(results, null); |
---|
61 | private XAHelper mHelper; |
---|
62 | private String mSchemaURI; |
---|
63 | private IXDAReadOnly mSchema; |
---|
64 | private CatalogResolver mResolver; |
---|
65 | |
---|
66 | /** Creates a new instance of XSDValidatorContentHandler */ |
---|
67 | public XSDValidatorHandler(XAHelper aHelper, String aSchemaURI, IXDAReadOnly aSchema) |
---|
68 | { mHelper=aHelper; |
---|
69 | mResolver = new CatalogResolver(); |
---|
70 | mSchemaURI = aSchemaURI; |
---|
71 | mSchema=aSchema; |
---|
72 | try |
---|
73 | { wrt.appendPath("/","/b","f"); |
---|
74 | wrt.appendPath("/b","/errors",""); |
---|
75 | wrt.appendPath("/b","/warnings",""); |
---|
76 | } |
---|
77 | catch(Exception e) |
---|
78 | { e.printStackTrace(); |
---|
79 | } |
---|
80 | } |
---|
81 | |
---|
82 | /*Receive notification of a recoverable parser error.*/ |
---|
83 | public void error(SAXParseException e) |
---|
84 | { try |
---|
85 | { wrt.appendPath("/b/errors","/error",e.getMessage()); |
---|
86 | } |
---|
87 | catch(Exception locale) |
---|
88 | { locale.printStackTrace(); |
---|
89 | } |
---|
90 | errorcount++; |
---|
91 | } |
---|
92 | |
---|
93 | /*Report a fatal XML parsing error.*/ |
---|
94 | public void fatalError(SAXParseException e) |
---|
95 | { try |
---|
96 | { wrt.appendPath("/b/errors","/error",e.getMessage()); |
---|
97 | } |
---|
98 | catch(Exception locale) |
---|
99 | { locale.printStackTrace(); |
---|
100 | } |
---|
101 | errorcount++; |
---|
102 | } |
---|
103 | /* Receive notification of a parser warning. */ |
---|
104 | public void warning(SAXParseException e) |
---|
105 | { try |
---|
106 | { wrt.appendPath("/b/warnings","/warning",e.getMessage()); |
---|
107 | } |
---|
108 | catch(Exception locale) |
---|
109 | { locale.printStackTrace(); |
---|
110 | } |
---|
111 | warningcount++; |
---|
112 | } |
---|
113 | |
---|
114 | /** @return true if document is valid */ |
---|
115 | public boolean isvalid() |
---|
116 | { if(errorcount>0) return false; |
---|
117 | else return true; |
---|
118 | } |
---|
119 | |
---|
120 | /** @return the number of warnings */ |
---|
121 | public int getWarnings() |
---|
122 | { return warningcount; |
---|
123 | } |
---|
124 | |
---|
125 | /** @return the number of errors */ |
---|
126 | public int getErrors() |
---|
127 | { return warningcount; |
---|
128 | } |
---|
129 | |
---|
130 | /** @return a cannonical boolean document */ |
---|
131 | public Document getDocument() |
---|
132 | { try |
---|
133 | { if(errorcount>0) |
---|
134 | { wrt.setText("/b","f"); |
---|
135 | } |
---|
136 | else wrt.setText("/b","t"); |
---|
137 | return wrt.toDOM(); |
---|
138 | } |
---|
139 | catch(Exception e) |
---|
140 | { e.printStackTrace(); |
---|
141 | } |
---|
142 | return null; |
---|
143 | } |
---|
144 | |
---|
145 | public InputSource resolveEntity(String publicId, String systemId) |
---|
146 | throws SAXException |
---|
147 | { try |
---|
148 | { InputSource result; |
---|
149 | if (systemId.equals(mSchemaURI)) |
---|
150 | { StringWriter sw=new StringWriter(); |
---|
151 | mSchema.serialize(sw,false); |
---|
152 | StringReader reader = new StringReader( sw.toString() ); |
---|
153 | result = new InputSource(reader); |
---|
154 | } |
---|
155 | else |
---|
156 | { result = mResolver.resolveEntity(publicId,systemId); |
---|
157 | if (result == null) |
---|
158 | { result = mHelper.resolveEntity(publicId,systemId); |
---|
159 | } |
---|
160 | } |
---|
161 | return result; |
---|
162 | } catch (Exception e) |
---|
163 | { return null; |
---|
164 | } |
---|
165 | } |
---|
166 | } |
---|