kevinross    2003/07/10 07:32:55

  Modified:    java/src/org/apache/xindice/core Collection.java
  Log:
  PR: 21463
  Patch Submitted by: Kevin O'Neill ([EMAIL PROTECTED])
  Reviewed by: Kevin Ross
  
  When a call is made to fetch resource list from a collection without a filer
  (like the database root collection) an error is thrown. The makes writing a
  generic collection browser most bothersome as you have to "know" that the root
  collection will throw exceptions with you ask for the number of resource it
  contains. A better option is to return empty constants in these cases. For
  example getResourceCount() will always return 0 when called on a collection
  without a filer. This patch enables that.
  
  Revision  Changes    Path
  1.19      +42 -18    
xml-xindice/java/src/org/apache/xindice/core/Collection.java
  
  Index: Collection.java
  ===================================================================
  RCS file: 
/home/cvs/xml-xindice/java/src/org/apache/xindice/core/Collection.java,v
  retrieving revision 1.18
  retrieving revision 1.19
  diff -u -r1.18 -r1.19
  --- Collection.java   13 Jun 2003 14:09:13 -0000      1.18
  +++ Collection.java   10 Jul 2003 14:32:54 -0000      1.19
  @@ -60,6 +60,8 @@
    */
   
   import org.apache.xindice.core.data.DocumentSet;
  +import org.apache.xindice.core.data.EmptyDocumentSet;
  +import org.apache.xindice.core.data.EmptyNodeSet;
   import org.apache.xindice.core.data.Key;
   import org.apache.xindice.core.data.NodeSet;
   import org.apache.xindice.core.data.Record;
  @@ -117,6 +119,10 @@
        private static final String SYMBOLS = "symbols";
        private static final String CLASSNAME = "xindice-class";
   
  +     private static final DocumentSet EMPTY_DOCUMENTSET = new 
EmptyDocumentSet();
  +     private static final NodeSet EMPTY_NODESET = new EmptyNodeSet();
  +     private static final String[] EMPTY_STRING_ARRAY = {};
  +
        private static Log log = LogFactory.getLog("org.apache.xindice.core");
   
        private static int host_id;
  @@ -1225,8 +1231,10 @@
         * @return The resulting NodeSet
         */
        public final NodeSet queryCollection(String style, String query, 
NamespaceMap nsMap) throws DBException {
  -             checkFiler(FaultCodes.QRY_STYLE_NOT_FOUND);
  -             return getQueryEngine().query(this, style, query, nsMap, null);
  +             // a collection in which you are unable to file documents will 
have no filer 
  +             // (for example the root collection). Rather than throwing an 
exception return 
  +             // a constant result (nothing)
  +             return null == filer ? EMPTY_NODESET : 
getQueryEngine().query(this, style, query, nsMap, null);
        }
   
        /**
  @@ -1256,9 +1264,10 @@
         * @return The DocumentSet
         */
        public final DocumentSet getDocumentSet() throws DBException {
  -             checkFiler(FaultCodes.COL_NO_FILER);
  -
  -             return new ColDocumentSet(filer.getRecordSet());
  +             // a collection in which you are unable to file documents will 
have no filer 
  +             // (for example the root collection). Rather than throwing an 
exception return 
  +             // a constant result (nothing)
  +             return null == filer ? EMPTY_DOCUMENTSET : new 
ColDocumentSet(filer.getRecordSet());
        }
   
        /**
  @@ -1268,17 +1277,31 @@
         * @return the list of document keys
         */
        public final String[] listDocuments() throws DBException {
  -             checkFiler(FaultCodes.COL_NO_FILER);
  -
  -             RecordSet set = filer.getRecordSet();
  -             ArrayList temp = new ArrayList();
  -
  -             while (set.hasMoreRecords()) {
  -                     Key key = set.getNextKey();
  -                     temp.add(key.toString());
  +             // a collection in which you are unable to file documents will 
have no filer 
  +             // (for example the root collection). Rather than throwing an 
exception return 
  +             // a constant result (nothing)          
  +             if (null == filer)
  +             {
  +                     return EMPTY_STRING_ARRAY;                              
                
                }
  +             else
  +             {
  +                     RecordSet set = filer.getRecordSet();
   
  -             return (String[]) temp.toArray(new String[0]);
  +                     // todo: what happens if the size if > than the size of 
an int.
  +                     // I'm pretty sure some sort of runtime exception will 
occur 
  +                     // in the ArrayList.add method.                         
  +
  +                     // give a hint to the size of the record set, saves on 
arraylist array copies.
  +                     ArrayList temp = new 
ArrayList((int)filer.getRecordCount());
  +     
  +                     while (set.hasMoreRecords()) {
  +                             Key key = set.getNextKey();
  +                             temp.add(key.toString());
  +                     }
  +     
  +                     return (String[]) temp.toArray(new String[0]);
  +             }
        }
   
        /**
  @@ -1288,9 +1311,10 @@
         * @return The Document count
         */
        public final long getDocumentCount() throws DBException {
  -             checkFiler(FaultCodes.COL_NO_FILER);
  -
  -             return filer.getRecordCount();
  +             // a collection in which you are unable to file documents will 
have no filer 
  +             // (for example the root collection). Rather than throwing an 
exception return 
  +             // a constant result (nothing)
  +             return null == filer ? 0 : filer.getRecordCount();
        }
   
        public void dispose() {
  
  
  

Reply via email to