01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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://jakarta.apache.org/'>Jakarta 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,
40: Attributes attributes) throws Exception {
41: currentValue = null;
42: currentName = null;
43: }
44:
45: /**
46: * @see org.apache.commons.digester.Rule#end(java.lang.String, java.lang.String)
47: */
48: public void end(String namespace, String name) {
49: if (currentName != null && currentValue != null) {
50: Object top = getDigester().peek();
51: if (top instanceof Descriptor) {
52: Descriptor descriptor = (Descriptor) top;
53: descriptor.getOptions().addOption(currentName,
54: 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 namespace, String name, String 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 namespace, String name, String text) {
80: currentValue = text;
81: }
82: };
83: }
84: }
|