KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xml > internal > security > transforms > params > XPath2FilterContainer


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17 package com.sun.org.apache.xml.internal.security.transforms.params;
18
19
20
21 import com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException;
22 import com.sun.org.apache.xml.internal.security.transforms.TransformParam;
23 import com.sun.org.apache.xml.internal.security.utils.ElementProxy;
24 import com.sun.org.apache.xml.internal.security.utils.HelperNodeList;
25 import org.w3c.dom.Document JavaDoc;
26 import org.w3c.dom.Element JavaDoc;
27 import org.w3c.dom.Node JavaDoc;
28 import org.w3c.dom.NodeList JavaDoc;
29
30
31 /**
32  * Implements the parameters for the <A
33  * HREF="http://www.w3.org/TR/xmldsig-filter2/">XPath Filter v2.0</A>.
34  *
35  * @author $Author: raul $
36  * @see <A HREF="http://www.w3.org/TR/xmldsig-filter2/">XPath Filter v2.0 (TR)</A>
37  * @see <A HREF="http://www.w3.org/Signature/Drafts/xmldsig-xfilter2/">XPath Filter v2.0 (editors copy)</A>
38  */

39 public class XPath2FilterContainer extends ElementProxy
40         implements TransformParam {
41
42    /** Field _ATT_FILTER */
43    private static final String JavaDoc _ATT_FILTER = "Filter";
44
45    /** Field _ATT_FILTER_VALUE_INTERSECT */
46    private static final String JavaDoc _ATT_FILTER_VALUE_INTERSECT = "intersect";
47
48    /** Field _ATT_FILTER_VALUE_SUBTRACT */
49    private static final String JavaDoc _ATT_FILTER_VALUE_SUBTRACT = "subtract";
50
51    /** Field _ATT_FILTER_VALUE_UNION */
52    private static final String JavaDoc _ATT_FILTER_VALUE_UNION = "union";
53
54    /** Field INTERSECT */
55    public static final String JavaDoc INTERSECT =
56       XPath2FilterContainer._ATT_FILTER_VALUE_INTERSECT;
57
58    /** Field SUBTRACT */
59    public static final String JavaDoc SUBTRACT =
60       XPath2FilterContainer._ATT_FILTER_VALUE_SUBTRACT;
61
62    /** Field UNION */
63    public static final String JavaDoc UNION =
64       XPath2FilterContainer._ATT_FILTER_VALUE_UNION;
65
66    /** Field _TAG_XPATH2 */
67    public static final String JavaDoc _TAG_XPATH2 = "XPath";
68
69    /** Field XPathFiler2NS */
70    public static final String JavaDoc XPathFilter2NS =
71       "http://www.w3.org/2002/06/xmldsig-filter2";
72
73    /**
74     * Constructor XPath2FilterContainer
75     *
76     */

77    private XPath2FilterContainer() {
78
79       // no instantiation
80
}
81
82    /**
83     * Constructor XPath2FilterContainer
84     *
85     * @param doc
86     * @param xpath2filter
87     * @param filterType
88     */

89    private XPath2FilterContainer(Document JavaDoc doc, String JavaDoc xpath2filter,
90                                  String JavaDoc filterType) {
91
92       super(doc);
93
94       this._constructionElement
95          .setAttributeNS(null, XPath2FilterContainer._ATT_FILTER, filterType);
96       this._constructionElement.appendChild(doc.createTextNode(xpath2filter));
97    }
98
99    /**
100     * Constructor XPath2FilterContainer
101     *
102     * @param element
103     * @param BaseURI
104     * @throws XMLSecurityException
105     */

106    private XPath2FilterContainer(Element JavaDoc element, String JavaDoc BaseURI)
107            throws XMLSecurityException {
108
109       super(element, BaseURI);
110
111       String JavaDoc filterStr = this._constructionElement.getAttributeNS(null,
112                             XPath2FilterContainer._ATT_FILTER);
113
114       if (!filterStr
115               .equals(XPath2FilterContainer
116               ._ATT_FILTER_VALUE_INTERSECT) &&!filterStr
117                  .equals(XPath2FilterContainer
118                  ._ATT_FILTER_VALUE_SUBTRACT) &&!filterStr
119                     .equals(XPath2FilterContainer._ATT_FILTER_VALUE_UNION)) {
120          Object JavaDoc exArgs[] = { XPath2FilterContainer._ATT_FILTER, filterStr,
121                              XPath2FilterContainer._ATT_FILTER_VALUE_INTERSECT
122                              + ", "
123                              + XPath2FilterContainer._ATT_FILTER_VALUE_SUBTRACT
124                              + " or "
125                              + XPath2FilterContainer._ATT_FILTER_VALUE_UNION };
126
127          throw new XMLSecurityException("attributeValueIllegal", exArgs);
128       }
129    }
130
131    /**
132     * Creates a new XPath2FilterContainer with the filter type "intersect".
133     *
134     * @param doc
135     * @param xpath2filter
136     * @return the filter.
137     */

138    public static XPath2FilterContainer newInstanceIntersect(Document JavaDoc doc,
139            String JavaDoc xpath2filter) {
140
141       return new XPath2FilterContainer(doc, xpath2filter,
142                                        XPath2FilterContainer
143                                           ._ATT_FILTER_VALUE_INTERSECT);
144    }
145
146    /**
147     * Creates a new XPath2FilterContainer with the filter type "subtract".
148     *
149     * @param doc
150     * @param xpath2filter
151     * @return the filter.
152     */

153    public static XPath2FilterContainer newInstanceSubtract(Document JavaDoc doc,
154            String JavaDoc xpath2filter) {
155
156       return new XPath2FilterContainer(doc, xpath2filter,
157                                        XPath2FilterContainer
158                                           ._ATT_FILTER_VALUE_SUBTRACT);
159    }
160
161    /**
162     * Creates a new XPath2FilterContainer with the filter type "union".
163     *
164     * @param doc
165     * @param xpath2filter
166     * @return the filter
167     */

168    public static XPath2FilterContainer newInstanceUnion(Document JavaDoc doc,
169            String JavaDoc xpath2filter) {
170
171       return new XPath2FilterContainer(doc, xpath2filter,
172                                        XPath2FilterContainer
173                                           ._ATT_FILTER_VALUE_UNION);
174    }
175
176    /**
177     * Method newInstances
178     *
179     * @param doc
180     * @param params
181     * @return the nodelist with the data
182     */

183    public static NodeList newInstances(Document JavaDoc doc, String JavaDoc[][] params) {
184
185       HelperNodeList nl = new HelperNodeList();
186
187       nl.appendChild(doc.createTextNode("\n"));
188
189       for (int i = 0; i < params.length; i++) {
190          String JavaDoc type = params[i][0];
191          String JavaDoc xpath = params[i][1];
192
193          if (!(type.equals(XPath2FilterContainer
194                  ._ATT_FILTER_VALUE_INTERSECT) || type
195                     .equals(XPath2FilterContainer
196                     ._ATT_FILTER_VALUE_SUBTRACT) || type
197                        .equals(XPath2FilterContainer
198                           ._ATT_FILTER_VALUE_UNION))) {
199             throw new IllegalArgumentException JavaDoc("The type(" + i + ")=\"" + type
200                                                + "\" is illegal");
201          }
202
203          XPath2FilterContainer c = new XPath2FilterContainer(doc, xpath, type);
204
205          nl.appendChild(c.getElement());
206          nl.appendChild(doc.createTextNode("\n"));
207       }
208
209       return nl;
210    }
211
212    /**
213     * Creates a XPath2FilterContainer from an existing Element; needed for verification.
214     *
215     * @param element
216     * @param BaseURI
217     * @return the filter
218     *
219     * @throws XMLSecurityException
220     */

221    public static XPath2FilterContainer newInstance(
222            Element JavaDoc element, String JavaDoc BaseURI) throws XMLSecurityException {
223       return new XPath2FilterContainer(element, BaseURI);
224    }
225
226    /**
227     * Returns <code>true</code> if the <code>Filter</code> attribute has value "intersect".
228     *
229     * @return <code>true</code> if the <code>Filter</code> attribute has value "intersect".
230     */

231    public boolean isIntersect() {
232
233       return this._constructionElement
234          .getAttributeNS(null, XPath2FilterContainer._ATT_FILTER)
235          .equals(XPath2FilterContainer._ATT_FILTER_VALUE_INTERSECT);
236    }
237
238    /**
239     * Returns <code>true</code> if the <code>Filter</code> attribute has value "subtract".
240     *
241     * @return <code>true</code> if the <code>Filter</code> attribute has value "subtract".
242     */

243    public boolean isSubtract() {
244
245       return this._constructionElement
246          .getAttributeNS(null, XPath2FilterContainer._ATT_FILTER)
247          .equals(XPath2FilterContainer._ATT_FILTER_VALUE_SUBTRACT);
248    }
249
250    /**
251     * Returns <code>true</code> if the <code>Filter</code> attribute has value "union".
252     *
253     * @return <code>true</code> if the <code>Filter</code> attribute has value "union".
254     */

255    public boolean isUnion() {
256
257       return this._constructionElement
258          .getAttributeNS(null, XPath2FilterContainer._ATT_FILTER)
259          .equals(XPath2FilterContainer._ATT_FILTER_VALUE_UNION);
260    }
261
262    /**
263     * Returns the XPath 2 Filter String
264     *
265     * @return the XPath 2 Filter String
266     */

267    public String JavaDoc getXPathFilterStr() {
268       return this.getTextFromTextChild();
269    }
270
271    /**
272     * Returns the first Text node which contains information from the XPath 2
273     * Filter String. We must use this stupid hook to enable the here() function
274     * to work.
275     *
276     * $todo$ I dunno whether this crashes: <XPath> here()<!-- comment -->/ds:Signature[1]</XPath>
277     * @return the first Text node which contains information from the XPath 2 Filter String
278     */

279    public Node JavaDoc getXPathFilterTextNode() {
280
281       NodeList children = this._constructionElement.getChildNodes();
282       int length = children.getLength();
283
284       for (int i = 0; i < length; i++) {
285          if (children.item(i).getNodeType() == Node.TEXT_NODE) {
286             return children.item(i);
287          }
288       }
289
290       return null;
291    }
292
293    /**
294     * Method getBaseLocalName
295     *
296     * @return the XPATH2 tag
297     */

298    public final String JavaDoc getBaseLocalName() {
299       return XPath2FilterContainer._TAG_XPATH2;
300    }
301
302    /**
303     * Method getBaseNamespace
304     *
305     * @return XPATH2 tag namespace
306     */

307    public final String JavaDoc getBaseNamespace() {
308       return XPath2FilterContainer.XPathFilter2NS;
309    }
310 }
311
Popular Tags