View Javadoc

1   package org.apache.commons.digester3.substitution;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.commons.digester3.Substitutor;
23  
24  import org.xml.sax.Attributes;
25  
26  /**
27   * Substitutor implementation that support variable replacement for both attributes and body text. The actual expansion
28   * of variables into text is delegated to {@link VariableExpander} implementations. Supports setting an expander just
29   * for body text or just for attributes. Also supported is setting no expanders for body text and for attributes.
30   * 
31   * @since 1.6
32   */
33  public class VariableSubstitutor
34      extends Substitutor
35  {
36  
37      /**
38       * The expander to be used to expand variables in the attributes. Null when no expansion should be performed.
39       */
40      private final VariableExpander attributesExpander;
41  
42      /**
43       * Attributes implementation that (lazily) performs variable substitution. Will be lazily created when needed then
44       * reused.
45       */
46      private final VariableAttributes variableAttributes;
47  
48      /**
49       * The expander to be used to expand variables in the body text. Null when no expansion should be performed.
50       */
51      private final VariableExpander bodyTextExpander;
52  
53      /**
54       * Constructs a Substitutor which uses the same VariableExpander for both body text and attibutes.
55       * 
56       * @param expander VariableExpander implementation, null if no substitutions are to be performed
57       */
58      public VariableSubstitutor( VariableExpander expander )
59      {
60          this( expander, expander );
61      }
62  
63      /**
64       * Constructs a Substitutor.
65       * 
66       * @param attributesExpander VariableExpander implementation to be used for attributes, null if no attribute
67       *            substitutions are to be performed
68       * @param bodyTextExpander VariableExpander implementation to be used for bodyTextExpander, null if no attribute
69       *            substitutions are to be performed
70       */
71      public VariableSubstitutor( VariableExpander attributesExpander, VariableExpander bodyTextExpander )
72      {
73          this.attributesExpander = attributesExpander;
74          this.bodyTextExpander = bodyTextExpander;
75          variableAttributes = new VariableAttributes();
76      }
77  
78      /**
79       * {@inheritDoc}
80       */
81      @Override
82      public Attributes substitute( Attributes attributes )
83      {
84          Attributes results = attributes;
85          if ( attributesExpander != null )
86          {
87              variableAttributes.init( attributes, attributesExpander );
88              results = variableAttributes;
89          }
90          return results;
91      }
92  
93      /**
94       * {@inheritDoc}
95       */
96      @Override
97      public String substitute( String bodyText )
98      {
99          String result = bodyText;
100         if ( bodyTextExpander != null )
101         {
102             result = bodyTextExpander.expand( bodyText );
103         }
104         return result;
105     }
106 
107 }