KickJava   Java API By Example, From Geeks To Geeks.

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


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

17 package com.sun.org.apache.xml.internal.security.transforms;
18
19
20
21 import java.io.IOException JavaDoc;
22 import java.io.OutputStream JavaDoc;
23
24 import com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException;
25 import com.sun.org.apache.xml.internal.security.c14n.Canonicalizer;
26 import com.sun.org.apache.xml.internal.security.c14n.InvalidCanonicalizerException;
27 import com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException;
28 import com.sun.org.apache.xml.internal.security.signature.XMLSignatureException;
29 import com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput;
30 import com.sun.org.apache.xml.internal.security.utils.Constants;
31 import com.sun.org.apache.xml.internal.security.utils.SignatureElementProxy;
32 import com.sun.org.apache.xml.internal.security.utils.XMLUtils;
33 import org.w3c.dom.DOMException JavaDoc;
34 import org.w3c.dom.Document JavaDoc;
35 import org.w3c.dom.Element JavaDoc;
36 import org.w3c.dom.NodeList JavaDoc;
37
38
39 /**
40  * Holder of the {@link com.sun.org.apache.xml.internal.security.transforms.Transform} steps to be performed on the data.
41  * The input to the first Transform is the result of dereferencing the <code>URI</code> attribute of the <code>Reference</code> element.
42  * The output from the last Transform is the input for the <code>DigestMethod algorithm</code>
43  *
44  * @author Christian Geuer-Pollmann
45  * @see Transform
46  * @see com.sun.org.apache.xml.internal.security.signature.Reference
47  */

48 public class Transforms extends SignatureElementProxy {
49
50    /** {@link java.util.logging} logging facility */
51     static java.util.logging.Logger JavaDoc log =
52         java.util.logging.Logger.getLogger(Transforms.class.getName());
53    //J-
54
/** Canonicalization - Required Canonical XML (omits comments) */
55    public static final String JavaDoc TRANSFORM_C14N_OMIT_COMMENTS = Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS;
56    /** Canonicalization - Recommended Canonical XML with Comments */
57    public static final String JavaDoc TRANSFORM_C14N_WITH_COMMENTS = Canonicalizer.ALGO_ID_C14N_WITH_COMMENTS;
58    /** Canonicalization - Required Exclusive Canonicalization (omits comments) */
59    public static final String JavaDoc TRANSFORM_C14N_EXCL_OMIT_COMMENTS = Canonicalizer.ALGO_ID_C14N_EXCL_OMIT_COMMENTS;
60    /** Canonicalization - Recommended Exclusive Canonicalization with Comments */
61    public static final String JavaDoc TRANSFORM_C14N_EXCL_WITH_COMMENTS = Canonicalizer.ALGO_ID_C14N_EXCL_WITH_COMMENTS;
62    /** Transform - Optional XSLT */
63    public static final String JavaDoc TRANSFORM_XSLT = "http://www.w3.org/TR/1999/REC-xslt-19991116";
64    /** Transform - Required base64 decoding */
65    public static final String JavaDoc TRANSFORM_BASE64_DECODE = Constants.SignatureSpecNS + "base64";
66    /** Transform - Recommended XPath */
67    public static final String JavaDoc TRANSFORM_XPATH = "http://www.w3.org/TR/1999/REC-xpath-19991116";
68    /** Transform - Required Enveloped Signature */
69    public static final String JavaDoc TRANSFORM_ENVELOPED_SIGNATURE = Constants.SignatureSpecNS + "enveloped-signature";
70    /** Transform - XPointer */
71    public static final String JavaDoc TRANSFORM_XPOINTER = "http://www.w3.org/TR/2001/WD-xptr-20010108";
72    /** Transform - XPath Filter v2.0 */
73    public static final String JavaDoc TRANSFORM_XPATH2FILTER04 = "http://www.w3.org/2002/04/xmldsig-filter2";
74    /** Transform - XPath Filter */
75    public static final String JavaDoc TRANSFORM_XPATH2FILTER = "http://www.w3.org/2002/06/xmldsig-filter2";
76    /** Transform - XPath Filter CHGP private*/
77    public static final String JavaDoc TRANSFORM_XPATHFILTERCHGP = "http://www.nue.et-inf.uni-siegen.de/~geuer-pollmann/#xpathFilter";
78    //J+
79
Element JavaDoc []transforms;
80    /**
81     * Consturcts {@link Transforms}
82     *
83     * @param doc the {@link Document} in which <code>XMLsignature</code> will be placed
84     */

85    public Transforms(Document JavaDoc doc) {
86
87       super(doc);
88
89       XMLUtils.addReturnToElement(this._constructionElement);
90    }
91
92    /**
93     * Consturcts {@link Transforms} from {@link Element} which is <code>Transforms</code> Element
94     *
95     * @param element is <code>Transforms</code> element
96     * @param BaseURI the URI where the XML instance was stored
97     * @throws DOMException
98     * @throws InvalidTransformException
99     * @throws TransformationException
100     * @throws XMLSecurityException
101     * @throws XMLSignatureException
102     */

103    public Transforms(Element JavaDoc element, String JavaDoc BaseURI)
104            throws DOMException JavaDoc, XMLSignatureException,
105                   InvalidTransformException, TransformationException,
106                   XMLSecurityException {
107
108       super(element, BaseURI);
109
110       int numberOfTransformElems = this.getLength();
111
112       if (numberOfTransformElems == 0) {
113
114          // At least ont Transform element must be present. Bad.
115
Object JavaDoc exArgs[] = { Constants._TAG_TRANSFORM,
116                              Constants._TAG_TRANSFORMS };
117
118          throw new TransformationException("xml.WrongContent", exArgs);
119       }
120    }
121
122    /**
123     * Adds the <code>Transform</code> with the specified <code>Transform algorithm URI</code>
124     *
125     * @param transformURI the URI form of transform that indicates which transformation is applied to data
126     * @throws TransformationException
127     */

128    public void addTransform(String JavaDoc transformURI)
129            throws TransformationException {
130
131       try {
132          if (true)
133             if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Transforms.addTransform(" + transformURI + ")");
134
135          Transform transform = Transform.getInstance(this._doc, transformURI);
136
137          this.addTransform(transform);
138       } catch (InvalidTransformException ex) {
139          throw new TransformationException("empty", ex);
140       }
141    }
142
143    /**
144     * Adds the <code>Transform</code> with the specified <code>Transform algorithm URI</code>
145     *
146     * @param transformURI the URI form of transform that indicates which transformation is applied to data
147     * @param contextElement
148     * @throws TransformationException
149     * @see Transform#getInstance(Document doc, String algorithmURI, Element childElement)
150     */

151    public void addTransform(String JavaDoc transformURI, Element JavaDoc contextElement)
152            throws TransformationException {
153
154       try {
155          if (true)
156             if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Transforms.addTransform(" + transformURI + ")");
157
158          Transform transform = Transform.getInstance(this._doc, transformURI,
159                                                      contextElement);
160
161          this.addTransform(transform);
162       } catch (InvalidTransformException ex) {
163          throw new TransformationException("empty", ex);
164       }
165    }
166
167    /**
168     * Adds the <code>Transform</code> with the specified <code>Transform algorithm URI</code>
169     *
170     * @param transformURI the URI form of transform that indicates which transformation is applied to data
171     * @param contextNodes
172     * @throws TransformationException
173     * @see Transform#getInstance(Document doc, String algorithmURI, NodeList contextNodes)
174     */

175    public void addTransform(String JavaDoc transformURI, NodeList JavaDoc contextNodes)
176            throws TransformationException {
177
178       try {
179          Transform transform = Transform.getInstance(this._doc, transformURI,
180                                                      contextNodes);
181
182          this.addTransform(transform);
183       } catch (InvalidTransformException ex) {
184          throw new TransformationException("empty", ex);
185       }
186    }
187
188    /**
189     * Adds a user-provided Transform step.
190     *
191     * @param transform {@link Transform} object
192     */

193    private void addTransform(Transform transform) {
194       if (true)
195         if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Transforms.addTransform(" + transform.getURI() + ")");
196
197       Element JavaDoc transformElement = transform.getElement();
198
199       this._constructionElement.appendChild(transformElement);
200       XMLUtils.addReturnToElement(this._constructionElement);
201    }
202
203    /**
204     * Applies all included <code>Transform</code>s to xmlSignatureInput and returns the result of these transformations.
205     *
206     * @param xmlSignatureInput the input for the <code>Transform</code>s
207     * @return the result of the <code>Transforms</code>
208     * @throws TransformationException
209     */

210    public XMLSignatureInput performTransforms(
211            XMLSignatureInput xmlSignatureInput) throws TransformationException {
212          return performTransforms(xmlSignatureInput,null);
213    }
214    
215    /**
216     * Applies all included <code>Transform</code>s to xmlSignatureInput and returns the result of these transformations.
217     *
218     * @param xmlSignatureInput the input for the <code>Transform</code>s
219     * @param os where to output the last transformation.
220     * @return the result of the <code>Transforms</code>
221     * @throws TransformationException
222     */

223     public XMLSignatureInput performTransforms(
224             XMLSignatureInput xmlSignatureInput,OutputStream JavaDoc os) throws TransformationException {
225
226       try {
227         int last=this.getLength()-1;
228          for (int i = 0; i < last; i++) {
229             Transform t = this.item(i);
230             if (true) {
231                 if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "Preform the (" + i + ")th " + t.getURI() + " transform");
232             }
233             xmlSignatureInput = t.performTransform(xmlSignatureInput);
234          }
235          if (last>=0) {
236             Transform t = this.item(last);
237             xmlSignatureInput = t.performTransform(xmlSignatureInput, os);
238          }
239
240
241          return xmlSignatureInput;
242       } catch (IOException JavaDoc ex) {
243          throw new TransformationException("empty", ex);
244       // } catch (ParserConfigurationException ex) { throw new TransformationException("empty", ex);
245
// } catch (SAXException ex) { throw new TransformationException("empty", ex);
246
} catch (CanonicalizationException ex) {
247          throw new TransformationException("empty", ex);
248       } catch (InvalidCanonicalizerException ex) {
249          throw new TransformationException("empty", ex);
250       }
251    }
252
253    /**
254     * Return the nonnegative number of transformations.
255     *
256     * @return the number of transformations
257     */

258    public int getLength()
259    {
260         /*Element nscontext = XMLUtils.createDSctx(this._doc, "ds",
261                                                   Constants.SignatureSpecNS);
262          NodeList transformElems =
263             XPathAPI.selectNodeList(this._constructionElement,
264                                     "./ds:Transform", nscontext);
265          return transformElems.getLength();*/

266        if (transforms==null) {
267         transforms=XMLUtils.selectDsNodes(this._constructionElement.getFirstChild(),
268            "Transform");
269        }
270        return transforms.length;
271   }
272
273    /**
274     * Return the <it>i</it><sup>th</sup> <code>{@link Transform}</code>.
275     * Valid <code>i</code> values are 0 to <code>{@link #getLength}-1</code>.
276     *
277     * @param i index of {@link Transform} to return
278     * @return the <it>i</it><sup>th</sup> transforms
279     * @throws TransformationException
280     */

281    public Transform item(int i) throws TransformationException {
282     
283         try {
284             if (transforms==null) {
285                 transforms=XMLUtils.selectDsNodes(this._constructionElement.getFirstChild(),
286                 "Transform");
287             }
288             return new Transform(transforms[i], this._baseURI);
289         } catch (XMLSecurityException ex) {
290             throw new TransformationException("empty", ex);
291         }
292    }
293
294    /** @inheritDoc */
295    public String JavaDoc getBaseLocalName() {
296       return Constants._TAG_TRANSFORMS;
297    }
298 }
299
Popular Tags