KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > webdav > client > methods > dasl > SearchBuilder


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.webdav.client.methods.dasl;
20
21 import java.util.*;
22
23 import javax.xml.parsers.*;
24
25 import org.openharmonise.vfs.search.*;
26 import org.openharmonise.webdav.client.*;
27 import org.w3c.dom.*;
28
29
30 /**
31  * Builds WebDAV DASL search request XML from a virtual file system
32  * search {@link org.openharmonise.vfs.search.Query} object.
33  *
34  * @author Matthew Large
35  * @version $Revision: 1.1 $
36  *
37  */

38 public class SearchBuilder {
39
40     /**
41      * Iniital path of search.
42      */

43     private String JavaDoc m_sInitialPath = "";
44
45     /**
46      * Constructs a new search builder.
47      *
48      * @param sInitialPath Initial path
49      */

50     public SearchBuilder(String JavaDoc sInitialPath) {
51         super();
52         this.m_sInitialPath = sInitialPath;
53     }
54     
55     /**
56      * Builds DASL search XML from virtual file system query.
57      *
58      * @param query Query
59      * @return Root DASL search element
60      */

61     public Element buildSearchXML(Query query) {
62         Element elSearch = null;
63         
64         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
65         factory.setNamespaceAware(true);
66         Document xmlDoc = null;
67         try {
68             xmlDoc = factory.newDocumentBuilder().newDocument();
69         } catch (ParserConfigurationException e) {
70             e.printStackTrace();
71         }
72         
73         elSearch = xmlDoc.createElementNS(AbstractWebDAVMethod.WEBDAV_NAMESPACE, "basicsearch");
74         xmlDoc.appendChild(elSearch);
75         
76         elSearch.appendChild( this.buildSelect( query.getSelectProperties(), xmlDoc));
77         elSearch.appendChild( this.buildFrom( query.getScopes(), xmlDoc));
78         elSearch.appendChild( this.buildWhere( query.getConditionGroups(), xmlDoc));
79         elSearch.appendChild( this.buildOrderBy( query.getOrders(), xmlDoc));
80         
81         return elSearch;
82     }
83     
84     /**
85      * Builds the select XML.
86      *
87      * @param selects List of {@link Select} objects
88      * @param xmlDoc Owning XML document
89      * @return Root of select XML
90      */

91     private Element buildSelect(List selects, Document xmlDoc) {
92         Element elSelect = xmlDoc.createElementNS(AbstractWebDAVMethod.WEBDAV_NAMESPACE, "select");
93         
94         Iterator itor = selects.iterator();
95         while(itor.hasNext()) {
96             Select select = (Select)itor.next();
97             Element elProp = xmlDoc.createElementNS(AbstractWebDAVMethod.WEBDAV_NAMESPACE, "prop");
98             elSelect.appendChild(elProp);
99             Element elPropElement = xmlDoc.createElementNS(select.getPropNamespace(), select.getPropName());
100             elProp.appendChild(elPropElement);
101         }
102         
103         return elSelect;
104     }
105     
106     /**
107      * Builds the from XML.
108      *
109      * @param scopes List of {@link Scope} objects
110      * @param xmlDoc Owning XML document
111      * @return Root of from XML
112      */

113     private Element buildFrom(List scopes, Document xmlDoc) {
114         Element elFrom = xmlDoc.createElementNS(AbstractWebDAVMethod.WEBDAV_NAMESPACE, "from");
115         
116         Iterator itor = scopes.iterator();
117         while(itor.hasNext()) {
118             Scope scope = (Scope)itor.next();
119             Element elScope = xmlDoc.createElementNS(AbstractWebDAVMethod.WEBDAV_NAMESPACE, "scope");
120             elFrom.appendChild(elScope);
121             
122             Element elHREF = xmlDoc.createElementNS(AbstractWebDAVMethod.WEBDAV_NAMESPACE, "href");
123             Text txt = xmlDoc.createTextNode( this.m_sInitialPath + scope.getDir());
124             elHREF.appendChild(txt);
125             elScope.appendChild(elHREF);
126             
127             Element elDepth = xmlDoc.createElementNS(AbstractWebDAVMethod.WEBDAV_NAMESPACE, "depth");
128             if( scope.includeSubDirs() ) {
129                 txt = xmlDoc.createTextNode("infinity");
130                 elDepth.appendChild(txt);
131             } else {
132                 txt = xmlDoc.createTextNode("1");
133                 elDepth.appendChild(txt);
134             }
135             elScope.appendChild(elDepth);
136         }
137         
138         return elFrom;
139     }
140     
141     /**
142      * Builds the where XML.
143      *
144      * @param conditionGroups List of {@link ConditionGroup} objects
145      * @param xmlDoc Owning XML document
146      * @return Root of where XML
147      */

148     private Element buildWhere(List conditionGroups, Document xmlDoc) {
149         Element elWhere = xmlDoc.createElementNS(AbstractWebDAVMethod.WEBDAV_NAMESPACE, "where");
150         
151         Iterator itor = conditionGroups.iterator();
152         while(itor.hasNext()) {
153             ConditionGroup group = (ConditionGroup)itor.next();
154             elWhere.appendChild( this.processConditionGroup(group, xmlDoc) );
155         }
156         
157         return elWhere;
158     }
159     
160     /**
161      * Builds the where XML.
162      *
163      * @param group Condition group
164      * @param xmlDoc Owning XML document
165      * @return Root of where XML
166      */

167     private Element processConditionGroup(ConditionGroup group, Document xmlDoc) {
168         Element elRetn = null;
169         
170         if(group.getType().equals(ConditionGroup.OR)) {
171             elRetn = xmlDoc.createElementNS(AbstractWebDAVMethod.WEBDAV_NAMESPACE, "or");
172         } else {
173             elRetn = xmlDoc.createElementNS(AbstractWebDAVMethod.WEBDAV_NAMESPACE, "and");
174         }
175         
176         Iterator itor = group.getContentConditions().iterator();
177         while(itor.hasNext()) {
178             ContentCondition condition = (ContentCondition)itor.next();
179             elRetn.appendChild( this.processContentCondition(condition, xmlDoc) );
180         }
181         
182         itor = group.getPropertyConditions().iterator();
183         while(itor.hasNext()) {
184             PropertyCondition condition = (PropertyCondition)itor.next();
185             elRetn.appendChild( this.processPropertyCondition(condition, xmlDoc) );
186         }
187         
188         itor = group.getConditionGroups().iterator();
189         while(itor.hasNext()) {
190             ConditionGroup innerGroup = (ConditionGroup)itor.next();
191             elRetn.appendChild( this.processConditionGroup(innerGroup, xmlDoc) );
192         }
193         
194         return elRetn;
195     }
196     
197     /**
198      * Builds the where XML.
199      *
200      * @param condition Content condition
201      * @param xmlDoc Owning XML document
202      * @return Root of where XML
203      */

204     private Element processContentCondition(ContentCondition condition, Document xmlDoc) {
205         Element elRetn = this.operatorTranslation( condition.getOperator(), xmlDoc);
206         
207         Text txt = xmlDoc.createTextNode((String JavaDoc)condition.getValues().get(0));
208         elRetn.appendChild(txt);
209         
210         return elRetn;
211     }
212     
213     /**
214      * Builds the where XML.
215      *
216      * @param condition Property condition
217      * @param xmlDoc Owning XML document
218      * @return Root of where XML
219      */

220     private Element processPropertyCondition(PropertyCondition condition, Document xmlDoc) {
221         Element elRetn = this.operatorTranslation( condition.getOperator(), xmlDoc);
222         
223         Element elProp = xmlDoc.createElementNS(AbstractWebDAVMethod.WEBDAV_NAMESPACE, "prop");
224         elRetn.appendChild(elProp);
225         Element elPropElement = xmlDoc.createElementNS(condition.getPropNamespaceURI(), condition.getPropName());
226         elProp.appendChild(elPropElement);
227         Element elLiteral = xmlDoc.createElementNS(AbstractWebDAVMethod.WEBDAV_NAMESPACE, "literal");
228         elRetn.appendChild(elLiteral);
229         Text txt = xmlDoc.createTextNode((String JavaDoc)condition.getValues().get(0));
230         elLiteral.appendChild(txt);
231         
232         return elRetn;
233     }
234     
235     /**
236      * Translated operators into DASL type operator elements.
237      *
238      * @param sOperator Opterator
239      * @param xmlDoc Owning XML document
240      * @return Operator element
241      */

242     private Element operatorTranslation(String JavaDoc sOperator, Document xmlDoc) {
243         Element elRetn = null;
244         
245         if(sOperator.equals(">")) {
246             elRetn = xmlDoc.createElementNS(AbstractWebDAVMethod.WEBDAV_NAMESPACE, "gt");
247         } else if(sOperator.equals("<")) {
248             elRetn = xmlDoc.createElementNS(AbstractWebDAVMethod.WEBDAV_NAMESPACE, "lt");
249         } else if(sOperator.equals("<=")) {
250             elRetn = xmlDoc.createElementNS(AbstractWebDAVMethod.WEBDAV_NAMESPACE, "lte");
251         } else if(sOperator.equals(">=")) {
252             elRetn = xmlDoc.createElementNS(AbstractWebDAVMethod.WEBDAV_NAMESPACE, "gte");
253         } else if(sOperator.equals("contains")) {
254             elRetn = xmlDoc.createElementNS(AbstractWebDAVMethod.WEBDAV_NAMESPACE, "contains");
255         } else {
256             elRetn = xmlDoc.createElementNS(AbstractWebDAVMethod.WEBDAV_NAMESPACE, "eq");
257         }
258         
259         return elRetn;
260     }
261     
262     /**
263      * Builds the order XML.
264      *
265      * @param orders List of {@link Order} objects
266      * @param xmlDoc Owning XML document
267      * @return Root of order XML
268      */

269     private Element buildOrderBy(List orders, Document xmlDoc) {
270         Element elOrderBy = xmlDoc.createElementNS(AbstractWebDAVMethod.WEBDAV_NAMESPACE, "orderby");
271         
272         Iterator itor = orders.iterator();
273         while(itor.hasNext()) {
274             Order order = (Order)itor.next();
275             elOrderBy.appendChild( this.processOrder(order, xmlDoc) );
276         }
277         
278         return elOrderBy;
279     }
280     
281     /**
282      * Builds the order XML.
283      *
284      * @param order Order
285      * @param xmlDoc Owning XML document
286      * @return Root of order XML
287      */

288     private Element processOrder(Order order, Document xmlDoc) {
289         Element elRetn = xmlDoc.createElementNS(AbstractWebDAVMethod.WEBDAV_NAMESPACE, "order");
290         
291         Element elProp = xmlDoc.createElementNS(AbstractWebDAVMethod.WEBDAV_NAMESPACE, "prop");
292         elRetn.appendChild(elProp);
293         Element elPropElement = xmlDoc.createElementNS(order.getPropNamespaceURI(), order.getPropName());
294         elProp.appendChild(elPropElement);
295         
296         Element elDirection = null;
297         if( order.getDirection().equals(Order.ASC) ) {
298             elDirection = xmlDoc.createElementNS(AbstractWebDAVMethod.WEBDAV_NAMESPACE, "ascending");
299         } else {
300             elDirection = xmlDoc.createElementNS(AbstractWebDAVMethod.WEBDAV_NAMESPACE, "descending");
301         }
302         elRetn.appendChild(elDirection);
303         
304         return elRetn;
305     }
306
307 }
308
Popular Tags