1 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 38 public class SearchBuilder { 39 40 43 private String m_sInitialPath = ""; 44 45 50 public SearchBuilder(String sInitialPath) { 51 super(); 52 this.m_sInitialPath = sInitialPath; 53 } 54 55 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 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 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 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 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 204 private Element processContentCondition(ContentCondition condition, Document xmlDoc) { 205 Element elRetn = this.operatorTranslation( condition.getOperator(), xmlDoc); 206 207 Text txt = xmlDoc.createTextNode((String )condition.getValues().get(0)); 208 elRetn.appendChild(txt); 209 210 return elRetn; 211 } 212 213 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 )condition.getValues().get(0)); 230 elLiteral.appendChild(txt); 231 232 return elRetn; 233 } 234 235 242 private Element operatorTranslation(String 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 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 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 |