KickJava   Java API By Example, From Geeks To Geeks.

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


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.implementations;
18
19
20
21 import javax.xml.transform.TransformerException JavaDoc;
22
23 import com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityRuntimeException;
24 import com.sun.org.apache.xml.internal.security.signature.NodeFilter;
25 import com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput;
26 import com.sun.org.apache.xml.internal.security.transforms.TransformSpi;
27 import com.sun.org.apache.xml.internal.security.transforms.TransformationException;
28 import com.sun.org.apache.xml.internal.security.transforms.Transforms;
29 import com.sun.org.apache.xml.internal.security.utils.CachedXPathAPIHolder;
30 import com.sun.org.apache.xml.internal.security.utils.CachedXPathFuncHereAPI;
31 import com.sun.org.apache.xml.internal.security.utils.Constants;
32 import com.sun.org.apache.xml.internal.security.utils.XMLUtils;
33 import com.sun.org.apache.xml.internal.utils.PrefixResolverDefault;
34 import com.sun.org.apache.xpath.internal.objects.XObject;
35 import org.w3c.dom.DOMException JavaDoc;
36 import org.w3c.dom.Element JavaDoc;
37 import org.w3c.dom.Node JavaDoc;
38
39
40 /**
41  * Class TransformXPath
42  *
43  * Implements the <CODE>http://www.w3.org/TR/1999/REC-xpath-19991116</CODE>
44  * transform.
45  *
46  * @author Christian Geuer-Pollmann
47  * @see <a HREF="http://www.w3.org/TR/1999/REC-xpath-19991116">XPath</a>
48  *
49  */

50 public class TransformXPath extends TransformSpi {
51
52    /** {@link java.util.logging} logging facility */
53     static java.util.logging.Logger JavaDoc log =
54         java.util.logging.Logger.getLogger(TransformXPath.class.getName());
55
56    /** Field implementedTransformURI */
57    public static final String JavaDoc implementedTransformURI =
58       Transforms.TRANSFORM_XPATH;
59
60
61    /**
62     * Method engineGetURI
63     *
64     * @inheritDoc
65     */

66    protected String JavaDoc engineGetURI() {
67       return implementedTransformURI;
68    }
69
70    /**
71     * Method enginePerformTransform
72     * @inheritDoc
73     * @param input
74     *
75     * @throws TransformationException
76     */

77    protected XMLSignatureInput enginePerformTransform(XMLSignatureInput input)
78            throws TransformationException {
79
80       try {
81
82          /**
83           * If the actual input is an octet stream, then the application MUST
84           * convert the octet stream to an XPath node-set suitable for use by
85           * Canonical XML with Comments. (A subsequent application of the
86           * REQUIRED Canonical XML algorithm would strip away these comments.)
87           *
88           * ...
89           *
90           * The evaluation of this expression includes all of the document's nodes
91           * (including comments) in the node-set representing the octet stream.
92           */

93           CachedXPathAPIHolder.setDoc(this._transformObject.getElement().getOwnerDocument());
94          
95          
96
97          Element JavaDoc xpathElement =XMLUtils.selectDsNode(
98             this._transformObject.getElement().getFirstChild(),
99                Constants._TAG_XPATH,0);
100
101          if (xpathElement == null) {
102             Object JavaDoc exArgs[] = { "ds:XPath", "Transform" };
103
104             throw new TransformationException("xml.WrongContent", exArgs);
105          }
106          Node JavaDoc xpathnode = xpathElement.getChildNodes().item(0);
107          String JavaDoc str=CachedXPathFuncHereAPI.getStrFromNode(xpathnode);
108          input.setNeedsToBeExpanded(needsCircunvent(str));
109          if (xpathnode == null) {
110             throw new DOMException JavaDoc(DOMException.HIERARCHY_REQUEST_ERR,
111                                    "Text must be in ds:Xpath");
112          }
113
114
115          input.addNodeFilter(new XPathNodeFilter( xpathElement, xpathnode, str));
116          input.setNodeSet(true);
117          return input;
118       } catch (DOMException JavaDoc ex) {
119          throw new TransformationException("empty", ex);
120       }
121    }
122
123    /**
124     * @param str
125     * @return true if needs to be circunvent for bug.
126     */

127     private boolean needsCircunvent(String JavaDoc str) {
128         return true;
129         //return str.contains("namespace");
130

131     }
132     class XPathNodeFilter implements NodeFilter {
133          PrefixResolverDefault prefixResolver;
134          CachedXPathFuncHereAPI xPathFuncHereAPI =
135              new CachedXPathFuncHereAPI(CachedXPathAPIHolder.getCachedXPathAPI());
136           ;
137         Node JavaDoc xpathnode;
138         String JavaDoc str;
139         XPathNodeFilter(Element JavaDoc xpathElement,
140                 Node JavaDoc xpathnode, String JavaDoc str) {
141             this.xpathnode=xpathnode;
142             this.str=str;
143             prefixResolver =new PrefixResolverDefault(xpathElement);
144         }
145             
146
147         /**
148          * @see com.sun.org.apache.xml.internal.security.signature.NodeFilter#isNodeInclude(org.w3c.dom.Node)
149          */

150         public boolean isNodeInclude(Node JavaDoc currentNode) {
151             XObject includeInResult;
152             try {
153                 includeInResult = xPathFuncHereAPI.eval(currentNode,
154                         xpathnode, str,prefixResolver);
155                 return includeInResult.bool();
156             } catch (TransformerException JavaDoc e) {
157                 Object JavaDoc[] eArgs = {currentNode};
158                 throw new XMLSecurityRuntimeException("signature.Transform.node", eArgs, e);
159             }
160             catch (Exception JavaDoc e) {
161                 Object JavaDoc[] eArgs = {currentNode, new Short JavaDoc(currentNode.getNodeType())};
162                 throw new XMLSecurityRuntimeException("signature.Transform.nodeAndType",eArgs, e);
163             }
164         }
165     }
166 }
167
Popular Tags