View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17   
18  package org.apache.commons.betwixt.digester;
19  
20  import org.apache.commons.betwixt.Descriptor;
21  import org.apache.commons.digester.Rule;
22  import org.xml.sax.Attributes;
23  
24  /**
25   * Maps option tree to an option in the 
26   * {@link org.apache.commons.betwixt.Options}
27   * on the current description.
28   * @author <a href='http://commons.apache.org/'>Apache Commons Team</a>
29   * @since 0.5
30   */
31  public class OptionRule extends Rule {
32      
33      private String currentValue;
34      private String currentName;
35      
36      /**
37       * @see org.apache.commons.digester.Rule#begin(java.lang.String, java.lang.String, Attributes)
38       */
39      public void begin(String namespace, String name, Attributes attributes)
40          throws Exception {
41          currentValue = null;
42          currentName = null;
43      }
44  
45  
46  
47      /**
48       * @see org.apache.commons.digester.Rule#end(java.lang.String, java.lang.String)
49       */
50      public void end(String namespace, String name) {
51          if (currentName != null && currentValue != null) {
52              Object top = getDigester().peek();
53              if (top instanceof Descriptor) {
54                  Descriptor descriptor = (Descriptor) top;
55                  descriptor.getOptions().addOption(currentName, currentValue);
56              }
57          }
58      }
59      
60      /**
61       * Gets the rule that maps the <code>name</code> element
62       * associated with the option
63       * @return <code>Rule</code>, not null
64       */
65      public Rule getNameRule() {
66          return new Rule() {
67              public void body(String namespace, String name, String text) {
68                  currentName = text;
69              }            
70          };
71      }
72  
73      /**
74       * Gets the rule that maps the <code>value</code> element
75       * associated with the option
76       * @return <code>Rule</code>, not null
77       */
78      public Rule getValueRule() {
79          return new Rule() {
80              public void body(String namespace, String name, String text) {
81                  currentValue = text;
82              }            
83          };
84      }
85  }