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

net.sf.saxon.trans.ConfigurationReader 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.trans;


import net.sf.saxon.Configuration;
import net.sf.saxon.expr.instruct.ResultDocument;
import net.sf.saxon.expr.parser.ExpressionLocation;
import net.sf.saxon.functions.ResolveURI;
import net.sf.saxon.lib.*;
import net.sf.saxon.om.NameChecker;
import net.sf.saxon.om.NamespaceResolver;
import net.sf.saxon.tree.util.FastStringBuffer;
import net.sf.saxon.type.SchemaException;
import org.xml.sax.*;

import javax.xml.transform.ErrorListener;
import javax.xml.transform.Source;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamSource;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.*;

/**
 * Class used to read a config.xml file and transfer all settings from the file to the Configuration
 */

public class ConfigurationReader implements ContentHandler, NamespaceResolver {

    private int level = 0;
    private String section = null;
    private FastStringBuffer buffer = new FastStringBuffer(100);
    protected Configuration config;
    private ClassLoader classLoader = null;
    private List errors = new ArrayList();
    private Locator locator;
    private Stack> namespaceStack = new Stack>();

    public ConfigurationReader() {
    }

    /**
     * Set the ClassLoader to be used for dynamic loading of the configuration, and for dynamic loading
     * of other classes used within the configuration. By default the class loader of this class is used.
     * @param classLoader the ClassLoader to be used
     */

    public void setClassLoader(ClassLoader classLoader) {
        this.classLoader = classLoader;
    }

    /**
     * Create a Configuration based on the contents of this configuration file
     * @param source the Source of the configuration file
     * @return the constructed Configuration
     * @throws XPathException
     */

    public Configuration makeConfiguration(Source source) throws XPathException {
        InputSource is;
        XMLReader parser = null;
        if (source instanceof SAXSource) {
            parser = ((SAXSource)source).getXMLReader();
            is = ((SAXSource)source).getInputSource();
        } else if (source instanceof StreamSource) {
            is = new InputSource(source.getSystemId());
            is.setCharacterStream(((StreamSource)source).getReader());
            is.setByteStream(((StreamSource)source).getInputStream());
        } else {
            throw new XPathException("Source for configuration file must be a StreamSource or SAXSource");
        }
        if (parser == null) {
            // Don't use the parser from the pool, it might be validating
            parser = Configuration.getPlatform().loadParser();
            try {
                parser.setFeature("http://xml.org/sax/features/namespaces", true);
                parser.setFeature("http://xml.org/sax/features/namespace-prefixes", false);
            } catch (SAXNotRecognizedException e) {
                throw new TransformerFactoryConfigurationError(e);
            } catch (SAXNotSupportedException e) {
                throw new TransformerFactoryConfigurationError(e);
            }
        }
        try {
            parser.setContentHandler(this);
            parser.parse(is);
        } catch (IOException e) {
            throw new XPathException("Failed to read config file", e);
        } catch (SAXException e) {
            throw new XPathException("Failed to parse config file", e);
        }

        if (!errors.isEmpty()) {
            ErrorListener listener;
            if (config == null) {
                listener = new StandardErrorListener();
            } else {
                listener = config.getErrorListener();
            }
            try {
                for (XPathException err: errors) {
                    listener.warning(err);
                }
            } catch (TransformerException e) {
                //
            }
            throw errors.get(0);
        }
        return config;
    }

    public void setDocumentLocator(Locator locator) {
        this.locator = locator;
    }

    public void startDocument() throws SAXException {
        namespaceStack.push(new ArrayList());
    }

    public void endDocument() throws SAXException {
        namespaceStack.pop();
    }

    public void startPrefixMapping(String prefix, String uri) throws SAXException {
        namespaceStack.peek().add(new String[]{prefix, uri});
    }

    public void endPrefixMapping(String prefix) throws SAXException {

    }

    public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
        buffer.setLength(0);
        if (NamespaceConstant.SAXON_CONFIGURATION.equals(uri)) {
            if (level == 0) {
                if (!"configuration".equals(localName)) {
                    error(localName, null, null, "configuration");
                }
                String edition = atts.getValue("edition");
                if (edition == null) {
                    edition = "HE";
                }
                if (edition.equals("HE")) {
                    config = new Configuration();
                } else if (edition.equals("PE")) {
                    config = Configuration.makeLicensedConfiguration(classLoader, "com.saxonica.config.ProfessionalConfiguration");
                } else if (edition.equals("EE")) {
                    config = Configuration.makeLicensedConfiguration(classLoader, "com.saxonica.config.EnterpriseConfiguration");
                } else {
                    error("configuration", "edition", edition, "HE|PE|EE");
                    config = new Configuration();
                }
                config.getDynamicLoader().setClassLoader(classLoader);
            }
            if (level == 1) {
                section = localName;
                if ("global".equals(localName)) {
                    readGlobalElement(atts);
                } else if ("serialization".equals(localName)) {
                    readSerializationElement(atts, config.getNameChecker());
                } else if ("xquery".equals(localName)) {
                    readXQueryElement(atts);
                } else if ("xslt".equals(localName)) {
                    readXsltElement(atts);
                } else if ("xsd".equals(localName)) {
                    readXsdElement(atts);
                } else if ("resources".equals(localName)) {
                    // no action until later
                } else if ("collations".equals(localName)) {
                    // no action until later
                } else if ("localizations".equals(localName)) {
                    readLocalizationsElement(atts);
                } else {
                    error(localName, null, null, null);
            }
            } else if (level==2) {
                if ("resources".equals(section)) {
                    // no action until endElement()
                } else if ("collations".equals(section)) {
                    if (!"collation".equals(localName)) {
                        error(localName, null, null, "collation");
                    } else {
                        readCollation(atts);
                    }
                } else if ("localizations".equals(section)) {
                    if (!"localization".equals(localName)) {
                        error(localName, null, null, "localization");
                    } else {
                        readLocalization(atts);
                    }
                } else if ("xslt".equals(section)) {
                    if (!"extensionElement".equals(localName)) {
                        error(localName, null, null, "extensionElement");
                    } else {
                        readExtensionElement(atts);
                    }
                }
            }
        } else {
            errors.add(new XPathException("Configuration elements must be in namespace " + NamespaceConstant.SAXON_CONFIGURATION));
        }
        level++;
        namespaceStack.push(new ArrayList());
    }

    private void readGlobalElement(Attributes atts) {
        Properties props = new Properties();
        for (int i=0; i 0) {
                    config.setConfigurationProperty(FeatureKeys.DEFAULT_LANGUAGE, value);
                }
                if ("defaultCountry".equals(name) && value.length() > 0) {
                    config.setConfigurationProperty(FeatureKeys.DEFAULT_COUNTRY, value);
                }
            }
        }
    }

    private void readLocalization(Attributes atts) {
        String lang = null;
        Properties properties = new Properties();
        for (int i=0; i 0) {
                    lang = value;
                } else if (value.length() > 0) {
                    properties.setProperty(name, value);
                }
            }
        }
        if (lang != null) {
            LocalizerFactory factory = config.getLocalizerFactory();
            if (factory != null) {
                factory.setLanguageProperties(lang, properties);
            }
        }
    }

    /**
     * Process details of XSLT extension elements. Overridden in Saxon-PE configuration reader
     * @param atts the attributes of the extensionElement element in the configuration file
     */

    protected void readExtensionElement(Attributes atts) {
        XPathException err = new XPathException("Extension elements are not available in Saxon-HE");
        err.setLocator(ExpressionLocation.makeFromSax(locator));
        errors.add(err);
    }

    private void readXQueryElement(Attributes atts) {
        Properties props = new Properties();
        for (int i=0; i=0; i--) {
            List list = namespaceStack.get(i);
            for (String[] pair : list) {
                if (pair[0].equals(prefix)) {
                    return pair[1];
                }
            }
        }
        return null;
    }

    /**
     * Get an iterator over all the prefixes declared in this namespace context. This will include
     * the default namespace (prefix="") and the XML namespace where appropriate
     */

    /*@Nullable*/ public Iterator iteratePrefixes() {
        // Not used
        return null;
    }
}





© 2015 - 2025 Weber Informatics LLC | Privacy Policy