Clover coverage report - Drools - 2.0-rc2
Coverage timestamp: Wed May 11 2005 07:12:26 BST
file stats: LOC: 309   Methods: 7
NCLOC: 162   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
SemanticsReader.java 78.1% 82.1% 71.4% 80.2%
coverage coverage
 1    package org.drools.smf;
 2   
 3    /*
 4    * $Id: SemanticsReader.java,v 1.9.2.1 2005/04/07 17:32:15 mproctor Exp $
 5    *
 6    * Copyright 2001-2003 (C) The Werken Company. All Rights Reserved.
 7    *
 8    * Redistribution and use of this software and associated documentation
 9    * ("Software"), with or without modification, are permitted provided that the
 10    * following conditions are met:
 11    *
 12    * 1. Redistributions of source code must retain copyright statements and
 13    * notices. Redistributions must also contain a copy of this document.
 14    *
 15    * 2. Redistributions in binary form must reproduce the above copyright notice,
 16    * this list of conditions and the following disclaimer in the documentation
 17    * and/or other materials provided with the distribution.
 18    *
 19    * 3. The name "drools" must not be used to endorse or promote products derived
 20    * from this Software without prior written permission of The Werken Company.
 21    * For written permission, please contact bob@werken.com.
 22    *
 23    * 4. Products derived from this Software may not be called "drools" nor may
 24    * "drools" appear in their names without prior written permission of The Werken
 25    * Company. "drools" is a trademark of The Werken Company.
 26    *
 27    * 5. Due credit should be given to The Werken Company. (http://werken.com/)
 28    *
 29    * THIS SOFTWARE IS PROVIDED BY THE WERKEN COMPANY AND CONTRIBUTORS ``AS IS''
 30    * AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 31    * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 32    * ARE DISCLAIMED. IN NO EVENT SHALL THE WERKEN COMPANY OR ITS CONTRIBUTORS BE
 33    * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 34    * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 35    * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 36    * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 37    * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 38    * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 39    * POSSIBILITY OF SUCH DAMAGE.
 40    *
 41    */
 42   
 43    import java.io.IOException;
 44    import java.io.InputStream;
 45    import java.net.URL;
 46    import java.util.Enumeration;
 47    import java.util.Properties;
 48   
 49    /**
 50    * Loader of <code>SemanticModule</code> s from a set of properties.
 51    *
 52    * <p>
 53    * The required properties are:
 54    *
 55    * <ul>
 56    * <li><code>module.uri=<i>uri</i></code>: To denote the URI of the
 57    * module.</li>
 58    * <li><code><i>tagname</i>=<i>classname</i></code>. For each semantic
 59    * component to associate a tag with the component.
 60    * </ul>
 61    * </p>
 62    *
 63    * <p>
 64    * Instances of <code>SemanticsReader</code> are re-entrant and thread-safe.
 65    * The singleton may be used simultaneously by multiple threads.
 66    * </p>
 67    *
 68    * @author <a href="mailto:bob@werken.com">bob mcwhirter </a>
 69    *
 70    * @version $Id: SemanticsReader.java,v 1.9.2.1 2005/04/07 17:32:15 mproctor Exp $
 71    */
 72    public class SemanticsReader
 73    {
 74    // ----------------------------------------------------------------------
 75    // Class members
 76    // ----------------------------------------------------------------------
 77   
 78    /** Singleton instance. */
 79    private static final SemanticsReader INSTANCE = new SemanticsReader( );
 80   
 81    // ----------------------------------------------------------------------
 82    // Class methods
 83    // ----------------------------------------------------------------------
 84   
 85    /**
 86    * Retrieve the singleton instance.
 87    *
 88    * @return The singleton instance.
 89    */
 90  0 public static SemanticsReader getInstance()
 91    {
 92  0 return INSTANCE;
 93    }
 94   
 95    // ----------------------------------------------------------------------
 96    // Constructors
 97    // ----------------------------------------------------------------------
 98   
 99    /**
 100    * Construct.
 101    */
 102  18 public SemanticsReader()
 103    {
 104    // intentionally left blank
 105    }
 106   
 107    // ----------------------------------------------------------------------
 108    // Instance methods
 109    // ----------------------------------------------------------------------
 110   
 111  0 public SemanticModule read(URL url) throws IOException, SemanticsReaderException
 112    {
 113  0 return read(url, Thread.currentThread( ).getContextClassLoader( ));
 114    }
 115   
 116    /**
 117    * Read a semantic module descriptor from a <code>URL</code>.
 118    *
 119    * @param url The descriptor URL.
 120    *
 121    * @return The loaded semantic module.
 122    * @throws IOException
 123    * @throws SemanticsReaderException
 124    */
 125  12 public SemanticModule read(URL url, ClassLoader cl) throws IOException,
 126    SemanticsReaderException
 127    {
 128  12 InputStream in = url.openStream( );
 129   
 130  12 try
 131    {
 132  12 return read( in, cl );
 133    }
 134    finally
 135    {
 136  12 in.close( );
 137    }
 138    }
 139   
 140    /**
 141    * Read a semantic module descriptor from an <code>InputStream</code>.
 142    *
 143    * @param in
 144    * The descriptor input stream.
 145    *
 146    * @return The loaded semantic module.
 147    *
 148    * @throws IOException
 149    * If an error occurs while loading the module.
 150    * @throws SemanticsReaderException
 151    * If an error occurs while loading the module.
 152    */
 153  12 public SemanticModule read(InputStream in, ClassLoader cl) throws IOException, SemanticsReaderException
 154    {
 155  12 Properties props = new Properties( );
 156   
 157  12 props.load( in );
 158   
 159  12 String uri = props.getProperty( "module.uri" );
 160   
 161  12 if ( uri == null || uri.trim( ).equals( "" ) )
 162    {
 163  0 throw new SemanticsReaderException( "module.uri must be specified" );
 164    }
 165   
 166  12 SimpleSemanticModule module = new SimpleSemanticModule( uri.trim( ) );
 167   
 168  12 for ( Enumeration propNames = props.propertyNames( ); propNames.hasMoreElements( ); )
 169    {
 170  80 String key = (String) propNames.nextElement( );
 171   
 172  80 if ( key.equals( "module.uri" ) )
 173    {
 174  12 continue;
 175    }
 176   
 177  68 String className = props.getProperty( key );
 178   
 179  68 Class factoryClass;
 180  68 try
 181    {
 182  68 factoryClass = cl.loadClass( className );
 183    }
 184    catch ( ClassNotFoundException e )
 185    {
 186  0 throw new SemanticsReaderException( e );
 187    }
 188  68 if ( key.indexOf( "(" ) < 0 || key.indexOf( ")" ) < 0 )
 189    {
 190  0 throw new SemanticsReaderException( "invalid key: " + key );
 191    }
 192   
 193  68 String type = parseType( key );
 194   
 195  68 if ( type == null || type.equals( "" ) )
 196    {
 197  0 throw new SemanticsReaderException( "no type specified" );
 198    }
 199   
 200  68 String componentName = parseName( key );
 201   
 202  68 if ( componentName == null || componentName.equals( "" ) )
 203    {
 204  0 throw new SemanticsReaderException( "no component name specified" );
 205    }
 206   
 207  68 try
 208    {
 209  68 if ( "Rule".equals( type ) )
 210    {
 211  7 RuleFactory factory = (RuleFactory) factoryClass.newInstance( );
 212   
 213  7 module.addRuleFactory( componentName,
 214    factory );
 215    }
 216  61 else if ( "ObjectType".equals( type ) )
 217    {
 218  23 ObjectTypeFactory factory = (ObjectTypeFactory) factoryClass.newInstance( );
 219   
 220  23 module.addObjectTypeFactory( componentName,
 221    factory );
 222    }
 223  38 else if ( "Condition".equals( type ) )
 224    {
 225  5 ConditionFactory factory = (ConditionFactory) factoryClass.newInstance( );
 226   
 227  5 module.addConditionFactory( componentName,
 228    factory );
 229    }
 230  33 else if ( "Consequence".equals( type ) )
 231    {
 232  5 ConsequenceFactory factory = (ConsequenceFactory) factoryClass.newInstance( );
 233   
 234  5 module.addConsequenceFactory( componentName,
 235    factory );
 236    }
 237  28 else if ( "Duration".equals( type ) )
 238    {
 239  6 DurationFactory factory = (DurationFactory) factoryClass.newInstance( );
 240   
 241  6 module.addDurationFactory( componentName,
 242    factory );
 243    }
 244  22 else if ( "ImportEntry".equals( type ) )
 245    {
 246  11 ImportEntryFactory factory = (ImportEntryFactory) factoryClass.newInstance( );
 247   
 248  11 module.addImportEntryFactory( componentName,
 249    factory );
 250    }
 251  11 else if ( "ApplicationData".equals( type ) )
 252    {
 253  6 ApplicationDataFactory factory = (ApplicationDataFactory) factoryClass.newInstance( );
 254   
 255  6 module.addApplicationDataFactory( componentName,
 256    factory );
 257    }
 258  5 else if ( "Functions".equals( type ) )
 259    {
 260  5 FunctionsFactory factory = (FunctionsFactory) factoryClass.newInstance( );
 261   
 262  5 module.addFunctionsFactory( componentName,
 263    factory );
 264    }
 265    else
 266    {
 267  0 throw new SemanticsReaderException( "unknown type '" + type + "'" );
 268    }
 269    }
 270    catch ( InstantiationException e )
 271    {
 272  0 throw new SemanticsReaderException( e );
 273    }
 274    catch ( IllegalAccessException e )
 275    {
 276  0 throw new SemanticsReaderException( e );
 277    }
 278    }
 279   
 280  12 return module;
 281    }
 282   
 283  68 protected String parseType(String key)
 284    {
 285  68 int leftParen = key.indexOf( "(" );
 286   
 287  68 if ( leftParen < 0 )
 288    {
 289  0 return null;
 290    }
 291   
 292  68 return key.substring( 0,
 293    leftParen ).trim( );
 294    }
 295   
 296  68 protected String parseName(String key)
 297    {
 298  68 int leftParen = key.indexOf( "(" );
 299  68 int rightParen = key.indexOf( ")" );
 300   
 301  68 if ( leftParen < 0 || rightParen < 0 )
 302    {
 303  0 return null;
 304    }
 305   
 306  68 return key.substring( leftParen + 1,
 307    rightParen ).trim( );
 308    }
 309    }