KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xml > internal > security > transforms > implementations > TransformXSLT


1
2 /*
3  * Copyright 1999-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18 package com.sun.org.apache.xml.internal.security.transforms.implementations;
19
20
21
22 import java.io.ByteArrayInputStream JavaDoc;
23 import java.io.ByteArrayOutputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.OutputStream JavaDoc;
26
27 import javax.xml.XMLConstants JavaDoc;
28 import javax.xml.transform.Source JavaDoc;
29 import javax.xml.transform.Transformer JavaDoc;
30 import javax.xml.transform.TransformerConfigurationException JavaDoc;
31 import javax.xml.transform.TransformerException JavaDoc;
32 import javax.xml.transform.TransformerFactory JavaDoc;
33 import javax.xml.transform.dom.DOMSource JavaDoc;
34 import javax.xml.transform.stream.StreamResult JavaDoc;
35 import javax.xml.transform.stream.StreamSource JavaDoc;
36
37 import com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException;
38 import com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput;
39 import com.sun.org.apache.xml.internal.security.transforms.TransformSpi;
40 import com.sun.org.apache.xml.internal.security.transforms.TransformationException;
41 import com.sun.org.apache.xml.internal.security.transforms.Transforms;
42 import com.sun.org.apache.xml.internal.security.utils.XMLUtils;
43 import org.w3c.dom.Element JavaDoc;
44
45
46 /**
47  * Class TransformXSLT
48  *
49  * Implements the <CODE>http://www.w3.org/TR/1999/REC-xslt-19991116</CODE>
50  * transform.
51  *
52  * @author Christian Geuer-Pollmann
53  */

54 public class TransformXSLT extends TransformSpi {
55
56    /** Field implementedTransformURI */
57    public static final String JavaDoc implementedTransformURI =
58       Transforms.TRANSFORM_XSLT;
59    //J-
60
static final String JavaDoc XSLTSpecNS = "http://www.w3.org/1999/XSL/Transform";
61    static final String JavaDoc defaultXSLTSpecNSprefix = "xslt";
62    static final String JavaDoc XSLTSTYLESHEET = "stylesheet";
63
64
65    /**
66     * Method engineGetURI
67     *
68     * @inheritDoc
69     */

70    protected String JavaDoc engineGetURI() {
71       return implementedTransformURI;
72    }
73
74    /**
75     * Method enginePerformTransform
76     *
77     * @param input the input for this transform
78     * @return the result of this Transform
79     * @throws IOException
80     * @throws TransformationException
81     */

82    protected XMLSignatureInput enginePerformTransform(XMLSignatureInput input)
83            throws IOException JavaDoc,
84                   TransformationException {
85     return enginePerformTransform(input,null);
86    }
87     protected XMLSignatureInput enginePerformTransform(XMLSignatureInput input,OutputStream JavaDoc baos)
88     throws IOException JavaDoc,
89            TransformationException {
90       try {
91          Element JavaDoc transformElement = this._transformObject.getElement();
92
93          Element JavaDoc _xsltElement =
94             XMLUtils.selectNode(transformElement.getFirstChild(),
95                                                 XSLTSpecNS,"stylesheet", 0);
96
97          if (_xsltElement == null) {
98             Object JavaDoc exArgs[] = { "xslt:stylesheet", "Transform" };
99
100             throw new TransformationException("xml.WrongContent", exArgs);
101          }
102
103          TransformerFactory JavaDoc tFactory = TransformerFactory.newInstance();
104      // Process XSLT stylesheets in a secure manner
105
tFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
106
107          /*
108           * This transform requires an octet stream as input. If the actual
109           * input is an XPath node-set, then the signature application should
110           * attempt to convert it to octets (apply Canonical XML]) as described
111           * in the Reference Processing Model (section 4.3.3.2).
112           */

113          Source JavaDoc xmlSource =
114             new StreamSource JavaDoc(new ByteArrayInputStream JavaDoc(input.getBytes()));
115          Source JavaDoc stylesheet;
116
117          /*
118           * This complicated transformation of the stylesheet itself is necessary
119           * because of the need to get the pure style sheet. If we simply say
120           * Source stylesheet = new DOMSource(this._xsltElement);
121           * whereby this._xsltElement is not the rootElement of the Document,
122           * this causes problems;
123           * so we convert the stylesheet to byte[] and use this as input stream
124           */

125          {
126             ByteArrayOutputStream JavaDoc os = new ByteArrayOutputStream JavaDoc();
127             Transformer JavaDoc transformer = tFactory.newTransformer();
128             DOMSource JavaDoc source = new DOMSource JavaDoc(_xsltElement);
129             StreamResult JavaDoc result = new StreamResult JavaDoc(os);
130
131             transformer.transform(source, result);
132
133             stylesheet =
134                new StreamSource JavaDoc(new ByteArrayInputStream JavaDoc(os.toByteArray()));
135          }
136
137          Transformer JavaDoc transformer = tFactory.newTransformer(stylesheet);
138          if (baos==null) {
139                 ByteArrayOutputStream JavaDoc baos1 = new ByteArrayOutputStream JavaDoc();
140                StreamResult JavaDoc outputTarget = new StreamResult JavaDoc(baos1);
141                transformer.transform(xmlSource, outputTarget);
142                return new XMLSignatureInput(baos1.toByteArray());
143
144          }
145          StreamResult JavaDoc outputTarget = new StreamResult JavaDoc(baos);
146
147          transformer.transform(xmlSource, outputTarget);
148          XMLSignatureInput output=new XMLSignatureInput((byte[])null);
149          output.setOutputStream(baos);
150          return output;
151       } catch (XMLSecurityException ex) {
152          Object JavaDoc exArgs[] = { ex.getMessage() };
153
154          throw new TransformationException("generic.EmptyMessage", exArgs, ex);
155       } catch (TransformerConfigurationException JavaDoc ex) {
156          Object JavaDoc exArgs[] = { ex.getMessage() };
157
158          throw new TransformationException("generic.EmptyMessage", exArgs, ex);
159       } catch (TransformerException JavaDoc ex) {
160          Object JavaDoc exArgs[] = { ex.getMessage() };
161
162          throw new TransformationException("generic.EmptyMessage", exArgs, ex);
163       }
164    }
165 }
166
Popular Tags