author | mullan |
Mon, 26 Sep 2011 17:20:45 -0700 | |
changeset 10694 | cf59e2badd14 |
parent 2 | 90ce3da70b43 |
child 18780 | f47b920867e7 |
permissions | -rw-r--r-- |
2 | 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 |
*/ |
|
10694
cf59e2badd14
7088502: Security libraries don't build with javac -Werror
mullan
parents:
2
diff
changeset
|
157 |
@SuppressWarnings("rawtypes") |
2 | 158 |
public void fixupVariables(java.util.Vector vars, int globalsSize) { |
159 |
||
160 |
// do nothing |
|
161 |
} |
|
162 |
} |