Debian Codegraph @

src/main/java/com/ctc/wstx/msv/W3CMultiSchemaFactory.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.util.HashMap;
19 import java.util.Map;
20 import java.util.Set;
21 import java.util.TreeSet;
22
23 import javax.xml.parsers.SAXParserFactory;
24 import javax.xml.stream.XMLStreamException;
25 import javax.xml.transform.Source;
26 import javax.xml.transform.dom.DOMSource;
27
28 import org.w3c.dom.Document;
29 import org.w3c.dom.Element;
30 import org.w3c.dom.Node;
31
32 import org.xml.sax.Locator;
33
34 import com.ctc.wstx.msv.W3CSchema;
35 import com.sun.msv.grammar.ExpressionPool;
36 import com.sun.msv.grammar.xmlschema.XMLSchemaGrammar;
37 import com.sun.msv.grammar.xmlschema.XMLSchemaSchema;
38 import com.sun.msv.reader.GrammarReaderController;
39 import com.sun.msv.reader.State;
40 import com.sun.msv.reader.xmlschema.EmbeddedSchema;
41 import com.sun.msv.reader.xmlschema.MultiSchemaReader;
42 import com.sun.msv.reader.xmlschema.SchemaState;
43 import com.sun.msv.reader.xmlschema.WSDLGrammarReaderController;
44 import com.sun.msv.reader.xmlschema.XMLSchemaReader;
45
46 import org.codehaus.stax2.validation.XMLValidationSchema;
47
48 /**
49 * This is a StAX2 schema factory that can parse and create schema instances
50 * for creating validators that validate documents to check their validity
51 * against specific W3C Schema instances. It requires
52 * Sun Multi-Schema Validator
53 * (http://www.sun.com/software/xml/developers/multischema/)
54 * to work (bundled by Woodstox, no need to add dependency separately,
55 * and acts as a quite thin wrapper layer, similar to
56 * how matching RelaxNG validator works.
57 *<p>
58 * Note: intentionally does NOT (claim to) implement {@link org.codehaus.stax2.validation.XMLValidationSchemaFactory}
59 * since interface (that is, {@link W3CMultiSchemaFactory#createSchema(String, Map)})
60 * is not incompatible with that of other "standard" schema factories; means that
61 * usage must be explicit, direct and can not be triggered via Stax2 API.
62 *
63 * @since 6.2
64 *
65 * @author Daniel Kulp
66 */
67 public class W3CMultiSchemaFactory
68 {
69 private final SAXParserFactory parserFactory;
70
71 public W3CMultiSchemaFactory() {
72 parserFactory = SAXParserFactory.newInstance();
73 parserFactory.setNamespaceAware(true);
74 }
75
76 static class RecursiveAllowedXMLSchemaReader extends XMLSchemaReader {
77 Set<String> sysIds = new TreeSet<String>();
78 RecursiveAllowedXMLSchemaReader(GrammarReaderController controller, SAXParserFactory parserFactory) {
79 super(controller, parserFactory, new StateFactory() {
80 @Override
81 public State schemaHead(String expectedNamespace) {
82 return new SchemaState(expectedNamespace) {
83 private XMLSchemaSchema old;
84
85 @Override
86 protected void endSelf() {
87 super.endSelf();
88 RecursiveAllowedXMLSchemaReader r = (RecursiveAllowedXMLSchemaReader)reader;
89 r.currentSchema = old;
90 }
91
92 @Override
93 protected void onTargetNamespaceResolved(String targetNs, boolean ignoreContents) {
94
95 RecursiveAllowedXMLSchemaReader r = (RecursiveAllowedXMLSchemaReader)reader;
96 // sets new XMLSchemaGrammar object.
97 old = r.currentSchema;
98 r.currentSchema = r.getOrCreateSchema(targetNs);
99 if (ignoreContents) {
100 return;
101 }
102 if (!r.isSchemaDefined(r.currentSchema)) {
103 r.markSchemaAsDefined(r.currentSchema);
104 }
105 }
106 };
107 }
108 }, new ExpressionPool());
109 }
110
111 @Override
112 public void setLocator(Locator locator) {
113 if (locator == null && getLocator() != null && getLocator().getSystemId() != null) {
114 sysIds.add(getLocator().getSystemId());
115 }
116 super.setLocator(locator);
117 }
118
119 @Override
120 public void switchSource(Source source, State newState) {
121 String url = source.getSystemId();
122 if (url != null && sysIds.contains(url)) {
123 return;
124 }
125 super.switchSource(source, newState);
126 }
127
128 }
129
130 /**
131 * Creates an XMLValidateSchema that can be used to validate XML instances against
132 * any of the schemas defined in the Map of schemaSources.
133 *
134 * @param baseURI Base URI for resolving dependant schemas
135 * @param schemaSources Map of schemas, namespace to Source
136 */
137 public XMLValidationSchema createSchema(String baseURI,
138 Map<String, Source> schemaSources) throws XMLStreamException
139 {
140 Map<String, EmbeddedSchema> embeddedSources = new HashMap<String, EmbeddedSchema>();
141 for (Map.Entry<String, Source> source : schemaSources.entrySet()) {
142 if (source.getValue() instanceof DOMSource) {
143 Node nd = ((DOMSource)source.getValue()).getNode();
144 Element el = null;
145 if (nd instanceof Element) {
146 el = (Element)nd;
147 } else if (nd instanceof Document) {
148 el = ((Document)nd).getDocumentElement();
149 }
150 embeddedSources.put(source.getKey(), new EmbeddedSchema(source.getValue().getSystemId(), el));
151 }
152 }
153
154 WSDLGrammarReaderController ctrl = new WSDLGrammarReaderController(null, baseURI, embeddedSources);
155 final RecursiveAllowedXMLSchemaReader xmlSchemaReader = new RecursiveAllowedXMLSchemaReader(ctrl, parserFactory);
156 final MultiSchemaReader multiSchemaReader = new MultiSchemaReader(xmlSchemaReader);
157 for (Source source : schemaSources.values()) {
158 multiSchemaReader.parse(source);
159 }
160
161 XMLSchemaGrammar grammar = multiSchemaReader.getResult();
162 if (grammar == null) {
163 throw new XMLStreamException("Failed to load schemas");
164 }
165 return new W3CSchema(grammar);
166 }
167
168 }