001    package org.apache.commons.digester3;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one
005     * or more contributor license agreements.  See the NOTICE file
006     * distributed with this work for additional information
007     * regarding copyright ownership.  The ASF licenses this file
008     * to you under the Apache License, Version 2.0 (the
009     * "License"); you may not use this file except in compliance
010     * with the License.  You may obtain a copy of the License at
011     *
012     *   http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing,
015     * software distributed under the License is distributed on an
016     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017     * KIND, either express or implied.  See the License for the
018     * specific language governing permissions and limitations
019     * under the License.
020     */
021    
022    
023    /**
024     * <p>
025     * <code>AbstractRuleImpl</code> provides basic services for <code>Rules</code> implementations. Extending this class
026     * should make it easier to create a <code>Rules</code> implementation.
027     * </p>
028     * <p>
029     * <code>AbstractRuleImpl</code> manages the <code>Digester</code> and <code>namespaceUri</code> properties. If the
030     * subclass overrides {@link #registerRule} (rather than {@link #add}), then the <code>Digester</code> and
031     * <code>namespaceURI</code> of the <code>Rule</code> will be set correctly before it is passed to
032     * <code>registerRule</code>. The subclass can then perform whatever it needs to do to register the rule.
033     * </p>
034     * 
035     * @since 1.5
036     */
037    public abstract class AbstractRulesImpl
038        implements Rules
039    {
040    
041        // ------------------------------------------------------------- Fields
042    
043        /** Digester using this <code>Rules</code> implementation */
044        private Digester digester;
045    
046        /** Namespace uri to assoicate with subsequent <code>Rule</code>'s */
047        private String namespaceURI;
048    
049        // ------------------------------------------------------------- Properties
050    
051        /**
052         * {@inheritDoc}
053         */
054        public Digester getDigester()
055        {
056            return digester;
057        }
058    
059        /**
060         * {@inheritDoc}
061         */
062        public void setDigester( Digester digester )
063        {
064            this.digester = digester;
065        }
066    
067        /**
068         * {@inheritDoc}
069         */
070        public String getNamespaceURI()
071        {
072            return namespaceURI;
073        }
074    
075        /**
076         * {@inheritDoc}
077         */
078        public void setNamespaceURI( String namespaceURI )
079        {
080            this.namespaceURI = namespaceURI;
081        }
082    
083        // --------------------------------------------------------- Public Methods
084    
085        /**
086         * {@inheritDoc}
087         */
088        public final void add( String pattern, Rule rule )
089        {
090            // set up rule
091            if ( this.digester != null )
092            {
093                rule.setDigester( this.digester );
094            }
095    
096            if ( this.namespaceURI != null )
097            {
098                rule.setNamespaceURI( this.namespaceURI );
099            }
100    
101            registerRule( pattern, rule );
102        }
103    
104        /**
105         * Register rule at given pattern. The the Digester and namespaceURI properties of the given <code>Rule</code> can
106         * be assumed to have been set properly before this method is called.
107         * 
108         * @param pattern Nesting pattern to be matched for this Rule
109         * @param rule Rule instance to be registered
110         */
111        protected abstract void registerRule( String pattern, Rule rule );
112    
113    }