luetzkendorf    2004/11/01 09:50:18

  Modified:    src/share/org/apache/slide/search/basic NotNormalizer.java
  Added:       src/share/org/apache/slide/search/basic/expression
                        LikeExpression.java NotLikeExpression.java
  Log:
  added support of the DASL Like expression
  
  Revision  Changes    Path
  1.1                  
jakarta-slide/src/share/org/apache/slide/search/basic/expression/LikeExpression.java
  
  Index: LikeExpression.java
  ===================================================================
  /*
   * $Header: 
/home/cvs/jakarta-slide/src/share/org/apache/slide/search/basic/expression/LikeExpression.java,v
 1.1 2004/11/01 17:50:18 luetzkendorf Exp $
   * $Revision: 1.1 $
   * $Date: 2004/11/01 17:50:18 $
   *
   * ====================================================================
   *
   * Copyright 1999 The Apache Software Foundation 
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   *
   */
  package org.apache.slide.search.basic.expression;
  
  import org.apache.slide.common.SlideException;
  import org.apache.slide.search.InvalidQueryException;
  import org.apache.slide.search.basic.ComparableResource;
  import org.apache.slide.search.basic.ComparableResourcesPool;
  import org.apache.slide.search.basic.Literals;
  import org.jdom.Element;
  
  
  /**
   * Implements the like operator.
   */
  public class LikeExpression extends ComparePropertyExpression
  {
  
      private static char WILDCARD_STRING = '%';
      private static char WILDCARD_CHAR = '_';
   
      public LikeExpression(Element e,
              ComparableResourcesPool requestedResourcesPool)
              throws InvalidQueryException
      {
          super(e, requestedResourcesPool, true);
      }
      
      
      protected boolean compare(ComparableResource item)
      {
          try {
              Object value = item.getProperty(
                      comparedProperty.getProperty(),
                      comparedProperty.getPropNamespace()).getValue();
              if (value != null) {
                  return wildcardEquals(
                          comparedProperty.getLiteral(),
                          0,
                          value.toString(),
                          0);
              }
          } catch (SlideException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          } 
          return false;
      }
      
      public String toString () {
          return super.toString(Literals.LIKE);
      }
      
      /**
       * Determines if a word matches a wildcard pattern.
       * <small>Taken from Lucenes WildcardTermEnum.</small>
       */
      public static final boolean wildcardEquals(String pattern, int patternIdx,
        String string, int stringIdx)
      {
        for (int p = patternIdx; ; ++p)
        {
          for (int s = stringIdx; ; ++p, ++s)
          {
            // End of string yet?
            boolean sEnd = (s >= string.length());
            // End of pattern yet?
            boolean pEnd = (p >= pattern.length());
  
            // If we're looking at the end of the string...
            if (sEnd)
            {
              // Assume the only thing left on the pattern is/are wildcards
              boolean justWildcardsLeft = true;
  
              // Current wildcard position
              int wildcardSearchPos = p;
              // While we haven't found the end of the pattern,
              // and haven't encountered any non-wildcard characters
              while (wildcardSearchPos < pattern.length() && justWildcardsLeft)
              {
                // Check the character at the current position
                char wildchar = pattern.charAt(wildcardSearchPos);
                // If it's not a wildcard character, then there is more
                // pattern information after this/these wildcards.
  
                if (wildchar != WILDCARD_CHAR && wildchar != WILDCARD_STRING)
                {
                  justWildcardsLeft = false;
                }
                else
                {
                  // Look at the next character
                  wildcardSearchPos++;
                }
              }
  
              // This was a prefix wildcard search, and we've matched, so
              // return true.
              if (justWildcardsLeft)
              {
                return true;
              }
            }
  
            // If we've gone past the end of the string, or the pattern,
            // return false.
            if (sEnd || pEnd)
            {
              break;
            }
  
            // Match a single character, so continue.
            if (pattern.charAt(p) == WILDCARD_CHAR)
            {
              continue;
            }
  
            //
            if (pattern.charAt(p) == WILDCARD_STRING)
            {
              // Look at the character beyond the '*'.
              ++p;
              // Examine the string, starting at the last character.
              for (int i = string.length(); i >= s; --i)
              {
                if (wildcardEquals(pattern, p, string, i))
                {
                  return true;
                }
              }
              break;
            }
            if (pattern.charAt(p) != string.charAt(s))
            {
              break;
            }
          }
          return false;
        }
      }
      
      public static void main(String[] args) {
          System.out.println(wildcardEquals("test*", 0, "testtest", 0));
          System.out.println(wildcardEquals("test*", 0, "tesxtest", 0));
      }
      
  }
  
  
  
  1.1                  
jakarta-slide/src/share/org/apache/slide/search/basic/expression/NotLikeExpression.java
  
  Index: NotLikeExpression.java
  ===================================================================
  // vi: set ts=3 sw=3:
  package org.apache.slide.search.basic.expression;
  
  import org.apache.slide.common.SlideException;
  import org.apache.slide.search.InvalidQueryException;
  import org.apache.slide.search.basic.ComparableResource;
  import org.apache.slide.search.basic.ComparableResourcesPool;
  import org.apache.slide.search.basic.Literals;
  import org.jdom.Element;
  
  
  /**
   * Implements the <code>not-like</code> operator.
   */
  public class NotLikeExpression extends LikeExpression
  {
  
      public NotLikeExpression(Element e,
              ComparableResourcesPool requestedResourcesPool)
              throws InvalidQueryException
      {
          super(e, requestedResourcesPool);
      }
      
      protected boolean compare(ComparableResource item)
      {
          try {
              Object value = item.getProperty(
                      comparedProperty.getProperty(),
                      comparedProperty.getPropNamespace()).getValue();
              if (value != null) {
                  return !wildcardEquals(
                          comparedProperty.getLiteral(),
                          0,
                          value.toString(),
                          0);
              }
          } catch (SlideException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          } 
          return false;
      }
      
      public String toString () {
          return super.toString(Literals.NOT_LIKE);
      }
  
  }
  
  
  
  1.7       +10 -4     
jakarta-slide/src/share/org/apache/slide/search/basic/NotNormalizer.java
  
  Index: NotNormalizer.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-slide/src/share/org/apache/slide/search/basic/NotNormalizer.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- NotNormalizer.java        28 Jul 2004 09:35:01 -0000      1.6
  +++ NotNormalizer.java        1 Nov 2004 17:50:18 -0000       1.7
  @@ -256,6 +256,12 @@
           if (Literals.NOT_ISDEFINED.equals(name)) {
               return getNamedClone(expressionElement, Literals.ISDEFINED, 
NamespaceCache.DEFAULT_NAMESPACE);
           }
  +        if (Literals.LIKE.equals(name)) {
  +            return getNamedClone(expressionElement, Literals.NOT_LIKE, 
NamespaceCache.DEFAULT_NAMESPACE);
  +        }
  +        if (Literals.NOT_LIKE.equals(name)) {
  +            return getNamedClone(expressionElement, Literals.LIKE, 
NamespaceCache.DEFAULT_NAMESPACE);
  +        }
           return getNamedClone(expressionElement, expressionElement.getName(), 
expressionElement.getNamespace());
       }
       
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to