public void begin()

in commons-digester3-core/src/main/java/org/apache/commons/digester3/xmlrules/IncludeRule.java [55:146]


    public void begin( final String namespace, final String name, final Attributes attributes )
        throws Exception
    {
        // The path attribute gives the URI to another digester rules XML file
        final String fileName = attributes.getValue( "url" );
        if ( fileName != null && !fileName.isEmpty() )
        {
            final URL xmlRulesResource;

            if ( fileName.startsWith( CLASSPATH_URL_PREFIX ) )
            {
                String path = fileName.substring( CLASSPATH_URL_PREFIX.length() );
                if ( '/' == path.charAt( 0 ) )
                {
                    path = path.substring( 1 );
                }
                xmlRulesResource = this.targetRulesBinder.getContextClassLoader().getResource( path );
                if ( xmlRulesResource == null )
                {
                    targetRulesBinder.addError( "Resource '%s' not found, please make sure it is in the classpath",
                                                path );
                    return;
                }
            }
            else
            {
                try
                {
                    xmlRulesResource = new URL( fileName );
                }
                catch ( final MalformedURLException e )
                {
                    targetRulesBinder.addError( "An error occurred while including file from '%s': %s", fileName,
                                                e.getMessage() );
                    return;
                }
            }

            final Set<String> includedFiles = memoryRulesBinder.getIncludedFiles();
            final String xmlRulesResourceString = xmlRulesResource.toString();
            if ( includedFiles.add( xmlRulesResourceString ) )
            {
                try
                {
                    install( new FromXmlRulesModule()
                    {

                        @Override
                        protected void loadRules()
                        {
                            loadXMLRules( xmlRulesResource );
                        }

                    } );
                }
                finally
                {
                    includedFiles.remove( xmlRulesResourceString );
                }
            }
            else
            {
                targetRulesBinder.addError( "Circular file inclusion detected for XML rules: %s", xmlRulesResource );
            }
        }

        // The class attribute gives the name of a class that implements
        // the DigesterRulesSource interface
        final String className = attributes.getValue( "class" );
        if ( className != null && !className.isEmpty() )
        {
            try
            {
                final Class<?> cls = Class.forName( className );
                if ( !RulesModule.class.isAssignableFrom( cls ) )
                {
                    targetRulesBinder.addError( "Class '%s' if not a '%s' implementation", className,
                                                RulesModule.class.getName() );
                    return;
                }

                final RulesModule rulesSource = (RulesModule) cls.newInstance();

                install( rulesSource );
            }
            catch ( final Exception e )
            {
                targetRulesBinder.addError( "Impossible to include programmatic rules from class '%s': %s", className,
                                            e.getMessage() );
            }
        }
    }