001: /*
002: * (C) Copyright Simulacra Media Ltd, 2004. All rights reserved.
003: *
004: * The program is provided "AS IS" without any warranty express or
005: * implied, including the warranty of non-infringement and the implied
006: * warranties of merchantibility and fitness for a particular purpose.
007: * Simulacra Media Ltd will not be liable for any damages suffered by you as a result
008: * of using the Program. In no event will Simulacra Media Ltd be liable for any
009: * special, indirect or consequential damages or lost profits even if
010: * Simulacra Media Ltd has been advised of the possibility of their occurrence.
011: * Simulacra Media Ltd will not be liable for any third party claims against you.
012: */
013:
014: package com.ibm.webdav.basicsearch;
015:
016: import com.ibm.webdav.*;
017:
018: import java.net.URLDecoder;
019: import java.util.*;
020:
021: import org.w3c.dom.*;
022:
023: /**
024: * BasicSearchRequest implements the <code>SearchRequest</code>
025: * interface for the DASL basic search format.
026: *
027: * @author Michael Bell
028: * @version $Revision: 1.1 $
029: *
030: */
031: public class BasicSearchRequest implements SearchRequest {
032: public static final String TAG_BASICSEARCH = "basicsearch";
033: public static final String TAG_SELECT = "select";
034: public static final String TAG_FROM = "from";
035: public static final String TAG_WHERE = "where";
036: public static final String TAG_ORDERBY = "orderby";
037: public static final String TAG_ORDER = "order";
038: public static final String TAG_LIMIT = "limit";
039: public static final String TAG_PROP = "prop";
040: public static final String TAG_LITERAL = "literal";
041: public static final String TAG_ALLPROP = "allprop";
042: public static final String TAG_HREF = "href";
043: public static final String TAG_DEPTH = "depth";
044: public static final String TAG_DESC = "descending";
045: public static final String TAG_ASC = "ascending";
046: public static final String TAG_NRESULTS = "nresults";
047: private static final String DAV_NAMESPACE = "DAV:";
048: public static Hashtable LOG_OPS = new Hashtable();
049: public static Hashtable COMP_OPS = new Hashtable();
050: public static Hashtable STRING_OPS = new Hashtable();
051: public static Hashtable CONTENT_OPS = new Hashtable();
052: public static Hashtable SPECIAL_OPS = new Hashtable();
053: public static Hashtable ALL_OPS = new Hashtable();
054:
055: public static String OPERATOR_OR = "or";
056: public static String OPERATOR_AND = "and";
057:
058: static {
059: LOG_OPS.put(OPERATOR_AND, OPERATOR_AND);
060: LOG_OPS.put(OPERATOR_OR, OPERATOR_OR);
061:
062: COMP_OPS.put("eq", "=");
063: COMP_OPS.put("lt", "<");
064: COMP_OPS.put("gt", ">");
065: COMP_OPS.put("lte", "<=");
066: COMP_OPS.put("gte", ">=");
067:
068: STRING_OPS.put("like", "like");
069: CONTENT_OPS.put("contains", "contains");
070:
071: SPECIAL_OPS.put("isdefined", "isdefined");
072: SPECIAL_OPS.put("is-collection", "is-collection");
073:
074: ALL_OPS.putAll(LOG_OPS);
075: ALL_OPS.putAll(STRING_OPS);
076: ALL_OPS.putAll(CONTENT_OPS);
077: ALL_OPS.putAll(SPECIAL_OPS);
078: ALL_OPS.putAll(COMP_OPS);
079: }
080:
081: protected String m_scope_uri = null;
082: protected int m_scope_depth = 0;
083: protected boolean m_bSelectAllProps = false;
084: protected Vector m_select_props = new Vector();
085: protected SearchCondition m_condition = new SearchCondition();
086: protected Vector m_orderbyProps = new Vector();
087: protected Vector m_orderbyDirections = new Vector();
088: protected boolean m_bIncludeDefinitions = true;
089: protected int m_nLimit = -1;
090:
091: public BasicSearchRequest() {
092: }
093:
094: public void instantiateFromXML(Element xmlElement) throws Exception {
095: if (xmlElement.getLocalName().equals(
096: BasicSearchRequest.TAG_BASICSEARCH) == false) {
097: throw new Exception("Invalid Tag");
098: }
099:
100: NodeList nodes = xmlElement.getChildNodes();
101:
102: for (int i = 0; i < nodes.getLength(); i++) {
103: if (nodes.item(i).getNodeType() != Node.ELEMENT_NODE) {
104: continue;
105: }
106:
107: Element tempEl = (Element) nodes.item(i);
108: String sTagName = tempEl.getLocalName();
109:
110: if (sTagName.equals(BasicSearchRequest.TAG_SELECT)) {
111: NodeList props = tempEl.getElementsByTagNameNS(
112: DAV_NAMESPACE, BasicSearchRequest.TAG_PROP);
113:
114: if (props.getLength() > 0) {
115: for (int j = 0; j < props.getLength(); j++) {
116: Element propEl = (Element) props.item(j);
117:
118: NodeList nl = propEl.getElementsByTagName("*");
119:
120: if (nl.getLength() > 0) {
121: Element indpropEl = (Element) nl.item(0);
122: PropertyName pname = new PropertyName(
123: indpropEl);
124:
125: this .m_select_props.add(pname);
126: }
127: }
128: } else {
129: this .m_bSelectAllProps = true;
130: }
131: } else if (sTagName.equals(BasicSearchRequest.TAG_FROM)) {
132: Element depthEl = (Element) tempEl
133: .getElementsByTagNameNS(DAV_NAMESPACE,
134: BasicSearchRequest.TAG_DEPTH).item(0);
135: String sdepth = ((Text) depthEl.getFirstChild())
136: .getNodeValue();
137:
138: try {
139: this .m_scope_depth = Integer.parseInt(sdepth);
140: } catch (NumberFormatException e) {
141: m_scope_depth = -1;
142: }
143:
144: Element hrefEl = (Element) tempEl
145: .getElementsByTagNameNS(DAV_NAMESPACE,
146: BasicSearchRequest.TAG_HREF).item(0);
147:
148: this .m_scope_uri = ((Text) hrefEl.getFirstChild())
149: .getNodeValue();
150:
151: m_scope_uri = URLDecoder.decode(m_scope_uri, "UTF-8");
152:
153: } else if (sTagName.equals(BasicSearchRequest.TAG_WHERE)) {
154: Element condEl = (Element) tempEl.getElementsByTagName(
155: "*").item(0);
156:
157: m_condition = this .createCondition(condEl);
158: } else if (sTagName.equals(BasicSearchRequest.TAG_ORDERBY)) {
159: NodeList orderNodes = tempEl.getElementsByTagNameNS(
160: DAV_NAMESPACE, BasicSearchRequest.TAG_ORDER);
161:
162: for (int j = 0; j < orderNodes.getLength(); j++) {
163: Element orderEl = (Element) orderNodes.item(j);
164: Element propEl = (Element) orderEl
165: .getElementsByTagNameNS(DAV_NAMESPACE,
166: BasicSearchRequest.TAG_PROP)
167: .item(0);
168: this .m_orderbyProps.add(new PropertyName(
169: (Element) propEl.getElementsByTagName("*")
170: .item(0)));
171:
172: if (orderEl.getElementsByTagNameNS(DAV_NAMESPACE,
173: BasicSearchRequest.TAG_DESC).getLength() > 0) {
174: m_orderbyDirections
175: .add(SearchRequest.ORDER_DESC);
176: } else {
177: m_orderbyDirections
178: .add(SearchRequest.ORDER_ASC);
179: }
180: }
181: } else if (sTagName.equals(BasicSearchRequest.TAG_LIMIT)) {
182: Element numresults = (Element) tempEl
183: .getElementsByTagNameNS(DAV_NAMESPACE,
184: BasicSearchRequest.TAG_NRESULTS)
185: .item(0);
186: Text txt = (Text) numresults.getFirstChild();
187:
188: this .m_nLimit = Integer.parseInt(txt.getNodeValue());
189: }
190: }
191: }
192:
193: public int getResultLimit() {
194: return m_nLimit;
195: }
196:
197: public String getScopeURI() {
198: return m_scope_uri;
199: }
200:
201: public int getScopeDepth() {
202: return m_scope_depth;
203: }
204:
205: public SearchSchema getSearchSchema() throws Exception {
206: return new BasicSearchSchema();
207: }
208:
209: public SearchCondition getCondition() {
210: return this .m_condition;
211: }
212:
213: public Vector getSelectProperties() {
214: return this .m_select_props;
215: }
216:
217: public boolean isAllSelectProperties() {
218: return this .m_bSelectAllProps;
219: }
220:
221: public boolean isIncludePropertyDefinitions() {
222: return m_bIncludeDefinitions;
223: }
224:
225: public Vector getOrderByProperties() {
226: return this .m_orderbyProps;
227: }
228:
229: public String getOrderByDirection(PropertyName propName) {
230: return (String) this .m_orderbyDirections
231: .elementAt(this .m_orderbyProps.indexOf(propName));
232: }
233:
234: private SearchCondition createCondition(Element condEl)
235: throws Exception {
236: SearchCondition searchCond = new SearchCondition();
237: String sTagname = condEl.getLocalName();
238:
239: if (BasicSearchRequest.ALL_OPS.containsKey(sTagname) == false) {
240: throw new Exception("Invalid operator");
241: }
242:
243: if (BasicSearchRequest.LOG_OPS.containsKey(sTagname)) {
244: searchCond.setOperator(sTagname);
245:
246: NodeList nodes = condEl.getChildNodes();
247:
248: for (int i = 0; i < nodes.getLength(); i++) {
249: if (nodes.item(i).getNodeType() != Node.ELEMENT_NODE) {
250: continue;
251: }
252:
253: Element tempEl = (Element) nodes.item(i);
254:
255: if (LOG_OPS.containsKey(tempEl.getLocalName())) {
256: SearchCondition cond = createCondition(tempEl);
257: if (cond != null) {
258: searchCond.addCondition(cond);
259: }
260:
261: } else {
262: SearchConditionTerm term = createConditionTerm(tempEl);
263: if (term != null) {
264: searchCond.addCondition(term);
265: }
266: }
267: }
268: } else {
269: SearchConditionTerm term = createConditionTerm(condEl);
270:
271: if (term != null) {
272: searchCond.addCondition(term);
273: }
274: }
275:
276: return searchCond;
277: }
278:
279: private SearchConditionTerm createConditionTerm(Element condEl)
280: throws Exception {
281: SearchConditionTerm condTerm = null;
282: String sOperator = condEl.getLocalName();
283:
284: if (COMP_OPS.containsKey(sOperator)
285: || BasicSearchRequest.STRING_OPS.containsKey(sOperator)
286: || BasicSearchRequest.CONTENT_OPS
287: .containsKey(sOperator)
288: || BasicSearchRequest.SPECIAL_OPS
289: .containsKey(sOperator)) {
290: Element propEl = (Element) condEl.getElementsByTagNameNS(
291: DAV_NAMESPACE, BasicSearchRequest.TAG_PROP).item(0);
292:
293: if (propEl != null) {
294:
295: NodeList nodes = propEl.getElementsByTagName("*");
296:
297: if (nodes.getLength() > 0) {
298:
299: Element propPropEl = (Element) nodes.item(0);
300:
301: String sValue = null;
302:
303: if (BasicSearchRequest.SPECIAL_OPS
304: .containsKey(sOperator) == false) {
305: Element literalEl = (Element) condEl
306: .getElementsByTagNameNS(DAV_NAMESPACE,
307: BasicSearchRequest.TAG_LITERAL)
308: .item(0);
309: if (literalEl.getChildNodes().getLength() > 0) {
310: sValue = ((Text) literalEl.getFirstChild())
311: .getNodeValue();
312: }
313:
314: }
315:
316: if (sValue != null) {
317: condTerm = new SearchPropertyConditionTerm(
318: new PropertyName(propPropEl),
319: (String) BasicSearchRequest.ALL_OPS
320: .get(sOperator), sValue);
321: }
322:
323: }
324: } else {
325: String sVal = condEl.getFirstChild().getNodeValue();
326:
327: condTerm = new SearchConditionTerm(sOperator, sVal);
328:
329: }
330: } else {
331: throw new Exception("InvalidOperator - " + sOperator);
332: }
333:
334: return condTerm;
335: }
336:
337: private void addOrder(Element orderEl) {
338: String sTagname = orderEl.getLocalName();
339:
340: if (sTagname.equals(BasicSearchRequest.TAG_ORDER)) {
341: Element propEl = (Element) orderEl.getElementsByTagNameNS(
342: DAV_NAMESPACE, BasicSearchRequest.TAG_PROP).item(0);
343:
344: Element propPropEl = (Element) propEl.getElementsByTagName(
345: "*").item(0);
346: this .m_orderbyProps.add(new PropertyName(propPropEl));
347:
348: if (orderEl.getElementsByTagNameNS(DAV_NAMESPACE,
349: BasicSearchRequest.TAG_DESC).getLength() > 0) {
350: this.m_orderbyDirections.add(ORDER_DESC);
351: } else {
352: this.m_orderbyDirections.add(ORDER_ASC);
353: }
354: }
355: }
356: }
|