KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > digester > substitution > VariableSubstitutor


1 /* $Id: VariableSubstitutor.java 155412 2005-02-26 12:58:36Z dirkv $
2  *
3  * Copyright 2003-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * 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.digester.substitution;
19
20 import org.apache.commons.digester.Substitutor;
21
22 import org.xml.sax.Attributes JavaDoc;
23
24 /**
25  * Substitutor implementation that support variable replacement
26  * for both attributes and body text.
27  * The actual expansion of variables into text is delegated to {@link VariableExpander}
28  * implementations.
29  * Supports setting an expander just for body text or just for attributes.
30  * Also supported is setting no expanders for body text and for attributes.
31  *
32  * @since 1.6
33  */

34 public class VariableSubstitutor extends Substitutor {
35
36     /**
37      * The expander to be used to expand variables in the attributes.
38      * Null when no expansion should be performed.
39      */

40     private VariableExpander attributesExpander;
41     
42     /**
43      * Attributes implementation that (lazily) performs variable substitution.
44      * Will be lazily created when needed then reused.
45      */

46     private VariableAttributes variableAttributes;
47     
48     /**
49      * The expander to be used to expand variables in the body text.
50      * Null when no expansion should be performed.
51      */

52     private VariableExpander bodyTextExpander;
53     
54     /**
55      * Constructs a Substitutor which uses the same VariableExpander for both
56      * body text and attibutes.
57      * @param expander VariableExpander implementation,
58      * null if no substitutions are to be performed
59      */

60     public VariableSubstitutor(VariableExpander expander) {
61         this(expander, expander);
62     }
63     
64     /**
65      * Constructs a Substitutor.
66      * @param attributesExpander VariableExpander implementation to be used for attributes,
67      * null if no attribute substitutions are to be performed
68      * @param bodyTextExpander VariableExpander implementation to be used for bodyTextExpander,
69      * null if no attribute substitutions are to be performed
70      */

71     public VariableSubstitutor(VariableExpander attributesExpander, VariableExpander bodyTextExpander) {
72         this.attributesExpander = attributesExpander;
73         this.bodyTextExpander = bodyTextExpander;
74         variableAttributes = new VariableAttributes();
75     }
76
77     /**
78      * Substitutes the attributes (before they are passed to the
79      * <code>Rule</code> implementations's)
80      */

81     public Attributes JavaDoc substitute(Attributes JavaDoc attributes) {
82         Attributes JavaDoc results = attributes;
83         if (attributesExpander != null) {
84             variableAttributes.init(attributes, attributesExpander);
85             results = variableAttributes;
86         }
87         return results;
88     }
89     
90     /**
91      * Substitutes for the body text.
92      * This method may substitute values into the body text of the
93      * elements that Digester parses.
94      *
95      * @param bodyText the body text (as passed to <code>Digester</code>)
96      * @return the body text to be passed to the <code>Rule</code> implementations
97      */

98     public String JavaDoc substitute(String JavaDoc bodyText) {
99         String JavaDoc result = bodyText;
100         if (bodyTextExpander != null) {
101             result = bodyTextExpander.expand(bodyText);
102         }
103         return result;
104     }
105 }
106
Popular Tags