Clover coverage report - Drools - 2.0-rc2
Coverage timestamp: Wed May 11 2005 07:12:26 BST
file stats: LOC: 282   Methods: 9
NCLOC: 180   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
DefaultImporter.java 95.2% 87.1% 66.7% 88.4%
coverage coverage
 1    package org.drools.smf;
 2   
 3    import java.util.Collections;
 4    import java.util.HashMap;
 5    import java.util.HashSet;
 6    import java.util.Iterator;
 7    import java.util.Map;
 8    import java.util.Set;
 9   
 10    import org.drools.spi.ImportEntry;
 11    import org.drools.spi.Importer;
 12   
 13    /*
 14    * $Id: DefaultImporter.java,v 1.1.2.3 2005/05/03 23:45:47 mproctor Exp $
 15    *
 16    * Copyright 2001-2003 (C) The Werken Company. All Rights Reserved.
 17    *
 18    * Redistribution and use of this software and associated documentation
 19    * ("Software"), with or without modification, are permitted provided that the
 20    * following conditions are met:
 21    *
 22    * 1. Redistributions of source code must retain copyright statements and
 23    * notices. Redistributions must also contain a copy of this document.
 24    *
 25    * 2. Redistributions in binary form must reproduce the above copyright notice,
 26    * this list of conditions and the following disclaimer in the documentation
 27    * and/or other materials provided with the distribution.
 28    *
 29    * 3. The name "drools" must not be used to endorse or promote products derived
 30    * from this Software without prior written permission of The Werken Company.
 31    * For written permission, please contact bob@werken.com.
 32    *
 33    * 4. Products derived from this Software may not be called "drools" nor may
 34    * "drools" appear in their names without prior written permission of The Werken
 35    * Company. "drools" is a trademark of The Werken Company.
 36    *
 37    * 5. Due credit should be given to The Werken Company. (http://werken.com/)
 38    *
 39    * THIS SOFTWARE IS PROVIDED BY THE WERKEN COMPANY AND CONTRIBUTORS ``AS IS''
 40    * AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 41    * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 42    * ARE DISCLAIMED. IN NO EVENT SHALL THE WERKEN COMPANY OR ITS CONTRIBUTORS BE
 43    * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 44    * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 45    * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 46    * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 47    * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 48    * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 49    * POSSIBILITY OF SUCH DAMAGE.
 50    *
 51    */
 52   
 53    public class DefaultImporter
 54    implements
 55    Importer
 56    {
 57    private Set importEntrySet = Collections.EMPTY_SET;
 58   
 59    private Set importSet = Collections.EMPTY_SET;
 60   
 61    private Map cachedImports = Collections.EMPTY_MAP;
 62   
 63  106 public DefaultImporter()
 64    {
 65  106 super( );
 66    }
 67   
 68    /*
 69    * (non-Javadoc)
 70    *
 71    * @see org.drools.semantics.base.Importer#getImports()
 72    */
 73  0 public Set getImportEntries()
 74    {
 75  0 return this.importEntrySet;
 76    }
 77   
 78    /*
 79    * (non-Javadoc)
 80    *
 81    * @see org.drools.semantics.base.Importer#getImports( Class clazz )
 82    */
 83  265 public Set getImports()
 84    {
 85  265 if (! importEntrySet.isEmpty( ) )
 86    {
 87  97 if ( importSet == Collections.EMPTY_SET )
 88    {
 89  22 importSet = new HashSet( );
 90    }
 91   
 92  97 Iterator i = this.importEntrySet.iterator( );
 93  97 ImportEntry importEntry;
 94  97 while ( i.hasNext( ) )
 95    {
 96  216 importSet.add( ( (ImportEntry) i.next( ) ).getImportEntry() );
 97    }
 98    }
 99   
 100  265 return importSet;
 101    }
 102   
 103    /*
 104    * (non-Javadoc)
 105    *
 106    * @see org.drools.semantics.base.Importer#addImports(org.drools.spi.ImportEntry)
 107    */
 108  55 public void addImport(ImportEntry importEntry)
 109    {
 110  55 if ( this.importEntrySet == Collections.EMPTY_SET )
 111    {
 112  30 this.importEntrySet = new HashSet( );
 113    }
 114  55 this.importEntrySet.add( importEntry );
 115    }
 116   
 117  42 public Class lookupFromCache(String className)
 118    {
 119  42 if ( cachedImports == Collections.EMPTY_MAP )
 120    {
 121  23 return null;
 122    }
 123   
 124  19 return (Class) cachedImports.get( className );
 125   
 126    }
 127   
 128    /*
 129    * (non-Javadoc)
 130    *
 131    * @see org.drools.semantics.base.Importer#importClass(java.lang.ClassLoader,
 132    * java.lang.String)
 133    */
 134  188 public Class importClass(ClassLoader cl,
 135    String className) throws ClassNotFoundException
 136    {
 137  188 Class clazz = null;
 138   
 139    /* first try loading className */
 140  188 try
 141    {
 142  188 clazz = cl.loadClass( className );
 143    }
 144    catch ( ClassNotFoundException e )
 145    {
 146  42 clazz = null;
 147    }
 148   
 149    /* Now try the ruleset object type cache */
 150  188 if ( clazz == null )
 151    {
 152  42 clazz = lookupFromCache( className );
 153    }
 154   
 155    /* Now try the className with each of the given imports */
 156  188 if ( clazz == null )
 157    {
 158  41 Set validClazzCandidates = new HashSet( );
 159   
 160  41 Iterator it = importEntrySet.iterator( );
 161  41 while ( it.hasNext( ) )
 162    {
 163  72 clazz = importClass( cl,
 164    ((ImportEntry) it.next( )).getImportEntry( ),
 165    className.trim( ) );
 166  72 if ( clazz != null )
 167    {
 168  40 validClazzCandidates.add( clazz );
 169    }
 170    }
 171   
 172    /*
 173    * If there are more than one possible resolutions, complain about
 174    * the ambiguity
 175    */
 176  41 if ( validClazzCandidates.size( ) > 1 )
 177    {
 178  1 StringBuffer sb = new StringBuffer( );
 179  1 Iterator clazzCandIter = validClazzCandidates.iterator( );
 180  1 while ( clazzCandIter.hasNext( ) )
 181    {
 182  2 if ( 0 != sb.length( ) )
 183    {
 184  1 sb.append( ", " );
 185    }
 186  2 sb.append( ((Class) clazzCandIter.next( )).getName( ) );
 187    }
 188  1 throw new Error( "Unable to find unambiguously defined class '" + className + "', candidates are: [" + sb.toString( ) + "]" );
 189    }
 190  40 else if ( validClazzCandidates.size( ) == 1 )
 191    {
 192  38 clazz = (Class) validClazzCandidates.toArray( )[0];
 193    }
 194    else
 195    {
 196  2 clazz = null;
 197    }
 198   
 199    }
 200   
 201    /* We still can't find the class so throw an exception */
 202  187 if ( clazz == null )
 203    {
 204  2 throw new ClassNotFoundException( "Unable to find class '" + className + "'" );
 205    }
 206   
 207  185 return clazz;
 208    }
 209   
 210  72 private Class importClass(ClassLoader cl,
 211    String importText,
 212    String className)
 213    {
 214  72 String qualifiedClass = null;
 215  72 Class clazz = null;
 216   
 217  72 String convertedImportText;
 218  72 if ( importText.startsWith( "from " ) )
 219    {
 220  0 convertedImportText = convertFromPythonImport( importText );
 221    }
 222    else
 223    {
 224  72 convertedImportText = importText;
 225    }
 226   
 227    // not python
 228  72 if ( convertedImportText.endsWith( "*" ) )
 229    {
 230  7 qualifiedClass = convertedImportText.substring( 0,
 231    convertedImportText.indexOf( '*' ) ) + className;
 232    }
 233  65 else if ( convertedImportText.endsWith( "." + className ) )
 234    {
 235  34 qualifiedClass = convertedImportText;
 236    }
 237  31 else if ( convertedImportText.equals( className ) )
 238    {
 239  0 qualifiedClass = convertedImportText;
 240    }
 241   
 242  72 if ( qualifiedClass != null )
 243    {
 244  41 try
 245    {
 246  41 clazz = cl.loadClass( qualifiedClass );
 247    }
 248    catch ( ClassNotFoundException e )
 249    {
 250  1 clazz = null;
 251    }
 252    }
 253   
 254  72 if ( clazz != null )
 255    {
 256  40 if ( this.cachedImports == Collections.EMPTY_MAP )
 257    {
 258  23 this.cachedImports = new HashMap( );
 259    }
 260   
 261  40 this.cachedImports.put( className,
 262    clazz );
 263    }
 264   
 265  72 return clazz;
 266    }
 267   
 268  0 private String convertFromPythonImport(String packageText)
 269    {
 270  0 String fromString = "from ";
 271  0 String importString = "import ";
 272  0 int fromIndex = packageText.indexOf( fromString );
 273  0 int importIndex = packageText.indexOf( importString );
 274  0 return packageText.substring( fromIndex + fromString.length( ),
 275    importIndex ).trim( ) + "." + packageText.substring( importIndex + importString.length( ) ).trim( );
 276    }
 277   
 278  0 public boolean isEmpty()
 279    {
 280  0 return this.importEntrySet.isEmpty( );
 281    }
 282    }