Skip to content

Commit a3c0ed2

Browse files
author
Yuri Nesterenko
committed
8087283: Add support for the XML Signature here() function to the JDK XPath implementation
Reviewed-by: andrew Backport-of: 22fad64529a890dd3ae8b07c7981d9a720cf8e96
1 parent 1b4f32d commit a3c0ed2

File tree

4 files changed

+156
-28
lines changed

4 files changed

+156
-28
lines changed

jaxp/src/com/sun/org/apache/xpath/internal/compiler/FunctionTable.java

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
/*
2-
* reserved comment block
3-
* DO NOT REMOVE OR ALTER!
2+
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
43
*/
5-
/*
6-
* Copyright 1999-2005 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
4+
/**
5+
* Licensed to the Apache Software Foundation (ASF) under one
6+
* or more contributor license agreements. See the NOTICE file
7+
* distributed with this work for additional information
8+
* regarding copyright ownership. The ASF licenses this file
9+
* to you under the Apache License, Version 2.0 (the
10+
* "License"); you may not use this file except in compliance
11+
* with the License. You may obtain a copy of the License at
1112
*
12-
* http://www.apache.org/licenses/LICENSE-2.0
13+
* http://www.apache.org/licenses/LICENSE-2.0
1314
*
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.
15+
* Unless required by applicable law or agreed to in writing,
16+
* software distributed under the License is distributed on an
17+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18+
* KIND, either express or implied. See the License for the
19+
* specific language governing permissions and limitations
20+
* under the License.
1921
*/
2022
/*
2123
* $Id: FunctionTable.java,v 1.3 2005/09/28 13:49:34 pvedula Exp $
@@ -135,6 +137,9 @@ public class FunctionTable
135137
/** The 'unparsed-entity-uri()' id (XSLT). */
136138
public static final int FUNC_UNPARSED_ENTITY_URI = 36;
137139

140+
/** The 'here()' id (XML Signature). */
141+
public static final int FUNC_HERE = 37;
142+
138143
// Proprietary
139144

140145
/** The 'document-location()' id (Proprietary). */
@@ -162,7 +167,7 @@ public class FunctionTable
162167
* Number of built in functions. Be sure to update this as
163168
* built-in functions are added.
164169
*/
165-
private static final int NUM_BUILT_IN_FUNCS = 37;
170+
private static final int NUM_BUILT_IN_FUNCS = 38;
166171

167172
/**
168173
* Number of built-in functions that may be added.
@@ -229,6 +234,8 @@ public class FunctionTable
229234
com.sun.org.apache.xpath.internal.functions.FuncDoclocation.class;
230235
m_functions[FUNC_UNPARSED_ENTITY_URI] =
231236
com.sun.org.apache.xpath.internal.functions.FuncUnparsedEntityURI.class;
237+
m_functions[FUNC_HERE] =
238+
com.sun.org.apache.xpath.internal.functions.FuncHere.class;
232239
}
233240

234241
static{
@@ -302,6 +309,8 @@ public class FunctionTable
302309
new Integer(FunctionTable.FUNC_UNPARSED_ENTITY_URI));
303310
m_functionID.put(Keywords.FUNC_DOCLOCATION_STRING,
304311
new Integer(FunctionTable.FUNC_DOCLOCATION));
312+
m_functionID.put(Keywords.FUNC_HERE_STRING,
313+
new Integer(FunctionTable.FUNC_HERE));
305314
}
306315

307316
public FunctionTable(){

jaxp/src/com/sun/org/apache/xpath/internal/compiler/Keywords.java

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
/*
22
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
33
*/
4-
/*
5-
* Licensed to the Apache Software Foundation (ASF) under one or more
6-
* contributor license agreements. See the NOTICE file distributed with
7-
* this work for additional information regarding copyright ownership.
8-
* The ASF licenses this file to You under the Apache License, Version 2.0
9-
* (the "License"); you may not use this file except in compliance with
10-
* the License. You may obtain a copy of the License at
4+
/**
5+
* Licensed to the Apache Software Foundation (ASF) under one
6+
* or more contributor license agreements. See the NOTICE file
7+
* distributed with this work for additional information
8+
* regarding copyright ownership. The ASF licenses this file
9+
* to you under the Apache License, Version 2.0 (the
10+
* "License"); you may not use this file except in compliance
11+
* with the License. You may obtain a copy of the License at
1112
*
12-
* http://www.apache.org/licenses/LICENSE-2.0
13+
* http://www.apache.org/licenses/LICENSE-2.0
1314
*
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.
15+
* Unless required by applicable law or agreed to in writing,
16+
* software distributed under the License is distributed on an
17+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18+
* KIND, either express or implied. See the License for the
19+
* specific language governing permissions and limitations
20+
* under the License.
1921
*/
2022
/*
2123
* $Id: Keywords.java,v 1.2.4.1 2005/09/14 19:46:01 jeffsuttor Exp $
@@ -326,6 +328,11 @@ public class Keywords {
326328
public static final String FUNC_UNPARSED_ENTITY_URI_STRING
327329
= "unparsed-entity-uri";
328330

331+
/**
332+
* here function string (XML Signature).
333+
*/
334+
public static final String FUNC_HERE_STRING = "here";
335+
329336
// Proprietary, built in functions
330337
/**
331338
* current function string (Proprietary).
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
3+
*/
4+
/**
5+
* Licensed to the Apache Software Foundation (ASF) under one
6+
* or more contributor license agreements. See the NOTICE file
7+
* distributed with this work for additional information
8+
* regarding copyright ownership. The ASF licenses this file
9+
* to you under the Apache License, Version 2.0 (the
10+
* "License"); you may not use this file except in compliance
11+
* with the License. You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing,
16+
* software distributed under the License is distributed on an
17+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18+
* KIND, either express or implied. See the License for the
19+
* specific language governing permissions and limitations
20+
* under the License.
21+
*/
22+
package com.sun.org.apache.xpath.internal.functions;
23+
24+
import javax.xml.transform.TransformerException;
25+
import org.w3c.dom.Document;
26+
import org.w3c.dom.Node;
27+
import com.sun.org.apache.xml.internal.dtm.DTM;
28+
import com.sun.org.apache.xpath.internal.NodeSetDTM;
29+
import com.sun.org.apache.xpath.internal.XPathContext;
30+
import com.sun.org.apache.xpath.internal.objects.XNodeSet;
31+
import com.sun.org.apache.xpath.internal.objects.XObject;
32+
import com.sun.org.apache.xpath.internal.res.XPATHErrorResources;
33+
34+
/**
35+
* Execute the XML Signature here() function.
36+
*/
37+
public final class FuncHere extends Function {
38+
39+
private static final long serialVersionUID = 4328660760070034592L;
40+
41+
@Override
42+
public XObject execute(XPathContext xctxt) throws TransformerException {
43+
Node xpathOwnerNode = (Node)xctxt.getOwnerObject();
44+
if (xpathOwnerNode == null) {
45+
return null;
46+
}
47+
48+
int xpathOwnerNodeDTM = xctxt.getDTMHandleFromNode(xpathOwnerNode);
49+
int currentNode = xctxt.getCurrentNode();
50+
DTM dtm = xctxt.getDTM(currentNode);
51+
int docContext = dtm.getDocument();
52+
53+
if (docContext == DTM.NULL) {
54+
error(xctxt, XPATHErrorResources.ER_CONTEXT_HAS_NO_OWNERDOC, null);
55+
}
56+
57+
// check whether currentNode and the node containing the XPath
58+
// expression are in the same document
59+
Document currentDoc = getOwnerDocument(dtm.getNode(currentNode));
60+
Document xpathOwnerDoc = getOwnerDocument(xpathOwnerNode);
61+
62+
if (currentDoc != xpathOwnerDoc) {
63+
throw new TransformerException("Owner documents differ");
64+
}
65+
66+
XNodeSet nodes = new XNodeSet(xctxt.getDTMManager());
67+
NodeSetDTM nodeSet = nodes.mutableNodeset();
68+
69+
int hereNode = DTM.NULL;
70+
71+
switch (dtm.getNodeType(xpathOwnerNodeDTM)) {
72+
73+
case Node.ATTRIBUTE_NODE:
74+
case Node.PROCESSING_INSTRUCTION_NODE: {
75+
// returns a node-set containing the attribute / processing
76+
// instruction node
77+
hereNode = xpathOwnerNodeDTM;
78+
nodeSet.addNode(hereNode);
79+
break;
80+
}
81+
case Node.TEXT_NODE : {
82+
// returns a node-set containing the parent element of the
83+
// text node that directly bears the XPath expression
84+
hereNode = dtm.getParent(xpathOwnerNodeDTM);
85+
nodeSet.addNode(hereNode);
86+
break;
87+
}
88+
default :
89+
break;
90+
}
91+
92+
/** $todo$ Do I have to do this detach() call? */
93+
nodeSet.detach();
94+
95+
return nodes;
96+
}
97+
98+
private static Document getOwnerDocument(Node node) {
99+
if (node.getNodeType() == Node.DOCUMENT_NODE) {
100+
return (Document)node;
101+
}
102+
return node.getOwnerDocument();
103+
}
104+
105+
@Override
106+
public void fixupVariables(java.util.Vector vars, int globalsSize) { }
107+
}

jdk/src/share/classes/com/sun/org/apache/xml/internal/security/utils/XalanXPathAPI.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,11 @@ private XPath createXPath(String str, PrefixResolver prefixResolver) throws Tran
165165

166166
private static synchronized void fixupFunctionTable() {
167167
installed = false;
168+
if (new FunctionTable().functionAvailable("here")) {
169+
LOG.debug("Here function already registered");
170+
installed = true;
171+
return;
172+
}
168173
LOG.debug("Registering Here function");
169174
/**
170175
* Try to register our here() implementation as internal function.

0 commit comments

Comments
 (0)