KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.IOException JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24
25 import javax.xml.parsers.ParserConfigurationException JavaDoc;
26 import javax.xml.transform.TransformerException JavaDoc;
27
28 import com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException;
29 import com.sun.org.apache.xml.internal.security.c14n.InvalidCanonicalizerException;
30 import com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException;
31 import com.sun.org.apache.xml.internal.security.signature.NodeFilter;
32 import com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput;
33 import com.sun.org.apache.xml.internal.security.transforms.TransformSpi;
34 import com.sun.org.apache.xml.internal.security.transforms.TransformationException;
35 import com.sun.org.apache.xml.internal.security.transforms.Transforms;
36 import com.sun.org.apache.xml.internal.security.transforms.params.XPath2FilterContainer;
37 import com.sun.org.apache.xml.internal.security.utils.CachedXPathAPIHolder;
38 import com.sun.org.apache.xml.internal.security.utils.CachedXPathFuncHereAPI;
39 import com.sun.org.apache.xml.internal.security.utils.XMLUtils;
40 import org.w3c.dom.DOMException JavaDoc;
41 import org.w3c.dom.Document JavaDoc;
42 import org.w3c.dom.Element JavaDoc;
43 import org.w3c.dom.Node JavaDoc;
44 import org.w3c.dom.NodeList JavaDoc;
45 import org.xml.sax.SAXException JavaDoc;
46
47 /**
48  * Implements the <I>XML Signature XPath Filter v2.0</I>
49  *
50  * @author $Author: mullan $
51  * @see <A HREF="http://www.w3.org/TR/xmldsig-filter2/">XPath Filter v2.0 (TR)</A>
52  * @see <a HREF="http://www.w3.org/Signature/Drafts/xmldsig-xfilter2/">XPath Filter v2.0 (editors copy)</a>
53  */

54 public class TransformXPath2Filter extends TransformSpi {
55
56    /** {@link java.util.logging} logging facility */
57 // static java.util.logging.Logger log =
58
// java.util.logging.Logger.getLogger(
59
// TransformXPath2Filter.class.getName());
60

61    /** Field implementedTransformURI */
62    public static final String JavaDoc implementedTransformURI =
63       Transforms.TRANSFORM_XPATH2FILTER;
64    //J-
65
// contains the type of the filter
66

67    // contains the node set
68

69    /**
70     * Method engineGetURI
71     *
72     * @inheritDoc
73     */

74    protected String JavaDoc engineGetURI() {
75       return implementedTransformURI;
76    }
77
78
79
80    /**
81     * Method enginePerformTransform
82     * @inheritDoc
83     * @param input
84     *
85     * @throws TransformationException
86     */

87    protected XMLSignatureInput enginePerformTransform(XMLSignatureInput input)
88            throws TransformationException {
89       CachedXPathAPIHolder.setDoc(this._transformObject.getElement().getOwnerDocument());
90       try {
91           List JavaDoc unionNodes=new ArrayList JavaDoc();
92            List JavaDoc substractNodes=new ArrayList JavaDoc();
93            List JavaDoc intersectNodes=new ArrayList JavaDoc();
94
95          CachedXPathFuncHereAPI xPathFuncHereAPI =
96             new CachedXPathFuncHereAPI(CachedXPathAPIHolder.getCachedXPathAPI());
97
98          
99          Element JavaDoc []xpathElements =XMLUtils.selectNodes(
100                 this._transformObject.getElement().getFirstChild(),
101                    XPath2FilterContainer.XPathFilter2NS,
102                    XPath2FilterContainer._TAG_XPATH2);
103          int noOfSteps = xpathElements.length;
104
105
106          if (noOfSteps == 0) {
107             Object JavaDoc exArgs[] = { Transforms.TRANSFORM_XPATH2FILTER, "XPath" };
108
109             throw new TransformationException("xml.WrongContent", exArgs);
110          }
111
112          Document JavaDoc inputDoc = null;
113      if (input.getSubNode() != null) {
114             inputDoc = XMLUtils.getOwnerDocument(input.getSubNode());
115      } else {
116             inputDoc = XMLUtils.getOwnerDocument(input.getNodeSet());
117      }
118
119          for (int i = 0; i < noOfSteps; i++) {
120             Element JavaDoc xpathElement =XMLUtils.selectNode(
121                this._transformObject.getElement().getFirstChild(),
122                   XPath2FilterContainer.XPathFilter2NS,
123                   XPath2FilterContainer._TAG_XPATH2,i);
124             XPath2FilterContainer xpathContainer =
125                XPath2FilterContainer.newInstance(xpathElement,
126                                                    input.getSourceURI());
127            
128
129             NodeList JavaDoc subtreeRoots = xPathFuncHereAPI.selectNodeList(inputDoc,
130                                        xpathContainer.getXPathFilterTextNode(),
131                                        CachedXPathFuncHereAPI.getStrFromNode(xpathContainer.getXPathFilterTextNode()),
132                                        xpathContainer.getElement());
133             if (xpathContainer.isIntersect()) {
134                 intersectNodes.add(subtreeRoots);
135              } else if (xpathContainer.isSubtract()) {
136                  substractNodes.add(subtreeRoots);
137              } else if (xpathContainer.isUnion()) {
138                 unionNodes.add(subtreeRoots);
139              }
140          }
141
142          input.setNeedsToBeExpanded(true);
143          
144          input.addNodeFilter(new XPath2NodeFilter(unionNodes,substractNodes,intersectNodes));
145          input.setNodeSet(true);
146          return input;
147       } catch (TransformerException JavaDoc ex) {
148          throw new TransformationException("empty", ex);
149       } catch (DOMException JavaDoc ex) {
150          throw new TransformationException("empty", ex);
151       } catch (CanonicalizationException ex) {
152          throw new TransformationException("empty", ex);
153       } catch (InvalidCanonicalizerException ex) {
154          throw new TransformationException("empty", ex);
155       } catch (XMLSecurityException ex) {
156          throw new TransformationException("empty", ex);
157       } catch (SAXException JavaDoc ex) {
158          throw new TransformationException("empty", ex);
159       } catch (IOException JavaDoc ex) {
160          throw new TransformationException("empty", ex);
161       } catch (ParserConfigurationException JavaDoc ex) {
162          throw new TransformationException("empty", ex);
163       }
164    }
165 }
166
167 class XPath2NodeFilter implements NodeFilter {
168     XPath2NodeFilter(List JavaDoc unionNodes, List JavaDoc substractNodes,
169             List JavaDoc intersectNodes) {
170         this.unionNodes=unionNodes;
171         this.substractNodes=substractNodes;
172         this.intersectNodes=intersectNodes;
173     }
174     List JavaDoc unionNodes=new ArrayList JavaDoc();
175     List JavaDoc substractNodes=new ArrayList JavaDoc();
176     List JavaDoc intersectNodes=new ArrayList JavaDoc();
177
178
179    /**
180     * @see com.sun.org.apache.xml.internal.security.signature.NodeFilter#isNodeInclude(org.w3c.dom.Node)
181     */

182    public boolean isNodeInclude(Node JavaDoc currentNode) {
183        boolean notIncluded=false;
184        if (rooted(currentNode,substractNodes)) {
185            notIncluded=true;
186        } else if (!rooted(currentNode,intersectNodes)) {
187            notIncluded=true;
188        }
189        if (notIncluded && rooted(currentNode,unionNodes)) {
190            notIncluded=false;
191        }
192
193       return !notIncluded;
194
195    }
196
197    /**
198     * Method rooted
199     * @param currentNode
200     * @param nodeList
201     *
202     * @return if rooted bye the rootnodes
203     */

204    boolean rooted(Node JavaDoc currentNode, List JavaDoc nodeList ) {
205        for (int j=0;j<nodeList.size();j++) {
206            NodeList JavaDoc rootNodes=(NodeList JavaDoc) nodeList.get(j);
207       int length = rootNodes.getLength();
208
209       for (int i = 0; i < length; i++) {
210          Node JavaDoc rootNode = rootNodes.item(i);
211
212          if (XMLUtils.isDescendantOrSelf(rootNode,currentNode)) {
213             return true;
214          }
215       }
216    
217        }
218        return false;
219    }
220 }
221
Popular Tags