KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > search > basic > NotNormalizer


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/share/org/apache/slide/search/basic/NotNormalizer.java,v 1.6 2004/07/28 09:35:01 ib Exp $
3  * $Revision: 1.6 $
4  * $Date: 2004/07/28 09:35:01 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * 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, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23 package org.apache.slide.search.basic;
24
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27
28 import org.apache.slide.content.NodeProperty.NamespaceCache;
29 import org.apache.slide.search.BadQueryException;
30 import org.apache.slide.search.InvalidQueryException;
31 import org.jdom.Attribute;
32 import org.jdom.Element;
33 import org.jdom.Namespace;
34
35 /**
36  * This class recursivly removes all <code>&lt;not&gt;</code> expressions
37  * and replaces their successors by appropriate negated expressions. Since
38  * an example explains more than a thousand words:
39  *
40  * <pre>
41  * &lt;D:not&gt;
42  * &lt;D:or&gt;
43  * &lt;D:lt&gt;
44  * &lt;D:prop&gt;
45  * &lt;D:getcontentlength&gt;
46  * &lt;/D:prop&gt;
47  * &lt;D:literal&gt;1000&lt;/D:literal&gt;
48  * &lt;/D:lt&gt;
49  * &lt;D:eq&gt;
50  * &lt;D:prop&gt;
51  * &lt;D:getcontenttype&gt;
52  * &lt;/D:prop&gt;
53  * &lt;D:literal&gt;text/xml&lt;/D:literal&gt;
54  * &lt;/D:eq&gt;
55  * &lt;/D:or&gt;
56  * &lt;/D:not&gt;
57  * </pre>
58  *
59  * will be transformed to
60  *
61  * <pre>
62  * &lt;D:and&gt;
63  * &lt;D:not-lt&gt;
64  * &lt;D:prop&gt;
65  * &lt;D:getcontentlength&gt;
66  * &lt;/D:prop&gt;
67  * &lt;D:literal&gt;1000&lt;/D:literal&gt;
68  * &lt;/D:not-lt&gt;
69  * &lt;D:not-eq&gt;
70  * &lt;D:prop&gt;
71  * &lt;D:getcontenttype&gt;
72  * &lt;/D:prop&gt;
73  * &lt;D:literal&gt;text/xml&lt;/D:literal&gt;
74  * &lt;/D:not-eq&gt;
75  * &lt;/D:and&gt;
76  * </pre>
77  *
78  * @version $Revision: 1.6 $
79  *
80  **/

81 public class NotNormalizer {
82     
83     /**
84      * The message of the {@link org.apache.slide.search.InvalidQueryException
85      * InvalidQueryException} which is thrown by {@link #getQueryWithoutNotExpression
86      * getQueryWithoutNotExpression} if a <code>&lt;not&gt;</code> expression has
87      * NOT exactly one successor.
88      */

89     public static final String JavaDoc BAD_NOT_EXPRESSION_EXCEPTION_MESSAGE =
90         "not expression must contain exactly one nested expression";
91     
92     /**
93      * Returns the transformed query where all <code>&lt;not&gt;</code> elements
94      * has been removed.
95      *
96      * @param expressionElement the (root) Element of the query to transform.
97      *
98      * @return the transformed query where all <code>&lt;not&gt;</code> elements
99      * has been removed.
100      *
101      * @throws BadQueryException if transformin the query failed
102      * (see {@link #BAD_NOT_EXPRESSION_EXCEPTION_MESSAGE
103      * BAD_NOT_EXPRESSION_EXCEPTION_MESSAGE}).
104      */

105     public Element getQueryWithoutNotExpression(Element expressionElement) throws BadQueryException {
106         return getProcessedElement(expressionElement, false);
107     }
108     
109     /**
110      * Returns the appropriate replacement for the given <code>expressionElement</code>.
111      * If <code>negate</code> is true, the Element has to be negated.
112      *
113      * @param expressionElement the Element for which to return the
114      * appropriate replacement.
115      * @param negate if <code>negate</code> is true, the given
116      * Element has to be negated.
117      *
118      * @return the appropriate replacement for the given <code>expressionElement</code>.
119      *
120      * @throws BadQueryException if the Element could not be processed.
121      */

122     protected Element getProcessedElement(Element expressionElement, boolean negate) throws BadQueryException {
123         
124         Element result = null;
125         
126         if ( Literals.NOT.equals(expressionElement.getName()) &&
127             NamespaceCache.DEFAULT_URI.equals(expressionElement.getNamespaceURI()) ) {
128             
129             List JavaDoc children = expressionElement.getChildren();
130             if (children.size() != 1) {
131                 throw new InvalidQueryException(BAD_NOT_EXPRESSION_EXCEPTION_MESSAGE);
132             }
133             return getProcessedElement((Element)children.get(0), ! negate);
134         }
135         
136         if (negate) {
137             result = getNegatedQueryElement(expressionElement);
138         }
139         else {
140             result = getNamedClone(expressionElement, expressionElement.getName(), expressionElement.getNamespace());
141         }
142         Iterator JavaDoc childrenIterator = expressionElement.getChildren().iterator();
143         while (childrenIterator.hasNext()) {
144             result.addContent(getProcessedElement((Element)childrenIterator.next(), negate));
145         }
146         
147         return result;
148     }
149     
150     /**
151      * Returns the negation of the given <code>expressionElement</code>.
152      *
153      * @param expressionElement the Element to return the appropriate
154      * negation for.
155      *
156      * @return the negation of the given <code>expressionElement</code>.
157      *
158      * @throws BadQueryException if the Element could not be processed.
159      */

160     protected Element getNegatedQueryElement(Element expressionElement) throws BadQueryException {
161         
162         if (NamespaceCache.DEFAULT_URI.equals(expressionElement.getNamespaceURI())) {
163             return getNegatedDAVQueryElement(expressionElement);
164         }
165         if (NamespaceCache.SLIDE_URI.equals(expressionElement.getNamespaceURI())) {
166             return getNegatedSlideQueryElement(expressionElement);
167         }
168         else {
169             return getNegatedUnknownQueryElement(expressionElement);
170         }
171     }
172     
173     
174     /**
175      * Returns the negation of the given <code>expressionElement</code>,
176      * which is neither in <code>DAV:</code> nor
177      * <code>http://jakarta.apache.org/slide/</code> namespace.
178      *
179      * @param expressionElement the Element to return the appropriate
180      * negation for.
181      *
182      * @return the negation of the given <code>expressionElement</code>.
183      *
184      * @throws BadQueryException if the Element could not be processed.
185      */

186     protected Element getNegatedUnknownQueryElement(Element expressionElement) throws BadQueryException {
187         return getNamedClone(expressionElement, expressionElement.getName(), expressionElement.getNamespace());
188     }
189     
190     /**
191      * Returns the negation of the given <code>expressionElement</code>,
192      * which is in the <code>DAV:</code> namespace.
193      *
194      * @param expressionElement the Element to return the appropriate
195      * negation for.
196      *
197      * @return the negation of the given <code>expressionElement</code>.
198      *
199      * @throws BadQueryException if the Element could not be processed.
200      */

201     protected Element getNegatedDAVQueryElement(Element expressionElement) throws BadQueryException {
202         
203         String JavaDoc name = expressionElement.getName();
204         
205         if (Literals.AND.equals(name)) {
206             return getNamedClone(expressionElement, Literals.OR, NamespaceCache.DEFAULT_NAMESPACE);
207         }
208         if (Literals.OR.equals(name)) {
209             return getNamedClone(expressionElement, Literals.AND, NamespaceCache.DEFAULT_NAMESPACE);
210         }
211         if (Literals.GT.equals(name)) {
212             return getNamedClone(expressionElement, Literals.NOT_GT, NamespaceCache.DEFAULT_NAMESPACE);
213         }
214         if (Literals.NOT_GT.equals(name)) {
215             return getNamedClone(expressionElement, Literals.GT, NamespaceCache.DEFAULT_NAMESPACE);
216         }
217         if (Literals.GTE.equals(name)) {
218             return getNamedClone(expressionElement, Literals.NOT_GTE, NamespaceCache.DEFAULT_NAMESPACE);
219         }
220         if (Literals.NOT_GTE.equals(name)) {
221             return getNamedClone(expressionElement, Literals.GTE, NamespaceCache.DEFAULT_NAMESPACE);
222         }
223         if (Literals.LT.equals(name)) {
224             return getNamedClone(expressionElement, Literals.NOT_LT, NamespaceCache.DEFAULT_NAMESPACE);
225         }
226         if (Literals.NOT_LT.equals(name)) {
227             return getNamedClone(expressionElement, Literals.LT, NamespaceCache.DEFAULT_NAMESPACE);
228         }
229         if (Literals.LTE.equals(name)) {
230             return getNamedClone(expressionElement, Literals.NOT_LTE, NamespaceCache.DEFAULT_NAMESPACE);
231         }
232         if (Literals.NOT_LTE.equals(name)) {
233             return getNamedClone(expressionElement, Literals.LTE, NamespaceCache.DEFAULT_NAMESPACE);
234         }
235         if (Literals.EQ.equals(name)) {
236             return getNamedClone(expressionElement, Literals.NOT_EQ, NamespaceCache.DEFAULT_NAMESPACE);
237         }
238         if (Literals.NOT_EQ.equals(name)) {
239             return getNamedClone(expressionElement, Literals.EQ, NamespaceCache.DEFAULT_NAMESPACE);
240         }
241         if (Literals.CONTAINS.equals(name)) {
242             return getNamedClone(expressionElement, Literals.NOT_CONTAINS, NamespaceCache.DEFAULT_NAMESPACE);
243         }
244         if (Literals.NOT_CONTAINS.equals(name)) {
245             return getNamedClone(expressionElement, Literals.CONTAINS, NamespaceCache.DEFAULT_NAMESPACE);
246         }
247         if (Literals.ISCOLLECTION.equals(name)) {
248             return getNamedClone(expressionElement, Literals.NOT_ISCOLLECTION, NamespaceCache.DEFAULT_NAMESPACE);
249         }
250         if (Literals.NOT_ISCOLLECTION.equals(name)) {
251             return getNamedClone(expressionElement, Literals.ISCOLLECTION, NamespaceCache.DEFAULT_NAMESPACE);
252         }
253         if (Literals.ISDEFINED.equals(name)) {
254             return getNamedClone(expressionElement, Literals.NOT_ISDEFINED, NamespaceCache.DEFAULT_NAMESPACE);
255         }
256         if (Literals.NOT_ISDEFINED.equals(name)) {
257             return getNamedClone(expressionElement, Literals.ISDEFINED, NamespaceCache.DEFAULT_NAMESPACE);
258         }
259         return getNamedClone(expressionElement, expressionElement.getName(), expressionElement.getNamespace());
260     }
261     
262     /**
263      * Returns the negation of the given <code>expressionElement</code>,
264      * which is in the <code>http://jakarta.apache.org/slide/</code> namespace.
265      *
266      * @param expressionElement the Element to return the appropriate
267      * negation for.
268      *
269      * @return the negation of the given <code>expressionElement</code>.
270      *
271      * @throws BadQueryException if the Element could not be processed.
272      */

273     protected Element getNegatedSlideQueryElement(Element expressionElement) throws BadQueryException {
274         
275         String JavaDoc name = expressionElement.getName();
276         
277         if (Literals.ISPRINCIPAL.equals(name)) {
278             return getNamedClone(expressionElement, Literals.NOT_ISPRINCIPAL, NamespaceCache.SLIDE_NAMESPACE);
279         }
280         if (Literals.NOT_ISPRINCIPAL.equals(name)) {
281             return getNamedClone(expressionElement, Literals.ISPRINCIPAL, NamespaceCache.SLIDE_NAMESPACE);
282         }
283         if (Literals.PROPCONTAINS.equals(name)) {
284             return getNamedClone(expressionElement, Literals.NOT_PROPCONTAINS, NamespaceCache.SLIDE_NAMESPACE);
285         }
286         if (Literals.NOT_PROPCONTAINS.equals(name)) {
287             return getNamedClone(expressionElement, Literals.PROPCONTAINS, NamespaceCache.SLIDE_NAMESPACE);
288         }
289         return getNamedClone(expressionElement, expressionElement.getName(), expressionElement.getNamespace());
290     }
291     
292     /**
293      * Creates a new Element with the given <code>newName</code> in the
294      * <code>newNamespace</code> and sets the value (text) and attributes
295      * of the given <code>source</code> element.
296      *
297      * @param source the Element from which to copy the value and attributes.
298      * @param newName the name to use for the Element to create.
299      * @param newNamespace the Namespace to use for the Element to create.
300      *
301      * @return the created Element.
302      */

303     protected Element getNamedClone(Element source, String JavaDoc newName, Namespace newNamespace) {
304         
305         Element copy = new Element(newName, newNamespace);
306         copy.setText(source.getText());
307         Iterator JavaDoc it = source.getAttributes().iterator();
308     
309         while (it.hasNext()) {
310             Attribute attr = (Attribute)it.next();
311             copy.setAttribute ((Attribute)attr.clone());
312         }
313         
314         return copy;
315     }
316 }
317
318
Popular Tags