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 org.w3c.dom.Document; |
| 29 | import org.w3c.dom.Element; |
| 30 | import org.w3c.dom.Node; |
| 31 | import org.w3c.dom.NodeList; |
| 32 | |
| 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: raul $ |
| 39 | * @see <A HREF="http://www.w3.org/TR/xmldsig-filter2/">XPath Filter v2.0 (TR)</A> |
| 40 | * @see <A HREF="http://www.w3.org/Signature/Drafts/xmldsig-xfilter2/">XPath Filter v2.0 (editors copy)</A> |
| 41 | */ |
| 42 | public class XPath2FilterContainer04 extends ElementProxy |
| 43 | implements TransformParam { |
| 44 | |
| 45 | /** Field _ATT_FILTER */ |
| 46 | private static final String _ATT_FILTER = "Filter"; |
| 47 | |
| 48 | /** Field _ATT_FILTER_VALUE_INTERSECT */ |
| 49 | private static final String _ATT_FILTER_VALUE_INTERSECT = "intersect"; |
| 50 | |
| 51 | /** Field _ATT_FILTER_VALUE_SUBTRACT */ |
| 52 | private static final String _ATT_FILTER_VALUE_SUBTRACT = "subtract"; |
| 53 | |
| 54 | /** Field _ATT_FILTER_VALUE_UNION */ |
| 55 | private static final String _ATT_FILTER_VALUE_UNION = "union"; |
| 56 | |
| 57 | /** Field _TAG_XPATH2 */ |
| 58 | public static final String _TAG_XPATH2 = "XPath"; |
| 59 | |
| 60 | /** Field XPathFiler2NS */ |
| 61 | public static final String XPathFilter2NS = |
| 62 | "http://www.w3.org/2002/04/xmldsig-filter2"; |
| 63 | |
| 64 | /** |
| 65 | * Constructor XPath2FilterContainer04 |
| 66 | * |
| 67 | */ |
| 68 | private XPath2FilterContainer04() { |
| 69 | |
| 70 | // no instantiation |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Constructor XPath2FilterContainer04 |
| 75 | * |
| 76 | * @param doc |
| 77 | * @param xpath2filter |
| 78 | * @param filterType |
| 79 | */ |
| 80 | private XPath2FilterContainer04(Document doc, String xpath2filter, |
| 81 | String filterType) { |
| 82 | |
| 83 | super(doc); |
| 84 | |
| 85 | this._constructionElement.setAttributeNS(null, XPath2FilterContainer04._ATT_FILTER, |
| 86 | filterType); |
| 87 | |
| 88 | if ((xpath2filter.length() > 2) |
| 89 | && (!Character.isWhitespace(xpath2filter.charAt(0)))) { |
| 90 | this._constructionElement.appendChild(doc.createTextNode("\n" |
| 91 | + xpath2filter + "\n")); |
| 92 | } else { |
| 93 | this._constructionElement |
| 94 | .appendChild(doc.createTextNode(xpath2filter)); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Constructor XPath2FilterContainer04 |
| 100 | * |
| 101 | * @param element |
| 102 | * @param BaseURI |
| 103 | * @throws XMLSecurityException |
| 104 | */ |
| 105 | private XPath2FilterContainer04(Element element, String BaseURI) |
| 106 | throws XMLSecurityException { |
| 107 | |
| 108 | super(element, BaseURI); |
| 109 | |
| 110 | String filterStr = |
| 111 | this._constructionElement |
| 112 | .getAttributeNS(null, XPath2FilterContainer04._ATT_FILTER); |
| 113 | |
| 114 | if (!filterStr |
| 115 | .equals(XPath2FilterContainer04 |
| 116 | ._ATT_FILTER_VALUE_INTERSECT) &&!filterStr |
| 117 | .equals(XPath2FilterContainer04 |
| 118 | ._ATT_FILTER_VALUE_SUBTRACT) &&!filterStr |
| 119 | .equals(XPath2FilterContainer04._ATT_FILTER_VALUE_UNION)) { |
| 120 | Object exArgs[] = { XPath2FilterContainer04._ATT_FILTER, filterStr, |
| 121 | XPath2FilterContainer04._ATT_FILTER_VALUE_INTERSECT |
| 122 | + ", " |
| 123 | + XPath2FilterContainer04._ATT_FILTER_VALUE_SUBTRACT |
| 124 | + " or " |
| 125 | + XPath2FilterContainer04._ATT_FILTER_VALUE_UNION }; |
| 126 | |
| 127 | throw new XMLSecurityException("attributeValueIllegal", exArgs); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Creates a new XPath2FilterContainer04 with the filter type "intersect". |
| 133 | * |
| 134 | * @param doc |
| 135 | * @param xpath2filter |
| 136 | * @return the instance |
| 137 | */ |
| 138 | public static XPath2FilterContainer04 newInstanceIntersect(Document doc, |
| 139 | String xpath2filter) { |
| 140 | |
| 141 | return new XPath2FilterContainer04(doc, xpath2filter, |
| 142 | XPath2FilterContainer04 |
| 143 | ._ATT_FILTER_VALUE_INTERSECT); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Creates a new XPath2FilterContainer04 with the filter type "subtract". |
| 148 | * |
| 149 | * @param doc |
| 150 | * @param xpath2filter |
| 151 | * @return the instance |
| 152 | */ |
| 153 | public static XPath2FilterContainer04 newInstanceSubtract(Document doc, |
| 154 | String xpath2filter) { |
| 155 | |
| 156 | return new XPath2FilterContainer04(doc, xpath2filter, |
| 157 | XPath2FilterContainer04 |
| 158 | ._ATT_FILTER_VALUE_SUBTRACT); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Creates a new XPath2FilterContainer04 with the filter type "union". |
| 163 | * |
| 164 | * @param doc |
| 165 | * @param xpath2filter |
| 166 | * @return the instance |
| 167 | */ |
| 168 | public static XPath2FilterContainer04 newInstanceUnion(Document doc, |
| 169 | String xpath2filter) { |
| 170 | |
| 171 | return new XPath2FilterContainer04(doc, xpath2filter, |
| 172 | XPath2FilterContainer04 |
| 173 | ._ATT_FILTER_VALUE_UNION); |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Creates a XPath2FilterContainer04 from an existing Element; needed for verification. |
| 178 | * |
| 179 | * @param element |
| 180 | * @param BaseURI |
| 181 | * @return the instance |
| 182 | * |
| 183 | * @throws XMLSecurityException |
| 184 | */ |
| 185 | public static XPath2FilterContainer04 newInstance( |
| 186 | Element element, String BaseURI) throws XMLSecurityException { |
| 187 | return new XPath2FilterContainer04(element, BaseURI); |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Returns <code>true</code> if the <code>Filter</code> attribute has value "intersect". |
| 192 | * |
| 193 | * @return <code>true</code> if the <code>Filter</code> attribute has value "intersect". |
| 194 | */ |
| 195 | public boolean isIntersect() { |
| 196 | |
| 197 | return this._constructionElement |
| 198 | .getAttributeNS(null, XPath2FilterContainer04._ATT_FILTER) |
| 199 | .equals(XPath2FilterContainer04._ATT_FILTER_VALUE_INTERSECT); |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Returns <code>true</code> if the <code>Filter</code> attribute has value "subtract". |
| 204 | * |
| 205 | * @return <code>true</code> if the <code>Filter</code> attribute has value "subtract". |
| 206 | */ |
| 207 | public boolean isSubtract() { |
| 208 | |
| 209 | return this._constructionElement |
| 210 | .getAttributeNS(null, XPath2FilterContainer04._ATT_FILTER) |
| 211 | .equals(XPath2FilterContainer04._ATT_FILTER_VALUE_SUBTRACT); |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Returns <code>true</code> if the <code>Filter</code> attribute has value "union". |
| 216 | * |
| 217 | * @return <code>true</code> if the <code>Filter</code> attribute has value "union". |
| 218 | */ |
| 219 | public boolean isUnion() { |
| 220 | |
| 221 | return this._constructionElement |
| 222 | .getAttributeNS(null, XPath2FilterContainer04._ATT_FILTER) |
| 223 | .equals(XPath2FilterContainer04._ATT_FILTER_VALUE_UNION); |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Returns the XPath 2 Filter String |
| 228 | * |
| 229 | * @return the XPath 2 Filter String |
| 230 | */ |
| 231 | public String getXPathFilterStr() { |
| 232 | return this.getTextFromTextChild(); |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Returns the first Text node which contains information from the XPath 2 |
| 237 | * Filter String. We must use this stupid hook to enable the here() function |
| 238 | * to work. |
| 239 | * |
| 240 | * $todo$ I dunno whether this crashes: <XPath> here()<!-- comment -->/ds:Signature[1]</XPath> |
| 241 | * @return the first Text node which contains information from the XPath 2 Filter String |
| 242 | */ |
| 243 | public Node getXPathFilterTextNode() { |
| 244 | NodeList children = this._constructionElement.getChildNodes(); |
| 245 | int length = children.getLength(); |
| 246 | |
| 247 | for (int i = 0; i < length; i++) { |
| 248 | if (children.item(i).getNodeType() == Node.TEXT_NODE) { |
| 249 | return children.item(i); |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | return null; |
| 254 | } |
| 255 | |
| 256 | /** @inheritDoc */ |
| 257 | public final String getBaseLocalName() { |
| 258 | return XPath2FilterContainer04._TAG_XPATH2; |
| 259 | } |
| 260 | |
| 261 | /** @inheritDoc */ |
| 262 | public final String getBaseNamespace() { |
| 263 | return XPath2FilterContainer04.XPathFilter2NS; |
| 264 | } |
| 265 | } |