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