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

📄 rulesetreader.java

📁 drools 一个开放源码的规则引擎
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
            {
                validPeer = true;
            }
            else if ( allowedPeer != null && ((Class) allowedPeer).isInstance( peer ) )
            {
                validPeer = true;
            }
        }
        if ( !validPeer )
        {
            throw new SAXParseException( "<" + localName + "> is after an invalid element",
                                         getLocator( ) );
        }

        if ( !allowNesting )
        {
            it = this.parents.iterator( );
            while ( !invalidNesting && it.hasNext( ) )
            {
                if ( nodeClass.isInstance( it.next( ) ) )
                {
                    invalidNesting = true;
                }
            }
        }
        if ( invalidNesting )
        {
            throw new SAXParseException( "<" + localName + ">  may not be nested",
                                         getLocator( ) );
        }

    }

    /**
     * Start a configuration node.
     *
     * @param name
     *            Tag name.
     * @param attrs
     *            Tag attributes.
     */
    protected void startConfiguration(String name,
                                      Attributes attrs)
    {
        this.characters = new StringBuffer( );

        DefaultConfiguration config = new DefaultConfiguration( name );

        int numAttrs = attrs.getLength( );

        for ( int i = 0; i < numAttrs; ++i )
        {
            config.setAttribute( attrs.getLocalName( i ),
                                 attrs.getValue( i ) );
        }

        // lets add the namespaces as attributes
        for (Iterator iter = namespaces.entrySet().iterator(); iter.hasNext(); ) 
        {
            Map.Entry entry = (Map.Entry) iter.next();
            String ns = (String) entry.getKey();
            String value = (String) entry.getValue();
            if (ns == null || ns.length() == 0) {
                ns = "xmlns";
            }
            else 
            {
                ns = "xmlns:" + ns;
            }
            config.setAttribute(ns, value);
        }
            
        
        if ( this.configurationStack.isEmpty( ) )
        {
            this.configurationStack.addLast( config );
        }
        else
        {
            ((DefaultConfiguration) this.configurationStack.getLast( )).addChild( config );
            this.configurationStack.addLast( config );
        }
    }

    /**
     * @param chars
     * @param start
     * @param len
     * @see org.xml.sax.ContentHandler
     */
    public void characters(char[] chars,
                           int start,
                           int len)
    {
        if ( this.characters != null )
        {
            this.characters.append( chars,
                                    start,
                                    len );
        }
    }

    /**
     * End a configuration node.
     *
     * @return The configuration.
     */
    protected Configuration endConfiguration()
    {
        DefaultConfiguration config = (DefaultConfiguration) this.configurationStack.removeLast( );
        if ( this.characters != null )
        {
            config.setText( this.characters.toString( ) );
        }

        this.characters = null;

        return config;
    }

    SemanticModule lookupSemanticModule(String uri,
                                        String localName) throws SAXParseException
    {
        SemanticModule module;
        try
        {
            module = this.repo.lookupSemanticModule( uri );
        }
        catch ( NoSuchSemanticModuleException e )
        {
            throw new SAXParseException( "no semantic module for namespace '" + uri + "' (" + localName + ")",
                                         getLocator( ) );
        }
        return module;
    }

    /**
     * Returned Handler can be null. Calling method decides whether to throw an
     * exception or not.
     *
     * @param uri
     * @param localName
     * @return
     */
    private Handler getHandler(String uri,
                               String localName)
    {
        String type = null;
        if ( localName.equals( "rule-set" ) )
        {
            type = "RuleSet";
        }
        else if ( localName.equals( "parameter" ) )
        {
            type = "Parameter";
        }
        else
        {
            SemanticModule module;
            try
            {
                module = this.repo.lookupSemanticModule( uri );
                type = module.getType( localName );
            }
            catch ( NoSuchSemanticModuleException e )
            {
                // swallow as we just return a null handler
            }
        }

        Handler handler = (Handler) this.handlers.get( type );

        return handler;
    }

    LinkedList getParents()
    {
        return this.parents;
    }

    Object getParent(Class parent)
    {
        ListIterator it = this.parents.listIterator( this.parents.size( ) );
        Object node = null;
        while ( it.hasPrevious( ) )
        {
            node = it.previous( );
            if ( parent.isInstance( node ) ) break;
        }
        return node;
    }

    Object getPeer()
    {
        return this.peer;
    }

    Object getCurrent()
    {
        return this.current;
    }

    public InputSource resolveEntity(String publicId,
                                     String systemId) throws SAXException {
        try {
            InputSource inputSource = resolveSchema(publicId, systemId);
            if ( inputSource != null ) {
                return inputSource;
            }
            if ( entityResolver != null ) {
                return entityResolver.resolveEntity(publicId, systemId);
            }
        } catch (IOException ioe) {
        }
        return null;
    }

    public void startPrefixMapping(String prefix, String uri) throws SAXException {
        super.startPrefixMapping(prefix, uri);
        namespaces.put(prefix, uri);
    }

    public void endPrefixMapping(String prefix) throws SAXException {
        super.endPrefixMapping(prefix);  
        namespaces.remove(prefix);
    }

    private void print(SAXParseException x)
    {
        String msg = this.message.format( new Object[]{x.getSystemId( ), new Integer( x.getLineNumber( ) ), new Integer( x.getColumnNumber( ) ), x.getMessage( )} );
        System.out.println( msg );
    }

    public void warning(SAXParseException x)
    {
        print( x );
    }

    public void error(SAXParseException x)
    {
        print( x );
    }

    public void fatalError(SAXParseException x) throws SAXParseException
    {
        print( x );
        throw x;
    }
    
    private InputSource resolveSchema(String publicId, String systemId)
    throws SAXException, IOException {
        // Schema files must end with xsd
        if (!systemId.toLowerCase().endsWith("xsd")) {
            return null;
        }
        
        // try the actual location given by systemId
        try {
            URL url = new URL(systemId);
            return new InputSource(url.openStream());
        } catch (Exception e) {
        }
        
        // Try and get the index for the filename, else return null
        String xsd;
        int index = systemId.lastIndexOf("/");
        if (index == -1) {
            index = systemId.lastIndexOf("\\");
        }
        if (index != -1) {
            xsd = systemId.substring(index + 1);
        } else {
            xsd = systemId;
        }
        
        ClassLoader cl = Thread.currentThread().getContextClassLoader();
        
        if (cl == null) {
            cl = RuleSetReader.class.getClassLoader();
        }
        
        // Try looking in META-INF
        try {
            return new InputSource(cl.getResourceAsStream("META-INF/" + xsd));
        } catch (Exception e) {
        }
        
        // Try looking in /META-INF
        try {
            return new InputSource(cl.getResourceAsStream("/META-INF/" + xsd));
        } catch (Exception e) {
        }
        
        // Try looking at root of classpath
        try {
            return new InputSource(cl.getResourceAsStream("/" + xsd));
        } catch (Exception e) {
        }
        
        // Try current working directory
        try {
            return new InputSource(new BufferedInputStream(new FileInputStream(
                    xsd)));
        } catch (Exception e) {
        }
        
        cl = ClassLoader.getSystemClassLoader();
        
        // Try looking in META-INF
        try {
            return new InputSource(cl.getResourceAsStream("META-INF/" + xsd));
        } catch (Exception e) {
        }
        
        // Try looking in /META-INF
        try {
            return new InputSource(cl.getResourceAsStream("/META-INF/" + xsd));
        } catch (Exception e) {
        }
        
        // Try looking at root of classpath
        try {
            return new InputSource(cl.getResourceAsStream("/" + xsd));
        } catch (Exception e) {
        }
        
        // Try current working directory
        try {
            return new InputSource(new BufferedInputStream(new FileInputStream(
                    xsd)));
        } catch (Exception e) {
        }
        return null;
    }    
    
    /**
     * Intializes EntityResolver that is configured via system property ENTITY_RESOLVER_PROPERTY_NAME.
     */
    private void initEntityResolver() {
        String entityResolveClazzName = System.getProperty(ENTITY_RESOLVER_PROPERTY_NAME);
        if ( entityResolveClazzName != null && entityResolveClazzName.length() > 0 ) {
            try {
                Class entityResolverClazz = Thread.currentThread().getContextClassLoader().loadClass(entityResolveClazzName);
                entityResolver = (EntityResolver)entityResolverClazz.newInstance();
            } catch (Exception ignoreIt) {
            }
        }
    }    
    
}

⌨️ 快捷键说明

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