1 13 14 package com.ibm.webdav.basicsearch; 15 16 import com.ibm.webdav.*; 17 18 import java.net.URLDecoder ; 19 import java.util.*; 20 21 import org.w3c.dom.*; 22 23 31 public class BasicSearchRequest implements SearchRequest { 32 public static final String TAG_BASICSEARCH = "basicsearch"; 33 public static final String TAG_SELECT = "select"; 34 public static final String TAG_FROM = "from"; 35 public static final String TAG_WHERE = "where"; 36 public static final String TAG_ORDERBY = "orderby"; 37 public static final String TAG_ORDER = "order"; 38 public static final String TAG_LIMIT = "limit"; 39 public static final String TAG_PROP = "prop"; 40 public static final String TAG_LITERAL = "literal"; 41 public static final String TAG_ALLPROP = "allprop"; 42 public static final String TAG_HREF = "href"; 43 public static final String TAG_DEPTH = "depth"; 44 public static final String TAG_DESC = "descending"; 45 public static final String TAG_ASC = "ascending"; 46 public static final String TAG_NRESULTS = "nresults"; 47 private static final String DAV_NAMESPACE = "DAV:"; 48 public static Hashtable LOG_OPS = new Hashtable(); 49 public static Hashtable COMP_OPS = new Hashtable(); 50 public static Hashtable STRING_OPS = new Hashtable(); 51 public static Hashtable CONTENT_OPS = new Hashtable(); 52 public static Hashtable SPECIAL_OPS = new Hashtable(); 53 public static Hashtable ALL_OPS = new Hashtable(); 54 55 public static String OPERATOR_OR = "or"; 56 public static String OPERATOR_AND = "and"; 57 58 static { 59 LOG_OPS.put(OPERATOR_AND, OPERATOR_AND); 60 LOG_OPS.put(OPERATOR_OR, OPERATOR_OR); 61 62 COMP_OPS.put("eq", "="); 63 COMP_OPS.put("lt", "<"); 64 COMP_OPS.put("gt", ">"); 65 COMP_OPS.put("lte", "<="); 66 COMP_OPS.put("gte", ">="); 67 68 STRING_OPS.put("like", "like"); 69 CONTENT_OPS.put("contains", "contains"); 70 71 SPECIAL_OPS.put("isdefined", "isdefined"); 72 SPECIAL_OPS.put("is-collection", "is-collection"); 73 74 ALL_OPS.putAll(LOG_OPS); 75 ALL_OPS.putAll(STRING_OPS); 76 ALL_OPS.putAll(CONTENT_OPS); 77 ALL_OPS.putAll(SPECIAL_OPS); 78 ALL_OPS.putAll(COMP_OPS); 79 } 80 81 protected String m_scope_uri = null; 82 protected int m_scope_depth = 0; 83 protected boolean m_bSelectAllProps = false; 84 protected Vector m_select_props = new Vector(); 85 protected SearchCondition m_condition = new SearchCondition(); 86 protected Vector m_orderbyProps = new Vector(); 87 protected Vector m_orderbyDirections = new Vector(); 88 protected boolean m_bIncludeDefinitions = true; 89 protected int m_nLimit = -1; 90 91 public BasicSearchRequest() { 92 } 93 94 public void instantiateFromXML(Element xmlElement) throws Exception { 95 if (xmlElement 96 .getLocalName() 97 .equals(BasicSearchRequest.TAG_BASICSEARCH) 98 == false) { 99 throw new Exception ("Invalid Tag"); 100 } 101 102 NodeList nodes = xmlElement.getChildNodes(); 103 104 for (int i = 0; i < nodes.getLength(); i++) { 105 if (nodes.item(i).getNodeType() != Node.ELEMENT_NODE) { 106 continue; 107 } 108 109 Element tempEl = (Element) nodes.item(i); 110 String sTagName = tempEl.getLocalName(); 111 112 if (sTagName.equals(BasicSearchRequest.TAG_SELECT)) { 113 NodeList props = 114 tempEl.getElementsByTagNameNS( 115 DAV_NAMESPACE, 116 BasicSearchRequest.TAG_PROP); 117 118 if (props.getLength() > 0) { 119 for (int j = 0; j < props.getLength(); j++) { 120 Element propEl = (Element) props.item(j); 121 122 NodeList nl = propEl.getElementsByTagName("*"); 123 124 if (nl.getLength() > 0) { 125 Element indpropEl = (Element) nl.item(0); 126 PropertyName pname = new PropertyName(indpropEl); 127 128 129 this.m_select_props.add(pname); 130 } 131 } 132 } else { 133 this.m_bSelectAllProps = true; 134 } 135 } else if (sTagName.equals(BasicSearchRequest.TAG_FROM)) { 136 Element depthEl = 137 (Element) tempEl 138 .getElementsByTagNameNS( 139 DAV_NAMESPACE, 140 BasicSearchRequest.TAG_DEPTH) 141 .item(0); 142 String sdepth = ((Text) depthEl.getFirstChild()).getNodeValue(); 143 144 try { 145 this.m_scope_depth = Integer.parseInt(sdepth); 146 } catch (NumberFormatException e) { 147 m_scope_depth = -1; 148 } 149 150 Element hrefEl = 151 (Element) tempEl 152 .getElementsByTagNameNS( 153 DAV_NAMESPACE, 154 BasicSearchRequest.TAG_HREF) 155 .item(0); 156 157 this.m_scope_uri = 158 ((Text) hrefEl.getFirstChild()).getNodeValue(); 159 160 m_scope_uri = URLDecoder.decode(m_scope_uri, "UTF-8"); 161 162 } else if (sTagName.equals(BasicSearchRequest.TAG_WHERE)) { 163 Element condEl = 164 (Element) tempEl.getElementsByTagName("*").item(0); 165 166 m_condition = this.createCondition(condEl); 167 } else if (sTagName.equals(BasicSearchRequest.TAG_ORDERBY)) { 168 NodeList orderNodes = 169 tempEl.getElementsByTagNameNS( 170 DAV_NAMESPACE, 171 BasicSearchRequest.TAG_ORDER); 172 173 for (int j = 0; j < orderNodes.getLength(); j++) { 174 Element orderEl = (Element) orderNodes.item(j); 175 Element propEl = 176 (Element) orderEl 177 .getElementsByTagNameNS( 178 DAV_NAMESPACE, 179 BasicSearchRequest.TAG_PROP) 180 .item(0); 181 this.m_orderbyProps.add( 182 new PropertyName( 183 (Element) propEl.getElementsByTagName("*").item( 184 0))); 185 186 if (orderEl 187 .getElementsByTagNameNS( 188 DAV_NAMESPACE, 189 BasicSearchRequest.TAG_DESC) 190 .getLength() 191 > 0) { 192 m_orderbyDirections.add(SearchRequest.ORDER_DESC); 193 } else { 194 m_orderbyDirections.add(SearchRequest.ORDER_ASC); 195 } 196 } 197 } else if (sTagName.equals(BasicSearchRequest.TAG_LIMIT)) { 198 Element numresults = 199 (Element) tempEl 200 .getElementsByTagNameNS( 201 DAV_NAMESPACE, 202 BasicSearchRequest.TAG_NRESULTS) 203 .item(0); 204 Text txt = (Text) numresults.getFirstChild(); 205 206 this.m_nLimit = Integer.parseInt(txt.getNodeValue()); 207 } 208 } 209 } 210 211 public int getResultLimit() { 212 return m_nLimit; 213 } 214 215 public String getScopeURI() { 216 return m_scope_uri; 217 } 218 219 public int getScopeDepth() { 220 return m_scope_depth; 221 } 222 223 public SearchSchema getSearchSchema() throws Exception { 224 return new BasicSearchSchema(); 225 } 226 227 public SearchCondition getCondition() { 228 return this.m_condition; 229 } 230 231 public Vector getSelectProperties() { 232 return this.m_select_props; 233 } 234 235 public boolean isAllSelectProperties() { 236 return this.m_bSelectAllProps; 237 } 238 239 public boolean isIncludePropertyDefinitions() { 240 return m_bIncludeDefinitions; 241 } 242 243 public Vector getOrderByProperties() { 244 return this.m_orderbyProps; 245 } 246 247 public String getOrderByDirection(PropertyName propName) { 248 return (String ) this.m_orderbyDirections.elementAt( 249 this.m_orderbyProps.indexOf(propName)); 250 } 251 252 private SearchCondition createCondition(Element condEl) throws Exception { 253 SearchCondition searchCond = new SearchCondition(); 254 String sTagname = condEl.getLocalName(); 255 256 if (BasicSearchRequest.ALL_OPS.containsKey(sTagname) == false) { 257 throw new Exception ("Invalid operator"); 258 } 259 260 if (BasicSearchRequest.LOG_OPS.containsKey(sTagname)) { 261 searchCond.setOperator(sTagname); 262 263 NodeList nodes = condEl.getChildNodes(); 264 265 for (int i = 0; i < nodes.getLength(); i++) { 266 if (nodes.item(i).getNodeType() != Node.ELEMENT_NODE) { 267 continue; 268 } 269 270 Element tempEl = (Element) nodes.item(i); 271 272 if (LOG_OPS.containsKey(tempEl.getLocalName())) { 273 SearchCondition cond = createCondition(tempEl); 274 if (cond != null) { 275 searchCond.addCondition(cond); 276 } 277 278 } else { 279 SearchConditionTerm term = createConditionTerm(tempEl); 280 if (term != null) { 281 searchCond.addCondition(term); 282 } 283 } 284 } 285 } else { 286 SearchConditionTerm term = createConditionTerm(condEl); 287 288 if (term != null) { 289 searchCond.addCondition(term); 290 } 291 } 292 293 return searchCond; 294 } 295 296 private SearchConditionTerm createConditionTerm(Element condEl) 297 throws Exception { 298 SearchConditionTerm condTerm = null; 299 String sOperator = condEl.getLocalName(); 300 301 if (COMP_OPS.containsKey(sOperator) 302 || BasicSearchRequest.STRING_OPS.containsKey(sOperator) 303 || BasicSearchRequest.CONTENT_OPS.containsKey(sOperator) 304 || BasicSearchRequest.SPECIAL_OPS.containsKey(sOperator)) { 305 Element propEl = 306 (Element) condEl 307 .getElementsByTagNameNS( 308 DAV_NAMESPACE, 309 BasicSearchRequest.TAG_PROP) 310 .item(0); 311 312 if (propEl != null) { 313 314 NodeList nodes = propEl.getElementsByTagName("*"); 315 316 if (nodes.getLength() > 0) { 317 318 Element propPropEl = (Element) nodes.item(0); 319 320 String sValue = null; 321 322 if (BasicSearchRequest.SPECIAL_OPS.containsKey(sOperator) 323 == false) { 324 Element literalEl = 325 (Element) condEl 326 .getElementsByTagNameNS( 327 DAV_NAMESPACE, 328 BasicSearchRequest.TAG_LITERAL) 329 .item(0); 330 if(literalEl.getChildNodes().getLength() > 0) { 331 sValue = 332 ((Text) literalEl.getFirstChild()).getNodeValue(); 333 } 334 335 } 336 337 if(sValue != null) { 338 condTerm = 339 new SearchPropertyConditionTerm( 340 new PropertyName(propPropEl), 341 (String ) BasicSearchRequest.ALL_OPS.get( 342 sOperator), 343 sValue); 344 } 345 346 } 347 } else { 348 String sVal = condEl.getFirstChild().getNodeValue(); 349 350 condTerm = new SearchConditionTerm(sOperator,sVal); 351 352 } 353 } else { 354 throw new Exception ("InvalidOperator - " + sOperator); 355 } 356 357 return condTerm; 358 } 359 360 private void addOrder(Element orderEl) { 361 String sTagname = orderEl.getLocalName(); 362 363 if (sTagname.equals(BasicSearchRequest.TAG_ORDER)) { 364 Element propEl = 365 (Element) orderEl 366 .getElementsByTagNameNS( 367 DAV_NAMESPACE, 368 BasicSearchRequest.TAG_PROP) 369 .item(0); 370 371 Element propPropEl = 372 (Element) propEl.getElementsByTagName("*").item(0); 373 this.m_orderbyProps.add(new PropertyName(propPropEl)); 374 375 if (orderEl 376 .getElementsByTagNameNS( 377 DAV_NAMESPACE, 378 BasicSearchRequest.TAG_DESC) 379 .getLength() 380 > 0) { 381 this.m_orderbyDirections.add(ORDER_DESC); 382 } else { 383 this.m_orderbyDirections.add(ORDER_ASC); 384 } 385 } 386 } 387 } | Popular Tags |