J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 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.params; |
| 22 | |
| 23 | |
| 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.HelperNodeList; |
| 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 | /** |
| 36 | * Implements the parameters for the <A |
| 37 | * HREF="http://www.w3.org/TR/xmldsig-filter2/">XPath Filter v2.0</A>. |
| 38 | * |
| 39 | * @author $Author: raul $ |
| 40 | * @see <A HREF="http://www.w3.org/TR/xmldsig-filter2/">XPath Filter v2.0 (TR)</A> |
| 41 | * @see <A HREF="http://www.w3.org/Signature/Drafts/xmldsig-xfilter2/">XPath Filter v2.0 (editors copy)</A> |
| 42 | */ |
| 43 | public class XPath2FilterContainer extends ElementProxy |
| 44 | implements TransformParam { |
| 45 | |
| 46 | /** Field _ATT_FILTER */ |
| 47 | private static final String _ATT_FILTER = "Filter"; |
| 48 | |
| 49 | /** Field _ATT_FILTER_VALUE_INTERSECT */ |
| 50 | private static final String _ATT_FILTER_VALUE_INTERSECT = "intersect"; |
| 51 | |
| 52 | /** Field _ATT_FILTER_VALUE_SUBTRACT */ |
| 53 | private static final String _ATT_FILTER_VALUE_SUBTRACT = "subtract"; |
| 54 | |
| 55 | /** Field _ATT_FILTER_VALUE_UNION */ |
| 56 | private static final String _ATT_FILTER_VALUE_UNION = "union"; |
| 57 | |
| 58 | /** Field INTERSECT */ |
| 59 | public static final String INTERSECT = |
| 60 | XPath2FilterContainer._ATT_FILTER_VALUE_INTERSECT; |
| 61 | |
| 62 | /** Field SUBTRACT */ |
| 63 | public static final String SUBTRACT = |
| 64 | XPath2FilterContainer._ATT_FILTER_VALUE_SUBTRACT; |
| 65 | |
| 66 | /** Field UNION */ |
| 67 | public static final String UNION = |
| 68 | XPath2FilterContainer._ATT_FILTER_VALUE_UNION; |
| 69 | |
| 70 | /** Field _TAG_XPATH2 */ |
| 71 | public static final String _TAG_XPATH2 = "XPath"; |
| 72 | |
| 73 | /** Field XPathFiler2NS */ |
| 74 | public static final String XPathFilter2NS = |
| 75 | "http://www.w3.org/2002/06/xmldsig-filter2"; |
| 76 | |
| 77 | /** |
| 78 | * Constructor XPath2FilterContainer |
| 79 | * |
| 80 | */ |
| 81 | private XPath2FilterContainer() { |
| 82 | |
| 83 | // no instantiation |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Constructor XPath2FilterContainer |
| 88 | * |
| 89 | * @param doc |
| 90 | * @param xpath2filter |
| 91 | * @param filterType |
| 92 | */ |
| 93 | private XPath2FilterContainer(Document doc, String xpath2filter, |
| 94 | String filterType) { |
| 95 | |
| 96 | super(doc); |
| 97 | |
| 98 | this._constructionElement |
| 99 | .setAttributeNS(null, XPath2FilterContainer._ATT_FILTER, filterType); |
| 100 | this._constructionElement.appendChild(doc.createTextNode(xpath2filter)); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Constructor XPath2FilterContainer |
| 105 | * |
| 106 | * @param element |
| 107 | * @param BaseURI |
| 108 | * @throws XMLSecurityException |
| 109 | */ |
| 110 | private XPath2FilterContainer(Element element, String BaseURI) |
| 111 | throws XMLSecurityException { |
| 112 | |
| 113 | super(element, BaseURI); |
| 114 | |
| 115 | String filterStr = this._constructionElement.getAttributeNS(null, |
| 116 | XPath2FilterContainer._ATT_FILTER); |
| 117 | |
| 118 | if (!filterStr |
| 119 | .equals(XPath2FilterContainer |
| 120 | ._ATT_FILTER_VALUE_INTERSECT) &&!filterStr |
| 121 | .equals(XPath2FilterContainer |
| 122 | ._ATT_FILTER_VALUE_SUBTRACT) &&!filterStr |
| 123 | .equals(XPath2FilterContainer._ATT_FILTER_VALUE_UNION)) { |
| 124 | Object exArgs[] = { XPath2FilterContainer._ATT_FILTER, filterStr, |
| 125 | XPath2FilterContainer._ATT_FILTER_VALUE_INTERSECT |
| 126 | + ", " |
| 127 | + XPath2FilterContainer._ATT_FILTER_VALUE_SUBTRACT |
| 128 | + " or " |
| 129 | + XPath2FilterContainer._ATT_FILTER_VALUE_UNION }; |
| 130 | |
| 131 | throw new XMLSecurityException("attributeValueIllegal", exArgs); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Creates a new XPath2FilterContainer with the filter type "intersect". |
| 137 | * |
| 138 | * @param doc |
| 139 | * @param xpath2filter |
| 140 | * @return the filter. |
| 141 | */ |
| 142 | public static XPath2FilterContainer newInstanceIntersect(Document doc, |
| 143 | String xpath2filter) { |
| 144 | |
| 145 | return new XPath2FilterContainer(doc, xpath2filter, |
| 146 | XPath2FilterContainer |
| 147 | ._ATT_FILTER_VALUE_INTERSECT); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Creates a new XPath2FilterContainer with the filter type "subtract". |
| 152 | * |
| 153 | * @param doc |
| 154 | * @param xpath2filter |
| 155 | * @return the filter. |
| 156 | */ |
| 157 | public static XPath2FilterContainer newInstanceSubtract(Document doc, |
| 158 | String xpath2filter) { |
| 159 | |
| 160 | return new XPath2FilterContainer(doc, xpath2filter, |
| 161 | XPath2FilterContainer |
| 162 | ._ATT_FILTER_VALUE_SUBTRACT); |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Creates a new XPath2FilterContainer with the filter type "union". |
| 167 | * |
| 168 | * @param doc |
| 169 | * @param xpath2filter |
| 170 | * @return the filter |
| 171 | */ |
| 172 | public static XPath2FilterContainer newInstanceUnion(Document doc, |
| 173 | String xpath2filter) { |
| 174 | |
| 175 | return new XPath2FilterContainer(doc, xpath2filter, |
| 176 | XPath2FilterContainer |
| 177 | ._ATT_FILTER_VALUE_UNION); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Method newInstances |
| 182 | * |
| 183 | * @param doc |
| 184 | * @param params |
| 185 | * @return the nodelist with the data |
| 186 | */ |
| 187 | public static NodeList newInstances(Document doc, String[][] params) { |
| 188 | |
| 189 | HelperNodeList nl = new HelperNodeList(); |
| 190 | |
| 191 | nl.appendChild(doc.createTextNode("\n")); |
| 192 | |
| 193 | for (int i = 0; i < params.length; i++) { |
| 194 | String type = params[i][0]; |
| 195 | String xpath = params[i][1]; |
| 196 | |
| 197 | if (!(type.equals(XPath2FilterContainer |
| 198 | ._ATT_FILTER_VALUE_INTERSECT) || type |
| 199 | .equals(XPath2FilterContainer |
| 200 | ._ATT_FILTER_VALUE_SUBTRACT) || type |
| 201 | .equals(XPath2FilterContainer |
| 202 | ._ATT_FILTER_VALUE_UNION))) { |
| 203 | throw new IllegalArgumentException("The type(" + i + ")=\"" + type |
| 204 | + "\" is illegal"); |
| 205 | } |
| 206 | |
| 207 | XPath2FilterContainer c = new XPath2FilterContainer(doc, xpath, type); |
| 208 | |
| 209 | nl.appendChild(c.getElement()); |
| 210 | nl.appendChild(doc.createTextNode("\n")); |
| 211 | } |
| 212 | |
| 213 | return nl; |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Creates a XPath2FilterContainer from an existing Element; needed for verification. |
| 218 | * |
| 219 | * @param element |
| 220 | * @param BaseURI |
| 221 | * @return the filter |
| 222 | * |
| 223 | * @throws XMLSecurityException |
| 224 | */ |
| 225 | public static XPath2FilterContainer newInstance( |
| 226 | Element element, String BaseURI) throws XMLSecurityException { |
| 227 | return new XPath2FilterContainer(element, BaseURI); |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Returns <code>true</code> if the <code>Filter</code> attribute has value "intersect". |
| 232 | * |
| 233 | * @return <code>true</code> if the <code>Filter</code> attribute has value "intersect". |
| 234 | */ |
| 235 | public boolean isIntersect() { |
| 236 | |
| 237 | return this._constructionElement |
| 238 | .getAttributeNS(null, XPath2FilterContainer._ATT_FILTER) |
| 239 | .equals(XPath2FilterContainer._ATT_FILTER_VALUE_INTERSECT); |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Returns <code>true</code> if the <code>Filter</code> attribute has value "subtract". |
| 244 | * |
| 245 | * @return <code>true</code> if the <code>Filter</code> attribute has value "subtract". |
| 246 | */ |
| 247 | public boolean isSubtract() { |
| 248 | |
| 249 | return this._constructionElement |
| 250 | .getAttributeNS(null, XPath2FilterContainer._ATT_FILTER) |
| 251 | .equals(XPath2FilterContainer._ATT_FILTER_VALUE_SUBTRACT); |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Returns <code>true</code> if the <code>Filter</code> attribute has value "union". |
| 256 | * |
| 257 | * @return <code>true</code> if the <code>Filter</code> attribute has value "union". |
| 258 | */ |
| 259 | public boolean isUnion() { |
| 260 | |
| 261 | return this._constructionElement |
| 262 | .getAttributeNS(null, XPath2FilterContainer._ATT_FILTER) |
| 263 | .equals(XPath2FilterContainer._ATT_FILTER_VALUE_UNION); |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * Returns the XPath 2 Filter String |
| 268 | * |
| 269 | * @return the XPath 2 Filter String |
| 270 | */ |
| 271 | public String getXPathFilterStr() { |
| 272 | return this.getTextFromTextChild(); |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Returns the first Text node which contains information from the XPath 2 |
| 277 | * Filter String. We must use this stupid hook to enable the here() function |
| 278 | * to work. |
| 279 | * |
| 280 | * $todo$ I dunno whether this crashes: <XPath> here()<!-- comment -->/ds:Signature[1]</XPath> |
| 281 | * @return the first Text node which contains information from the XPath 2 Filter String |
| 282 | */ |
| 283 | public Node getXPathFilterTextNode() { |
| 284 | |
| 285 | NodeList children = this._constructionElement.getChildNodes(); |
| 286 | int length = children.getLength(); |
| 287 | |
| 288 | for (int i = 0; i < length; i++) { |
| 289 | if (children.item(i).getNodeType() == Node.TEXT_NODE) { |
| 290 | return children.item(i); |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | return null; |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * Method getBaseLocalName |
| 299 | * |
| 300 | * @return the XPATH2 tag |
| 301 | */ |
| 302 | public final String getBaseLocalName() { |
| 303 | return XPath2FilterContainer._TAG_XPATH2; |
| 304 | } |
| 305 | |
| 306 | /** |
| 307 | * Method getBaseNamespace |
| 308 | * |
| 309 | * @return XPATH2 tag namespace |
| 310 | */ |
| 311 | public final String getBaseNamespace() { |
| 312 | return XPath2FilterContainer.XPathFilter2NS; |
| 313 | } |
| 314 | } |