1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 /**
   6  * Licensed to the Apache Software Foundation (ASF) under one
   7  * or more contributor license agreements. See the NOTICE file
   8  * distributed with this work for additional information
   9  * regarding copyright ownership. The ASF licenses this file
  10  * to you under the Apache License, Version 2.0 (the
  11  * "License"); you may not use this file except in compliance
  12  * with the License. You may obtain a copy of the License at
  13  *
  14  * http://www.apache.org/licenses/LICENSE-2.0
  15  *
  16  * Unless required by applicable law or agreed to in writing,
  17  * software distributed under the License is distributed on an
  18  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  19  * KIND, either express or implied. See the License for the
  20  * specific language governing permissions and limitations
  21  * under the License.
  22  */
  23 package com.sun.org.apache.xml.internal.security.transforms.params;
  24 
  25 import com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException;
  26 import com.sun.org.apache.xml.internal.security.transforms.TransformParam;
  27 import com.sun.org.apache.xml.internal.security.utils.ElementProxy;
  28 import com.sun.org.apache.xml.internal.security.utils.XMLUtils;
  29 import org.w3c.dom.Document;
  30 import org.w3c.dom.Element;
  31 import org.w3c.dom.Node;
  32 import org.w3c.dom.NodeList;
  33 
  34 /**
  35  * Implements the parameters for the <A
  36  * HREF="http://www.w3.org/TR/xmldsig-filter2/">XPath Filter v2.0</A>.
  37  *
  38  * @author $Author: coheigea $
  39  * @see <A HREF="http://www.w3.org/TR/xmldsig-filter2/">XPath Filter v2.0 (TR)</A>
  40  */
  41 public class XPath2FilterContainer04 extends ElementProxy implements TransformParam {
  42 
  43     /** Field _ATT_FILTER */
  44     private static final String _ATT_FILTER = "Filter";
  45 
  46     /** Field _ATT_FILTER_VALUE_INTERSECT */
  47     private static final String _ATT_FILTER_VALUE_INTERSECT = "intersect";
  48 
  49     /** Field _ATT_FILTER_VALUE_SUBTRACT */
  50     private static final String _ATT_FILTER_VALUE_SUBTRACT = "subtract";
  51 
  52     /** Field _ATT_FILTER_VALUE_UNION */
  53     private static final String _ATT_FILTER_VALUE_UNION = "union";
  54 
  55     /** Field _TAG_XPATH2 */
  56     public static final String _TAG_XPATH2 = "XPath";
  57 
  58     /** Field XPathFiler2NS */
  59     public static final String XPathFilter2NS =
  60         "http://www.w3.org/2002/04/xmldsig-filter2";
  61 
  62     /**
  63      * Constructor XPath2FilterContainer04
  64      *
  65      */
  66     private XPath2FilterContainer04() {
  67 
  68         // no instantiation
  69     }
  70 
  71     /**
  72      * Constructor XPath2FilterContainer04
  73      *
  74      * @param doc
  75      * @param xpath2filter
  76      * @param filterType
  77      */
  78     private XPath2FilterContainer04(Document doc, String xpath2filter, String filterType) {
  79         super(doc);
  80 
  81         this.constructionElement.setAttributeNS(
  82             null, XPath2FilterContainer04._ATT_FILTER, filterType);
  83 
  84         if ((xpath2filter.length() > 2)
  85             && (!Character.isWhitespace(xpath2filter.charAt(0)))) {
  86             XMLUtils.addReturnToElement(this.constructionElement);
  87             this.constructionElement.appendChild(doc.createTextNode(xpath2filter));
  88             XMLUtils.addReturnToElement(this.constructionElement);
  89         } else {
  90             this.constructionElement.appendChild(doc.createTextNode(xpath2filter));
  91         }
  92     }
  93 
  94     /**
  95      * Constructor XPath2FilterContainer04
  96      *
  97      * @param element
  98      * @param BaseURI
  99      * @throws XMLSecurityException
 100      */
 101     private XPath2FilterContainer04(Element element, String BaseURI)
 102         throws XMLSecurityException {
 103 
 104         super(element, BaseURI);
 105 
 106         String filterStr =
 107             this.constructionElement.getAttributeNS(null, XPath2FilterContainer04._ATT_FILTER);
 108 
 109         if (!filterStr.equals(XPath2FilterContainer04._ATT_FILTER_VALUE_INTERSECT) 
 110             && !filterStr.equals(XPath2FilterContainer04._ATT_FILTER_VALUE_SUBTRACT) 
 111             && !filterStr.equals(XPath2FilterContainer04._ATT_FILTER_VALUE_UNION)) {
 112             Object exArgs[] = { XPath2FilterContainer04._ATT_FILTER, filterStr,
 113                                 XPath2FilterContainer04._ATT_FILTER_VALUE_INTERSECT
 114                                 + ", "
 115                                 + XPath2FilterContainer04._ATT_FILTER_VALUE_SUBTRACT
 116                                 + " or "
 117                                 + XPath2FilterContainer04._ATT_FILTER_VALUE_UNION };
 118 
 119             throw new XMLSecurityException("attributeValueIllegal", exArgs);
 120         }
 121     }
 122 
 123     /**
 124      * Creates a new XPath2FilterContainer04 with the filter type "intersect".
 125      *
 126      * @param doc
 127      * @param xpath2filter
 128      * @return the instance
 129      */
 130     public static XPath2FilterContainer04 newInstanceIntersect(
 131         Document doc, String xpath2filter
 132     ) {
 133         return new XPath2FilterContainer04(
 134             doc, xpath2filter, XPath2FilterContainer04._ATT_FILTER_VALUE_INTERSECT);
 135     }
 136 
 137     /**
 138      * Creates a new XPath2FilterContainer04 with the filter type "subtract".
 139      *
 140      * @param doc
 141      * @param xpath2filter
 142      * @return the instance
 143      */
 144     public static XPath2FilterContainer04 newInstanceSubtract(
 145         Document doc, String xpath2filter
 146     ) {
 147         return new XPath2FilterContainer04(
 148             doc, xpath2filter, XPath2FilterContainer04._ATT_FILTER_VALUE_SUBTRACT);
 149     }
 150 
 151     /**
 152      * Creates a new XPath2FilterContainer04 with the filter type "union".
 153      *
 154      * @param doc
 155      * @param xpath2filter
 156      * @return the instance
 157      */
 158     public static XPath2FilterContainer04 newInstanceUnion(
 159         Document doc, String xpath2filter
 160     ) {
 161         return new XPath2FilterContainer04(
 162             doc, xpath2filter, XPath2FilterContainer04._ATT_FILTER_VALUE_UNION);
 163     }
 164 
 165     /**
 166      * Creates a XPath2FilterContainer04 from an existing Element; needed for verification.
 167      *
 168      * @param element
 169      * @param BaseURI
 170      * @return the instance
 171      *
 172      * @throws XMLSecurityException
 173      */
 174     public static XPath2FilterContainer04 newInstance(
 175         Element element, String BaseURI
 176     ) throws XMLSecurityException {
 177         return new XPath2FilterContainer04(element, BaseURI);
 178     }
 179 
 180     /**
 181      * Returns <code>true</code> if the <code>Filter</code> attribute has value "intersect".
 182      *
 183      * @return <code>true</code> if the <code>Filter</code> attribute has value "intersect".
 184      */
 185     public boolean isIntersect() {
 186         return this.constructionElement.getAttributeNS(
 187             null, XPath2FilterContainer04._ATT_FILTER
 188         ).equals(XPath2FilterContainer04._ATT_FILTER_VALUE_INTERSECT);
 189     }
 190 
 191     /**
 192      * Returns <code>true</code> if the <code>Filter</code> attribute has value "subtract".
 193      *
 194      * @return <code>true</code> if the <code>Filter</code> attribute has value "subtract".
 195      */
 196     public boolean isSubtract() {
 197         return this.constructionElement.getAttributeNS(
 198             null, XPath2FilterContainer04._ATT_FILTER
 199         ).equals(XPath2FilterContainer04._ATT_FILTER_VALUE_SUBTRACT);
 200     }
 201 
 202     /**
 203      * Returns <code>true</code> if the <code>Filter</code> attribute has value "union".
 204      *
 205      * @return <code>true</code> if the <code>Filter</code> attribute has value "union".
 206      */
 207     public boolean isUnion() {
 208         return this.constructionElement.getAttributeNS(
 209             null, XPath2FilterContainer04._ATT_FILTER
 210         ).equals(XPath2FilterContainer04._ATT_FILTER_VALUE_UNION);
 211     }
 212 
 213     /**
 214      * Returns the XPath 2 Filter String
 215      *
 216      * @return the XPath 2 Filter String
 217      */
 218     public String getXPathFilterStr() {
 219         return this.getTextFromTextChild();
 220     }
 221 
 222     /**
 223      * Returns the first Text node which contains information from the XPath 2
 224      * Filter String. We must use this stupid hook to enable the here() function
 225      * to work.
 226      *
 227      * $todo$ I dunno whether this crashes: <XPath> here()<!-- comment -->/ds:Signature[1]</XPath>
 228      * @return the first Text node which contains information from the XPath 2 Filter String
 229      */
 230     public Node getXPathFilterTextNode() {
 231         NodeList children = this.constructionElement.getChildNodes();
 232         int length = children.getLength();
 233 
 234         for (int i = 0; i < length; i++) {
 235             if (children.item(i).getNodeType() == Node.TEXT_NODE) {
 236                 return children.item(i);
 237             }
 238         }
 239 
 240         return null;
 241     }
 242 
 243     /** @inheritDoc */
 244     public final String getBaseLocalName() {
 245         return XPath2FilterContainer04._TAG_XPATH2;
 246     }
 247 
 248     /** @inheritDoc */
 249     public final String getBaseNamespace() {
 250         return XPath2FilterContainer04.XPathFilter2NS;
 251     }
 252 }