jdk/src/share/classes/com/sun/org/apache/xml/internal/security/transforms/implementations/TransformXSLT.java
author mullan
Mon, 26 Sep 2011 17:20:45 -0700
changeset 10694 cf59e2badd14
parent 1337 e8d6cef36199
child 18240 cda839ac048f
permissions -rw-r--r--
7088502: Security libraries don't build with javac -Werror Summary: Changes to files in src/share/classes/com/sun/org/apache/xml/internal/security and its subpackages to remove warnings Reviewed-by: mullan Contributed-by: kurchi.subhra.hazra@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * reserved comment block
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT REMOVE OR ALTER!
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
/*
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
     6
 * Copyright 1999-2007 The Apache Software Foundation.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *  Licensed under the Apache License, Version 2.0 (the "License");
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *  you may not use this file except in compliance with the License.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *  You may obtain a copy of the License at
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 *      http://www.apache.org/licenses/LICENSE-2.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *  Unless required by applicable law or agreed to in writing, software
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 *  distributed under the License is distributed on an "AS IS" BASIS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 *  See the License for the specific language governing permissions and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *  limitations under the License.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
package com.sun.org.apache.xml.internal.security.transforms.implementations;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
import java.io.ByteArrayInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
import java.io.ByteArrayOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
import java.io.OutputStream;
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    27
import java.lang.reflect.Method;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.xml.transform.Source;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.xml.transform.Transformer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.xml.transform.TransformerConfigurationException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.xml.transform.TransformerException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.xml.transform.TransformerFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.xml.transform.dom.DOMSource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.xml.transform.stream.StreamResult;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.xml.transform.stream.StreamSource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput;
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    40
import com.sun.org.apache.xml.internal.security.transforms.Transform;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import com.sun.org.apache.xml.internal.security.transforms.TransformSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import com.sun.org.apache.xml.internal.security.transforms.TransformationException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import com.sun.org.apache.xml.internal.security.transforms.Transforms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import com.sun.org.apache.xml.internal.security.utils.XMLUtils;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import org.w3c.dom.Element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * Class TransformXSLT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * Implements the <CODE>http://www.w3.org/TR/1999/REC-xslt-19991116</CODE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * transform.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * @author Christian Geuer-Pollmann
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
public class TransformXSLT extends TransformSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
   /** Field implementedTransformURI */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
   public static final String implementedTransformURI =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
      Transforms.TRANSFORM_XSLT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
   //J-
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
   static final String XSLTSpecNS              = "http://www.w3.org/1999/XSL/Transform";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
   static final String defaultXSLTSpecNSprefix = "xslt";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
   static final String XSLTSTYLESHEET          = "stylesheet";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    65
   static java.util.logging.Logger log =
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    66
      java.util.logging.Logger.getLogger(
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    67
         TransformXSLT.class.getName());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    * Method engineGetURI
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    * @inheritDoc
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
   protected String engineGetURI() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
      return implementedTransformURI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    * Method enginePerformTransform
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    * @param input the input for this transform
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    * @return the result of this Transform
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    * @throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    * @throws TransformationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    */
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    86
   protected XMLSignatureInput enginePerformTransform
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    87
        (XMLSignatureInput input, Transform _transformObject)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
           throws IOException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                  TransformationException {
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    90
        return enginePerformTransform(input, null, _transformObject);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
   }
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    92
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    93
    protected XMLSignatureInput enginePerformTransform(XMLSignatureInput input,OutputStream baos, Transform _transformObject)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    throws IOException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
           TransformationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
      try {
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    97
         Element transformElement = _transformObject.getElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
         Element _xsltElement =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            XMLUtils.selectNode(transformElement.getFirstChild(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                                                XSLTSpecNS,"stylesheet", 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
         if (_xsltElement == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            Object exArgs[] = { "xslt:stylesheet", "Transform" };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            throw new TransformationException("xml.WrongContent", exArgs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
         TransformerFactory tFactory = TransformerFactory.newInstance();
10694
cf59e2badd14 7088502: Security libraries don't build with javac -Werror
mullan
parents: 1337
diff changeset
   110
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
         // Process XSLT stylesheets in a secure manner
10694
cf59e2badd14 7088502: Security libraries don't build with javac -Werror
mullan
parents: 1337
diff changeset
   112
         tFactory.setFeature("http://javax.xml.XMLConstants/feature/secure-processing", Boolean.TRUE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
         /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
          * This transform requires an octet stream as input. If the actual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
          * input is an XPath node-set, then the signature application should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
          * attempt to convert it to octets (apply Canonical XML]) as described
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
          * in the Reference Processing Model (section 4.3.3.2).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
          */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
         Source xmlSource =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            new StreamSource(new ByteArrayInputStream(input.getBytes()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
         Source stylesheet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
         /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
          * This complicated transformation of the stylesheet itself is necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
          * because of the need to get the pure style sheet. If we simply say
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
          * Source stylesheet = new DOMSource(this._xsltElement);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
          * whereby this._xsltElement is not the rootElement of the Document,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
          * this causes problems;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
          * so we convert the stylesheet to byte[] and use this as input stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
          */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
         {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            ByteArrayOutputStream os = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            Transformer transformer = tFactory.newTransformer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            DOMSource source = new DOMSource(_xsltElement);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            StreamResult result = new StreamResult(os);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            transformer.transform(source, result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            stylesheet =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
               new StreamSource(new ByteArrayInputStream(os.toByteArray()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
         Transformer transformer = tFactory.newTransformer(stylesheet);
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   144
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   145
         // Force Xalan to use \n as line separator on all OSes. This
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   146
         // avoids OS specific signature validation failures due to line
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   147
         // separator differences in the transformed output. Unfortunately,
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   148
         // this is not a standard JAXP property so will not work with non-Xalan
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   149
         // implementations.
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   150
         try {
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   151
            transformer.setOutputProperty
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   152
               ("{http://xml.apache.org/xalan}line-separator", "\n");
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   153
         } catch (Exception e) {
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   154
            log.log(java.util.logging.Level.WARNING, "Unable to set Xalan line-separator property: "
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   155
               + e.getMessage());
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   156
         }
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   157
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
         if (baos==null) {
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   159
            ByteArrayOutputStream baos1 = new ByteArrayOutputStream();
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   160
            StreamResult outputTarget = new StreamResult(baos1);
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   161
            transformer.transform(xmlSource, outputTarget);
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   162
            return new XMLSignatureInput(baos1.toByteArray());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
         StreamResult outputTarget = new StreamResult(baos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
         transformer.transform(xmlSource, outputTarget);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
         XMLSignatureInput output=new XMLSignatureInput((byte[])null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
         output.setOutputStream(baos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
         return output;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
      } catch (XMLSecurityException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
         Object exArgs[] = { ex.getMessage() };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
         throw new TransformationException("generic.EmptyMessage", exArgs, ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
      } catch (TransformerConfigurationException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
         Object exArgs[] = { ex.getMessage() };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
         throw new TransformationException("generic.EmptyMessage", exArgs, ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
      } catch (TransformerException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
         Object exArgs[] = { ex.getMessage() };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
         throw new TransformationException("generic.EmptyMessage", exArgs, ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
}