KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* $Id: MultiVariableExpander.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 java.util.Map JavaDoc;
21 import java.util.ArrayList JavaDoc;
22
23 /**
24  * <p>Expands variable references from multiple sources.</p>
25  *
26  * @since 1.6
27  */

28
29 public class MultiVariableExpander implements VariableExpander {
30     private int nEntries = 0;
31     private ArrayList JavaDoc markers = new ArrayList JavaDoc(2);
32     private ArrayList JavaDoc sources = new ArrayList JavaDoc(2);
33     
34     public MultiVariableExpander() {
35     }
36     
37     public void addSource(String JavaDoc marker, Map JavaDoc source) {
38         ++nEntries;
39         markers.add(marker);
40         sources.add(source);
41     }
42
43     /*
44      * Expands any variable declarations using any of the known
45      * variable marker strings.
46      *
47      * @throws IllegalArgumentException if the input param references
48      * a variable which is not known to the specified source.
49      */

50     public String JavaDoc expand(String JavaDoc param) {
51         for(int i=0; i<nEntries; ++i) {
52             param = expand(
53                 param,
54                 (String JavaDoc) markers.get(i),
55                 (Map JavaDoc) sources.get(i));
56         }
57         return param;
58     }
59     
60     /**
61      * Replace any occurrences within the string of the form
62      * "marker{key}" with the value from source[key].
63      * <p>
64      * Commonly, the variable marker is "$", in which case variables
65      * are indicated by ${key} in the string.
66      * <p>
67      * Returns the string after performing all substitutions.
68      * <p>
69      * If no substitutions were made, the input string object is
70      * returned (not a copy).
71      *
72      * @throws IllegalArgumentException if the input param references
73      * a variable which is not known to the specified source.
74      */

75     public String JavaDoc expand(String JavaDoc str, String JavaDoc marker, Map JavaDoc source) {
76         String JavaDoc startMark = marker + "{";
77         int markLen = startMark.length();
78         
79         int index = 0;
80         for(;;)
81         {
82             index = str.indexOf(startMark, index);
83             if (index == -1)
84             {
85                 return str;
86             }
87             
88             int startIndex = index + markLen;
89             if (startIndex > str.length())
90             {
91                 throw new IllegalArgumentException JavaDoc(
92                     "var expression starts at end of string");
93             }
94             
95             int endIndex = str.indexOf("}", index + markLen);
96             if (endIndex == -1)
97             {
98                 throw new IllegalArgumentException JavaDoc(
99                     "var expression starts but does not end");
100             }
101             
102             String JavaDoc key = str.substring(index+markLen, endIndex);
103             Object JavaDoc value = source.get(key);
104             if (value == null) {
105                 throw new IllegalArgumentException JavaDoc(
106                     "parameter [" + key + "] is not defined.");
107             }
108             String JavaDoc varValue = value.toString();
109             
110             str = str.substring(0, index) + varValue + str.substring(endIndex+1);
111             index += varValue.length();
112         }
113     }
114         
115 }
116
Popular Tags