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  import org.xml.sax.Attributes;
24  
25  /**
26   * This Substitutor chains two Substitutors <code>a</code> and <code>b</code>.
27   * All values to substitute are first handled by <code>a</code> and passed to
28   * <code>b</code> afterwards.
29   */
30  public class CompoundSubstitutor
31      extends Substitutor
32  {
33  
34      /**
35       * Substitutor a
36       */
37      private final Substitutor a;
38  
39      /**
40       * Substitutor b
41       */
42      private final Substitutor b;
43  
44      /**
45       * Creates a new CompoundSubstitutor instance. All values overgiven to <code>substitute()</code>
46       * are first handled by <code>a</code> and passed to <code>b</code> afterwards.
47       * Both Substitutor have to be not null.
48       *
49       * @param a Substitutor a
50       * @param b Substitutor b
51       */
52      public CompoundSubstitutor( Substitutor a, Substitutor b )
53      {
54          if ( a == null )
55          {
56              throw new IllegalArgumentException( "First Substitutor must be not null" );
57          }
58          if ( b == null )
59          {
60              throw new IllegalArgumentException( "Second Substitutor must be not null" );
61          }
62          this.a = a;
63          this.b = b;
64      }
65  
66      /**
67       * {@inheritDoc}
68       */
69      @Override
70      public Attributes substitute( Attributes attributes )
71      {
72          return b.substitute( a.substitute( attributes ) );
73      }
74  
75      /**
76       * {@inheritDoc}
77       */
78      @Override
79      public String substitute( String bodyText )
80      {
81          return b.substitute( a.substitute( bodyText ) );
82      }
83  
84  }