001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.webdav.client.methods.dasl;
020:
021: import java.util.*;
022:
023: import javax.xml.parsers.*;
024:
025: import org.openharmonise.vfs.search.*;
026: import org.openharmonise.webdav.client.*;
027: import org.w3c.dom.*;
028:
029: /**
030: * Builds WebDAV DASL search request XML from a virtual file system
031: * search {@link org.openharmonise.vfs.search.Query} object.
032: *
033: * @author Matthew Large
034: * @version $Revision: 1.1 $
035: *
036: */
037: public class SearchBuilder {
038:
039: /**
040: * Iniital path of search.
041: */
042: private String m_sInitialPath = "";
043:
044: /**
045: * Constructs a new search builder.
046: *
047: * @param sInitialPath Initial path
048: */
049: public SearchBuilder(String sInitialPath) {
050: super ();
051: this .m_sInitialPath = sInitialPath;
052: }
053:
054: /**
055: * Builds DASL search XML from virtual file system query.
056: *
057: * @param query Query
058: * @return Root DASL search element
059: */
060: public Element buildSearchXML(Query query) {
061: Element elSearch = null;
062:
063: DocumentBuilderFactory factory = DocumentBuilderFactory
064: .newInstance();
065: factory.setNamespaceAware(true);
066: Document xmlDoc = null;
067: try {
068: xmlDoc = factory.newDocumentBuilder().newDocument();
069: } catch (ParserConfigurationException e) {
070: e.printStackTrace();
071: }
072:
073: elSearch = xmlDoc.createElementNS(
074: AbstractWebDAVMethod.WEBDAV_NAMESPACE, "basicsearch");
075: xmlDoc.appendChild(elSearch);
076:
077: elSearch.appendChild(this .buildSelect(query
078: .getSelectProperties(), xmlDoc));
079: elSearch.appendChild(this .buildFrom(query.getScopes(), xmlDoc));
080: elSearch.appendChild(this .buildWhere(
081: query.getConditionGroups(), xmlDoc));
082: elSearch.appendChild(this .buildOrderBy(query.getOrders(),
083: xmlDoc));
084:
085: return elSearch;
086: }
087:
088: /**
089: * Builds the select XML.
090: *
091: * @param selects List of {@link Select} objects
092: * @param xmlDoc Owning XML document
093: * @return Root of select XML
094: */
095: private Element buildSelect(List selects, Document xmlDoc) {
096: Element elSelect = xmlDoc.createElementNS(
097: AbstractWebDAVMethod.WEBDAV_NAMESPACE, "select");
098:
099: Iterator itor = selects.iterator();
100: while (itor.hasNext()) {
101: Select select = (Select) itor.next();
102: Element elProp = xmlDoc.createElementNS(
103: AbstractWebDAVMethod.WEBDAV_NAMESPACE, "prop");
104: elSelect.appendChild(elProp);
105: Element elPropElement = xmlDoc.createElementNS(select
106: .getPropNamespace(), select.getPropName());
107: elProp.appendChild(elPropElement);
108: }
109:
110: return elSelect;
111: }
112:
113: /**
114: * Builds the from XML.
115: *
116: * @param scopes List of {@link Scope} objects
117: * @param xmlDoc Owning XML document
118: * @return Root of from XML
119: */
120: private Element buildFrom(List scopes, Document xmlDoc) {
121: Element elFrom = xmlDoc.createElementNS(
122: AbstractWebDAVMethod.WEBDAV_NAMESPACE, "from");
123:
124: Iterator itor = scopes.iterator();
125: while (itor.hasNext()) {
126: Scope scope = (Scope) itor.next();
127: Element elScope = xmlDoc.createElementNS(
128: AbstractWebDAVMethod.WEBDAV_NAMESPACE, "scope");
129: elFrom.appendChild(elScope);
130:
131: Element elHREF = xmlDoc.createElementNS(
132: AbstractWebDAVMethod.WEBDAV_NAMESPACE, "href");
133: Text txt = xmlDoc.createTextNode(this .m_sInitialPath
134: + scope.getDir());
135: elHREF.appendChild(txt);
136: elScope.appendChild(elHREF);
137:
138: Element elDepth = xmlDoc.createElementNS(
139: AbstractWebDAVMethod.WEBDAV_NAMESPACE, "depth");
140: if (scope.includeSubDirs()) {
141: txt = xmlDoc.createTextNode("infinity");
142: elDepth.appendChild(txt);
143: } else {
144: txt = xmlDoc.createTextNode("1");
145: elDepth.appendChild(txt);
146: }
147: elScope.appendChild(elDepth);
148: }
149:
150: return elFrom;
151: }
152:
153: /**
154: * Builds the where XML.
155: *
156: * @param conditionGroups List of {@link ConditionGroup} objects
157: * @param xmlDoc Owning XML document
158: * @return Root of where XML
159: */
160: private Element buildWhere(List conditionGroups, Document xmlDoc) {
161: Element elWhere = xmlDoc.createElementNS(
162: AbstractWebDAVMethod.WEBDAV_NAMESPACE, "where");
163:
164: Iterator itor = conditionGroups.iterator();
165: while (itor.hasNext()) {
166: ConditionGroup group = (ConditionGroup) itor.next();
167: elWhere.appendChild(this .processConditionGroup(group,
168: xmlDoc));
169: }
170:
171: return elWhere;
172: }
173:
174: /**
175: * Builds the where XML.
176: *
177: * @param group Condition group
178: * @param xmlDoc Owning XML document
179: * @return Root of where XML
180: */
181: private Element processConditionGroup(ConditionGroup group,
182: Document xmlDoc) {
183: Element elRetn = null;
184:
185: if (group.getType().equals(ConditionGroup.OR)) {
186: elRetn = xmlDoc.createElementNS(
187: AbstractWebDAVMethod.WEBDAV_NAMESPACE, "or");
188: } else {
189: elRetn = xmlDoc.createElementNS(
190: AbstractWebDAVMethod.WEBDAV_NAMESPACE, "and");
191: }
192:
193: Iterator itor = group.getContentConditions().iterator();
194: while (itor.hasNext()) {
195: ContentCondition condition = (ContentCondition) itor.next();
196: elRetn.appendChild(this .processContentCondition(condition,
197: xmlDoc));
198: }
199:
200: itor = group.getPropertyConditions().iterator();
201: while (itor.hasNext()) {
202: PropertyCondition condition = (PropertyCondition) itor
203: .next();
204: elRetn.appendChild(this .processPropertyCondition(condition,
205: xmlDoc));
206: }
207:
208: itor = group.getConditionGroups().iterator();
209: while (itor.hasNext()) {
210: ConditionGroup innerGroup = (ConditionGroup) itor.next();
211: elRetn.appendChild(this .processConditionGroup(innerGroup,
212: xmlDoc));
213: }
214:
215: return elRetn;
216: }
217:
218: /**
219: * Builds the where XML.
220: *
221: * @param condition Content condition
222: * @param xmlDoc Owning XML document
223: * @return Root of where XML
224: */
225: private Element processContentCondition(ContentCondition condition,
226: Document xmlDoc) {
227: Element elRetn = this .operatorTranslation(condition
228: .getOperator(), xmlDoc);
229:
230: Text txt = xmlDoc.createTextNode((String) condition.getValues()
231: .get(0));
232: elRetn.appendChild(txt);
233:
234: return elRetn;
235: }
236:
237: /**
238: * Builds the where XML.
239: *
240: * @param condition Property condition
241: * @param xmlDoc Owning XML document
242: * @return Root of where XML
243: */
244: private Element processPropertyCondition(
245: PropertyCondition condition, Document xmlDoc) {
246: Element elRetn = this .operatorTranslation(condition
247: .getOperator(), xmlDoc);
248:
249: Element elProp = xmlDoc.createElementNS(
250: AbstractWebDAVMethod.WEBDAV_NAMESPACE, "prop");
251: elRetn.appendChild(elProp);
252: Element elPropElement = xmlDoc.createElementNS(condition
253: .getPropNamespaceURI(), condition.getPropName());
254: elProp.appendChild(elPropElement);
255: Element elLiteral = xmlDoc.createElementNS(
256: AbstractWebDAVMethod.WEBDAV_NAMESPACE, "literal");
257: elRetn.appendChild(elLiteral);
258: Text txt = xmlDoc.createTextNode((String) condition.getValues()
259: .get(0));
260: elLiteral.appendChild(txt);
261:
262: return elRetn;
263: }
264:
265: /**
266: * Translated operators into DASL type operator elements.
267: *
268: * @param sOperator Opterator
269: * @param xmlDoc Owning XML document
270: * @return Operator element
271: */
272: private Element operatorTranslation(String sOperator,
273: Document xmlDoc) {
274: Element elRetn = null;
275:
276: if (sOperator.equals(">")) {
277: elRetn = xmlDoc.createElementNS(
278: AbstractWebDAVMethod.WEBDAV_NAMESPACE, "gt");
279: } else if (sOperator.equals("<")) {
280: elRetn = xmlDoc.createElementNS(
281: AbstractWebDAVMethod.WEBDAV_NAMESPACE, "lt");
282: } else if (sOperator.equals("<=")) {
283: elRetn = xmlDoc.createElementNS(
284: AbstractWebDAVMethod.WEBDAV_NAMESPACE, "lte");
285: } else if (sOperator.equals(">=")) {
286: elRetn = xmlDoc.createElementNS(
287: AbstractWebDAVMethod.WEBDAV_NAMESPACE, "gte");
288: } else if (sOperator.equals("contains")) {
289: elRetn = xmlDoc.createElementNS(
290: AbstractWebDAVMethod.WEBDAV_NAMESPACE, "contains");
291: } else {
292: elRetn = xmlDoc.createElementNS(
293: AbstractWebDAVMethod.WEBDAV_NAMESPACE, "eq");
294: }
295:
296: return elRetn;
297: }
298:
299: /**
300: * Builds the order XML.
301: *
302: * @param orders List of {@link Order} objects
303: * @param xmlDoc Owning XML document
304: * @return Root of order XML
305: */
306: private Element buildOrderBy(List orders, Document xmlDoc) {
307: Element elOrderBy = xmlDoc.createElementNS(
308: AbstractWebDAVMethod.WEBDAV_NAMESPACE, "orderby");
309:
310: Iterator itor = orders.iterator();
311: while (itor.hasNext()) {
312: Order order = (Order) itor.next();
313: elOrderBy.appendChild(this .processOrder(order, xmlDoc));
314: }
315:
316: return elOrderBy;
317: }
318:
319: /**
320: * Builds the order XML.
321: *
322: * @param order Order
323: * @param xmlDoc Owning XML document
324: * @return Root of order XML
325: */
326: private Element processOrder(Order order, Document xmlDoc) {
327: Element elRetn = xmlDoc.createElementNS(
328: AbstractWebDAVMethod.WEBDAV_NAMESPACE, "order");
329:
330: Element elProp = xmlDoc.createElementNS(
331: AbstractWebDAVMethod.WEBDAV_NAMESPACE, "prop");
332: elRetn.appendChild(elProp);
333: Element elPropElement = xmlDoc.createElementNS(order
334: .getPropNamespaceURI(), order.getPropName());
335: elProp.appendChild(elPropElement);
336:
337: Element elDirection = null;
338: if (order.getDirection().equals(Order.ASC)) {
339: elDirection = xmlDoc.createElementNS(
340: AbstractWebDAVMethod.WEBDAV_NAMESPACE, "ascending");
341: } else {
342: elDirection = xmlDoc
343: .createElementNS(
344: AbstractWebDAVMethod.WEBDAV_NAMESPACE,
345: "descending");
346: }
347: elRetn.appendChild(elDirection);
348:
349: return elRetn;
350: }
351:
352: }
|