[jsword-svn] jsword/java/jsword/org/crosswire/jsword/book s

jswordcvs at crosswire.org jswordcvs at crosswire.org
Wed Jul 27 14:35:09 MST 2005


Update of /cvs/jsword/jsword/java/jsword/org/crosswire/jsword/book
In directory www.crosswire.org:/tmp/cvs-serv20562/java/jsword/org/crosswire/jsword/book

Modified Files:
	BookFilters.java 
Log Message:
new custom filter method

Index: BookFilters.java
===================================================================
RCS file: /cvs/jsword/jsword/java/jsword/org/crosswire/jsword/book/BookFilters.java,v
retrieving revision 1.14
retrieving revision 1.15
diff -C2 -d -r1.14 -r1.15
*** BookFilters.java	11 Jun 2005 20:41:05 -0000	1.14
--- BookFilters.java	27 Jul 2005 21:35:07 -0000	1.15
***************
*** 22,25 ****
--- 22,31 ----
  package org.crosswire.jsword.book;
  
+ import java.lang.reflect.Method;
+ import java.util.ArrayList;
+ import java.util.List;
+ 
+ import org.crosswire.common.util.Logger;
+ 
  /**
   * Some common implementations of BookFilter.
***************
*** 275,277 ****
--- 281,379 ----
          };
      }
+ 
+     /**
+      * A simple default filter that returns everything.
+      * The match parameter is a set of name value pairs like this:
+      * <br/>
+      * <code>initials=ESV;type=Bible;driverName=Sword</code><br/>
+      * Before the = there must be the name of a property on Book and after
+      * the value to match (.toString()) is called on the results of the getter.
+      * @param match a ; separated list of properties (of Book) to match
+      * @see Book
+      */
+     public static BookFilter getCustom(String match)
+     {
+         return new CustomBookFilter(match);
+     }
+ 
+     /**
+      * Custom Filter
+      */
+     private static class CustomBookFilter implements BookFilter
+     {
+         /**
+          * Ctor
+          * @param match The match spec.
+          * @see BookFilters#getCustom(String)
+          */
+         public CustomBookFilter(String match)
+         {
+             List cache = new ArrayList();
+             String[] filters = match.split(";"); //$NON-NLS-1$
+             for (int i = 0; i < filters.length; i++ )
+             {
+                 String[] parts = filters[i].split("="); //$NON-NLS-1$
+                 if (parts.length != 2 || parts[0].length() == 0 || parts[1].length() == 0)
+                 {
+                     throw new IllegalArgumentException("Filter format is 'property=value', given: " + filters[i]); //$NON-NLS-1$
+                 }
+ 
+                 Test test = new Test();
+ 
+                 String gettername = "get" + Character.toTitleCase(parts[0].charAt(0)) + parts[0].substring(1); //$NON-NLS-1$
+                 try
+                 {
+                     test.property = Book.class.getMethod(gettername, null);
+                     test.result = parts[1];
+                 }
+                 catch (Exception ex)
+                 {
+                     throw new IllegalArgumentException("Missing property: " + parts[0] + " in Book"); //$NON-NLS-1$ //$NON-NLS-2$
+                 }
+ 
+                 cache.add(test);
+             }
+ 
+             tests = (Test[]) cache.toArray(new Test[cache.size()]);
+         }
+ 
+         /* (non-Javadoc)
+          * @see org.crosswire.jsword.book.BookFilter#test(org.crosswire.jsword.book.Book)
+          */
+         public boolean test(Book book)
+         {
+             for (int i = 0; i < tests.length; i++ )
+             {
+                 Test test = tests[i];
+                 try
+                 {
+                     Object result = test.property.invoke(book, null);
+                     if (!test.result.equals(result.toString()))
+                     {
+                         return false;
+                     }
+                 }
+                 catch (Exception ex)
+                 {
+                     log.warn("Error while testing property " + test.property.getName() + " on " + book.getName(), ex); //$NON-NLS-1$ //$NON-NLS-2$
+                     return false;
+                 }
+             }
+ 
+             return true;
+         }
+         
+         private Test[] tests;
+ 
+         private class Test
+         {
+             protected String result;
+             protected Method property;
+         }
+     }
+ 
+     /**
+      * The log stream
+      */
+     protected static final Logger log = Logger.getLogger(BookFilters.class);
  }



More information about the jsword-svn mailing list