1 /* 2 * reserved comment block 3 * DO NOT REMOVE OR ALTER! 4 */ 5 /* 6 * Copyright 1999-2004 The Apache Software Foundation. 7 * 8 * Licensed under the Apache License, Version 2.0 (the "License"); 9 * you may not use this file except in compliance with the License. 10 * You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, software 15 * distributed under the License is distributed on an "AS IS" BASIS, 16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 * See the License for the specific language governing permissions and 18 * limitations under the License. 19 * 20 */ 21 package com.sun.org.apache.xml.internal.security.transforms.implementations; 22 23 24 25 import javax.xml.transform.TransformerException; 26 27 import com.sun.org.apache.xml.internal.dtm.DTM; 28 import com.sun.org.apache.xml.internal.security.utils.I18n; 29 import com.sun.org.apache.xml.internal.security.utils.XMLUtils; 30 import com.sun.org.apache.xpath.internal.NodeSetDTM; 31 import com.sun.org.apache.xpath.internal.XPathContext; 32 import com.sun.org.apache.xpath.internal.functions.Function; 33 import com.sun.org.apache.xpath.internal.objects.XNodeSet; 34 import com.sun.org.apache.xpath.internal.objects.XObject; 35 import com.sun.org.apache.xpath.internal.res.XPATHErrorResources; 36 import org.w3c.dom.Document; 37 import org.w3c.dom.Node; 38 39 40 /** 41 * The 'here()' function returns a node-set containing the attribute or 42 * processing instruction node or the parent element of the text node 43 * that directly bears the XPath expression. This expression results 44 * in an error if the containing XPath expression does not appear in the 45 * same XML document against which the XPath expression is being evaluated. 46 * 47 * Mainpart is stolen from FuncId.java 48 * 49 * This does crash under Xalan2.2.D7 and works under Xalan2.2.D9 50 * 51 * To get this baby to work, a special trick has to be used. The function needs 52 * access to the Node where the XPath expression has been defined. This is done 53 * by constructing a {@link FuncHere} which has this Node as 'owner'. 54 * 55 * @see "http://www.w3.org/Signature/Drafts/xmldsig-core/Overview.html#function-here" 56 */ 57 public class FuncHere extends Function { 58 59 /** 60 * 61 */ 62 private static final long serialVersionUID = 1L; 63 64 /** 65 * The here function returns a node-set containing the attribute or 66 * processing instruction node or the parent element of the text node 67 * that directly bears the XPath expression. This expression results 68 * in an error if the containing XPath expression does not appear in the 69 * same XML document against which the XPath expression is being evaluated. 70 * 71 * @param xctxt 72 * @return the xobject 73 * @throws javax.xml.transform.TransformerException 74 */ 75 public XObject execute(XPathContext xctxt) 76 throws javax.xml.transform.TransformerException { 77 78 Node xpathOwnerNode = (Node) xctxt.getOwnerObject(); 79 80 if (xpathOwnerNode == null) { 81 return null; 82 } 83 84 int xpathOwnerNodeDTM = xctxt.getDTMHandleFromNode(xpathOwnerNode); 85 86 int currentNode = xctxt.getCurrentNode(); 87 DTM dtm = xctxt.getDTM(currentNode); 88 int docContext = dtm.getDocument(); 89 90 if (DTM.NULL == docContext) { 91 error(xctxt, XPATHErrorResources.ER_CONTEXT_HAS_NO_OWNERDOC, null); 92 } 93 94 { 95 96 // check whether currentNode and the node containing the XPath expression 97 // are in the same document 98 Document currentDoc = 99 XMLUtils.getOwnerDocument(dtm.getNode(currentNode)); 100 Document xpathOwnerDoc = XMLUtils.getOwnerDocument(xpathOwnerNode); 101 102 if (currentDoc != xpathOwnerDoc) { 103 throw new TransformerException(I18n 104 .translate("xpath.funcHere.documentsDiffer")); 105 } 106 } 107 108 XNodeSet nodes = new XNodeSet(xctxt.getDTMManager()); 109 NodeSetDTM nodeSet = nodes.mutableNodeset(); 110 111 { 112 int hereNode = DTM.NULL; 113 114 switch (dtm.getNodeType(xpathOwnerNodeDTM)) { 115 116 case Node.ATTRIBUTE_NODE : { 117 // returns a node-set containing the attribute 118 hereNode = xpathOwnerNodeDTM; 119 120 nodeSet.addNode(hereNode); 121 122 break; 123 } 124 case Node.PROCESSING_INSTRUCTION_NODE : { 125 // returns a node-set containing the processing instruction node 126 hereNode = xpathOwnerNodeDTM; 127 128 nodeSet.addNode(hereNode); 129 130 break; 131 } 132 case Node.TEXT_NODE : { 133 // returns a node-set containing the parent element of the 134 // text node that directly bears the XPath expression 135 hereNode = dtm.getParent(xpathOwnerNodeDTM); 136 137 nodeSet.addNode(hereNode); 138 139 break; 140 } 141 default : 142 break; 143 } 144 } 145 146 /** $todo$ Do I have to do this detach() call? */ 147 nodeSet.detach(); 148 149 return nodes; 150 } 151 152 /** 153 * No arguments to process, so this does nothing. 154 * @param vars 155 * @param globalsSize 156 */ 157 @SuppressWarnings("rawtypes") 158 public void fixupVariables(java.util.Vector vars, int globalsSize) { 159 160 // do nothing 161 } 162 } | 1 /* 2 * reserved comment block 3 * DO NOT REMOVE OR ALTER! 4 */ 5 /** 6 * Licensed to the Apache Software Foundation (ASF) under one 7 * or more contributor license agreements. See the NOTICE file 8 * distributed with this work for additional information 9 * regarding copyright ownership. The ASF licenses this file 10 * to you under the Apache License, Version 2.0 (the 11 * "License"); you may not use this file except in compliance 12 * with the License. You may obtain a copy of the License at 13 * 14 * http://www.apache.org/licenses/LICENSE-2.0 15 * 16 * Unless required by applicable law or agreed to in writing, 17 * software distributed under the License is distributed on an 18 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 19 * KIND, either express or implied. See the License for the 20 * specific language governing permissions and limitations 21 * under the License. 22 */ 23 package com.sun.org.apache.xml.internal.security.transforms.implementations; 24 25 import javax.xml.transform.TransformerException; 26 27 import com.sun.org.apache.xml.internal.dtm.DTM; 28 import com.sun.org.apache.xml.internal.security.utils.I18n; 29 import com.sun.org.apache.xml.internal.security.utils.XMLUtils; 30 import com.sun.org.apache.xpath.internal.NodeSetDTM; 31 import com.sun.org.apache.xpath.internal.XPathContext; 32 import com.sun.org.apache.xpath.internal.functions.Function; 33 import com.sun.org.apache.xpath.internal.objects.XNodeSet; 34 import com.sun.org.apache.xpath.internal.objects.XObject; 35 import com.sun.org.apache.xpath.internal.res.XPATHErrorResources; 36 import org.w3c.dom.Document; 37 import org.w3c.dom.Node; 38 39 /** 40 * The 'here()' function returns a node-set containing the attribute or 41 * processing instruction node or the parent element of the text node 42 * that directly bears the XPath expression. This expression results 43 * in an error if the containing XPath expression does not appear in the 44 * same XML document against which the XPath expression is being evaluated. 45 * 46 * Mainpart is stolen from FuncId.java 47 * 48 * This does crash under Xalan2.2.D7 and works under Xalan2.2.D9 49 * 50 * To get this baby to work, a special trick has to be used. The function needs 51 * access to the Node where the XPath expression has been defined. This is done 52 * by constructing a {@link FuncHere} which has this Node as 'owner'. 53 * 54 * @see "http://www.w3.org/Signature/Drafts/xmldsig-core/Overview.html#function-here" 55 */ 56 public class FuncHere extends Function { 57 58 /** 59 * 60 */ 61 private static final long serialVersionUID = 1L; 62 63 /** 64 * The here function returns a node-set containing the attribute or 65 * processing instruction node or the parent element of the text node 66 * that directly bears the XPath expression. This expression results 67 * in an error if the containing XPath expression does not appear in the 68 * same XML document against which the XPath expression is being evaluated. 69 * 70 * @param xctxt 71 * @return the xobject 72 * @throws javax.xml.transform.TransformerException 73 */ 74 @Override 75 public XObject execute(XPathContext xctxt) 76 throws javax.xml.transform.TransformerException { 77 78 Node xpathOwnerNode = (Node) xctxt.getOwnerObject(); 79 80 if (xpathOwnerNode == null) { 81 return null; 82 } 83 84 int xpathOwnerNodeDTM = xctxt.getDTMHandleFromNode(xpathOwnerNode); 85 86 int currentNode = xctxt.getCurrentNode(); 87 DTM dtm = xctxt.getDTM(currentNode); 88 int docContext = dtm.getDocument(); 89 90 if (DTM.NULL == docContext) { 91 error(xctxt, XPATHErrorResources.ER_CONTEXT_HAS_NO_OWNERDOC, null); 92 } 93 94 { 95 // check whether currentNode and the node containing the XPath expression 96 // are in the same document 97 Document currentDoc = 98 XMLUtils.getOwnerDocument(dtm.getNode(currentNode)); 99 Document xpathOwnerDoc = XMLUtils.getOwnerDocument(xpathOwnerNode); 100 101 if (currentDoc != xpathOwnerDoc) { 102 throw new TransformerException(I18n.translate("xpath.funcHere.documentsDiffer")); 103 } 104 } 105 106 XNodeSet nodes = new XNodeSet(xctxt.getDTMManager()); 107 NodeSetDTM nodeSet = nodes.mutableNodeset(); 108 109 { 110 int hereNode = DTM.NULL; 111 112 switch (dtm.getNodeType(xpathOwnerNodeDTM)) { 113 114 case Node.ATTRIBUTE_NODE : 115 case Node.PROCESSING_INSTRUCTION_NODE : { 116 // returns a node-set containing the attribute / processing instruction node 117 hereNode = xpathOwnerNodeDTM; 118 119 nodeSet.addNode(hereNode); 120 121 break; 122 } 123 case Node.TEXT_NODE : { 124 // returns a node-set containing the parent element of the 125 // text node that directly bears the XPath expression 126 hereNode = dtm.getParent(xpathOwnerNodeDTM); 127 128 nodeSet.addNode(hereNode); 129 130 break; 131 } 132 default : 133 break; 134 } 135 } 136 137 /** $todo$ Do I have to do this detach() call? */ 138 nodeSet.detach(); 139 140 return nodes; 141 } 142 143 /** 144 * No arguments to process, so this does nothing. 145 * @param vars 146 * @param globalsSize 147 */ 148 @SuppressWarnings("rawtypes") 149 public void fixupVariables(java.util.Vector vars, int globalsSize) { 150 // do nothing 151 } 152 } |