1 17 package com.sun.org.apache.xml.internal.security.transforms.implementations; 18 19 20 21 import javax.xml.transform.TransformerException ; 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 ; 36 import org.w3c.dom.Element ; 37 import org.w3c.dom.Node ; 38 39 40 50 public class TransformXPath extends TransformSpi { 51 52 53 static java.util.logging.Logger log = 54 java.util.logging.Logger.getLogger(TransformXPath.class.getName()); 55 56 57 public static final String implementedTransformURI = 58 Transforms.TRANSFORM_XPATH; 59 60 61 66 protected String engineGetURI() { 67 return implementedTransformURI; 68 } 69 70 77 protected XMLSignatureInput enginePerformTransform(XMLSignatureInput input) 78 throws TransformationException { 79 80 try { 81 82 93 CachedXPathAPIHolder.setDoc(this._transformObject.getElement().getOwnerDocument()); 94 95 96 97 Element xpathElement =XMLUtils.selectDsNode( 98 this._transformObject.getElement().getFirstChild(), 99 Constants._TAG_XPATH,0); 100 101 if (xpathElement == null) { 102 Object exArgs[] = { "ds:XPath", "Transform" }; 103 104 throw new TransformationException("xml.WrongContent", exArgs); 105 } 106 Node xpathnode = xpathElement.getChildNodes().item(0); 107 String str=CachedXPathFuncHereAPI.getStrFromNode(xpathnode); 108 input.setNeedsToBeExpanded(needsCircunvent(str)); 109 if (xpathnode == null) { 110 throw new DOMException (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 ex) { 119 throw new TransformationException("empty", ex); 120 } 121 } 122 123 127 private boolean needsCircunvent(String str) { 128 return true; 129 131 } 132 class XPathNodeFilter implements NodeFilter { 133 PrefixResolverDefault prefixResolver; 134 CachedXPathFuncHereAPI xPathFuncHereAPI = 135 new CachedXPathFuncHereAPI(CachedXPathAPIHolder.getCachedXPathAPI()); 136 ; 137 Node xpathnode; 138 String str; 139 XPathNodeFilter(Element xpathElement, 140 Node xpathnode, String str) { 141 this.xpathnode=xpathnode; 142 this.str=str; 143 prefixResolver =new PrefixResolverDefault(xpathElement); 144 } 145 146 147 150 public boolean isNodeInclude(Node currentNode) { 151 XObject includeInResult; 152 try { 153 includeInResult = xPathFuncHereAPI.eval(currentNode, 154 xpathnode, str,prefixResolver); 155 return includeInResult.bool(); 156 } catch (TransformerException e) { 157 Object [] eArgs = {currentNode}; 158 throw new XMLSecurityRuntimeException("signature.Transform.node", eArgs, e); 159 } 160 catch (Exception e) { 161 Object [] eArgs = {currentNode, new Short (currentNode.getNodeType())}; 162 throw new XMLSecurityRuntimeException("signature.Transform.nodeAndType",eArgs, e); 163 } 164 } 165 } 166 } 167 | Popular Tags |