wam         2002/07/04 08:22:00

  Modified:    src/share/org/apache/slide/search/basic BasicQueryImpl.java
  Added:       src/share/org/apache/slide/search ISearchToken.java
  Log:
  refactored
  
  Revision  Changes    Path
  1.1                  
jakarta-slide/src/share/org/apache/slide/search/ISearchToken.java
  
  Index: ISearchToken.java
  ===================================================================
  /*
   * $Header: 
/home/cvs/jakarta-slide/src/share/org/apache/slide/search/ISearchToken.java,v 1.1 
2002/07/04 15:22:00 wam Exp $
   * $Revision: 1.1 $
   * $Date: 2002/07/04 15:22:00 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Slide", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * [Additional notices, if required by prior licensing conditions]
   *
   */
  
  package org.apache.slide.search;
  
  import org.apache.slide.common.SlideToken;
  import org.apache.slide.common.Namespace;
  import org.apache.slide.content.Content;
  import org.apache.slide.structure.Structure;
  
  /**
   * Represents the context of a search requerst.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]";>Martin Wallmer</a>
   * @version $Revision: 1.1 $
   */
  public interface ISearchToken {
      
      
      /**
       * Method getNamespace
       *
       * @return   the namespace
       *
       */
      public Namespace getNamespace ();
      
      /**
       * Method getSlideToken
       *
       * @return   the slideToken
       *
       */
      public SlideToken getSlideToken();
      
      /**
       * Method getContentHelper
       *
       * @return   the contentHelper
       *
       */
      public Content getContentHelper();
      
      /**
       * Method getStructureHelper
       *
       * @return   the structureHelper
       *
       */
      public Structure getStructureHelper();
      
      /**
       * Method getMaxDepth
       *
       * @return   max depth as configured in Domain.xml
       *
       */
      public int getMaxDepth();
      
      /**
       * Method getSlideContext
       *
       * @return   a SlideUri
       *
       */
      public SlideUri getSlideContext ();
  }
  
  
  
  
  1.5       +341 -24   
jakarta-slide/src/share/org/apache/slide/search/basic/BasicQueryImpl.java
  
  Index: BasicQueryImpl.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-slide/src/share/org/apache/slide/search/basic/BasicQueryImpl.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- BasicQueryImpl.java       21 Jun 2002 13:26:25 -0000      1.4
  +++ BasicQueryImpl.java       4 Jul 2002 15:22:00 -0000       1.5
  @@ -64,6 +64,7 @@
   package org.apache.slide.search.basic;
   import org.apache.slide.search.*;
   
  +import org.apache.slide.common.PropertyParseException;
   import org.apache.slide.search.SearchQuery;
   import org.apache.slide.search.SearchException;
   import org.apache.slide.search.BadQueryException;
  @@ -94,7 +95,71 @@
    * @author <a href="mailto:[EMAIL PROTECTED]";>Martin Wallmer</a>
    * @version $Revision$
    */
  -public class BasicQueryImpl extends BasicQuery {
  +public class BasicQueryImpl extends SearchQuery implements IBasicQuery {
  +    
  +    /**
  +     * Message of a BadQueryException that is thrown if the query element
  +     * is <code>null</code>.
  +     */
  +    public static final String NO_QUERY_ELEMENT = "No query element";
  +    
  +    /**
  +     * Message of a BadQueryException that is thrown if the query element
  +     * does not contain a &lt;from&gt; element.
  +     */
  +    public static final String FROM_ELEMENT_MISSING = "Required element <from> not 
supplied";
  +    
  +    /**
  +     * Message of a BadQueryException that is thrown if the query element
  +     * does not contain a &lt;select&gt; element.
  +     */
  +    public static final String SELECT_ELEMENT_MISSING = "Required element <select> 
not supplied";
  +    
  +    /**
  +     * Message of a BadQueryException that is thrown if the query element
  +     * neither contains a &lt;from&gt; nor a &lt;from&gt; element.
  +     */
  +    public static final String PROP_OR_ALLPROP_ELEMENT_MISSING = "Required element 
<prop> or <allprop> not supplied";
  +    
  +    /** the element describing this query */
  +    protected Element queryElement;
  +    
  +    /** the namespace for this query */
  +    protected Namespace namespace;
  +    
  +    /** the scope of this query, <FROM> */
  +    protected QueryScope scope;
  +    
  +    /** the element describing the WHERE clauise */
  +    protected Element whereElement;
  +    
  +    /** List of requested properties, <SELECT> */
  +    protected RequestedProperties requestedProperties;
  +    
  +    /** <LIMIT> */
  +    protected int limit;
  +    
  +    /** ORDER BY */
  +    protected OrderBy orderBy;
  +    
  +    /** indicates, if a limit is defined */
  +    protected boolean limitDefined = false;
  +    
  +    /** The store for this query, may be used to access store parameters */
  +    protected AbstractStore store;
  +    
  +    /** the top level expression in the <WHERE> clause */
  +    protected IBasicExpression rootExpression;
  +    
  +    /** used to get the slidePath */
  +    protected SlideUri slideUri;
  +    
  +    /** The provider for the properties */
  +    protected PropertyProvider propertyProvider;
  +    
  +    /** the NotNormalizer, may be overridden in extending classes */
  +    protected NotNormalizer notNormalizer;
  +    
       
       /**
        * The provider which delivers the expression compiler to use.
  @@ -104,19 +169,38 @@
       /**
        * Creates a BasicQueryImpl.
        */
  -    public BasicQueryImpl(SearchToken searchToken) {
  -        this(searchToken, new ExpressionCompilerProvider());
  +    public BasicQueryImpl (SearchToken searchToken) {
  +        init (searchToken);
  +        notNormalizer = new NotNormalizer ();
       }
       
  +    /**
  +     * Default constructor, to enable creation by reflection
  +     */
  +    public BasicQueryImpl() {
  +        notNormalizer = new NotNormalizer ();
  +    }
       
       /**
  -     * Creates a BasicQueryImpl.
  +     * Initailize
  +     *
  +     * @param    token               a  SearchToken
  +     *
  +     */
  +    public void init (SearchToken token) {
  +        this.searchToken = token;
  +        slideUri = searchToken.getSlideContext();
  +        this.expressionCompilerProvider = new ExpressionCompilerProvider();
  +    }
  +    
  +    /**
  +     * Creates a BasicQueryImpl. Used for testing
        *
        * @param      expressionCompilerProvider  the provider which delivers the
        *                                         expression compiler to use.
        */
       public BasicQueryImpl(SearchToken searchToken, IBasicExpressionCompilerProvider 
expressionCompilerProvider) {
  -        super(searchToken);
  +        init (searchToken);
           this.expressionCompilerProvider = expressionCompilerProvider;
       }
       
  @@ -135,18 +219,18 @@
               
               IBasicResultSet resultSet = getExpression().execute();
               
  -                if (orderBy != null) {
  +            if (orderBy != null) {
                   result = new SearchQueryResult (resultSet,
  -                                                    orderBy.getComparator());
  -                }
  -                else {
  +                                                orderBy.getComparator());
  +            }
  +            else {
                   result = new SearchQueryResult (resultSet);
  -                }
  +            }
               if (resultSet.isPartialResultSet()) {
  -                    result.setStatus(SearchQueryResult.STATUS_PARTIAL_RESULT);
  -                    result.setDescription ("The server truncated the result set");
  -                }
  +                result.setStatus(SearchQueryResult.STATUS_PARTIAL_RESULT);
  +                result.setDescription ("The server truncated the result set");
               }
  +        }
           
           catch (InvalidScopeException e) {
               result = new SearchQueryResult ();
  @@ -168,8 +252,8 @@
               result.setHref (scope.getHref());
           }
           
  -            return result;
  -        }
  +        return result;
  +    }
       
       /**
        * builds the internal structure from the JDOM tree
  @@ -180,7 +264,8 @@
        *
        * @throws   BadQueryException
        */
  -    public void parseQuery(Element expressionElement, PropertyProvider 
propertyProvider) throws BadQueryException {
  +    public void parseQuery(Element expressionElement, PropertyProvider 
propertyProvider)
  +        throws BadQueryException {
           
           parseQueryWithoutExpression (expressionElement);
           IBasicExpressionCompiler expressionCompiler = 
expressionCompilerProvider.getCompiler(this, propertyProvider);
  @@ -193,14 +278,15 @@
               }
               
               Element whereWithoutNot =
  -                new 
NotNormalizer().getQueryWithoutNotExpression((Element)expressionList.get (0));
  +                
notNormalizer.getQueryWithoutNotExpression((Element)expressionList.get (0));
               rootExpression = expressionCompiler.compile(whereWithoutNot);
           }
           else {
               rootExpression = expressionCompiler.compile(null);
           }
       }
  -
  +    
  +    
       /**
        * This IBasicExpressionCompilerProvider implementation returns a
        * BasicQueryCompiler instance in method getCompiler().
  @@ -210,7 +296,7 @@
        * @author <a href="mailto:[EMAIL PROTECTED]";>Ralf Stuckert</a>
        **/
       public static class ExpressionCompilerProvider implements 
IBasicExpressionCompilerProvider {
  -
  +        
           /**
            * Returns an IBasicExpressionCompiler for the given parameters.
            *
  @@ -225,5 +311,236 @@
           }
       }
       
  +    
  +    
  +    /**
  +     * Method getSelectedProperties
  +     *
  +     * @return   a SelectedPropertyList
  +     */
  +    public RequestedProperties requestedProperties () {
  +        return requestedProperties;
  +    }
  +    
  +    /**
  +     * Method getExpression
  +     *
  +     * @return   a BasicExpression
  +     *
  +     */
  +    public IBasicExpression getExpression () {
  +        return rootExpression;
  +    }
  +    
  +    /**
  +     * Method getLimit
  +     *
  +     * @return   the value of <limit>
  +     */
  +    public int getLimit () {
  +        return limit;
  +    }
  +    
  +    /**
  +     * Method getPropertyProvider
  +     *
  +     * @return   a PropertyProvider
  +     *
  +     */
  +    public PropertyProvider getPropertyProvider () {
  +        return propertyProvider;
  +    }
  +    
  +    /**
  +     * QueryScope accessor
  +     *
  +     * @return   the Scope
  +     *
  +     */
  +    public QueryScope getScope () {
  +        return scope;
  +    }
  +    
  +    
  +    /**
  +     * Method getSlidePath
  +     *
  +     * @return   a String
  +     *
  +     * @throws   InvalidScopeException
  +     *
  +     */
  +    public String getSlidePath () throws InvalidScopeException {
  +        return slideUri.getSlidePath (scope.getHref());
  +    }
  +    
  +    /**
  +     * Method getSearchToken
  +     *
  +     * @return   a SearchToken
  +     *
  +     */
  +    public SearchToken getSearchToken (){
  +        return searchToken;
  +    }
  +    
  +    /**
  +     * Method getStore
  +     *
  +     * @return   an AbstractStore
  +     *
  +     */
  +    public AbstractStore getStore () {
  +        return store;
  +    }
  +    
  +    /**
  +     * Builds the internal structure from the JDOM tree. Concrete implementations
  +     * may use parseQueryElementWithoutExpression to create most of the
  +     * objects describing the query.
  +     *
  +     * @param    basicSearchElement  the (root) expression Element.
  +     * @param    propertyProvider    the PropertyProvider to use (may be
  +     *                               <code>null</code>).
  +     *
  +     * @throws   BadQueryException
  +     */
  +    public void parseQueryElement (Element basicSearchElement,
  +                                   PropertyProvider propertyProvider)
  +        
  +        throws BadQueryException
  +    {
  +        
  +        scope = getScope(basicSearchElement);
  +        this.propertyProvider = propertyProvider;
  +        
  +        // might be null in testsuite
  +        if (searchToken.getNamespace() != null) {
  +            Uri uri = new Uri (searchToken.getNamespace(), 
slideUri.getSlidePath(scope.getHref()));
  +            store = (AbstractStore)uri.getStore();
  +        }
  +        
  +        parseQuery(basicSearchElement, propertyProvider);
  +    }
  +    
  +    
  +    /**
  +     * Method isLimitDefined
  +     *
  +     * @return true if <limit> was specified
  +     */
  +    public boolean isLimitDefined () {
  +        return limitDefined;
  +    }
  +    
  +    
  +    /**
  +     * Needed to decide, which implementation of BasicQuery to load
  +     * (hier rausschmeissen, nach BasicSearchLanguage)
  +     * @param    basicSearchElementJDOMan Element
  +     *
  +     * @return   a QueryScope
  +     *
  +     * @throws   BadQueryException
  +     *
  +     */
  +    public static QueryScope getScope(Element basicSearchElementJDOM)
  +        throws BadQueryException
  +    {
  +        if (basicSearchElementJDOM == null)
  +            throw new BadQueryException (NO_QUERY_ELEMENT);
  +        
  +        Namespace namespace = basicSearchElementJDOM.getNamespace();
  +        Element fromElement = basicSearchElementJDOM.getChild
  +            (Literals.FROM, namespace);
  +        
  +        // FROM is mandatory
  +        if (fromElement == null)
  +            throw new BadQueryException (FROM_ELEMENT_MISSING);
  +        
  +        return new BasicQueryScope (fromElement);
  +    }
  +    
  +    /**
  +     * builds the internal structure from the JDOM tree. It may be used by the
  +     * concrete implementation of BasicQuery. It does NOT create the tree of
  +     * Expressions. This must be done in the specific implementation.
  +     *
  +     * @param    basicSearchElement                an Element
  +     *
  +     * @throws   BadQueryException
  +     */
  +    protected void parseQueryWithoutExpression (Element basicSearchElement)
  +        throws BadQueryException {
  +        
  +        if (basicSearchElement == null)
  +            throw new BadQueryException (NO_QUERY_ELEMENT);
  +        
  +        namespace = basicSearchElement.getNamespace();
  +        
  +        Element selectElement = basicSearchElement.getChild
  +            (Literals.SELECT, namespace);
  +        
  +        // SELECT is mandatory
  +        if (selectElement == null)
  +            throw new BadQueryException (SELECT_ELEMENT_MISSING);
  +        
  +        Element fromElement = basicSearchElement.getChild
  +            (Literals.FROM, namespace);
  +        
  +        // FROM is mandatory
  +        if (fromElement == null) {
  +            throw new BadQueryException (FROM_ELEMENT_MISSING);
  +        }
  +        
  +        whereElement = basicSearchElement.getChild
  +            (Literals.WHERE, namespace);
  +        
  +        Element orderByElement = basicSearchElement.getChild
  +            (Literals.ORDERBY, namespace);
  +        
  +        Element limitElement = basicSearchElement.getChild
  +            (Literals.LIMIT, namespace);
  +        
  +        Element propElement = selectElement.getChild (Literals.PROP, namespace);
  +        if (propElement == null) {
  +            propElement = selectElement.getChild (Literals.ALLPROP, namespace);
  +        }
  +        
  +        if (propElement == null) {
  +            throw new BadQueryException(PROP_OR_ALLPROP_ELEMENT_MISSING);
  +        }
  +        
  +        requestedProperties = createRequestedProperties (propElement);
  +        
  +        scope = new BasicQueryScope (fromElement);
  +        
  +        if (orderByElement != null) {
  +            orderBy = new OrderBy (orderByElement);
  +        }
  +        
  +        if (limitElement != null) {
  +            limit = new Integer (limitElement.getTextTrim()).intValue();
  +            limitDefined = true;
  +        }
  +    }
  +    
  +    /**
  +     * This method may be overridden, if a store specific implementation adds
  +     * new property semantic.
  +     *
  +     * @param    propElement         an Element
  +     *
  +     * @return   a RequestedProperties
  +     *
  +     */
  +    protected RequestedProperties createRequestedProperties (Element propElement) 
throws BadQueryException {
  +        try {
  +            return new RequestedPropertiesImpl (propElement);
  +        }
  +        catch (PropertyParseException e) {
  +            throw new BadQueryException (e.getMessage(), e);
  +        }
  +    }
   }
   
  
  
  

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

Reply via email to