Debian Codegraph @

src/main/java/com/ctc/wstx/msv/W3CSchemaFactory.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 javax.xml.parsers.SAXParserFactory;
19 import javax.xml.stream.*;
20
21 import org.xml.sax.InputSource;
22
23 import org.codehaus.stax2.validation.*;
24
25 import com.sun.msv.grammar.xmlschema.XMLSchemaGrammar;
26 import com.sun.msv.reader.GrammarReaderController;
27 import com.sun.msv.reader.xmlschema.XMLSchemaReader;
28
29 /**
30 * This is a StAX2 schema factory that can parse and create schema instances
31 * for creating validators that validate documents to check their validity
32 * against specific W3C Schema instances. It requires
33 * Sun Multi-Schema Validator
34 * (http://www.sun.com/software/xml/developers/multischema/)
35 * to work, and acts as a quite thin wrapper layer, similar to
36 * how matching RelaxNG validator works
37 */
38 public class W3CSchemaFactory
39 extends BaseSchemaFactory
40 {
41 /**
42 * For now, there's no need for fine-grained error/problem reporting
43 * infrastructure, so let's just use a dummy controller.
44 */
45 protected final GrammarReaderController mDummyController =
46 new com.sun.msv.reader.util.IgnoreController();
47
48 public W3CSchemaFactory()
49 {
50 super(XMLValidationSchema.SCHEMA_ID_W3C_SCHEMA);
51 }
52
53 /*
54 ////////////////////////////////////////////////////////////
55 // Non-public methods
56 ////////////////////////////////////////////////////////////
57 */
58
59 @Override
60 protected XMLValidationSchema loadSchema(InputSource src, Object sysRef)
61 throws XMLStreamException
62 {
63 /* 26-Oct-2007, TSa: Are sax parser factories safe to share?
64 * If not, should just create new instances for each
65 * parsed schema.
66 */
67 SAXParserFactory saxFactory = getSaxFactory();
68
69 MyGrammarController ctrl = new MyGrammarController();
70 XMLSchemaGrammar grammar = XMLSchemaReader.parse(src, saxFactory, ctrl);
71 if (grammar == null) {
72 String msg = "Failed to load W3C Schema from '"+sysRef+"'";
73 String emsg = ctrl.mErrorMsg;
74 if (emsg != null) {
75 msg = msg + ": "+emsg;
76 }
77 throw new XMLStreamException(msg);
78 }
79 return new W3CSchema(grammar);
80 }
81 }