1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 /*
   6  * Copyright  1999-2004 The Apache Software Foundation.
   7  *
   8  *  Licensed under the Apache License, Version 2.0 (the "License");
   9  *  you may not use this file except in compliance with the License.
  10  *  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.xml.internal.security.transforms.implementations;
  22 
  23 import java.io.OutputStream;
  24 
  25 import com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException;
  26 import com.sun.org.apache.xml.internal.security.c14n.implementations.Canonicalizer20010315ExclOmitComments;
  27 import com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException;
  28 import com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput;
  29 import com.sun.org.apache.xml.internal.security.transforms.Transform;
  30 import com.sun.org.apache.xml.internal.security.transforms.TransformSpi;
  31 import com.sun.org.apache.xml.internal.security.transforms.Transforms;
  32 import com.sun.org.apache.xml.internal.security.transforms.params.InclusiveNamespaces;
  33 import com.sun.org.apache.xml.internal.security.utils.XMLUtils;
  34 import org.w3c.dom.Element;
  35 
  36 /**
  37  * Class TransformC14NExclusive
  38  *
  39  * @author $Author: mullan $
  40  * @version $Revision: 1.5 $
  41  */
  42 public class TransformC14NExclusive extends TransformSpi {
  43 
  44    /** Field implementedTransformURI */
  45    public static final String implementedTransformURI =
  46       Transforms.TRANSFORM_C14N_EXCL_OMIT_COMMENTS;
  47 
  48    /**
  49     * Method engineGetURI
  50     *
  51     * @inheritDoc
  52     */
  53    protected String engineGetURI() {
  54       return implementedTransformURI;
  55    }
  56 
  57    /**
  58     * Method enginePerformTransform
  59     *
  60     * @param input
  61     * @return the transformed of the input
  62     * @throws CanonicalizationException
  63     */
  64    protected XMLSignatureInput enginePerformTransform
  65         (XMLSignatureInput input, Transform _transformObject)
  66            throws CanonicalizationException {
  67             return enginePerformTransform(input, null, _transformObject);
  68    }
  69 
  70     protected XMLSignatureInput enginePerformTransform(XMLSignatureInput input,OutputStream os, Transform _transformObject)
  71     throws CanonicalizationException {
  72       try {
  73          String inclusiveNamespaces = null;
  74 
  75          if (_transformObject
  76                  .length(InclusiveNamespaces
  77                     .ExclusiveCanonicalizationNamespace, InclusiveNamespaces
  78                     ._TAG_EC_INCLUSIVENAMESPACES) == 1) {
  79             Element inclusiveElement =
  80                 XMLUtils.selectNode(
  81                _transformObject.getElement().getFirstChild(),
  82                   InclusiveNamespaces.ExclusiveCanonicalizationNamespace,
  83                   InclusiveNamespaces._TAG_EC_INCLUSIVENAMESPACES,0);
  84 
  85             inclusiveNamespaces = new InclusiveNamespaces(inclusiveElement,
  86                     _transformObject.getBaseURI()).getInclusiveNamespaces();
  87          }
  88 
  89          Canonicalizer20010315ExclOmitComments c14n =
  90             new Canonicalizer20010315ExclOmitComments();
  91          if (os!=null) {
  92             c14n.setWriter(os);
  93          }
  94          byte []result;
  95          result =c14n.engineCanonicalize(input, inclusiveNamespaces);
  96 
  97          XMLSignatureInput output=new XMLSignatureInput(result);
  98          if (os!=null) {
  99             output.setOutputStream(os);
 100          }
 101          return output;
 102       } catch (XMLSecurityException ex) {
 103          throw new CanonicalizationException("empty", ex);
 104       }
 105    }
 106 }