Clover coverage report - Drools - 2.0-rc2
Coverage timestamp: Wed May 11 2005 07:12:26 BST
file stats: LOC: 190   Methods: 5
NCLOC: 121   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
RuleHandler.java 75% 86% 100% 85.7%
coverage coverage
 1    /*
 2    * $Id: RuleHandler.java,v 1.3.2.1 2005/04/30 13:49:43 mproctor Exp $
 3    *
 4    * Copyright 2001-2003 (C) The Werken Company. All Rights Reserved.
 5    *
 6    * Redistribution and use of this software and associated documentation
 7    * ("Software"), with or without modification, are permitted provided that the
 8    * following conditions are met:
 9    *
 10    * 1. Redistributions of source code must retain copyright statements and
 11    * notices. Redistributions must also contain a copy of this document.
 12    *
 13    * 2. Redistributions in binary form must reproduce the above copyright notice,
 14    * this list of conditions and the following disclaimer in the documentation
 15    * and/or other materials provided with the distribution.
 16    *
 17    * 3. The name "drools" must not be used to endorse or promote products derived
 18    * from this Software without prior written permission of The Werken Company.
 19    * For written permission, please contact bob@werken.com.
 20    *
 21    * 4. Products derived from this Software may not be called "drools" nor may
 22    * "drools" appear in their names without prior written permission of The Werken
 23    * Company. "drools" is a trademark of The Werken Company.
 24    *
 25    * 5. Due credit should be given to The Werken Company. (http://werken.com/)
 26    *
 27    * THIS SOFTWARE IS PROVIDED BY THE WERKEN COMPANY AND CONTRIBUTORS ``AS IS''
 28    * AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 29    * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 30    * ARE DISCLAIMED. IN NO EVENT SHALL THE WERKEN COMPANY OR ITS CONTRIBUTORS BE
 31    * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 32    * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 33    * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 34    * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 35    * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 36    * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 37    * POSSIBILITY OF SUCH DAMAGE.
 38    *
 39    */
 40    package org.drools.io;
 41   
 42    import java.util.HashSet;
 43   
 44    import org.drools.rule.ApplicationData;
 45    import org.drools.rule.Rule;
 46    import org.drools.rule.RuleConstructionException;
 47    import org.drools.rule.RuleSet;
 48    import org.drools.smf.Configuration;
 49    import org.drools.smf.FactoryException;
 50    import org.drools.smf.RuleFactory;
 51    import org.drools.smf.SemanticModule;
 52    import org.drools.spi.Functions;
 53    import org.drools.spi.ImportEntry;
 54    import org.xml.sax.Attributes;
 55    import org.xml.sax.SAXException;
 56    import org.xml.sax.SAXParseException;
 57   
 58    /**
 59    * @author mproctor
 60    *
 61    * TODO To change the template for this generated type comment go to Window -
 62    * Preferences - Java - Code Style - Code Templates
 63    */
 64    class RuleHandler extends BaseAbstractHandler
 65    implements
 66    Handler
 67    {
 68  66 RuleHandler(RuleSetReader ruleSetReader)
 69    {
 70  66 this.ruleSetReader = ruleSetReader;
 71   
 72  66 if ( (this.validParents == null) && (this.validPeers == null) )
 73    {
 74  66 this.validParents = new HashSet( );
 75  66 this.validParents.add( RuleSet.class );
 76   
 77  66 this.validPeers = new HashSet( );
 78  66 this.validPeers.add( null );
 79  66 this.validPeers.add( Rule.class );
 80  66 this.validPeers.add( ImportEntry.class );
 81  66 this.validPeers.add( ApplicationData.class );
 82  66 this.validPeers.add( Functions.class );
 83   
 84  66 this.allowNesting = false;
 85    }
 86    }
 87   
 88  79 public Object start(String uri,
 89    String localName,
 90    Attributes attrs) throws SAXException
 91    {
 92   
 93  79 SemanticModule module = this.ruleSetReader.lookupSemanticModule( uri,
 94    localName );
 95   
 96  79 RuleFactory factory = module.getRuleFactory( localName );
 97   
 98  79 this.ruleSetReader.startConfiguration( localName,
 99    attrs );
 100   
 101  79 Configuration config = this.ruleSetReader.endConfiguration( );
 102   
 103  79 Rule rule;
 104  79 try
 105    {
 106  79 rule = factory.newRule( this.ruleSetReader.getRuleSet( ),
 107    this.ruleSetReader.getFactoryContext( ),
 108    config );
 109   
 110  78 startRule( rule,
 111    attrs );
 112    }
 113    catch ( FactoryException e )
 114    {
 115  1 throw new SAXParseException( "error constructing rule",
 116    this.ruleSetReader.getLocator( ),
 117    e );
 118    }
 119  78 return rule;
 120    }
 121   
 122  78 private void startRule(Rule rule,
 123    Attributes attrs) throws SAXException
 124    {
 125  78 String salienceStr = attrs.getValue( "salience" );
 126  78 String noLoopStr = attrs.getValue( "no-loop" );
 127  78 String ruleDesc = attrs.getValue( "description" );
 128   
 129  78 if ( !(salienceStr == null || salienceStr.trim( ).equals( "" )) )
 130    {
 131  16 try
 132    {
 133  16 int salience = Integer.parseInt( salienceStr.trim( ) );
 134   
 135  16 rule.setSalience( salience );
 136    }
 137    catch ( NumberFormatException e )
 138    {
 139  0 throw new SAXParseException( "invalid number value for 'salience' attribute: " + salienceStr.trim( ),
 140    this.ruleSetReader.getLocator( ) );
 141    }
 142    }
 143   
 144  78 if ( !(noLoopStr == null || noLoopStr.trim( ).equals( "" )) )
 145    {
 146  0 try
 147    {
 148  0 boolean noLoop = new Boolean( noLoopStr.trim( ) ).booleanValue( );
 149  0 rule.setNoLoop( noLoop );
 150    }
 151    catch ( NumberFormatException e )
 152    {
 153  0 throw new SAXParseException( "invalid boolean value for 'no-loop' attribute: " + salienceStr.trim( ),
 154    this.ruleSetReader.getLocator( ) );
 155    }
 156    }
 157   
 158  78 if ( !(ruleDesc == null || ruleDesc.trim( ).equals( "" )) )
 159    {
 160  15 rule.setDocumentation( ruleDesc );
 161    }
 162   
 163  78 rule.setImporter( this.ruleSetReader.getRuleSet( ).getImporter( ) );
 164  78 rule.setApplicationData( this.ruleSetReader.getRuleSet( ).getApplicationData( ) );
 165    }
 166   
 167  78 public Object end(String uri,
 168    String localName) throws SAXException
 169    {
 170  78 try
 171    {
 172  78 this.ruleSetReader.getRuleSet( ).addRule( (Rule) this.ruleSetReader.getParents( ).getLast( ) );
 173    }
 174    catch ( RuleConstructionException e )
 175    {
 176  0 throw new SAXParseException( "error adding rule",
 177    this.ruleSetReader.getLocator( ),
 178    e );
 179    }
 180   
 181  78 return null;
 182   
 183    }
 184   
 185  157 public Class generateNodeFor()
 186    {
 187  157 return Rule.class;
 188    }
 189   
 190    }