Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The resolver acted properly with the following minimum set of code:</p> <pre><code>public class XsdUtils { static { System.setProperty("java.protocol.handler.pkgs", "org.fao.oek.protocols"); } private static XMLCatalogResolver cr; public static synchronized XMLCatalogResolver getResolver() { if (cr == null) { cr = new XMLCatalogResolver(new String[] { "classpath:xml-catalog.xml" }); } return cr; } public static void main(String[] args) throws MalformedURLException, IOException { XMLCatalogResolver resolver = getResolver(); URL url0 = new URL("classpath:xml-catalog.xml"); URL url1 = new URL(resolver.resolveURI("http://www.loc.gov/mods/v3")); url0.openConnection(); url1.openConnection(); } } </code></pre> <p>You can alternatively specify <code>java.protocol.handler.pkgs</code> as a JVM argument:</p> <pre><code>java -Djava.protocol.handler.pkgs=org.fao.oek.protocols ... </code></pre> <p>The <code>Handler</code> class was implemented as follows:</p> <pre><code>package org.fao.oek.protocols.classpath; import java.io.IOException; import java.net.URL; import java.net.URLConnection; public class Handler extends java.net.URLStreamHandler { @Override protected URLConnection openConnection(URL u) throws IOException { String resource = u.getPath(); if (!resource.startsWith("/")) resource = "/" + resource; System.out.println(getClass().getResource(resource)); return getClass().getResource(resource).openConnection(); } } </code></pre> <p>It is important to have the forward slash (<code>"/"</code>) when requesting the resource, as answered by this <em>Stack Overflow</em> question: <a href="https://stackoverflow.com/questions/573679/open-resource-with-relative-path-in-java">"open resource with relative path in java."</a></p> <p>Note the <code>main</code> method in <code>XsdUtils</code>. The output to the program when <code>xml-catalog.xml</code> and <code>mods-3.3.xsd</code> are on the classpath but not in a JAR is:</p> <pre><code>file:/workspace/8412798/target/classes/xml-catalog.xml file:/workspace/8412798/target/classes/org/me/myapp/xsd/mods-3.3.xsd </code></pre> <p>The output to the program when the files are in a JAR is:</p> <pre><code>jar:file:/workspace/8412798/target/stackoverflow.jar!/xml-catalog.xml jar:file:/workspace/8412798/target/stackoverflow.jar!/org/me/myapp/xsd/mods-3.3.xsd </code></pre> <p>With respect to this code in the original question:</p> <pre><code>new org.fao.oek.protocols.classpath.Handler(XsdUtils.class.getClassLoader()) </code></pre> <p>your <code>Handler</code> does not need a specific class loader unless you have configured your application to use a special class loader, like one extended from <a href="http://docs.oracle.com/javase/6/docs/api/java/net/URLClassLoader.html" rel="nofollow noreferrer"><code>URLClassLoader</code></a>.</p> <p><a href="http://java.sun.com/developer/onlineTraining/protocolhandlers/" rel="nofollow noreferrer">"A New Era for Java Protocol Handlers"</a> is a good resource about protocol handlers.</p> <p>Just to bring everything full circle, the following class uses <code>XsdUtils.getResolver()</code> to parse XML. It validates against the schemas specified in the <code>XMLCatalogResolver</code>:</p> <pre><code>public class SampleParser { public static void main(String[] args) throws Exception { String xml = "&lt;?xml version=\"1.0\"?&gt;" + // "&lt;mods ID=\"id\" version=\"3.3\" xmlns=\"http://www.loc.gov/mods/v3\"&gt;" + // "&lt;titleInfo&gt;&lt;/titleInfo&gt;" + // "&lt;/mods&gt;"; ByteArrayInputStream is = new ByteArrayInputStream(xml.getBytes()); XMLReader parser = XMLReaderFactory.createXMLReader(org.apache.xerces.parsers.SAXParser.class.getName()); parser.setFeature("http://xml.org/sax/features/validation", true); parser.setFeature("http://apache.org/xml/features/validation/schema", true); parser.setFeature("http://apache.org/xml/features/validation/schema-full-checking", true); parser.setProperty("http://apache.org/xml/properties/internal/entity-resolver", XsdUtils.getResolver()); parser.setErrorHandler(new ErrorHandler() { @Override public void error(SAXParseException exception) throws SAXException { System.out.println("error: " + exception); } @Override public void fatalError(SAXParseException exception) throws SAXException { System.out.println("fatalError: " + exception); } @Override public void warning(SAXParseException exception) throws SAXException { System.out.println("warning: " + exception); } }); parser.parse(new InputSource(is)); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload