001: /*
002: * Copyright (c) JForum Team
003: * All rights reserved.
004:
005: * Redistribution and use in source and binary forms,
006: * with or without modification, are permitted provided
007: * that the following conditions are met:
008:
009: * 1) Redistributions of source code must retain the above
010: * copyright notice, this list of conditions and the
011: * following disclaimer.
012: * 2) Redistributions in binary form must reproduce the
013: * above copyright notice, this list of conditions and
014: * the following disclaimer in the documentation and/or
015: * other materials provided with the distribution.
016: * 3) Neither the name of "Rafael Steil" nor
017: * the names of its contributors may be used to endorse
018: * or promote products derived from this software without
019: * specific prior written permission.
020: *
021: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
022: * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
023: * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
024: * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
025: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
026: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
027: * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
028: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
029: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
030: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
031: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
032: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
033: * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
034: * IN CONTRACT, STRICT LIABILITY, OR TORT
035: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
036: * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
037: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
038: *
039: * Created on May 31, 2004 by pieter
040: * The JForum Project
041: * http://www.jforum.net
042: */
043: package net.jforum.util.preferences;
044:
045: import java.util.HashMap;
046: import java.util.Map;
047:
048: /**
049: * @author Pieter
050: * @author Rafael Steil
051: * @version $Id: VariableExpander.java,v 1.6 2006/08/20 22:47:37 rafaelsteil Exp $
052: */
053: public class VariableExpander {
054: private VariableStore variables;
055:
056: private String pre;
057: private String post;
058:
059: private Map cache;
060:
061: public VariableExpander(VariableStore variables, String pre,
062: String post) {
063: this .variables = variables;
064: this .pre = pre;
065: this .post = post;
066: cache = new HashMap();
067: }
068:
069: public void clearCache() {
070: cache.clear();
071: }
072:
073: public String expandVariables(String source) {
074: String result = (String) this .cache.get(source);
075:
076: if (source == null || result != null) {
077: return result;
078: }
079:
080: int fIndex = source.indexOf(this .pre);
081:
082: if (fIndex == -1) {
083: return source;
084: }
085:
086: StringBuffer sb = new StringBuffer(source);
087:
088: while (fIndex > -1) {
089: int lIndex = sb.indexOf(this .post);
090:
091: int start = fIndex + this .pre.length();
092:
093: if (fIndex == 0) {
094: String varName = sb.substring(start, start + lIndex
095: - this .pre.length());
096: sb.replace(fIndex, fIndex + lIndex + 1, this .variables
097: .getVariableValue(varName));
098: } else {
099: String varName = sb.substring(start, lIndex);
100: sb.replace(fIndex, lIndex + 1, this.variables
101: .getVariableValue(varName));
102: }
103:
104: fIndex = sb.indexOf(this.pre);
105: }
106:
107: result = sb.toString();
108:
109: this.cache.put(source, result);
110:
111: return result;
112: }
113: }
|