⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rulesetreader.java

📁 drools 一个开放源码的规则引擎
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
package org.drools.io;

/*
 * 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 java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
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;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.drools.rule.Rule;
import org.drools.rule.RuleSet;
import org.drools.smf.Configuration;
import org.drools.smf.DefaultConfiguration;
import org.drools.smf.DefaultSemanticsRepository;
import org.drools.smf.NoSuchSemanticModuleException;
import org.drools.smf.SemanticModule;
import org.drools.smf.SemanticsRepository;
import org.drools.spi.RuleBaseContext;
import org.xml.sax.Attributes;
import org.xml.sax.EntityResolver;
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;

/**
 * <code>RuleSet</code> loader.
 *
 * Note you can override the default entity resolver by setting the System property of:
 *  <code>org.drools.io.EntityResolve</code> to your own custom entity resolver.
 *  This can be done using -Dorg.drools.io.EntityResolver=YourClassHere on the command line, for instance.
 *
 * @author <a href="mailto:bob@werken.com">bob mcwhirter </a>
 */
public class RuleSetReader extends DefaultHandler
{
    // ----------------------------------------------------------------------
    // Constants
    // ----------------------------------------------------------------------
    public static final String ENTITY_RESOLVER_PROPERTY_NAME = "org.drools.io.EntityResolver";

    /** 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;

    /** isValidating */
    private boolean             isValidating         = true;

    /** 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        characters;

    private Map                 handlers;

    private boolean             lastWasEndElement;

    private LinkedList          parents;

    private Object              peer;

    private Object              current;

    private RuleSet             ruleSet;

    private RuleBaseContext     factoryContext;
    
    private boolean             inHandledRuleSubElement;
    
    private MessageFormat       message              = new MessageFormat( "({0}: {1}, {2}): {3}" );
    
    private Map                 namespaces = new HashMap();

    EntityResolver      entityResolver;
    // ----------------------------------------------------------------------
    // 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.handlers = new HashMap( );
        this.handlers.put( "RuleSet",
                           new RuleSetHandler( this ) );
        this.handlers.put( "ImportEntry",
                           new ImportHandler( this ) );
        this.handlers.put( "ApplicationData",
                           new ApplicationDataHandler( this ) );
        this.handlers.put( "Functions",
                           new FunctionsHandler( this ) );
        this.handlers.put( "Rule",
                           new RuleHandler( this ) );
        this.handlers.put( "Parameter",
                           new ParameterHandler( this ) );
        // localNameMap.put( "declaration", new DeclarationHandler( this ) );
        this.handlers.put( "ObjectType",
                           new ObjectTypeHandler( this ) );
        this.handlers.put( "Condition",
                           new ConditionHandler( this ) );
        this.handlers.put( "Duration",
                           new DurationHandler( this ) );
        this.handlers.put( "Consequence",
                           new ConsequenceHandler( this ) );
        
        initEntityResolver();
        
    }

    /**
     * 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.
     */
    public RuleSetReader(SemanticsRepository repo)
    {
        this( );
        this.repo = repo;
    }

    /**
     * Construct.
     *
     * @param factoryContext
     */
    public RuleSetReader(RuleBaseContext factoryContext)
    {
        this( );
        this.factoryContext = factoryContext;
    }

    /**
     * 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,
                         RuleBaseContext context)
    {
        this( );
        this.repo = repo;
        this.factoryContext = context;
    }

    /**
     * Construct.
     *
     * @param parser
     *
     * @param repo
     *            The semantics repository.
     */
    public RuleSetReader(SAXParser parser,
                         SemanticsRepository repo)
    {
        this( );
        this.parser = parser;
        this.repo = repo;
    }

    /**
     * Construct.
     *
     * @param parser
     * @param context
     */
    public RuleSetReader(SAXParser parser,
                         RuleBaseContext context)
    {
        this( );
        this.parser = parser;
        this.factoryContext = context;
    }

    /**
     * Construct.
     *
     * @param repo
     *            The semantics repository.
     * @param parser
     *            The SAX parser.
     */
    public RuleSetReader(SemanticsRepository repo,
                         SAXParser parser,
                         RuleBaseContext context)
    {
        this( parser );
        this.repo = repo;
        this.factoryContext = context;
    }

    // ----------------------------------------------------------------------
    // Instance methods
    // ----------------------------------------------------------------------

    /**
     * Read a <code>RuleSet</code> from a <code>URL</code>.
     *
     * @param url
     *            The rule-set URL.
     *
     * @return The rule-set.
     */
    public RuleSet read(URL url) throws SAXException,
                                IOException
    {
        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.
     */
    public RuleSet read(Reader reader) throws SAXException,
                                      IOException
    {
        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.
     */
    public RuleSet read(InputStream inputStream) throws SAXException,
                                                IOException
    {
        return read( new InputSource( inputStream ) );
    }

    /**
     * Read a <code>RuleSet</code> from a URL.
     *

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -