📄 rulesetreader.java
字号:
package org.drools.io;/* * $Id: RuleSetReader.java,v 1.38 2004/11/28 20:01:12 mproctor Exp $ * * Copyright 2001-2003 (C) The Werken Company. All Rights Reserved. * * Redistribution and use of this software and associated documentation * ("Software"), with or without modification, are permitted provided that the * following conditions are met: * * 1. Redistributions of source code must retain copyright statements and * notices. Redistributions must also contain a copy of this document. * * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * 3. The name "drools" must not be used to endorse or promote products derived * from this Software without prior written permission of The Werken Company. * For written permission, please contact bob@werken.com. * * 4. Products derived from this Software may not be called "drools" nor may * "drools" appear in their names without prior written permission of The Werken * Company. "drools" is a trademark of The Werken Company. * * 5. Due credit should be given to The Werken Company. (http://werken.com/) * * THIS SOFTWARE IS PROVIDED BY THE WERKEN COMPANY AND CONTRIBUTORS ``AS IS'' * AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE WERKEN COMPANY OR ITS CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * */import org.drools.rule.RuleSet;import org.drools.smf.Configuration;import org.drools.smf.DefaultSemanticsRepository;import org.drools.smf.NoSuchSemanticModuleException;import org.drools.smf.SemanticModule;import org.drools.smf.SemanticsRepository;import org.xml.sax.Attributes;import org.xml.sax.InputSource;import org.xml.sax.Locator;import org.xml.sax.SAXException;import org.xml.sax.SAXNotRecognizedException;import org.xml.sax.SAXParseException;import org.xml.sax.helpers.DefaultHandler;import javax.xml.parsers.ParserConfigurationException;import javax.xml.parsers.SAXParser;import javax.xml.parsers.SAXParserFactory;import java.io.BufferedInputStream;import java.io.FileInputStream;import java.io.InputStream;import java.io.Reader;import java.net.URL;import java.text.MessageFormat;import java.util.HashMap;import java.util.Iterator;import java.util.LinkedList;import java.util.ListIterator;import java.util.Map;import java.util.Set;/** * <code>RuleSet</code> loader. * * @author <a href="mailto:bob@werken.com">bob mcwhirter </a> * * @version $Id: RuleSetReader.java,v 1.38 2004/11/28 20:01:12 mproctor Exp $ */public class RuleSetReader extends DefaultHandler{ // ---------------------------------------------------------------------- // Constants // ---------------------------------------------------------------------- /** Namespace URI for the general tags. */ public static final String RULES_NAMESPACE_URI = "http://drools.org/rules"; private static final String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage"; private static final String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema"; // ---------------------------------------------------------------------- // Instance members // ---------------------------------------------------------------------- /** SAX parser. */ private SAXParser parser; /** Locator for errors. */ private Locator locator; /** Repository of semantic modules. */ private SemanticsRepository repo; //private Map repo; /** Stack of configurations. */ private LinkedList configurationStack; /** Current configuration text. */ private StringBuffer characters2; private Map localNameMap; private boolean lastWasEndElement; private LinkedList parents; private Object peer; private Object current; private RuleSet ruleSet; private MessageFormat message = new MessageFormat( "({0}: {1}, {2}): {3}" ); // ---------------------------------------------------------------------- // Constructors // ---------------------------------------------------------------------- /** * Construct. * * <p> * Uses the default JAXP SAX parser and the default classpath-based * <code>DefaultSemanticModule</code>. * </p> */ public RuleSetReader() { //init this.configurationStack = new LinkedList( ); this.parents = new LinkedList( ); this.localNameMap = new HashMap( ); localNameMap.put( "rule-set", new RuleSetHandler( this ) ); localNameMap.put( "import", new ImportHandler( this ) ); localNameMap.put( "application-data", new ApplicationDataHandler( this ) ); localNameMap.put( "rule", new RuleHandler( this ) ); localNameMap.put( "parameter", new ParameterHandler( this ) ); //localNameMap.put( "declaration", new DeclarationHandler( this ) ); localNameMap.put( "class", new ObjectTypeHandler( this ) ); localNameMap.put( "class-field", new ObjectTypeHandler( this ) ); localNameMap.put( "condition", new ConditionHandler( this ) ); localNameMap.put( "duration", new DurationHandler( this ) ); localNameMap.put( "consequence", new ConsequenceHandler( this ) ); } /** * Construct. * * <p> * Uses the default classpath-based <code>DefaultSemanticModule</code>. * </p> * * @param parser * The SAX parser. */ public RuleSetReader( SAXParser parser ) { this( ); this.parser = parser; } /** * Construct. * * @param repo * The semantics repository. * @param parser * The SAX parser. */ public RuleSetReader( SemanticsRepository repo, SAXParser parser ) { this( parser ); this.repo = repo; } /** * Construct. * * @param repo * The semantics repository. */ public RuleSetReader( SemanticsRepository repo ) { this( ); this.repo = repo; } // ---------------------------------------------------------------------- // Instance methods // ---------------------------------------------------------------------- /** * Read a <code>RuleSet</code> from a <code>URL</code>. * * @param url * The rule-set URL. * * @return The rule-set. * * @throws Exception * If an error occurs during the parse. */ public RuleSet read( URL url ) throws Exception { return read( new InputSource( url.toExternalForm( ) ) ); } /** * Read a <code>RuleSet</code> from a <code>Reader</code>. * * @param reader * The reader containing the rule-set. * * @return The rule-set. * * @throws Exception * If an error occurs during the parse. */ public RuleSet read( Reader reader ) throws Exception { return read( new InputSource( reader ) ); } /** * Read a <code>RuleSet</code> from an <code>InputStream</code>. * * @param inputStream * The input-stream containing the rule-set. * * @return The rule-set. * * @throws Exception * If an error occurs during the parse. */ public RuleSet read( InputStream inputStream ) throws Exception { return read( new InputSource( inputStream ) ); } /** * Read a <code>RuleSet</code> from a URL. * * @param url * The rule-set URL. * * @return The rule-set. * * @throws Exception * If an error occurs during the parse. */ public RuleSet read( String url ) throws Exception { return read( new InputSource( url ) ); } /** * Read a <code>RuleSet</code> from an <code>InputSource</code>. * * @param in * The rule-set input-source. * * @return The rule-set. * * @throws Exception * If an error occurs during the parse. */ public RuleSet read( InputSource in ) throws Exception { SAXParser parser; if ( this.parser == null ) { SAXParserFactory factory = SAXParserFactory.newInstance( ); factory.setNamespaceAware( true ); String isValidating = System .getProperty( "drools.schema.validating" ); if ( isValidating == null ) isValidating = "true"; factory.setValidating( Boolean.valueOf( isValidating ) .booleanValue( ) ); parser = factory.newSAXParser( ); try { parser.setProperty( JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA ); } catch ( SAXNotRecognizedException e ) { System.err .println( "Your SAX parser is not JAXP 1.2 compliant." ); } } else { parser = this.parser; } if ( !parser.isNamespaceAware( ) ) { throw new ParserConfigurationException( "parser must be namespace-aware" ); } if ( this.repo == null ) { try { this.repo = DefaultSemanticsRepository.getInstance( ); } catch ( Exception e ) { throw new SAXException("Unable to reference a Semantics Repository"); } } parser.parse( in, this ); return this.ruleSet; } public SemanticsRepository getSemanticsRepository() { return this.repo; } void setRuleSet( RuleSet ruleSet ) { this.ruleSet = ruleSet; } public RuleSet getRuleSet() { return this.ruleSet; } /** * @see org.xml.sax.ContentHandler */ public void setLocator( Locator locator ) { this.locator = locator; } /** * Get the <code>Locator</code>. * * @return The locator. */ public Locator getLocator() { return this.locator; } public void startDocument() { this.ruleSet = null; this.current = null; this.peer = null; this.lastWasEndElement = false; this.parents.clear(); this.characters2 = null; this.configurationStack.clear();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -