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