Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6932807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:45:08+00:00 2026-05-27T11:45:08+00:00

Below are some code fragments that indicate what I am trying at the moment,

  • 0

Below are some code fragments that indicate what I am trying at the moment, but its unreliable. Princiaply I think
because you can only register a protocol handler once, and occasionally other libraries may be doing this first.

import org.apache.xerces.util.XMLCatalogResolver;



public static synchronized XMLCatalogResolver getResolver() {
        String c[] = {"classpath:xml-catalog.xml"};
        if (cr==null) {

            log.debug("Registering new protcol handler for classpath");
            ConfigurableStreamHandlerFactory configurableStreamHandlerFactory = new ConfigurableStreamHandlerFactory("classpath", new org.fao.oek.protocols.classpath.Handler(XsdUtils.class.getClassLoader()));
            configurableStreamHandlerFactory.addHandler("http", new sun.net.www.protocol.http.Handler());


            URL.setURLStreamHandlerFactory(configurableStreamHandlerFactory);

            log.debug("Creating new catalog resolver");

        cr = new XMLCatalogResolver(c);

        }
        return cr;
    }

xml-catalog.xml contains:

<catalog xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog">
<group  prefer="public"  xml:base="classpath:org/me/myapp/xsd/" >  
  <uri name="http://www.w3.org/XML/1998/namespace" uri="xml.xsd"/>
  <uri name="http://www.w3.org/1999/xlink" uri="xlink.xsd" />
  <uri name="http://www.w3.org/2001/XMLSchema" uri="XMLSchema.xsd" />
  <uri name="http://purl.org/dc/elements/1.1/" uri="dc.xsd" />
  <uri name="http://www.loc.gov/mods/v3"  uri="mods-3.3.xsd" />
 </group>
</catalog>

Obviously – the xsd files exist at the right place in the classpath.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T11:45:09+00:00Added an answer on May 27, 2026 at 11:45 am

    The resolver acted properly with the following minimum set of 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();
        }
    }
    

    You can alternatively specify java.protocol.handler.pkgs as a JVM argument:

    java -Djava.protocol.handler.pkgs=org.fao.oek.protocols ...
    

    The Handler class was implemented as follows:

    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();
        }
    }
    

    It is important to have the forward slash ("/") when requesting the resource, as answered by this Stack Overflow question: “open resource with relative path in java.”

    Note the main method in XsdUtils. The output to the program when xml-catalog.xml and mods-3.3.xsd are on the classpath but not in a JAR is:

    file:/workspace/8412798/target/classes/xml-catalog.xml
    file:/workspace/8412798/target/classes/org/me/myapp/xsd/mods-3.3.xsd
    

    The output to the program when the files are in a JAR is:

    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
    

    With respect to this code in the original question:

    new org.fao.oek.protocols.classpath.Handler(XsdUtils.class.getClassLoader())
    

    your Handler does not need a specific class loader unless you have configured your application to use a special class loader, like one extended from URLClassLoader.

    “A New Era for Java Protocol Handlers” is a good resource about protocol handlers.

    Just to bring everything full circle, the following class uses XsdUtils.getResolver() to parse XML. It validates against the schemas specified in the XMLCatalogResolver:

    public class SampleParser {
        public static void main(String[] args) throws Exception {
            String xml = "<?xml version=\"1.0\"?>" + //
                    "<mods ID=\"id\" version=\"3.3\" xmlns=\"http://www.loc.gov/mods/v3\">" + //
                    "<titleInfo></titleInfo>" + //
                    "</mods>";
            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));
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Below is some code I've written that is effective, but makes too many database
I have some really funky code. As you can see from the code below
In the code below I'm trying to load some images and put them in
I have below some code in C which uses the fork() system call, but
I have read that some of the JVMs out there can optimize code execution
Below is some code from a C++ procedure. The original can be found here;
Below is some code that is used to pass around a reference to a
Below is some code that I'm using to create objects with Visual Basic: For
below is some code I am using to translate a map array into SQL
When I use a conditional statement targeting IE6 and below with some PHP code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.