src/share/jaxws_classes/com/sun/xml/internal/bind/v2/util/XmlFactory.java

changeset 368
0989ad8c0860
child 397
b99d7e355d4b
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/bind/v2/util/XmlFactory.java	Tue Apr 09 14:51:13 2013 +0100
     1.3 @@ -0,0 +1,189 @@
     1.4 +/*
     1.5 + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.xml.internal.bind.v2.util;
    1.30 +
    1.31 +import com.sun.xml.internal.bind.Util;
    1.32 +import com.sun.xml.internal.bind.v2.Messages;
    1.33 +import java.util.logging.Level;
    1.34 +import java.util.logging.Logger;
    1.35 +import javax.xml.XMLConstants;
    1.36 +import javax.xml.parsers.DocumentBuilderFactory;
    1.37 +import javax.xml.parsers.ParserConfigurationException;
    1.38 +import javax.xml.parsers.SAXParserFactory;
    1.39 +import javax.xml.transform.TransformerConfigurationException;
    1.40 +import javax.xml.transform.TransformerFactory;
    1.41 +import javax.xml.validation.SchemaFactory;
    1.42 +import javax.xml.xpath.XPathFactory;
    1.43 +import javax.xml.xpath.XPathFactoryConfigurationException;
    1.44 +import org.xml.sax.SAXNotRecognizedException;
    1.45 +import org.xml.sax.SAXNotSupportedException;
    1.46 +
    1.47 +/**
    1.48 + * Provides helper methods for creating properly configured XML parser
    1.49 + * factory instances with namespace support turned on and configured for
    1.50 + * security.
    1.51 + * @author snajper
    1.52 + */
    1.53 +public class XmlFactory {
    1.54 +
    1.55 +    private static final Logger LOGGER = Logger.getLogger(XmlFactory.class.getName());
    1.56 +
    1.57 +    /**
    1.58 +     * If true XML security features when parsing XML documents will be disabled.
    1.59 +     * The default value is false.
    1.60 +     *
    1.61 +     * Boolean
    1.62 +     * @since 2.2.6
    1.63 +     */
    1.64 +    private static final String DISABLE_XML_SECURITY  = "com.sun.xml.internal.bind.disableXmlSecurity";
    1.65 +
    1.66 +    public static final boolean DISABLE_SECURE_PROCESSING =
    1.67 +            Boolean.parseBoolean(Util.getSystemProperty(DISABLE_XML_SECURITY));
    1.68 +
    1.69 +    private static boolean xmlFeatureValue(boolean runtimeSetting) {
    1.70 +        return !(DISABLE_SECURE_PROCESSING || runtimeSetting);
    1.71 +    }
    1.72 +
    1.73 +    /**
    1.74 +     * Returns properly configured (e.g. security features) schema factory
    1.75 +     * - namespaceAware == true
    1.76 +     * - securityProcessing == is set based on security processing property, default is true
    1.77 +     */
    1.78 +    public static SchemaFactory createSchemaFactory(final String language, boolean disableSecureProcessing) throws IllegalStateException {
    1.79 +        try {
    1.80 +            SchemaFactory factory = SchemaFactory.newInstance(language);
    1.81 +            if (LOGGER.isLoggable(Level.FINE)) {
    1.82 +                LOGGER.log(Level.FINE, "SchemaFactory instance: {0}", factory);
    1.83 +            }
    1.84 +            factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, xmlFeatureValue(disableSecureProcessing));
    1.85 +            return factory;
    1.86 +        } catch (SAXNotRecognizedException ex) {
    1.87 +            LOGGER.log(Level.SEVERE, null, ex);
    1.88 +            throw new IllegalStateException(ex);
    1.89 +        } catch (SAXNotSupportedException ex) {
    1.90 +            LOGGER.log(Level.SEVERE, null, ex);
    1.91 +            throw new IllegalStateException(ex);
    1.92 +        } catch (AbstractMethodError er) {
    1.93 +            LOGGER.log(Level.SEVERE, null, er);
    1.94 +            throw new IllegalStateException(Messages.INVALID_JAXP_IMPLEMENTATION.format(), er);
    1.95 +        }
    1.96 +    }
    1.97 +
    1.98 +    /**
    1.99 +     * Returns properly configured (e.g. security features) parser factory
   1.100 +     * - namespaceAware == true
   1.101 +     * - securityProcessing == is set based on security processing property, default is true
   1.102 +     */
   1.103 +    public static SAXParserFactory createParserFactory(boolean disableSecureProcessing) throws IllegalStateException {
   1.104 +        try {
   1.105 +            SAXParserFactory factory = SAXParserFactory.newInstance();
   1.106 +            if (LOGGER.isLoggable(Level.FINE)) {
   1.107 +                LOGGER.log(Level.FINE, "SAXParserFactory instance: {0}", factory);
   1.108 +            }
   1.109 +            factory.setNamespaceAware(true);
   1.110 +            factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, xmlFeatureValue(disableSecureProcessing));
   1.111 +            return factory;
   1.112 +        } catch (ParserConfigurationException ex) {
   1.113 +            LOGGER.log(Level.SEVERE, null, ex);
   1.114 +            throw new IllegalStateException( ex);
   1.115 +        } catch (SAXNotRecognizedException ex) {
   1.116 +            LOGGER.log(Level.SEVERE, null, ex);
   1.117 +            throw new IllegalStateException( ex);
   1.118 +        } catch (SAXNotSupportedException ex) {
   1.119 +            LOGGER.log(Level.SEVERE, null, ex);
   1.120 +            throw new IllegalStateException( ex);
   1.121 +        } catch (AbstractMethodError er) {
   1.122 +            LOGGER.log(Level.SEVERE, null, er);
   1.123 +            throw new IllegalStateException(Messages.INVALID_JAXP_IMPLEMENTATION.format(), er);
   1.124 +        }
   1.125 +    }
   1.126 +
   1.127 +    /**
   1.128 +     * Returns properly configured (e.g. security features) factory
   1.129 +     * - securityProcessing == is set based on security processing property, default is true
   1.130 +     */
   1.131 +    public static XPathFactory createXPathFactory(boolean disableSecureProcessing) throws IllegalStateException {
   1.132 +        try {
   1.133 +            XPathFactory factory = XPathFactory.newInstance();
   1.134 +            if (LOGGER.isLoggable(Level.FINE)) {
   1.135 +                LOGGER.log(Level.FINE, "XPathFactory instance: {0}", factory);
   1.136 +            }
   1.137 +            factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, xmlFeatureValue(disableSecureProcessing));
   1.138 +            return factory;
   1.139 +        } catch (XPathFactoryConfigurationException ex) {
   1.140 +            LOGGER.log(Level.SEVERE, null, ex);
   1.141 +            throw new IllegalStateException( ex);
   1.142 +        } catch (AbstractMethodError er) {
   1.143 +            LOGGER.log(Level.SEVERE, null, er);
   1.144 +            throw new IllegalStateException(Messages.INVALID_JAXP_IMPLEMENTATION.format(), er);
   1.145 +        }
   1.146 +    }
   1.147 +
   1.148 +    /**
   1.149 +     * Returns properly configured (e.g. security features) factory
   1.150 +     * - securityProcessing == is set based on security processing property, default is true
   1.151 +     */
   1.152 +    public static TransformerFactory createTransformerFactory(boolean disableSecureProcessing) throws IllegalStateException {
   1.153 +        try {
   1.154 +            TransformerFactory factory = TransformerFactory.newInstance();
   1.155 +            if (LOGGER.isLoggable(Level.FINE)) {
   1.156 +                LOGGER.log(Level.FINE, "TransformerFactory instance: {0}", factory);
   1.157 +            }
   1.158 +            factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, xmlFeatureValue(disableSecureProcessing));
   1.159 +            return factory;
   1.160 +        } catch (TransformerConfigurationException ex) {
   1.161 +            LOGGER.log(Level.SEVERE, null, ex);
   1.162 +            throw new IllegalStateException( ex);
   1.163 +        } catch (AbstractMethodError er) {
   1.164 +            LOGGER.log(Level.SEVERE, null, er);
   1.165 +            throw new IllegalStateException(Messages.INVALID_JAXP_IMPLEMENTATION.format(), er);
   1.166 +        }
   1.167 +    }
   1.168 +
   1.169 +    /**
   1.170 +     * Returns properly configured (e.g. security features) factory
   1.171 +     * - namespaceAware == true
   1.172 +     * - securityProcessing == is set based on security processing property, default is true
   1.173 +     */
   1.174 +    public static DocumentBuilderFactory createDocumentBuilderFactory(boolean disableSecureProcessing) throws IllegalStateException {
   1.175 +        try {
   1.176 +            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
   1.177 +            if (LOGGER.isLoggable(Level.FINE)) {
   1.178 +                LOGGER.log(Level.FINE, "DocumentBuilderFactory instance: {0}", factory);
   1.179 +            }
   1.180 +            factory.setNamespaceAware(true);
   1.181 +            factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, xmlFeatureValue(disableSecureProcessing));
   1.182 +            return factory;
   1.183 +        } catch (ParserConfigurationException ex) {
   1.184 +            LOGGER.log(Level.SEVERE, null, ex);
   1.185 +            throw new IllegalStateException( ex);
   1.186 +        } catch (AbstractMethodError er) {
   1.187 +            LOGGER.log(Level.SEVERE, null, er);
   1.188 +            throw new IllegalStateException(Messages.INVALID_JAXP_IMPLEMENTATION.format(), er);
   1.189 +        }
   1.190 +    }
   1.191 +
   1.192 +}

mercurial