KickJava   Java API By Example, From Geeks To Geeks.

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


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.transforms.Transforms;
24 import com.sun.org.apache.xml.internal.security.utils.ElementProxy;
25 import com.sun.org.apache.xml.internal.security.utils.XMLUtils;
26 import org.w3c.dom.Document JavaDoc;
27 import org.w3c.dom.Element JavaDoc;
28 import org.w3c.dom.Node JavaDoc;
29
30
31 /**
32  * Implements the parameters for a custom Transform which has a better performance
33  * thatn the xfilter2.
34  *
35  * @author $Author: raul $
36  */

37 public class XPathFilterCHGPContainer extends ElementProxy
38         implements TransformParam {
39
40    /** Field _ATT_FILTER_VALUE_INTERSECT */
41    private static final String JavaDoc _TAG_INCLUDE_BUT_SEARCH = "IncludeButSearch";
42
43    /** Field _ATT_FILTER_VALUE_SUBTRACT */
44    private static final String JavaDoc _TAG_EXCLUDE_BUT_SEARCH = "ExcludeButSearch";
45
46    /** Field _ATT_FILTER_VALUE_UNION */
47    private static final String JavaDoc _TAG_EXCLUDE = "Exclude";
48
49    /** Field _TAG_XPATHCHGP */
50    public static final String JavaDoc _TAG_XPATHCHGP = "XPathAlternative";
51
52    /** Field _ATT_INCLUDESLASH */
53    public static final String JavaDoc _ATT_INCLUDESLASH = "IncludeSlashPolicy";
54
55    /** Field IncludeSlash */
56    public static final boolean IncludeSlash = true;
57
58    /** Field ExcludeSlash */
59    public static final boolean ExcludeSlash = false;
60
61    /**
62     * Constructor XPathFilterCHGPContainer
63     *
64     */

65    private XPathFilterCHGPContainer() {
66
67       // no instantiation
68
}
69
70    /**
71     * Constructor XPathFilterCHGPContainer
72     *
73     * @param doc
74     * @param includeSlashPolicy
75     * @param includeButSearch
76     * @param excludeButSearch
77     * @param exclude
78     */

79    private XPathFilterCHGPContainer(Document JavaDoc doc, boolean includeSlashPolicy,
80                                     String JavaDoc includeButSearch,
81                                     String JavaDoc excludeButSearch, String JavaDoc exclude) {
82
83       super(doc);
84
85       if (includeSlashPolicy) {
86          this._constructionElement
87             .setAttributeNS(null, XPathFilterCHGPContainer._ATT_INCLUDESLASH, "true");
88       } else {
89          this._constructionElement
90             .setAttributeNS(null, XPathFilterCHGPContainer._ATT_INCLUDESLASH, "false");
91       }
92
93       if ((includeButSearch != null)
94               && (includeButSearch.trim().length() > 0)) {
95          Element JavaDoc includeButSearchElem =
96             ElementProxy.createElementForFamily(doc, this.getBaseNamespace(),
97                                         XPathFilterCHGPContainer
98                                            ._TAG_INCLUDE_BUT_SEARCH);
99
100          includeButSearchElem
101             .appendChild(this._doc
102                .createTextNode(indentXPathText(includeButSearch)));
103          this._constructionElement.appendChild(doc.createTextNode("\n"));
104          this._constructionElement.appendChild(includeButSearchElem);
105       }
106
107       if ((excludeButSearch != null)
108               && (excludeButSearch.trim().length() > 0)) {
109          Element JavaDoc excludeButSearchElem =
110          ElementProxy.createElementForFamily(doc, this.getBaseNamespace(),
111                                         XPathFilterCHGPContainer
112                                            ._TAG_EXCLUDE_BUT_SEARCH);
113
114          excludeButSearchElem
115             .appendChild(this._doc
116                .createTextNode(indentXPathText(excludeButSearch)));
117          this._constructionElement.appendChild(doc.createTextNode("\n"));
118          this._constructionElement.appendChild(excludeButSearchElem);
119       }
120
121       if ((exclude != null) && (exclude.trim().length() > 0)) {
122          Element JavaDoc excludeElem = ElementProxy.createElementForFamily(doc,
123                                   this.getBaseNamespace(),
124                                   XPathFilterCHGPContainer._TAG_EXCLUDE);
125
126          excludeElem
127             .appendChild(this._doc.createTextNode(indentXPathText(exclude)));
128          this._constructionElement.appendChild(doc.createTextNode("\n"));
129          this._constructionElement.appendChild(excludeElem);
130       }
131
132       this._constructionElement.appendChild(doc.createTextNode("\n"));
133    }
134
135    /**
136     * Method indentXPathText
137     *
138     * @param xp
139     * @return the string with enters
140     */

141    static String JavaDoc indentXPathText(String JavaDoc xp) {
142
143       if ((xp.length() > 2) && (!Character.isWhitespace(xp.charAt(0)))) {
144          return "\n" + xp + "\n";
145       }
146       return xp;
147       
148    }
149
150    /**
151     * Constructor XPathFilterCHGPContainer
152     *
153     * @param element
154     * @param BaseURI
155     * @throws XMLSecurityException
156     */

157    private XPathFilterCHGPContainer(Element JavaDoc element, String JavaDoc BaseURI)
158            throws XMLSecurityException {
159       super(element, BaseURI);
160    }
161
162    /**
163     * Creates a new XPathFilterCHGPContainer; needed for generation.
164     *
165     * @param doc
166     * @param includeSlashPolicy
167     * @param includeButSearch
168     * @param excludeButSearch
169     * @param exclude
170     * @return the created object
171     */

172    public static XPathFilterCHGPContainer getInstance(Document JavaDoc doc,
173            boolean includeSlashPolicy, String JavaDoc includeButSearch,
174            String JavaDoc excludeButSearch, String JavaDoc exclude) {
175
176       return new XPathFilterCHGPContainer(doc, includeSlashPolicy,
177                                           includeButSearch, excludeButSearch,
178                                           exclude);
179    }
180
181    /**
182     * Creates a XPathFilterCHGPContainer from an existing Element; needed for verification.
183     *
184     * @param element
185     * @param BaseURI
186     *
187     * @throws XMLSecurityException
188     * @return the created object.
189     */

190    public static XPathFilterCHGPContainer getInstance(
191            Element JavaDoc element, String JavaDoc BaseURI) throws XMLSecurityException {
192       return new XPathFilterCHGPContainer(element, BaseURI);
193    }
194
195    /**
196     * Method getXStr
197     *
198     * @param type
199     * @return The Xstr
200     */

201    private String JavaDoc getXStr(String JavaDoc type) {
202
203       if (this.length(this.getBaseNamespace(), type) != 1) {
204          return "";
205       }
206
207       Element JavaDoc xElem = XMLUtils.selectNode(this._constructionElement.getFirstChild(), this.getBaseNamespace(),
208                          type,0);
209
210       return XMLUtils.getFullTextChildrenFromElement(xElem);
211    }
212
213    /**
214     * Method getIncludeButSearch
215     *
216     * @return the string
217     */

218    public String JavaDoc getIncludeButSearch() {
219       return this.getXStr(XPathFilterCHGPContainer._TAG_INCLUDE_BUT_SEARCH);
220    }
221
222    /**
223     * Method getExcludeButSearch
224     *
225     * @return the string
226     */

227    public String JavaDoc getExcludeButSearch() {
228       return this.getXStr(XPathFilterCHGPContainer._TAG_EXCLUDE_BUT_SEARCH);
229    }
230
231    /**
232     * Method getExclude
233     *
234     * @return the string
235     */

236    public String JavaDoc getExclude() {
237       return this.getXStr(XPathFilterCHGPContainer._TAG_EXCLUDE);
238    }
239
240    /**
241     * Method getIncludeSlashPolicy
242     *
243     * @return the string
244     */

245    public boolean getIncludeSlashPolicy() {
246
247       return this._constructionElement
248          .getAttributeNS(null, XPathFilterCHGPContainer._ATT_INCLUDESLASH)
249          .equals("true");
250    }
251
252    /**
253     * Returns the first Text node which contains information from the XPath
254     * Filter String. We must use this stupid hook to enable the here() function
255     * to work.
256     *
257     * $todo$ I dunno whether this crashes: <XPath> he<!-- comment -->re()/ds:Signature[1]</XPath>
258     * @param type
259     * @return the first Text node which contains information from the XPath 2 Filter String
260     */

261    private Node JavaDoc getHereContextNode(String JavaDoc type) {
262
263       if (this.length(this.getBaseNamespace(), type) != 1) {
264          return null;
265       }
266
267       return XMLUtils.selectNodeText(this._constructionElement.getFirstChild(), this.getBaseNamespace(),
268                          type,0);
269    }
270
271    /**
272     * Method getHereContextNodeIncludeButSearch
273     *
274     * @return the string
275     */

276    public Node JavaDoc getHereContextNodeIncludeButSearch() {
277       return this
278          .getHereContextNode(XPathFilterCHGPContainer._TAG_INCLUDE_BUT_SEARCH);
279    }
280
281    /**
282     * Method getHereContextNodeExcludeButSearch
283     *
284     * @return the string
285     */

286    public Node JavaDoc getHereContextNodeExcludeButSearch() {
287       return this
288          .getHereContextNode(XPathFilterCHGPContainer._TAG_EXCLUDE_BUT_SEARCH);
289    }
290
291    /**
292     * Method getHereContextNodeExclude
293     *
294     * @return the string
295     */

296    public Node JavaDoc getHereContextNodeExclude() {
297       return this.getHereContextNode(XPathFilterCHGPContainer._TAG_EXCLUDE);
298    }
299
300    /**
301     * Method getBaseLocalName
302     *
303     * @inheritDoc
304     */

305    public final String JavaDoc getBaseLocalName() {
306       return XPathFilterCHGPContainer._TAG_XPATHCHGP;
307    }
308
309    /**
310     * Method getBaseNamespace
311     *
312     * @inheritDoc
313     */

314    public final String JavaDoc getBaseNamespace() {
315       return Transforms.TRANSFORM_XPATHFILTERCHGP;
316    }
317 }
318
Popular Tags