01: /*
02: * Copyright 2008 Gerd Ziegler (www.gerdziegler.de)
03: * Licensed under the Apache License, Version 2.0 (the "License");
04: * you may not use this file except in compliance with the License.
05: * You may obtain a copy of the License at
06: * http://www.apache.org/licenses/LICENSE-2.0
07: * Unless required by applicable law or agreed to in writing,
08: * software distributed under the License is distributed on an
09: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
10: * either express or implied. See the License for the specific
11: * language governing permissions and limitations under the License.
12: * 09.03.2008
13: * @author www.gerdziegler.de
14: */
15: package org.ztemplates.render.impl;
16:
17: import java.util.Map;
18:
19: import org.apache.log4j.Logger;
20:
21: public class ZReplaceUtil {
22: private static final Logger log = Logger
23: .getLogger(ZReplaceUtil.class);
24:
25: public static StringBuffer replace(StringBuffer in,
26: Map<String, Object> val) {
27: StringBuffer out = new StringBuffer();
28: for (int i = 0; i < in.length(); i++) {
29: int idx1 = in.indexOf("${", i);
30: if (idx1 < 0) {
31: out.append(in, i, in.length());
32: return out;
33: } else {
34: out.append(in, i, idx1);
35: i = idx1;
36: int idx2 = in.indexOf("}", idx1);
37: if (idx2 < 0) {
38: out.append(in, idx1, in.length());
39: return out;
40: } else {
41: String key = in.substring(idx1 + 2, idx2);
42: Object value = val.get(key);
43: if (value == null) {
44: out.append(in, i, idx2 + 1);
45: } else {
46: out.append(value);
47: }
48: i = idx2;
49: }
50: }
51:
52: }
53: return out;
54: }
55: }
|