001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     * 
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     * 
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017     
018    package org.apache.commons.betwixt.digester;
019    
020    import org.apache.commons.betwixt.Descriptor;
021    import org.apache.commons.digester.Rule;
022    import org.xml.sax.Attributes;
023    
024    /**
025     * Maps option tree to an option in the 
026     * {@link org.apache.commons.betwixt.Options}
027     * on the current description.
028     * @author <a href='http://commons.apache.org/'>Apache Commons Team</a>
029     * @since 0.5
030     */
031    public class OptionRule extends Rule {
032        
033        private String currentValue;
034        private String currentName;
035        
036        /**
037         * @see org.apache.commons.digester.Rule#begin(java.lang.String, java.lang.String, Attributes)
038         */
039        public void begin(String namespace, String name, Attributes attributes)
040            throws Exception {
041            currentValue = null;
042            currentName = null;
043        }
044    
045    
046    
047        /**
048         * @see org.apache.commons.digester.Rule#end(java.lang.String, java.lang.String)
049         */
050        public void end(String namespace, String name) {
051            if (currentName != null && currentValue != null) {
052                Object top = getDigester().peek();
053                if (top instanceof Descriptor) {
054                    Descriptor descriptor = (Descriptor) top;
055                    descriptor.getOptions().addOption(currentName, currentValue);
056                }
057            }
058        }
059        
060        /**
061         * Gets the rule that maps the <code>name</code> element
062         * associated with the option
063         * @return <code>Rule</code>, not null
064         */
065        public Rule getNameRule() {
066            return new Rule() {
067                public void body(String namespace, String name, String text) {
068                    currentName = text;
069                }            
070            };
071        }
072    
073        /**
074         * Gets the rule that maps the <code>value</code> element
075         * associated with the option
076         * @return <code>Rule</code>, not null
077         */
078        public Rule getValueRule() {
079            return new Rule() {
080                public void body(String namespace, String name, String text) {
081                    currentValue = text;
082                }            
083            };
084        }
085    }