001 package org.apache.commons.digester3.substitution;
002
003 /*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements. See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership. The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License. You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied. See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022 import org.apache.commons.digester3.Substitutor;
023 import org.xml.sax.Attributes;
024
025 /**
026 * This Substitutor chains two Substitutors <code>a</code> and <code>b</code>.
027 * All values to substitute are first handled by <code>a</code> and passed to
028 * <code>b</code> afterwards.
029 */
030 public class CompoundSubstitutor
031 extends Substitutor
032 {
033
034 /**
035 * Substitutor a
036 */
037 private final Substitutor a;
038
039 /**
040 * Substitutor b
041 */
042 private final Substitutor b;
043
044 /**
045 * Creates a new CompoundSubstitutor instance. All values overgiven to <code>substitute()</code>
046 * are first handled by <code>a</code> and passed to <code>b</code> afterwards.
047 * Both Substitutor have to be not null.
048 *
049 * @param a Substitutor a
050 * @param b Substitutor b
051 */
052 public CompoundSubstitutor( Substitutor a, Substitutor b )
053 {
054 if ( a == null )
055 {
056 throw new IllegalArgumentException( "First Substitutor must be not null" );
057 }
058 if ( b == null )
059 {
060 throw new IllegalArgumentException( "Second Substitutor must be not null" );
061 }
062 this.a = a;
063 this.b = b;
064 }
065
066 /**
067 * {@inheritDoc}
068 */
069 @Override
070 public Attributes substitute( Attributes attributes )
071 {
072 return b.substitute( a.substitute( attributes ) );
073 }
074
075 /**
076 * {@inheritDoc}
077 */
078 @Override
079 public String substitute( String bodyText )
080 {
081 return b.substitute( a.substitute( bodyText ) );
082 }
083
084 }