Debian Codegraph @

src/test/java/wstxtest/msv/TestWsdlValidation.java

raw
1 /**
2 *
3 */
4 package wstxtest.msv;
5
6 import java.io.IOException;
7 import java.net.URL;
8
9 import javax.xml.namespace.QName;
10 import javax.xml.parsers.DocumentBuilder;
11 import javax.xml.parsers.DocumentBuilderFactory;
12 import javax.xml.parsers.SAXParserFactory;
13 import javax.xml.stream.XMLStreamConstants;
14 import javax.xml.transform.dom.DOMSource;
15
16 import org.codehaus.stax2.XMLInputFactory2;
17 import org.codehaus.stax2.XMLStreamReader2;
18 import org.w3c.dom.Document;
19 import org.w3c.dom.ls.LSResourceResolver;
20 import org.xml.sax.InputSource;
21 import org.xml.sax.Locator;
22 import org.xml.sax.SAXException;
23
24 import stax2.BaseStax2Test;
25
26 import com.ctc.wstx.msv.W3CSchema;
27 import com.sun.msv.grammar.xmlschema.XMLSchemaGrammar;
28 import com.sun.msv.reader.GrammarReaderController2;
29 import com.sun.msv.reader.xmlschema.WSDLSchemaReader;
30
31 public class TestWsdlValidation extends BaseStax2Test {
32
33 private static class LocalController implements GrammarReaderController2 {
34
35 @Override
36 public LSResourceResolver getLSResourceResolver() {
37 return null;
38 }
39
40 @Override
41 public void error(Locator[] locs, String errorMessage, Exception nestedException) {
42 StringBuffer errors = new StringBuffer();
43 for (Locator loc : locs) {
44 errors.append("in " + loc.getSystemId() + " " + loc.getLineNumber() + ":"
45 + loc.getColumnNumber());
46 }
47 throw new RuntimeException(errors.toString(), nestedException);
48 }
49
50 @Override
51 public void warning(Locator[] locs, String errorMessage) {
52 StringBuffer errors = new StringBuffer();
53 for (Locator loc : locs) {
54 errors.append("in " + loc.getSystemId() + " " + loc.getLineNumber() + ":"
55 + loc.getColumnNumber());
56 }
57 // no warning allowed.
58 throw new RuntimeException("warning: " + errors.toString());
59 }
60
61 @Override
62 public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
63 return null;
64 }
65 }
66
67 private XMLSchemaGrammar wsdlgrammar;
68 private W3CSchema schema;
69
70 @Override
71 protected void setUp() throws Exception {
72 super.setUp();
73 DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
74 documentBuilderFactory.setNamespaceAware(true);
75 DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
76 URL wsdlUri = getClass().getResource("test.wsdl");
77 Document wsdl = documentBuilder.parse(wsdlUri.openStream());
78 String wsdlSystemId = wsdlUri.toExternalForm();
79 DOMSource source = new DOMSource(wsdl);
80 source.setSystemId(wsdlSystemId);
81
82 LocalController controller = new LocalController();
83 SAXParserFactory factory = SAXParserFactory.newInstance();
84 factory.setNamespaceAware(true);
85 wsdlgrammar = WSDLSchemaReader.read(source, factory, controller);
86 schema = new W3CSchema(wsdlgrammar);
87 }
88
89 public void testWsdlValidation() throws Exception {
90 String runMe = System.getProperty("testWsdlValidation");
91 if (runMe == null || "".equals(runMe)) {
92 return;
93 }
94 XMLInputFactory2 factory = getInputFactory();
95 XMLStreamReader2 reader = (XMLStreamReader2) factory.createXMLStreamReader(getClass().getResourceAsStream("test-message.xml"), "utf-8");
96 QName msgQName = new QName("http://server.hw.demo/", "sayHi");
97 while (true) {
98 int what = reader.nextTag();
99 if (what == XMLStreamConstants.START_ELEMENT) {
100 if (reader.getName().equals(msgQName)) {
101 reader.validateAgainst(schema);
102 }
103 } else if (what == XMLStreamConstants.END_ELEMENT) {
104 if (reader.getName().equals(msgQName)) {
105 reader.stopValidatingAgainst(schema);
106 }
107 } else if (what == XMLStreamConstants.END_DOCUMENT) {
108 break;
109 }
110 }
111 }
112 }