1 23 package org.apache.slide.search.basic; 24 25 import java.util.ArrayList ; 26 import java.util.Collection ; 27 import java.util.HashMap ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 import java.util.Map ; 31 import org.apache.slide.content.NodeProperty.NamespaceCache; 32 import org.apache.slide.search.BadQueryException; 33 import org.apache.slide.search.PropertyProvider; 34 import org.jdom.Element; 35 36 43 public class BasicExpressionCompiler implements IBasicExpressionCompiler{ 44 45 48 protected IBasicQuery query = null; 49 50 53 protected PropertyProvider propertyProvider = null; 54 55 58 protected IBasicExpressionFactory genericExpressionFactory = null; 59 60 64 protected IBasicExpressionFactory propertiesExpressionFactory = null; 65 66 70 protected IBasicExpressionFactory contentExpressionFactory = null; 71 72 76 protected boolean alreadyTriedToInstantiate = false; 77 78 79 80 89 public BasicExpressionCompiler (IBasicQuery query, PropertyProvider propertyProvider) throws BadQueryException { 90 this.query = query; 91 this.propertyProvider = propertyProvider; 92 93 genericExpressionFactory = new BasicExpressionFactory(); 94 95 propertiesExpressionFactory = query.getPropertiesExpressionFactory() == null ? 96 genericExpressionFactory : query.getPropertiesExpressionFactory(); 97 98 contentExpressionFactory = query.getContentExpressionFactory() == null ? 99 genericExpressionFactory : query.getContentExpressionFactory(); 100 101 propertiesExpressionFactory.init(query, propertyProvider); 102 contentExpressionFactory.init(query, propertyProvider); 103 genericExpressionFactory.init(query, propertyProvider); 104 } 105 106 107 108 118 public IBasicExpression compile(Element expressionElement) throws BadQueryException { 119 120 if (isMergeExpression(expressionElement)) { 121 122 List expressionsToMerge = new ArrayList (); 123 Iterator iterator = expressionElement.getChildren().iterator(); 124 while (iterator.hasNext()) { 125 expressionsToMerge.add(compile((Element)iterator.next())); 126 } 127 return createMergeExpression(expressionElement.getName(), 128 expressionElement.getNamespaceURI(), 129 expressionsToMerge); 130 } 131 else { 132 return createExpression(expressionElement); 133 } 134 } 135 136 151 private IBasicExpression createMergeExpression (String name, String namespace, 152 List expressionsToMerge) 153 throws BadQueryException 154 { 155 156 Map expressionsByFactory = new HashMap (); 157 IBasicExpression mergeExpression = null; 158 Iterator it = expressionsToMerge.iterator(); 159 160 while (it.hasNext()) { 162 IBasicExpression exp = (IBasicExpression)it.next(); 163 IBasicExpressionFactory fac = exp.getFactory(); 164 List expList = (List )expressionsByFactory.get(fac); 165 if (expList == null) { 166 expList = new ArrayList (); 167 expressionsByFactory.put (fac, expList); 168 } 169 170 expList.add (exp); 171 } 172 173 it = expressionsByFactory.keySet().iterator(); 175 while (it.hasNext()) { 176 IBasicExpressionFactory fac = (IBasicExpressionFactory)it.next(); 177 List expList = (List )expressionsByFactory.get (fac); 178 179 180 if (expList.size() > 1) { 181 expressionsByFactory.put 183 (fac, fac.createMergeExpression (name, namespace, expList)); 184 } 185 else { 186 expressionsByFactory.put (fac, expList.get(0)); 188 } 189 } 190 191 Collection exprToMerge = expressionsByFactory.values(); 194 195 if (expressionsByFactory.size() > 1) { 196 197 mergeExpression = 198 genericExpressionFactory.createMergeExpression (name, namespace, 199 exprToMerge); 200 } 201 else { 202 it = expressionsByFactory.values().iterator(); 203 if (it.hasNext()) 204 mergeExpression = (IBasicExpression)it.next(); 205 } 206 207 return mergeExpression; 208 } 209 210 222 private IBasicExpression createExpression (Element expressionElement) 223 throws BadQueryException 224 { 225 IBasicExpression expression = null; 226 227 if (isContentExpression (expressionElement)) { 228 expression = contentExpressionFactory.createExpression(expressionElement); 229 } 230 else { 231 expression = propertiesExpressionFactory.createExpression(expressionElement); 232 } 233 234 if (expression == null) { 235 expression = genericExpressionFactory.createExpression (expressionElement); 236 } 237 238 return expression; 239 } 240 241 242 251 public static boolean isMergeExpression(Element expressionElement) { 252 253 boolean isMerge = false; 254 if ( (expressionElement != null) && 255 NamespaceCache.DEFAULT_URI.equals(expressionElement.getNamespaceURI()) ) { 256 257 isMerge = Literals.AND.equals(expressionElement.getName()) || 258 Literals.OR.equals(expressionElement.getName()); 259 } 260 return isMerge; 261 } 262 263 264 275 public static boolean isContentExpression (Element expressionElement) { 276 boolean isContent = false; 277 if ( (expressionElement != null) && 278 NamespaceCache.DEFAULT_URI.equals(expressionElement.getNamespaceURI()) ) { 279 280 isContent = Literals.CONTAINS.equals(expressionElement.getName()); 281 } 282 return isContent; 283 } 284 } 285 286 | Popular Tags |