1 23 package org.apache.slide.search.basic; 24 25 import java.util.Iterator ; 26 import java.util.List ; 27 28 import org.apache.slide.content.NodeProperty.NamespaceCache; 29 import org.apache.slide.search.BadQueryException; 30 import org.apache.slide.search.InvalidQueryException; 31 import org.jdom.Attribute; 32 import org.jdom.Element; 33 import org.jdom.Namespace; 34 35 81 public class NotNormalizer { 82 83 89 public static final String BAD_NOT_EXPRESSION_EXCEPTION_MESSAGE = 90 "not expression must contain exactly one nested expression"; 91 92 105 public Element getQueryWithoutNotExpression(Element expressionElement) throws BadQueryException { 106 return getProcessedElement(expressionElement, false); 107 } 108 109 122 protected Element getProcessedElement(Element expressionElement, boolean negate) throws BadQueryException { 123 124 Element result = null; 125 126 if ( Literals.NOT.equals(expressionElement.getName()) && 127 NamespaceCache.DEFAULT_URI.equals(expressionElement.getNamespaceURI()) ) { 128 129 List children = expressionElement.getChildren(); 130 if (children.size() != 1) { 131 throw new InvalidQueryException(BAD_NOT_EXPRESSION_EXCEPTION_MESSAGE); 132 } 133 return getProcessedElement((Element)children.get(0), ! negate); 134 } 135 136 if (negate) { 137 result = getNegatedQueryElement(expressionElement); 138 } 139 else { 140 result = getNamedClone(expressionElement, expressionElement.getName(), expressionElement.getNamespace()); 141 } 142 Iterator childrenIterator = expressionElement.getChildren().iterator(); 143 while (childrenIterator.hasNext()) { 144 result.addContent(getProcessedElement((Element)childrenIterator.next(), negate)); 145 } 146 147 return result; 148 } 149 150 160 protected Element getNegatedQueryElement(Element expressionElement) throws BadQueryException { 161 162 if (NamespaceCache.DEFAULT_URI.equals(expressionElement.getNamespaceURI())) { 163 return getNegatedDAVQueryElement(expressionElement); 164 } 165 if (NamespaceCache.SLIDE_URI.equals(expressionElement.getNamespaceURI())) { 166 return getNegatedSlideQueryElement(expressionElement); 167 } 168 else { 169 return getNegatedUnknownQueryElement(expressionElement); 170 } 171 } 172 173 174 186 protected Element getNegatedUnknownQueryElement(Element expressionElement) throws BadQueryException { 187 return getNamedClone(expressionElement, expressionElement.getName(), expressionElement.getNamespace()); 188 } 189 190 201 protected Element getNegatedDAVQueryElement(Element expressionElement) throws BadQueryException { 202 203 String name = expressionElement.getName(); 204 205 if (Literals.AND.equals(name)) { 206 return getNamedClone(expressionElement, Literals.OR, NamespaceCache.DEFAULT_NAMESPACE); 207 } 208 if (Literals.OR.equals(name)) { 209 return getNamedClone(expressionElement, Literals.AND, NamespaceCache.DEFAULT_NAMESPACE); 210 } 211 if (Literals.GT.equals(name)) { 212 return getNamedClone(expressionElement, Literals.NOT_GT, NamespaceCache.DEFAULT_NAMESPACE); 213 } 214 if (Literals.NOT_GT.equals(name)) { 215 return getNamedClone(expressionElement, Literals.GT, NamespaceCache.DEFAULT_NAMESPACE); 216 } 217 if (Literals.GTE.equals(name)) { 218 return getNamedClone(expressionElement, Literals.NOT_GTE, NamespaceCache.DEFAULT_NAMESPACE); 219 } 220 if (Literals.NOT_GTE.equals(name)) { 221 return getNamedClone(expressionElement, Literals.GTE, NamespaceCache.DEFAULT_NAMESPACE); 222 } 223 if (Literals.LT.equals(name)) { 224 return getNamedClone(expressionElement, Literals.NOT_LT, NamespaceCache.DEFAULT_NAMESPACE); 225 } 226 if (Literals.NOT_LT.equals(name)) { 227 return getNamedClone(expressionElement, Literals.LT, NamespaceCache.DEFAULT_NAMESPACE); 228 } 229 if (Literals.LTE.equals(name)) { 230 return getNamedClone(expressionElement, Literals.NOT_LTE, NamespaceCache.DEFAULT_NAMESPACE); 231 } 232 if (Literals.NOT_LTE.equals(name)) { 233 return getNamedClone(expressionElement, Literals.LTE, NamespaceCache.DEFAULT_NAMESPACE); 234 } 235 if (Literals.EQ.equals(name)) { 236 return getNamedClone(expressionElement, Literals.NOT_EQ, NamespaceCache.DEFAULT_NAMESPACE); 237 } 238 if (Literals.NOT_EQ.equals(name)) { 239 return getNamedClone(expressionElement, Literals.EQ, NamespaceCache.DEFAULT_NAMESPACE); 240 } 241 if (Literals.CONTAINS.equals(name)) { 242 return getNamedClone(expressionElement, Literals.NOT_CONTAINS, NamespaceCache.DEFAULT_NAMESPACE); 243 } 244 if (Literals.NOT_CONTAINS.equals(name)) { 245 return getNamedClone(expressionElement, Literals.CONTAINS, NamespaceCache.DEFAULT_NAMESPACE); 246 } 247 if (Literals.ISCOLLECTION.equals(name)) { 248 return getNamedClone(expressionElement, Literals.NOT_ISCOLLECTION, NamespaceCache.DEFAULT_NAMESPACE); 249 } 250 if (Literals.NOT_ISCOLLECTION.equals(name)) { 251 return getNamedClone(expressionElement, Literals.ISCOLLECTION, NamespaceCache.DEFAULT_NAMESPACE); 252 } 253 if (Literals.ISDEFINED.equals(name)) { 254 return getNamedClone(expressionElement, Literals.NOT_ISDEFINED, NamespaceCache.DEFAULT_NAMESPACE); 255 } 256 if (Literals.NOT_ISDEFINED.equals(name)) { 257 return getNamedClone(expressionElement, Literals.ISDEFINED, NamespaceCache.DEFAULT_NAMESPACE); 258 } 259 return getNamedClone(expressionElement, expressionElement.getName(), expressionElement.getNamespace()); 260 } 261 262 273 protected Element getNegatedSlideQueryElement(Element expressionElement) throws BadQueryException { 274 275 String name = expressionElement.getName(); 276 277 if (Literals.ISPRINCIPAL.equals(name)) { 278 return getNamedClone(expressionElement, Literals.NOT_ISPRINCIPAL, NamespaceCache.SLIDE_NAMESPACE); 279 } 280 if (Literals.NOT_ISPRINCIPAL.equals(name)) { 281 return getNamedClone(expressionElement, Literals.ISPRINCIPAL, NamespaceCache.SLIDE_NAMESPACE); 282 } 283 if (Literals.PROPCONTAINS.equals(name)) { 284 return getNamedClone(expressionElement, Literals.NOT_PROPCONTAINS, NamespaceCache.SLIDE_NAMESPACE); 285 } 286 if (Literals.NOT_PROPCONTAINS.equals(name)) { 287 return getNamedClone(expressionElement, Literals.PROPCONTAINS, NamespaceCache.SLIDE_NAMESPACE); 288 } 289 return getNamedClone(expressionElement, expressionElement.getName(), expressionElement.getNamespace()); 290 } 291 292 303 protected Element getNamedClone(Element source, String newName, Namespace newNamespace) { 304 305 Element copy = new Element(newName, newNamespace); 306 copy.setText(source.getText()); 307 Iterator it = source.getAttributes().iterator(); 308 309 while (it.hasNext()) { 310 Attribute attr = (Attribute)it.next(); 311 copy.setAttribute ((Attribute)attr.clone()); 312 } 313 314 return copy; 315 } 316 } 317 318 | Popular Tags |