All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.saxon.lib.StandardModuleURIResolver Maven / Gradle / Ivy

There is a newer version: 10.5
Show newest version
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2013 Saxonica Limited.
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
// If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
// This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

package net.sf.saxon.lib;

import net.sf.saxon.functions.ResolveURI;
import net.sf.saxon.trans.XPathException;

import javax.xml.transform.stream.StreamSource;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;


/**
 * This class is the standard ModuleURIResolver used to implement the "import module" declaration
 * in a Query Prolog. It is used when no user-defined ModuleURIResolver has been specified, or when
 * the user-defined ModuleURIResolver decides to delegate to the standard ModuleURIResolver.
 *
 * @author Michael H. Kay
*/

public class StandardModuleURIResolver implements ModuleURIResolver {

    private static StandardModuleURIResolver THE_INSTANCE = new StandardModuleURIResolver();

    /**
     * Get the singular instance of this class
     * @return the singular instance of this class
     */

    public static StandardModuleURIResolver getInstance() {
        return THE_INSTANCE;
    }

    /**
     * Create a StandardModuleURIResolver. Although the class is generally used as a singleton,
     * a public constructor is provided so that the class can be named in configuration files and
     * instantiated in the same way as user-written module URI resolvers.
     */

    public StandardModuleURIResolver() {

    }

    /**
     * Resolve a module URI and associated location hints.
     * @param moduleURI The module namespace URI of the module to be imported; or null when
     * loading a non-library module.
     * @param baseURI The base URI of the module containing the "import module" declaration;
     * null if no base URI is known
     * @param locations The set of URIs specified in the "at" clause of "import module",
     * which serve as location hints for the module
     * @return an array of StreamSource objects each identifying the contents of a module to be
     * imported. Each StreamSource must contain a
     * non-null absolute System ID which will be used as the base URI of the imported module,
     * and either an InputSource or a Reader representing the text of the module.
     * @throws XPathException (error XQST0059) if the module cannot be located
    */

    public StreamSource[] resolve(String moduleURI, String baseURI, String[] locations) throws XPathException {
        if (locations.length == 0) {
            XPathException err = new XPathException("Cannot locate module for namespace " + moduleURI);
            err.setErrorCode("XQST0059");
            err.setIsStaticError(true);
            throw err;
        } else {
            // One or more locations given: import modules from all these locations
            StreamSource[] sources = new StreamSource[locations.length];
            for (int m=0; m=0) {
                         pos = contentType.indexOf('=', pos + 7);
                         if (pos>=0) {
                             contentType = contentType.substring(pos + 1);
                         }
                         if ((pos = contentType.indexOf(';')) > 0) {
                             contentType = contentType.substring(0, pos);
                         }

                         // attributes can have comment fields (RFC 822)
                         if ((pos = contentType.indexOf('(')) > 0) {
                             contentType = contentType.substring(0, pos);
                         }
                         // ... and values may be quoted
                         if ((pos = contentType.indexOf('"')) > 0) {
                             contentType = contentType.substring(pos + 1,
                                     contentType.indexOf('"', pos + 2));
                         }
                         encoding = contentType.trim();
                     }
                 }
             }
             StreamSource ss = new StreamSource();
             if (encoding == null) {
                 ss.setInputStream(is);
             } else {
                 ss.setReader(new InputStreamReader(is, encoding));
             }
             ss.setSystemId(absoluteURL.toString());
             return ss;
         } catch (IOException err) {
             XPathException se = new XPathException(err);
             se.setErrorCode("XQST0059");
             se.setIsStaticError(true);
             throw se;
         }

     }
}





© 2015 - 2025 Weber Informatics LLC | Privacy Policy