{"id":5978,"date":"2014-04-11T06:57:41","date_gmt":"2014-04-11T06:57:41","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/04\/11\/java-servlets-xml-validation-against-xsd-collection-of-common-programming-errors\/"},"modified":"2014-04-11T06:57:41","modified_gmt":"2014-04-11T06:57:41","slug":"java-servlets-xml-validation-against-xsd-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/04\/11\/java-servlets-xml-validation-against-xsd-collection-of-common-programming-errors\/","title":{"rendered":"Java servlets: xml validation against xsd-Collection of common programming errors"},"content":{"rendered":"<p>I have servlet which uses utility packaged in .jar archive:<\/p>\n<pre><code>@Override\npublic void init() throws ServletException {\n    ...\n    try (InputStream stream = getClass().getResourceAsStream(\"\/fileToParse.xml\")) {\n        App.check(stream);\n    } catch (Exception e) {\n        throw new ServletException(e);\n    }\n    ...\n}\n<\/code><\/pre>\n<p>This utility takes xml file stream, performs validation against xsd schema and parses it:<\/p>\n<pre><code>public class App {\n    private static final String JAXP_SCHEMA_LANGUAGE = \"http:\/\/java.sun.com\/xml\/jaxp\/properties\/schemaLanguage\";\n    private static final String W3C_XML_SCHEMA = \"http:\/\/www.w3.org\/2001\/XMLSchema\"; \n    ...   \n    public static void check(InputStream stream) {    \n        SAXParserFactory spf = SAXParserFactory.newInstance();\n        spf.setNamespaceAware(true);\n        spf.setValidating(true);\n        MyHandler handler = new MyHandler();\n        try {\n            SAXParser sp = spf.newSAXParser();\n            sp.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);\n            XMLReader rdr = sp.getXMLReader();\n            rdr.setContentHandler(handler);\n            rdr.setErrorHandler(new MyErrorHandler());\n            rdr.parse(new InputSource(stream));\n        }\n        ...\n    }\n    ...\n}\n<\/code><\/pre>\n<p>xsd file starts with:<\/p>\n<pre><code>\n\n    \n        ...\n<\/code><\/pre>\n<p>xml file:<\/p>\n<pre><code>\n\n    ...\n<\/code><\/pre>\n<p>.war structure:<\/p>\n<pre><code>css\/\njs\/\nWEB-INF\/\n    classes\/\n        mypackage\/\n            MyServlet.class\n        fileToParse.xml\n    lib\/\n        App.jar\n    web.xml\n<\/code><\/pre>\n<p>App.jar structure:<\/p>\n<pre><code>mypackage2\/\n    App.class\nappContext.xsd\n<\/code><\/pre>\n<p>Servlet Init method throws exception:<\/p>\n<pre><code>...\nat java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [na:1.7.0_21]\n    at java.lang.Thread.run(Thread.java:722) [na:1.7.0_21]\nCaused by: java.io.FileNotFoundException: \/PATH_TO_TOMCAT\/bin\/appContext.xsd (No such file or directory)\n    at java.io.FileInputStream.open(Native Method) ~[na:1.7.0_21]\n    at java.io.FileInputStream.(FileInputStream.java:138) ~[na:1.7.0_21]\n<\/code><\/pre>\n<p>How I can specify to SAXParser where is xsd schema needed to validate xml file?<\/p>\n<p>P.S. Sorry for my bad English<\/p>\n<p><strong>UPD:<\/strong><\/p>\n<p>I&#8217;m trying to add this property:<\/p>\n<pre><code>    private static final String JAXP_SCHEMA_SOURCE =\n            \"http:\/\/java.sun.com\/xml\/jaxp\/properties\/schemaSource\";\n    ....\n    public static void check(InputStream stream) {\n        ...\n        try {\n            ...\n            sp.setProperty(JAXP_SCHEMA_SOURCE, new File(getPath(\"\/appContext.xsd\")));\n            ...\n        }\n    }\n    public static String getPath(String path) {\n        return App.class.getResource(path).toString();\n    }\n<\/code><\/pre>\n<p>Now I have this exception:<\/p>\n<pre><code>ERROR mypackage2.App - Error: URI=null Line=5: schema_reference.4: Failed to read schema document 'jar:file:\/PATH_TO_TOMCAT\/webapps\/myapp\/WEB-INF\/lib\/App.jar!\/appContext.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not .\norg.xml.sax.SAXParseException: schema_reference.4: Failed to read schema document 'jar:file:\/PATH_TO_TOMCAT\/webapps\/myapp\/WEB-INF\/lib\/App.jar!\/appContext.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not .\n    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:198) ~[na:1.7.0_21]\n<\/code><\/pre>\n<p><strong>UPD2:<\/strong> With &#8220;classpath:appContext.xsd&#8221; in xml file:<\/p>\n<pre><code>WARN  mypackage.App - Warning: URI=null Line=5: schema_reference.4: Failed to read schema document 'classpath:appContext.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not .\norg.xml.sax.SAXParseException: schema_reference.4: Failed to read schema document 'classpath:appContext.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not .\n    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:198) ~[na:1.7.0_21]\n...\n    at java.lang.Thread.run(Thread.java:722) [na:1.7.0_21]\nCaused by: java.net.MalformedURLException: unknown protocol: classpath\n    at java.net.URL.(URL.java:592) ~[na:1.7.0_21\n;;;\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I have servlet which uses utility packaged in .jar archive: @Override public void init() throws ServletException { &#8230; try (InputStream stream = getClass().getResourceAsStream(&#8220;\/fileToParse.xml&#8221;)) { App.check(stream); } catch (Exception e) { throw new ServletException(e); } &#8230; } This utility takes xml file stream, performs validation against xsd schema and parses it: public class App { private [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5978","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5978","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=5978"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5978\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=5978"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=5978"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=5978"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}