KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > betwixt > digester > OptionRule


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16  
17 package org.apache.commons.betwixt.digester;
18
19 import org.apache.commons.betwixt.Descriptor;
20 import org.apache.commons.digester.Rule;
21 import org.xml.sax.Attributes JavaDoc;
22
23 /**
24  * Maps option tree to an option in the
25  * {@link org.apache.commons.betwixt.Options}
26  * on the current description.
27  * @author <a HREF='http://jakarta.apache.org/'>Jakarta Commons Team</a>
28  * @since 0.5
29  */

30 public class OptionRule extends Rule {
31     
32     private String JavaDoc currentValue;
33     private String JavaDoc currentName;
34     
35     /**
36      * @see org.apache.commons.digester.Rule#begin(java.lang.String, java.lang.String, Attributes)
37      */

38     public void begin(String JavaDoc namespace, String JavaDoc name, Attributes JavaDoc attributes)
39         throws Exception JavaDoc {
40         currentValue = null;
41         currentName = null;
42     }
43
44
45
46     /**
47      * @see org.apache.commons.digester.Rule#end(java.lang.String, java.lang.String)
48      */

49     public void end(String JavaDoc namespace, String JavaDoc name) {
50         if (currentName != null && currentValue != null) {
51             Object JavaDoc top = getDigester().peek();
52             if (top instanceof Descriptor) {
53                 Descriptor descriptor = (Descriptor) top;
54                 descriptor.getOptions().addOption(currentName, currentValue);
55             }
56         }
57     }
58     
59     /**
60      * Gets the rule that maps the <code>name</code> element
61      * associated with the option
62      * @return <code>Rule</code>, not null
63      */

64     public Rule getNameRule() {
65         return new Rule() {
66             public void body(String JavaDoc namespace, String JavaDoc name, String JavaDoc text) {
67                 currentName = text;
68             }
69         };
70     }
71
72     /**
73      * Gets the rule that maps the <code>value</code> element
74      * associated with the option
75      * @return <code>Rule</code>, not null
76      */

77     public Rule getValueRule() {
78         return new Rule() {
79             public void body(String JavaDoc namespace, String JavaDoc name, String JavaDoc text) {
80                 currentValue = text;
81             }
82         };
83     }
84 }
85
Popular Tags