001: /*
002: * $Header$
003: * $Revision: 7957 $
004: * $Date: 2007-08-23 04:22:35 -0700 $
005: *
006: * ====================================================================
007: *
008: * Copyright 1999-2004 The Apache Software Foundation
009: *
010: * Licensed under the Apache License, Version 2.0 (the "License");
011: * you may not use this file except in compliance with the License.
012: * You may obtain a copy of the License at
013: *
014: * http://www.apache.org/licenses/LICENSE-2.0
015: *
016: * Unless required by applicable law or agreed to in writing, software
017: * distributed under the License is distributed on an "AS IS" BASIS,
018: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019: * See the License for the specific language governing permissions and
020: * limitations under the License.
021: *
022: */
023: package org.apache.slide.index.lucene.expressions;
024:
025: import org.apache.lucene.index.Term;
026: import org.apache.lucene.search.BooleanClause;
027: import org.apache.lucene.search.BooleanQuery;
028: import org.apache.lucene.search.PrefixQuery;
029: import org.apache.lucene.search.TermQuery;
030: import org.apache.lucene.search.WildcardQuery;
031: import org.apache.lucene.search.WildcardTermEnum;
032:
033: import org.apache.slide.index.lucene.Index;
034: import org.apache.slide.index.lucene.IndexConfiguration;
035: import org.apache.slide.search.BadQueryException;
036: import org.jdom.Element;
037:
038: /**
039: * Implements the <code>like</code> operator.
040: *
041: */
042: public class LikeExpression extends AbstractLuceneExpression {
043:
044: public LikeExpression(Index index, Element element, boolean negated)
045: throws BadQueryException {
046: super (index);
047:
048: Element prop = getPropertyElement(element);
049: String field = IndexConfiguration.generateFieldName(prop
050: .getNamespaceURI(), prop.getName());
051: Element literal = getLiteralElement(element);
052: String text = literal.getTextTrim();
053:
054: // TODO check what to do with Date or Int fields
055:
056: if (!index.getConfiguration().isCaseSensitive())
057: text = text.toLowerCase();
058:
059: if (text.indexOf('_') == -1
060: && text.indexOf('%') == text.length() - 1) {
061: // some thing like "apple%"
062: setQuery(new PrefixQuery(new Term(field, text.substring(0,
063: text.length() - 1))));
064: } else {
065: setQuery(new WildcardQuery(new Term(field,
066: transformQuerytext(text))));
067: }
068:
069: if (negated) {
070: BooleanQuery booleanQuery = new BooleanQuery();
071: booleanQuery.add(new TermQuery(new Term(
072: Index.IS_DEFINED_FIELD_NAME, field)),
073: BooleanClause.Occur.MUST);
074: booleanQuery.add(getQuery(), BooleanClause.Occur.MUST_NOT); // prohibited
075: setQuery(booleanQuery);
076: }
077: }
078:
079: private String transformQuerytext(String text)
080: throws BadQueryException {
081:
082: StringBuffer result = new StringBuffer(text.length());
083:
084: for (int i = 0, l = text.length(); i < l; i++) {
085: char c = text.charAt(i);
086: switch (c) {
087: case '%':
088: result.append(WildcardTermEnum.WILDCARD_STRING);
089: break;
090: case '_':
091: result.append(WildcardTermEnum.WILDCARD_CHAR);
092: break;
093: default:
094: result.append(c);
095: }
096: }
097:
098: return result.toString();
099: }
100: }
|