< prev index next >

src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/trax/TransformerFactoryImpl.java

Print this page
rev 959 : 8162598: XSLTC transformer swallows empty namespace declaration which is needed to undeclare default namespace
   1 /*
   2  * Copyright (c) 2007, 2015, Oracle and/or its affiliates. All rights reserved.
   3  */
   4 /*
   5  * Licensed to the Apache Software Foundation (ASF) under one or more
   6  * contributor license agreements.  See the NOTICE file distributed with
   7  * this work for additional information regarding copyright ownership.
   8  * The ASF licenses this file to You under the Apache License, Version 2.0
   9  * (the "License"); you may not use this file except in compliance with
  10  * the License.  You may obtain a copy of the License at
  11  *
  12  *     http://www.apache.org/licenses/LICENSE-2.0
  13  *
  14  * Unless required by applicable law or agreed to in writing, software
  15  * distributed under the License is distributed on an "AS IS" BASIS,
  16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17  * See the License for the specific language governing permissions and
  18  * limitations under the License.
  19  */
  20 /*
  21  * $Id: TransformerFactoryImpl.java,v 1.8 2007/04/09 21:30:41 joehw Exp $
  22  */
  23 
  24 package com.sun.org.apache.xalan.internal.xsltc.trax;
  25 
  26 import com.sun.org.apache.xalan.internal.XalanConstants;
  27 import com.sun.org.apache.xalan.internal.utils.FactoryImpl;
  28 import com.sun.org.apache.xalan.internal.utils.FeatureManager;
  29 import com.sun.org.apache.xalan.internal.utils.FeaturePropertyBase;
  30 import com.sun.org.apache.xalan.internal.utils.FeaturePropertyBase.State;
  31 import com.sun.org.apache.xalan.internal.utils.ObjectFactory;
  32 import com.sun.org.apache.xalan.internal.utils.SecuritySupport;
  33 import com.sun.org.apache.xalan.internal.utils.XMLSecurityManager;
  34 import com.sun.org.apache.xalan.internal.utils.XMLSecurityPropertyManager;
  35 import com.sun.org.apache.xalan.internal.utils.XMLSecurityPropertyManager.Property;
  36 import com.sun.org.apache.xalan.internal.xsltc.compiler.Constants;
  37 import com.sun.org.apache.xalan.internal.xsltc.compiler.SourceLoader;
  38 import com.sun.org.apache.xalan.internal.xsltc.compiler.XSLTC;
  39 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
  40 import com.sun.org.apache.xalan.internal.xsltc.dom.XSLTCDTMManager;
  41 import com.sun.org.apache.xml.internal.utils.StopParseException;
  42 import com.sun.org.apache.xml.internal.utils.StylesheetPIHandler;
  43 import java.io.File;
  44 import java.io.FileInputStream;
  45 import java.io.FileNotFoundException;
  46 import java.io.FilenameFilter;
  47 import java.io.IOException;
  48 import java.io.InputStream;
  49 import java.net.MalformedURLException;
  50 import java.net.URL;

  51 import java.util.Enumeration;
  52 import java.util.Map;
  53 import java.util.Properties;
  54 import java.util.Vector;
  55 import java.util.zip.ZipEntry;
  56 import java.util.zip.ZipFile;
  57 import javax.xml.XMLConstants;
  58 import javax.xml.parsers.SAXParser;
  59 import javax.xml.parsers.SAXParserFactory;
  60 import javax.xml.transform.ErrorListener;
  61 import javax.xml.transform.Source;
  62 import javax.xml.transform.Templates;
  63 import javax.xml.transform.Transformer;
  64 import javax.xml.transform.TransformerConfigurationException;
  65 import javax.xml.transform.TransformerException;
  66 import javax.xml.transform.TransformerFactory;
  67 import javax.xml.transform.URIResolver;
  68 import javax.xml.transform.dom.DOMResult;
  69 import javax.xml.transform.dom.DOMSource;
  70 import javax.xml.transform.sax.SAXResult;


 581             DOMSource.FEATURE,
 582             DOMResult.FEATURE,
 583             SAXSource.FEATURE,
 584             SAXResult.FEATURE,
 585             StAXSource.FEATURE,
 586             StAXResult.FEATURE,
 587             StreamSource.FEATURE,
 588             StreamResult.FEATURE,
 589             SAXTransformerFactory.FEATURE,
 590             SAXTransformerFactory.FEATURE_XMLFILTER,
 591             XalanConstants.ORACLE_FEATURE_SERVICE_MECHANISM
 592         };
 593 
 594         // feature name cannot be null
 595         if (name == null) {
 596             ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_GET_FEATURE_NULL_NAME);
 597             throw new NullPointerException(err.toString());
 598         }
 599 
 600         // Inefficient, but array is small
 601         for (int i =0; i < features.length; i++) {
 602             if (name.equals(features[i])) {
 603                 return true;
 604             }
 605         }
 606         // secure processing?
 607         if (name.equals(XMLConstants.FEATURE_SECURE_PROCESSING)) {
 608                 return !_isNotSecureProcessing;
 609         }
 610 
 611         /** Check to see if the property is managed by the security manager **/
 612         String propertyValue = (_featureManager != null) ?
 613                 _featureManager.getValueAsString(name) : null;
 614         if (propertyValue != null) {
 615             return Boolean.parseBoolean(propertyValue);
 616         }
 617 
 618         // Feature not supported
 619         return false;
 620     }
 621     /**


 788      * and every transformation.
 789      *
 790      * @return A Templates object that can be used to create Transformers.
 791      * @throws TransformerConfigurationException
 792      */
 793     @Override
 794     public Transformer newTransformer(Source source) throws
 795         TransformerConfigurationException
 796     {
 797         final Templates templates = newTemplates(source);
 798         final Transformer transformer = templates.newTransformer();
 799         if (_uriResolver != null) {
 800             transformer.setURIResolver(_uriResolver);
 801         }
 802         return(transformer);
 803     }
 804 
 805     /**
 806      * Pass warning messages from the compiler to the error listener
 807      */
 808     private void passWarningsToListener(Vector messages)
 809         throws TransformerException
 810     {
 811         if (_errorListener == null || messages == null) {
 812             return;
 813         }
 814         // Pass messages to listener, one by one
 815         final int count = messages.size();
 816         for (int pos = 0; pos < count; pos++) {
 817             ErrorMsg msg = (ErrorMsg)messages.elementAt(pos);
 818             // Workaround for the TCK failure ErrorListener.errorTests.error001.
 819             if (msg.isWarningError())
 820                 _errorListener.error(
 821                     new TransformerConfigurationException(msg.toString()));
 822             else
 823                 _errorListener.warning(
 824                     new TransformerConfigurationException(msg.toString()));
 825         }
 826     }
 827 
 828     /**
 829      * Pass error messages from the compiler to the error listener
 830      */
 831     private void passErrorsToListener(Vector messages) {
 832         try {
 833             if (_errorListener == null || messages == null) {
 834                 return;
 835             }
 836             // Pass messages to listener, one by one
 837             final int count = messages.size();
 838             for (int pos = 0; pos < count; pos++) {
 839                 String message = messages.elementAt(pos).toString();
 840                 _errorListener.error(new TransformerException(message));
 841             }
 842         }
 843         catch (TransformerException e) {
 844             // nada
 845         }
 846     }
 847 
 848     /**
 849      * javax.xml.transform.sax.TransformerFactory implementation.
 850      * Process the Source into a Templates object, which is a a compiled
 851      * representation of the source.
 852      *
 853      * @param source The input stylesheet - DOMSource not supported!!!
 854      * @return A Templates object that can be used to create Transformers.
 855      * @throws TransformerConfigurationException
 856      */
 857     @Override
 858     public Templates newTemplates(Source source)
 859         throws TransformerConfigurationException


 875             }
 876             catch (ClassNotFoundException cnfe) {
 877                 ErrorMsg err = new ErrorMsg(ErrorMsg.CLASS_NOT_FOUND_ERR, transletName);
 878                 throw new TransformerConfigurationException(err.toString());
 879             }
 880             catch (Exception e) {
 881                 ErrorMsg err = new ErrorMsg(
 882                                      new ErrorMsg(ErrorMsg.RUNTIME_ERROR_KEY)
 883                                      + e.getMessage());
 884                 throw new TransformerConfigurationException(err.toString());
 885             }
 886         }
 887 
 888         // If _autoTranslet is true, we will try to load the bytecodes
 889         // from the translet classes without compiling the stylesheet.
 890         if (_autoTranslet)  {
 891             byte[][] bytecodes;
 892             String transletClassName = getTransletBaseName(source);
 893 
 894             if (_packageName != null)
 895                transletClassName = _packageName + "." + transletClassName;
 896 
 897             if (_jarFileName != null)
 898                 bytecodes = getBytecodesFromJar(source, transletClassName);
 899             else
 900                 bytecodes = getBytecodesFromClasses(source, transletClassName);
 901 
 902             if (bytecodes != null) {
 903                 if (_debug) {
 904                     if (_jarFileName != null)
 905                         System.err.println(new ErrorMsg(
 906                             ErrorMsg.TRANSFORM_WITH_JAR_STR, transletClassName, _jarFileName));
 907                     else
 908                         System.err.println(new ErrorMsg(
 909                             ErrorMsg.TRANSFORM_WITH_TRANSLET_STR, transletClassName));
 910                 }
 911 
 912                 // Reset the per-session attributes to their default values
 913                 // after each newTemplates() call.
 914                 resetTransientAttributes();
 915                 return new TemplatesImpl(bytecodes, transletClassName, null, _indentNumber, this);


 994 
 995         // Reset the per-session attributes to their default values
 996         // after each newTemplates() call.
 997         resetTransientAttributes();
 998 
 999         // Pass compiler warnings to the error listener
1000         if (_errorListener != this) {
1001             try {
1002                 passWarningsToListener(xsltc.getWarnings());
1003             }
1004             catch (TransformerException e) {
1005                 throw new TransformerConfigurationException(e);
1006             }
1007         }
1008         else {
1009             xsltc.printWarnings();
1010         }
1011 
1012         // Check that the transformation went well before returning
1013     if (bytecodes == null) {
1014         Vector errs = xsltc.getErrors();
1015         ErrorMsg err;
1016         if (errs != null) {
1017             err = (ErrorMsg)errs.elementAt(errs.size()-1);
1018         } else {
1019             err = new ErrorMsg(ErrorMsg.JAXP_COMPILE_ERR);
1020         }
1021         Throwable cause = err.getCause();
1022         TransformerConfigurationException exc;
1023         if (cause != null) {
1024             exc =  new TransformerConfigurationException(cause.getMessage(), cause);
1025         } else {
1026             exc =  new TransformerConfigurationException(err.toString());
1027         }
1028 
1029         // Pass compiler errors to the error listener
1030         if (_errorListener != null) {
1031             passErrorsToListener(xsltc.getErrors());
1032 
1033             // As required by TCK 1.2, send a fatalError to the
1034             // error listener because compilation of the stylesheet
1035             // failed and no further processing will be possible.
1036             try {
1037                 _errorListener.fatalError(exc);


   1 /*
   2  * Copyright (c) 2007, 2016, Oracle and/or its affiliates. All rights reserved.
   3  */
   4 /*
   5  * Licensed to the Apache Software Foundation (ASF) under one or more
   6  * contributor license agreements.  See the NOTICE file distributed with
   7  * this work for additional information regarding copyright ownership.
   8  * The ASF licenses this file to You under the Apache License, Version 2.0
   9  * (the "License"); you may not use this file except in compliance with
  10  * the License.  You may obtain a copy of the License at
  11  *
  12  *     http://www.apache.org/licenses/LICENSE-2.0
  13  *
  14  * Unless required by applicable law or agreed to in writing, software
  15  * distributed under the License is distributed on an "AS IS" BASIS,
  16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17  * See the License for the specific language governing permissions and
  18  * limitations under the License.
  19  */



  20 
  21 package com.sun.org.apache.xalan.internal.xsltc.trax;
  22 
  23 import com.sun.org.apache.xalan.internal.XalanConstants;
  24 import com.sun.org.apache.xalan.internal.utils.FactoryImpl;
  25 import com.sun.org.apache.xalan.internal.utils.FeatureManager;
  26 import com.sun.org.apache.xalan.internal.utils.FeaturePropertyBase;
  27 import com.sun.org.apache.xalan.internal.utils.FeaturePropertyBase.State;
  28 import com.sun.org.apache.xalan.internal.utils.ObjectFactory;
  29 import com.sun.org.apache.xalan.internal.utils.SecuritySupport;
  30 import com.sun.org.apache.xalan.internal.utils.XMLSecurityManager;
  31 import com.sun.org.apache.xalan.internal.utils.XMLSecurityPropertyManager;
  32 import com.sun.org.apache.xalan.internal.utils.XMLSecurityPropertyManager.Property;
  33 import com.sun.org.apache.xalan.internal.xsltc.compiler.Constants;
  34 import com.sun.org.apache.xalan.internal.xsltc.compiler.SourceLoader;
  35 import com.sun.org.apache.xalan.internal.xsltc.compiler.XSLTC;
  36 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
  37 import com.sun.org.apache.xalan.internal.xsltc.dom.XSLTCDTMManager;
  38 import com.sun.org.apache.xml.internal.utils.StopParseException;
  39 import com.sun.org.apache.xml.internal.utils.StylesheetPIHandler;
  40 import java.io.File;
  41 import java.io.FileInputStream;
  42 import java.io.FileNotFoundException;
  43 import java.io.FilenameFilter;
  44 import java.io.IOException;
  45 import java.io.InputStream;
  46 import java.net.MalformedURLException;
  47 import java.net.URL;
  48 import java.util.ArrayList;
  49 import java.util.Enumeration;
  50 import java.util.Map;
  51 import java.util.Properties;
  52 import java.util.Vector;
  53 import java.util.zip.ZipEntry;
  54 import java.util.zip.ZipFile;
  55 import javax.xml.XMLConstants;
  56 import javax.xml.parsers.SAXParser;
  57 import javax.xml.parsers.SAXParserFactory;
  58 import javax.xml.transform.ErrorListener;
  59 import javax.xml.transform.Source;
  60 import javax.xml.transform.Templates;
  61 import javax.xml.transform.Transformer;
  62 import javax.xml.transform.TransformerConfigurationException;
  63 import javax.xml.transform.TransformerException;
  64 import javax.xml.transform.TransformerFactory;
  65 import javax.xml.transform.URIResolver;
  66 import javax.xml.transform.dom.DOMResult;
  67 import javax.xml.transform.dom.DOMSource;
  68 import javax.xml.transform.sax.SAXResult;


 579             DOMSource.FEATURE,
 580             DOMResult.FEATURE,
 581             SAXSource.FEATURE,
 582             SAXResult.FEATURE,
 583             StAXSource.FEATURE,
 584             StAXResult.FEATURE,
 585             StreamSource.FEATURE,
 586             StreamResult.FEATURE,
 587             SAXTransformerFactory.FEATURE,
 588             SAXTransformerFactory.FEATURE_XMLFILTER,
 589             XalanConstants.ORACLE_FEATURE_SERVICE_MECHANISM
 590         };
 591 
 592         // feature name cannot be null
 593         if (name == null) {
 594             ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_GET_FEATURE_NULL_NAME);
 595             throw new NullPointerException(err.toString());
 596         }
 597 
 598         // Inefficient, but array is small
 599         for (int i = 0; i < features.length; i++) {
 600             if (name.equals(features[i])) {
 601                 return true;
 602             }
 603         }
 604         // secure processing?
 605         if (name.equals(XMLConstants.FEATURE_SECURE_PROCESSING)) {
 606                 return !_isNotSecureProcessing;
 607         }
 608 
 609         /** Check to see if the property is managed by the security manager **/
 610         String propertyValue = (_featureManager != null) ?
 611                 _featureManager.getValueAsString(name) : null;
 612         if (propertyValue != null) {
 613             return Boolean.parseBoolean(propertyValue);
 614         }
 615 
 616         // Feature not supported
 617         return false;
 618     }
 619     /**


 786      * and every transformation.
 787      *
 788      * @return A Templates object that can be used to create Transformers.
 789      * @throws TransformerConfigurationException
 790      */
 791     @Override
 792     public Transformer newTransformer(Source source) throws
 793         TransformerConfigurationException
 794     {
 795         final Templates templates = newTemplates(source);
 796         final Transformer transformer = templates.newTransformer();
 797         if (_uriResolver != null) {
 798             transformer.setURIResolver(_uriResolver);
 799         }
 800         return(transformer);
 801     }
 802 
 803     /**
 804      * Pass warning messages from the compiler to the error listener
 805      */
 806     private void passWarningsToListener(ArrayList<ErrorMsg> messages)
 807         throws TransformerException
 808     {
 809         if (_errorListener == null || messages == null) {
 810             return;
 811         }
 812         // Pass messages to listener, one by one
 813         final int count = messages.size();
 814         for (int pos = 0; pos < count; pos++) {
 815             ErrorMsg msg = messages.get(pos);
 816             // Workaround for the TCK failure ErrorListener.errorTests.error001.
 817             if (msg.isWarningError())
 818                 _errorListener.error(
 819                     new TransformerConfigurationException(msg.toString()));
 820             else
 821                 _errorListener.warning(
 822                     new TransformerConfigurationException(msg.toString()));
 823         }
 824     }
 825 
 826     /**
 827      * Pass error messages from the compiler to the error listener
 828      */
 829     private void passErrorsToListener(ArrayList<ErrorMsg> messages) {
 830         try {
 831             if (_errorListener == null || messages == null) {
 832                 return;
 833             }
 834             // Pass messages to listener, one by one
 835             final int count = messages.size();
 836             for (int pos = 0; pos < count; pos++) {
 837                 String message = messages.get(pos).toString();
 838                 _errorListener.error(new TransformerException(message));
 839             }
 840         }
 841         catch (TransformerException e) {
 842             // nada
 843         }
 844     }
 845 
 846     /**
 847      * javax.xml.transform.sax.TransformerFactory implementation.
 848      * Process the Source into a Templates object, which is a a compiled
 849      * representation of the source.
 850      *
 851      * @param source The input stylesheet - DOMSource not supported!!!
 852      * @return A Templates object that can be used to create Transformers.
 853      * @throws TransformerConfigurationException
 854      */
 855     @Override
 856     public Templates newTemplates(Source source)
 857         throws TransformerConfigurationException


 873             }
 874             catch (ClassNotFoundException cnfe) {
 875                 ErrorMsg err = new ErrorMsg(ErrorMsg.CLASS_NOT_FOUND_ERR, transletName);
 876                 throw new TransformerConfigurationException(err.toString());
 877             }
 878             catch (Exception e) {
 879                 ErrorMsg err = new ErrorMsg(
 880                                      new ErrorMsg(ErrorMsg.RUNTIME_ERROR_KEY)
 881                                      + e.getMessage());
 882                 throw new TransformerConfigurationException(err.toString());
 883             }
 884         }
 885 
 886         // If _autoTranslet is true, we will try to load the bytecodes
 887         // from the translet classes without compiling the stylesheet.
 888         if (_autoTranslet)  {
 889             byte[][] bytecodes;
 890             String transletClassName = getTransletBaseName(source);
 891 
 892             if (_packageName != null)
 893                 transletClassName = _packageName + "." + transletClassName;
 894 
 895             if (_jarFileName != null)
 896                 bytecodes = getBytecodesFromJar(source, transletClassName);
 897             else
 898                 bytecodes = getBytecodesFromClasses(source, transletClassName);
 899 
 900             if (bytecodes != null) {
 901                 if (_debug) {
 902                     if (_jarFileName != null)
 903                         System.err.println(new ErrorMsg(
 904                             ErrorMsg.TRANSFORM_WITH_JAR_STR, transletClassName, _jarFileName));
 905                     else
 906                         System.err.println(new ErrorMsg(
 907                             ErrorMsg.TRANSFORM_WITH_TRANSLET_STR, transletClassName));
 908                 }
 909 
 910                 // Reset the per-session attributes to their default values
 911                 // after each newTemplates() call.
 912                 resetTransientAttributes();
 913                 return new TemplatesImpl(bytecodes, transletClassName, null, _indentNumber, this);


 992 
 993         // Reset the per-session attributes to their default values
 994         // after each newTemplates() call.
 995         resetTransientAttributes();
 996 
 997         // Pass compiler warnings to the error listener
 998         if (_errorListener != this) {
 999             try {
1000                 passWarningsToListener(xsltc.getWarnings());
1001             }
1002             catch (TransformerException e) {
1003                 throw new TransformerConfigurationException(e);
1004             }
1005         }
1006         else {
1007             xsltc.printWarnings();
1008         }
1009 
1010         // Check that the transformation went well before returning
1011     if (bytecodes == null) {
1012         ArrayList<ErrorMsg> errs = xsltc.getErrors();
1013         ErrorMsg err;
1014         if (errs != null) {
1015             err = errs.get(errs.size()-1);
1016         } else {
1017             err = new ErrorMsg(ErrorMsg.JAXP_COMPILE_ERR);
1018         }
1019         Throwable cause = err.getCause();
1020         TransformerConfigurationException exc;
1021         if (cause != null) {
1022             exc =  new TransformerConfigurationException(cause.getMessage(), cause);
1023         } else {
1024             exc =  new TransformerConfigurationException(err.toString());
1025         }
1026 
1027         // Pass compiler errors to the error listener
1028         if (_errorListener != null) {
1029             passErrorsToListener(xsltc.getErrors());
1030 
1031             // As required by TCK 1.2, send a fatalError to the
1032             // error listener because compilation of the stylesheet
1033             // failed and no further processing will be possible.
1034             try {
1035                 _errorListener.fatalError(exc);


< prev index next >