-
-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathW3CMultiSchemaFactory.java
More file actions
189 lines (168 loc) · 7.49 KB
/
Copy pathW3CMultiSchemaFactory.java
File metadata and controls
189 lines (168 loc) · 7.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/* Woodstox XML processor
*
* Copyright (c) 2004- Tatu Saloranta, tatu.saloranta@iki.fi
*
* Licensed under the License specified in the file LICENSE which is
* included with the source code.
* You may not use this file except in compliance with the License.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.ctc.wstx.msv;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMSource;
import org.codehaus.stax2.validation.XMLValidationSchema;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.EntityResolver;
import org.xml.sax.Locator;
import com.sun.msv.grammar.ExpressionPool;
import com.sun.msv.grammar.xmlschema.XMLSchemaGrammar;
import com.sun.msv.grammar.xmlschema.XMLSchemaSchema;
import com.sun.msv.reader.GrammarReaderController;
import com.sun.msv.reader.State;
import com.sun.msv.reader.xmlschema.EmbeddedSchema;
import com.sun.msv.reader.xmlschema.MultiSchemaReader;
import com.sun.msv.reader.xmlschema.SchemaState;
import com.sun.msv.reader.xmlschema.WSDLGrammarReaderController;
import com.sun.msv.reader.xmlschema.XMLSchemaReader;
/**
* This is a StAX2 schema factory that can parse and create schema instances
* for creating validators that validate documents to check their validity
* against specific W3C Schema instances. It requires
* Sun Multi-Schema Validator
* (http://www.sun.com/software/xml/developers/multischema/)
* to work (bundled by Woodstox, no need to add dependency separately,
* and acts as a quite thin wrapper layer, similar to
* how matching RelaxNG validator works.
*<p>
* Note: intentionally does NOT (claim to) implement {@link org.codehaus.stax2.validation.XMLValidationSchemaFactory}
* since interface (that is, {@link W3CMultiSchemaFactory#createSchema(String, Map)})
* is not incompatible with that of other "standard" schema factories; means that
* usage must be explicit, direct and can not be triggered via Stax2 API.
*
* @since 6.2
*
* @author Daniel Kulp
*/
public class W3CMultiSchemaFactory
{
private final SAXParserFactory parserFactory;
private EntityResolver entityResolver;
public W3CMultiSchemaFactory() {
parserFactory = SAXParserFactory.newInstance();
parserFactory.setNamespaceAware(true);
}
static class RecursiveAllowedXMLSchemaReader extends XMLSchemaReader {
Set<String> sysIds = new TreeSet<String>();
RecursiveAllowedXMLSchemaReader(GrammarReaderController controller, SAXParserFactory parserFactory) {
super(controller, parserFactory, new StateFactory() {
@Override
public State schemaHead(String expectedNamespace) {
return new SchemaState(expectedNamespace) {
private XMLSchemaSchema old;
@Override
protected void endSelf() {
super.endSelf();
RecursiveAllowedXMLSchemaReader r = (RecursiveAllowedXMLSchemaReader)reader;
r.currentSchema = old;
}
@Override
protected void onTargetNamespaceResolved(String targetNs, boolean ignoreContents) {
RecursiveAllowedXMLSchemaReader r = (RecursiveAllowedXMLSchemaReader)reader;
// sets new XMLSchemaGrammar object.
old = r.currentSchema;
r.currentSchema = r.getOrCreateSchema(targetNs);
if (ignoreContents) {
return;
}
if (!r.isSchemaDefined(r.currentSchema)) {
r.markSchemaAsDefined(r.currentSchema);
}
}
};
}
}, new ExpressionPool());
}
@Override
public void setLocator(Locator locator) {
if (locator == null && getLocator() != null && getLocator().getSystemId() != null) {
sysIds.add(getLocator().getSystemId());
}
super.setLocator(locator);
}
@Override
public void switchSource(Source source, State newState) {
String url = source.getSystemId();
if (url != null && sysIds.contains(url)) {
return;
}
super.switchSource(source, newState);
}
}
/**
* Creates an XMLValidateSchema that can be used to validate XML instances against
* any of the schemas defined in the Map of schemaSources.
*
* @param baseURI Base URI for resolving dependant schemas
* @param schemaSources Map of schemas, namespace to Source
*/
public XMLValidationSchema createSchema(String baseURI,
Map<String, Source> schemaSources) throws XMLStreamException
{
return createSchema(baseURI, schemaSources, this.entityResolver);
}
/**
* Creates an XMLValidateSchema that can be used to validate XML instances against
* any of the schemas defined in the Map of schemaSources.
*
* @param baseURI Base URI for resolving dependant schemas
* @param schemaSources Map of schemas, namespace to Source
* @param entityResolver Entity resolver used to resolve path to unknown schemas
*/
public XMLValidationSchema createSchema(String baseURI,
Map<String, Source> schemaSources, EntityResolver entityResolver) throws XMLStreamException
{
Map<String, EmbeddedSchema> embeddedSources = new HashMap<String, EmbeddedSchema>();
for (Map.Entry<String, Source> source : schemaSources.entrySet()) {
if (source.getValue() instanceof DOMSource) {
Node nd = ((DOMSource)source.getValue()).getNode();
Element el = null;
if (nd instanceof Element) {
el = (Element)nd;
} else if (nd instanceof Document) {
el = ((Document)nd).getDocumentElement();
}
embeddedSources.put(source.getKey(), new EmbeddedSchema(source.getValue().getSystemId(), el));
}
}
final WSDLGrammarReaderController ctrl = new WSDLGrammarReaderController(null, baseURI, embeddedSources);
ctrl.setEntityResolver(entityResolver);
final RecursiveAllowedXMLSchemaReader xmlSchemaReader = new RecursiveAllowedXMLSchemaReader(ctrl, parserFactory);
final MultiSchemaReader multiSchemaReader = new MultiSchemaReader(xmlSchemaReader);
for (Source source : schemaSources.values()) {
multiSchemaReader.parse(source);
}
XMLSchemaGrammar grammar = multiSchemaReader.getResult();
if (grammar == null) {
throw new XMLStreamException("Failed to load schemas");
}
return new W3CSchema(grammar);
}
public W3CMultiSchemaFactory setEntityResolver(EntityResolver entityResolver)
{
this.entityResolver = entityResolver;
return this;
}
}