KickJava   Java API By Example, From Geeks To Geeks.

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


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.dtm.DTM;
24 import com.sun.org.apache.xml.internal.security.utils.I18n;
25 import com.sun.org.apache.xml.internal.security.utils.XMLUtils;
26 import com.sun.org.apache.xpath.internal.NodeSetDTM;
27 import com.sun.org.apache.xpath.internal.XPathContext;
28 import com.sun.org.apache.xpath.internal.functions.Function;
29 import com.sun.org.apache.xpath.internal.objects.XNodeSet;
30 import com.sun.org.apache.xpath.internal.objects.XObject;
31 import com.sun.org.apache.xpath.internal.res.XPATHErrorResources;
32 import org.w3c.dom.Document JavaDoc;
33 import org.w3c.dom.Node JavaDoc;
34
35
36 /**
37  * The 'here()' function returns a node-set containing the attribute or
38  * processing instruction node or the parent element of the text node
39  * that directly bears the XPath expression. This expression results
40  * in an error if the containing XPath expression does not appear in the
41  * same XML document against which the XPath expression is being evaluated.
42  *
43  * Mainpart is stolen from FuncId.java
44  *
45  * This does crash under Xalan2.2.D7 and works under Xalan2.2.D9
46  *
47  * To get this baby to work, a special trick has to be used. The function needs
48  * access to the Node where the XPath expression has been defined. This is done
49  * by constructing a {@link FuncHere} which has this Node as 'owner'.
50  *
51  * @see "http://www.w3.org/Signature/Drafts/xmldsig-core/Overview.html#function-here"
52  */

53 public class FuncHere extends Function {
54
55    /**
56      *
57      */

58     private static final long serialVersionUID = 1L;
59
60    /**
61     * The here function returns a node-set containing the attribute or
62     * processing instruction node or the parent element of the text node
63     * that directly bears the XPath expression. This expression results
64     * in an error if the containing XPath expression does not appear in the
65     * same XML document against which the XPath expression is being evaluated.
66     *
67     * @param xctxt
68     * @return the xobject
69     * @throws javax.xml.transform.TransformerException
70     */

71    public XObject execute(XPathContext xctxt)
72            throws javax.xml.transform.TransformerException JavaDoc {
73
74       Node JavaDoc xpathOwnerNode = (Node JavaDoc) xctxt.getOwnerObject();
75
76       if (xpathOwnerNode == null) {
77          return null;
78       }
79
80       int xpathOwnerNodeDTM = xctxt.getDTMHandleFromNode(xpathOwnerNode);
81
82       int currentNode = xctxt.getCurrentNode();
83       DTM dtm = xctxt.getDTM(currentNode);
84       int docContext = dtm.getDocument();
85
86       if (DTM.NULL == docContext) {
87          error(xctxt, XPATHErrorResources.ER_CONTEXT_HAS_NO_OWNERDOC, null);
88       }
89
90       {
91
92          // check whether currentNode and the node containing the XPath expression
93
// are in the same document
94
Document JavaDoc currentDoc =
95             XMLUtils.getOwnerDocument(dtm.getNode(currentNode));
96          Document JavaDoc xpathOwnerDoc = XMLUtils.getOwnerDocument(xpathOwnerNode);
97
98          if (currentDoc != xpathOwnerDoc) {
99             throw new TransformerException JavaDoc(I18n
100                .translate("xpath.funcHere.documentsDiffer"));
101          }
102       }
103
104       XNodeSet nodes = new XNodeSet(xctxt.getDTMManager());
105       NodeSetDTM nodeSet = nodes.mutableNodeset();
106
107       {
108          int hereNode = DTM.NULL;
109
110          switch (dtm.getNodeType(xpathOwnerNodeDTM)) {
111
112          case Node.ATTRIBUTE_NODE : {
113             // returns a node-set containing the attribute
114
hereNode = xpathOwnerNodeDTM;
115
116             nodeSet.addNode(hereNode);
117
118             break;
119          }
120          case Node.PROCESSING_INSTRUCTION_NODE : {
121             // returns a node-set containing the processing instruction node
122
hereNode = xpathOwnerNodeDTM;
123
124             nodeSet.addNode(hereNode);
125
126             break;
127          }
128          case Node.TEXT_NODE : {
129             // returns a node-set containing the parent element of the
130
// text node that directly bears the XPath expression
131
hereNode = dtm.getParent(xpathOwnerNodeDTM);
132
133             nodeSet.addNode(hereNode);
134
135             break;
136          }
137          default :
138             break;
139          }
140       }
141
142       /** $todo$ Do I have to do this detach() call? */
143       nodeSet.detach();
144
145       return nodes;
146    }
147
148    /**
149     * No arguments to process, so this does nothing.
150     * @param vars
151     * @param globalsSize
152     */

153    public void fixupVariables(java.util.Vector JavaDoc vars, int globalsSize) {
154
155       // do nothing
156
}
157 }
158
Popular Tags