001/* $Id: TestRule.java 1103635 2011-05-16 07:37:24Z simonetripodi $
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one or more
004 * contributor license agreements.  See the NOTICE file distributed with
005 * this work for additional information regarding copyright ownership.
006 * The ASF licenses this file to You under the Apache License, Version 2.0
007 * (the "License"); you may not use this file except in compliance with
008 * the License.  You may obtain a copy of the License at
009 *
010 *      http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.apache.commons.digester3;
020
021import java.util.List;
022
023import org.apache.commons.digester3.Rule;
024import org.apache.commons.digester3.binder.RuleProvider;
025import org.xml.sax.Attributes;
026
027/**
028 * <p>
029 * This rule implementation is intended to help test digester. The idea is that you can test which rule matches by
030 * looking at the identifier.
031 * </p>
032 * 
033 * @author Robert Burrell Donkin
034 */
035
036public class TestRule
037    extends Rule
038{
039
040    // ----------------------------------------------------- Instance Variables
041
042    /** String identifing this particular <code>TestRule</code> */
043    private String identifier;
044
045    /** Used when testing body text */
046    private String bodyText;
047
048    /** Used when testing call orders */
049    private List<Rule> order;
050
051    // ----------------------------------------------------------- Constructors
052
053    /**
054     * Base constructor.
055     * 
056     * @param identifier Used to tell which TestRule is which
057     */
058    public TestRule( String identifier )
059    {
060
061        this.identifier = identifier;
062    }
063
064    /**
065     * Constructor sets namespace URI.
066     * 
067     * @param identifier Used to tell which TestRule is which
068     * @param namespaceURI Set rule namespace
069     */
070    public TestRule( String identifier, String namespaceURI )
071    {
072
073        this.identifier = identifier;
074        setNamespaceURI( namespaceURI );
075
076    }
077
078    // ------------------------------------------------ Rule Implementation
079
080    /**
081     * 'Begin' call.
082     */
083    @Override
084    public void begin( String namespace, String name, Attributes attributes )
085        throws Exception
086    {
087        appendCall();
088    }
089
090    /**
091     * 'Body' call.
092     */
093    @Override
094    public void body( String namespace, String name, String text )
095        throws Exception
096    {
097        this.bodyText = text;
098        appendCall();
099    }
100
101    /**
102     * 'End' call.
103     */
104    @Override
105    public void end( String namespace, String name )
106        throws Exception
107    {
108        appendCall();
109    }
110
111    // ------------------------------------------------ Methods
112
113    /**
114     * If a list has been set, append this to the list.
115     */
116    protected void appendCall()
117    {
118        if ( order != null )
119            order.add( this );
120    }
121
122    /**
123     * Get the body text that was set.
124     */
125    public String getBodyText()
126    {
127        return bodyText;
128    }
129
130    /**
131     * Get the identifier associated with this test.
132     */
133    public String getIdentifier()
134    {
135        return identifier;
136    }
137
138    /**
139     * Get call order list.
140     */
141    public List<Rule> getOrder()
142    {
143        return order;
144    }
145
146    /**
147     * Set call order list
148     */
149    public void setOrder( List<Rule> order )
150    {
151        this.order = order;
152    }
153
154    /**
155     * Return the identifier.
156     */
157    @Override
158    public String toString()
159    {
160        return identifier;
161    }
162
163    public static class TestRuleProvider implements RuleProvider<TestRule>
164    {
165
166        private final String identifier;
167
168        private final List<Rule> callOrder;
169
170        public TestRuleProvider( String identifier )
171        {
172            this( identifier, null );
173        }
174
175        public TestRuleProvider( String identifier, List<Rule> callOrder )
176        {
177            this.identifier = identifier;
178            this.callOrder = callOrder;
179        }
180
181        public TestRule get()
182        {
183            TestRule testRule = new TestRule( identifier );
184            testRule.setOrder( callOrder );
185            return testRule;
186        }
187
188    }
189
190}