KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > search > basic > expression > ComparedProperty


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/share/org/apache/slide/search/basic/expression/ComparedProperty.java,v 1.4 2004/07/28 09:34:50 ib Exp $
3  * $Revision: 1.4 $
4  * $Date: 2004/07/28 09:34:50 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2002 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
24 package org.apache.slide.search.basic.expression;
25 import org.apache.slide.search.basic.*;
26
27 import org.jdom.Element;
28 import org.apache.slide.search.InvalidQueryException;
29 import org.apache.slide.content.NodeProperty.NamespaceCache;
30 import org.apache.slide.common.PropertyName;
31
32 import java.util.List JavaDoc;
33 import java.util.ArrayList JavaDoc;
34
35 /**
36  * represents a property, that is to be compared against a literal
37  *
38  * @version $Revision: 1.4 $
39  */

40 public class ComparedProperty {
41     
42     /** a list of known properties, that have a numeric type */
43     private static final List JavaDoc NUMERIC_PROPERTIES = new ArrayList JavaDoc ();
44     static {
45         NUMERIC_PROPERTIES.add (new PropertyName ("getcontentlength",
46                                                   NamespaceCache.DEFAULT_URI));
47     }
48     
49     /** the property's value <literal> */
50     protected String JavaDoc literal;
51     public void setLiteral (String JavaDoc literal) {
52         this.literal = literal;
53     }
54     public String JavaDoc getLiteral() {
55         return literal;
56     }
57     
58     /** the property name <prop> */
59     protected String JavaDoc property;
60     public void setProperty (String JavaDoc p) {
61         property = p;
62     }
63     public String JavaDoc getProperty() {
64         return property;
65     }
66     
67     /** the property's namespace */
68     protected String JavaDoc propNamespace;
69     public void setPropNamespace (String JavaDoc p) {
70         propNamespace = p;
71     }
72     public String JavaDoc getPropNamespace() {
73         return propNamespace;
74     }
75     
76     /** indicate, if compare should be casesensitive */
77     protected boolean casesensitive = true;
78     public boolean getCasesensitive() {
79         return casesensitive;
80     }
81     
82     
83     /**
84      * allows to create a ComparedProperty without parameters.
85      * property, namespace and literal msut be set with set methods
86      */

87     public ComparedProperty () {
88     }
89     
90     /**
91      * Creates a ComparedProperty with the given <code>propName</code>
92      * and <code>propNamespace</code>.
93      *
94      * @param property the property.
95      * @param propNamespace the property namespace.
96      */

97     public ComparedProperty (String JavaDoc property, String JavaDoc propNamespace) {
98         setProperty(property);
99         setPropNamespace(propNamespace);
100     }
101     
102     
103     /**
104      * Creates a compare expression according to Element e
105      *
106      * @param e jdom element, that describes the expression
107      *
108      */

109     public ComparedProperty (Element e) throws InvalidQueryException {
110         this (e, true);
111     }
112     
113     /**
114      * Creates a compare expression according to Element e.
115      *
116      * @param e jdom element, that describes the expression
117      * @param withLiteral indicates, if a literal is supplied or not (for example:
118      * iscollectionhas no literal)
119      *
120      */

121     public ComparedProperty (Element e, boolean withLiteral) throws InvalidQueryException {
122         setProperty(e);
123         if (withLiteral) {
124             literal = getLiteral (e);
125         }
126     }
127     
128     /**
129      * Indicates, if this property shall be treated as numeric in compare
130      *
131      * @return a boolean
132      *
133      */

134     public boolean isNumeric () {
135         return NUMERIC_PROPERTIES.contains (new PropertyName (property, propNamespace));
136     }
137     
138     /**
139      * extracts the property name of this expression
140      *
141      * @param e an Expression
142      *
143      * @return the property name as string
144      *
145      * @throws InvalidQueryException if no or more than one
146      * property was supplied
147      *
148      */

149     private void setProperty (Element e) throws InvalidQueryException {
150         String JavaDoc casesensAttr = e.getAttributeValue (Literals.CASESENSITIVE, NamespaceCache.DEFAULT_NAMESPACE);
151         if (casesensAttr != null) {
152             if (casesensAttr.equals ("1"))
153                 casesensitive = true;
154             else if (casesensAttr.equals ("0"))
155                 casesensitive = false;
156             else
157                 throw new InvalidQueryException ("casesensitive must be either \"0\" or \"1\"");
158         }
159         
160         Element propListElement = e.getChild (Literals.PROP, NamespaceCache.DEFAULT_NAMESPACE);
161         if (propListElement == null)
162             throw new InvalidQueryException
163                 ("No property element supplied");
164         
165         List JavaDoc propList = propListElement.getChildren();
166         
167         if (propList.size() != 1)
168             throw new InvalidQueryException
169                 ("Expected exactly 1 prop element, found " + propList.size());
170         
171         Element propElement = ((Element)propList.get(0));
172         property = propElement.getName();
173         propNamespace = propElement.getNamespace().getURI();
174     }
175     /**
176      * extracs the value of <literal> of an expression
177      *
178      * @param e an Expression
179      *
180      * @return the literal as string
181      *
182      * @throws InvalidQueryException if no <literal> found in e
183      *
184      */

185     private String JavaDoc getLiteral(Element e) throws InvalidQueryException {
186         Element lit = e.getChild (Literals.LITERAL, NamespaceCache.DEFAULT_NAMESPACE);
187         if (lit == null)
188             throw new InvalidQueryException
189                 ("No literal element supplied");
190         
191         return lit.getText ();
192     }
193 }
194
195
Popular Tags