1 17 package com.sun.org.apache.xml.internal.security.transforms.implementations; 18 19 20 21 import java.io.IOException ; 22 import java.util.ArrayList ; 23 import java.util.List ; 24 25 import javax.xml.parsers.ParserConfigurationException ; 26 import javax.xml.transform.TransformerException ; 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 ; 41 import org.w3c.dom.Document ; 42 import org.w3c.dom.Element ; 43 import org.w3c.dom.Node ; 44 import org.w3c.dom.NodeList ; 45 import org.xml.sax.SAXException ; 46 47 54 public class TransformXPath2Filter extends TransformSpi { 55 56 57 61 62 public static final String implementedTransformURI = 63 Transforms.TRANSFORM_XPATH2FILTER; 64 67 69 74 protected String engineGetURI() { 75 return implementedTransformURI; 76 } 77 78 79 80 87 protected XMLSignatureInput enginePerformTransform(XMLSignatureInput input) 88 throws TransformationException { 89 CachedXPathAPIHolder.setDoc(this._transformObject.getElement().getOwnerDocument()); 90 try { 91 List unionNodes=new ArrayList (); 92 List substractNodes=new ArrayList (); 93 List intersectNodes=new ArrayList (); 94 95 CachedXPathFuncHereAPI xPathFuncHereAPI = 96 new CachedXPathFuncHereAPI(CachedXPathAPIHolder.getCachedXPathAPI()); 97 98 99 Element []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 exArgs[] = { Transforms.TRANSFORM_XPATH2FILTER, "XPath" }; 108 109 throw new TransformationException("xml.WrongContent", exArgs); 110 } 111 112 Document 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 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 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 ex) { 148 throw new TransformationException("empty", ex); 149 } catch (DOMException 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 ex) { 158 throw new TransformationException("empty", ex); 159 } catch (IOException ex) { 160 throw new TransformationException("empty", ex); 161 } catch (ParserConfigurationException ex) { 162 throw new TransformationException("empty", ex); 163 } 164 } 165 } 166 167 class XPath2NodeFilter implements NodeFilter { 168 XPath2NodeFilter(List unionNodes, List substractNodes, 169 List intersectNodes) { 170 this.unionNodes=unionNodes; 171 this.substractNodes=substractNodes; 172 this.intersectNodes=intersectNodes; 173 } 174 List unionNodes=new ArrayList (); 175 List substractNodes=new ArrayList (); 176 List intersectNodes=new ArrayList (); 177 178 179 182 public boolean isNodeInclude(Node 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 204 boolean rooted(Node currentNode, List nodeList ) { 205 for (int j=0;j<nodeList.size();j++) { 206 NodeList rootNodes=(NodeList ) nodeList.get(j); 207 int length = rootNodes.getLength(); 208 209 for (int i = 0; i < length; i++) { 210 Node 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 |