Debian Codegraph @

src/main/java/com/ctc/wstx/msv/BaseSchemaFactory.java

raw
1 /* Woodstox XML processor
2 *
3 * Copyright (c) 2004- Tatu Saloranta, [email protected]
4 *
5 * Licensed under the License specified in the file LICENSE which is
6 * included with the source code.
7 * You may not use this file except in compliance with the License.
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 package com.ctc.wstx.msv;
17
18 import java.io.*;
19 import java.net.URL;
20
21 import javax.xml.parsers.SAXParserFactory;
22 import javax.xml.stream.*;
23
24 import org.xml.sax.InputSource;
25 import org.xml.sax.Locator;
26 import org.codehaus.stax2.validation.*;
27
28 import com.ctc.wstx.api.ValidatorConfig;
29 import com.ctc.wstx.exc.WstxIOException;
30 import com.ctc.wstx.util.URLUtil;
31
32 /**
33 * Shared base class extended by concrete schema factory implementations.
34 */
35 public abstract class BaseSchemaFactory
36 extends XMLValidationSchemaFactory
37 {
38 protected static SAXParserFactory sSaxFactory;
39
40 /**
41 * Current configurations for this factory
42 */
43 protected final ValidatorConfig mConfig;
44
45 protected BaseSchemaFactory(String schemaType)
46 {
47 super(schemaType);
48 mConfig = ValidatorConfig.createDefaults();
49 }
50
51 /*
52 ////////////////////////////////////////////////////////////
53 // Stax2, Configuration methods
54 ////////////////////////////////////////////////////////////
55 */
56
57 @Override
58 public boolean isPropertySupported(String propName) {
59 return mConfig.isPropertySupported(propName);
60 }
61
62 @Override
63 public boolean setProperty(String propName, Object value) {
64 return mConfig.setProperty(propName, value);
65 }
66
67 @Override
68 public Object getProperty(String propName) {
69 return mConfig.getProperty(propName);
70 }
71
72 /*
73 ////////////////////////////////////////////////////////////
74 // Stax2, Factory methods
75 ////////////////////////////////////////////////////////////
76 */
77
78 @Override
79 public XMLValidationSchema createSchema(InputStream in, String encoding,
80 String publicId, String systemId)
81 throws XMLStreamException
82 {
83 InputSource src = new InputSource(in);
84 src.setEncoding(encoding);
85 src.setPublicId(publicId);
86 src.setSystemId(systemId);
87 return loadSchema(src, systemId);
88 }
89
90 @Override
91 public XMLValidationSchema createSchema(Reader r, String publicId,
92 String systemId)
93 throws XMLStreamException
94 {
95 InputSource src = new InputSource(r);
96 src.setPublicId(publicId);
97 src.setSystemId(systemId);
98 return loadSchema(src, systemId);
99 }
100
101 @SuppressWarnings("resource")
102 @Override
103 public XMLValidationSchema createSchema(URL url)
104 throws XMLStreamException
105 {
106 try {
107 InputStream in = URLUtil.inputStreamFromURL(url);
108 InputSource src = new InputSource(in);
109 src.setSystemId(url.toExternalForm());
110 return loadSchema(src, url);
111 } catch (IOException ioe) {
112 throw new WstxIOException(ioe);
113 }
114 }
115
116 @SuppressWarnings("deprecation")
117 @Override
118 public XMLValidationSchema createSchema(File f)
119 throws XMLStreamException
120 {
121 try {
122 return createSchema(f.toURL());
123 } catch (IOException ioe) {
124 throw new WstxIOException(ioe);
125 }
126 }
127
128 /*
129 ////////////////////////////////////////////////////////////
130 // Methods sub-classes need to implement
131 ////////////////////////////////////////////////////////////
132 */
133
134 protected abstract XMLValidationSchema loadSchema(InputSource src, Object sysRef)
135 throws XMLStreamException;
136
137 /*
138 ////////////////////////////////////////////////////////////
139 // Internal/package methods
140 ////////////////////////////////////////////////////////////
141 */
142
143 /**
144 * We will essentially share a singleton sax parser factory;
145 * the reason being that constructing (or, rather, locating
146 * implementation class) is bit expensive.
147 */
148 protected synchronized static SAXParserFactory getSaxFactory()
149 {
150 if (sSaxFactory == null) {
151 sSaxFactory = SAXParserFactory.newInstance();
152 sSaxFactory.setNamespaceAware(true);
153 }
154 return sSaxFactory;
155 }
156
157 /*
158 ////////////////////////////////////////////////////////////
159 // Helper classes
160 ////////////////////////////////////////////////////////////
161 */
162
163 final static class MyGrammarController
164 extends com.sun.msv.reader.util.IgnoreController
165 {
166 public String mErrorMsg = null;
167
168 public MyGrammarController() { }
169
170 //public void warning(Locator[] locs, String errorMessage) { }
171
172 @Override
173 public void error(Locator[] locs, String msg, Exception nestedException )
174 {
175 if (mErrorMsg == null) {
176 mErrorMsg = msg;
177 } else {
178 mErrorMsg = mErrorMsg + "; " + msg;
179 }
180 }
181 }
182 }