commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From di...@apache.org
Subject cvs commit: jakarta-commons-sandbox/scaffold/src/java/org/apache/commons/scaffold/lucene SearchUtils.java Engine.java
Date Sun, 21 Mar 2004 21:10:34 GMT
dirkv       2004/03/21 13:10:34

  Modified:    scaffold/src/java/org/apache/commons/scaffold/lucene
                        SearchUtils.java Engine.java
  Log:
  Bugzilla Bug 27689: [scaffold] changing to the Apache 2.0 license removed source file contents.
  
  Revision  Changes    Path
  1.8       +105 -15   jakarta-commons-sandbox/scaffold/src/java/org/apache/commons/scaffold/lucene/SearchUtils.java
  
  Index: SearchUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/scaffold/src/java/org/apache/commons/scaffold/lucene/SearchUtils.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- SearchUtils.java	28 Feb 2004 03:35:45 -0000	1.7
  +++ SearchUtils.java	21 Mar 2004 21:10:34 -0000	1.8
  @@ -1,3 +1,19 @@
  + /*
  + * Copyright 2001,2004 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.commons.scaffold.lucene;
   
   
  @@ -14,18 +30,92 @@
   import org.apache.lucene.search.Hits;
   
   
  - /*
  - * Copyright 2001,2004 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.
  - */
  + /**
  +  * General purpose utility methods related to Hits
  +  *
  +  * @author Craig R. McClanahan
  +  * @author Ted Husted
  +  * @version $Revision$ $Date$
  +  */
  + public class SearchUtils {
  +
  +    /**
  +     * Populate the properties of the specified JavaBean from the specified
  +     * Lucene document, based on matching each parameter name against the
  +     * corresponding JavaBeans "property setter" methods in the bean's class.
  +     * See <code>BeanUtils.CopyProperites</code> for more about automatic 
  +     * conversion between types.
  +     *
  +     * @param bean The JavaBean whose properties are to be set
  +     * @param document The Lucene document whose parameters are to be used
  +     * to populate bean properties
  +     * @exception PopulateException if an exception is thrown while setting
  +     * property values
  +     */
  +    public static void populate(
  +            Object bean,
  +            Document document)
  +        throws PopulateException {
  +
  +        // Build a list of relevant fields and values
  +        HashMap properties = new HashMap();
  +
  +        // Iterator of field names
  +        Enumeration fields = document.fields();
  +
  +        while (fields.hasMoreElements()) {
  +            Field field = (Field) fields.nextElement();
  +            properties.put(field.name(),field.stringValue());
  +        }
  +
  +        // Set the corresponding properties of our bean
  +        try {
  +            BeanUtils.copyProperties(bean, properties);
  +        } catch (Throwable t) {
  +            throw new PopulateException(t);
  +        }
  +
  +    } // end populate()
  +
  +
  +    /**
  +      * Return a ArrayList of hits by looping and calling populate.
  +      * No assumptions are made about fields in document. Each is
  +      * processed with a separate call to populate. Whatever fields
  +      * in each document that match the target bean will be
  +      * transferred.
  +      *
  +      * @param hits The Hits whose documents are to be used
  +      * to populate bean properties
  +      * @param target An instance of the bean to populate
  +      * @exception PopulateException if an exception is thrown while setting
  +      * property values, populating the bean, or accessing the Hits
  +      */
  +     public static Collection getCollection(
  +            Object target,
  +            Hits hits)
  +        throws PopulateException {
  +
  +        // Use ArrayList to maintain sequence
  +        ArrayList list = new ArrayList();
  +
  +        // Acquire target class
  +        Class factory = target.getClass();
  +
  +        try {
  +            // Scroll to each document; populate bean, and add to list
  +            for (int i=0; i<hits.length(); ++i) {
  +                Object bean = factory.newInstance();
  +                Document doc = hits.doc(i);
  +                populate(bean,doc);
  +                list.add(bean);
  +            }
  +        } catch (Throwable t) {
  +            throw new PopulateException(t);
  +        }
  +        return ((Collection) list);
  +
  +    } // end getCollection()
  +
  +} // end SearchUtils
  +
  
  
  
  1.5       +235 -14   jakarta-commons-sandbox/scaffold/src/java/org/apache/commons/scaffold/lucene/Engine.java
  
  Index: Engine.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/scaffold/src/java/org/apache/commons/scaffold/lucene/Engine.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- Engine.java	28 Feb 2004 03:35:45 -0000	1.4
  +++ Engine.java	21 Mar 2004 21:10:34 -0000	1.5
  @@ -1,3 +1,19 @@
  +/*
  + * Copyright 2001,2004 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.commons.scaffold.lucene;
   
   
  @@ -19,18 +35,223 @@
   import org.apache.lucene.search.Searcher;
   
   
  -/*
  - * Copyright 2001,2004 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.
  +/**
  + * Search engine methods for Lucene.
  + *
  + * @author Ted Husted
  + * @version $Revision$ $Date$
  + * @todo Make singleton instead of static
    */
  +public final class Engine {
  +
  +
  +    /**
  +     * Default path for index ["/var/lucene/scaffold"].
  +     * Use the <code>setIndexPath</code> method on startup
  +     * to change.
  +     * Each application needs its own index path.
  +     */
  +    private static String indexPath;
  +
  +
  +    /**
  +     * Return path for index.
  +     */
  +    public static String getIndexPath() {
  +        return indexPath;
  +    }
  +
  +
  +    /**
  +     * Set path for index.
  +     * MUST be set at startup before using other methods.
  +     */
  +    public static void init(String _indexPath) {
  +        indexPath = _indexPath;
  +    }
  +
  +
  +    /**
  +     * Performance optimization to ensure JVM doesn't
  +     * create a new empty string whenever blankNull
  +     * needs one.
  +     */
  +    private static final String BLANK_STRING = "";
  +
  +
  +    /**
  +     * Convenience method to use an empty string in place
  +     * of a null for indexing.
  +     *
  +     * @param string The string to test for null
  +     * @return A blank String [BLANK_STRING] if string is null
  +     */
  +    public static final String blankNull(String string) {
  +        if (string==null) return BLANK_STRING;
  +        else return string;
  +    }
  +
  +
  +    /**
  +     * Return default analyzer for application
  +     * A non-English site should use a different analyzer
  +     *
  +     * @return The default analyzer
  +     * @exception Throws ResourceException on IO error
  +     */
  +    public static final Analyzer getAnalyzer() {
  +        return new StopAnalyzer();
  +    }
  +
  +
  +    /**
  +     * Return default searcher for application.
  +     *
  +     * @exception Throws ResourceException on IO error
  +     * @return default searcher
  +     */
  +    public static final Searcher getSearcher()
  +            throws ResourceException {
  +
  +        try {
  +
  +            return new IndexSearcher(getIndexPath());
  +
  +        }
  +        catch (IOException e) {
  +            throw new ResourceException(e);
  +        }
  +
  +    } // end getSearcher()
  +
  +
  +    /**
  +     * Return default writer for application.
  +     *
  +     * @param Set to true to create a new index; usually false.
  +     * @return default writer
  +     * @exception Throws ResourceException on IO error
  +     */
  +    public static final IndexWriter getIndexWriter(boolean create)
  +        throws ResourceException {
  +
  +        try {
  +
  +            return (new IndexWriter(getIndexPath(),
  +                getAnalyzer(),create));
  +
  +        }
  +        catch (IOException e) {
  +            throw new ResourceException(e);
  +        }
  +
  +    } // end getIndexWriter()
  +
  +
  +    /**
  +     * Return default reader for application.
  +     *
  +     * @return default reader
  +     * @exception Throws ResourceException on IO error
  +     */
  +    public static final IndexReader getIndexReader()
  +        throws ResourceException {
  +
  +        try {
  +
  +            return (IndexReader.open(getIndexPath()));
  +
  +        }
  +        catch (IOException e) {
  +            throw new ResourceException(e);
  +        }
  +
  +    } // end getIndexReader()
  +
  +
  +    /**
  +     * Return hits for model using default searcher.
  +     *
  +     * @return hits for model
  +     * @param query The query we are processing
  +     */
  +    public static final Hits getHits(Query query)
  +        throws ResourceException {
  +
  +        try {
  +
  +            return getSearcher().search(query);
  +
  +        }
  +        catch (IOException e) {
  +            throw new ResourceException(e);
  +        }
  +
  +    } // end getHits()
  +
  +
  +    /**
  +     * Return hits for value and field
  +     *
  +     * @param value The term to match ("Smith")
  +     * @param field The field to patch ("name")
  +     * @return hits for model
  +     */
  +    public static final Query getQuery(
  +            String value,
  +            String field) throws ParameterException {
  +
  +        try {
  +
  +            return QueryParser.parse(value,field, getAnalyzer());
  +
  +        }
  +        catch (ParseException e) {
  +            throw new ParameterException(e);
  +        }
  +
  +    } // end getQuery()
  +
  +
  +    /**
  +     * Object for synchronizing deletions to index
  +     */
  +    private static final Object INDEX_LOCK = new Object();
  +
  +
  +    /**
  +     * Delete record matching term from default index.
  +     * Access to the index is synchronized.
  +     *
  +     * @param term The term matching the entry to delete
  +     * @exception Throws ResourceException on IO error
  +     */
  +    public static final int deleteTerm(Term term)
  +        throws ResourceException {
  +
  +        int result = 0;
  +        synchronized (INDEX_LOCK) {
  +            IndexReader reader = null;
  +            try {
  +                reader = getIndexReader();
  +                result = reader.delete(term);
  +            }
  +            catch (IOException e) {
  +                throw new ResourceException(e);
  +            }
  +            finally {
  +                try {
  +                    if (reader!=null) reader.close();
  +                }
  +                catch (IOException e) {
  +                // do nothing
  +                }
  +            }
  +        }
  +        return result;
  +
  +    } // end deleteTerm()
  +
  +
  +} // end Engine
  +
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message