001: /*
002: * Template.java
003: *
004: *
005: * Copyright (c) 2003 Rimfaxe ApS (www.rimfaxe.com).
006: * All rights reserved.
007: *
008: * This package is written by Lars Andersen <lars@rimfaxe.com>
009: * and licensed by Rimfaxe ApS.
010: *
011: * Redistribution and use in source and binary forms, with or without
012: * modification, are permitted provided that the following conditions
013: * are met:
014: *
015: * 1. Redistributions of source code must retain the above copyright
016: * notice, this list of conditions and the following disclaimer.
017: *
018: * 2. Redistributions in binary form must reproduce the above copyright
019: * notice, this list of conditions and the following disclaimer in
020: * the documentation and/or other materials provided with the
021: * distribution.
022: *
023: * 3. The end-user documentation included with the redistribution, if
024: * any, must include the following acknowlegement:
025: * "This product includes software developed by Rimfaxe ApS
026: (www.rimfaxe.com)"
027: * Alternately, this acknowlegement may appear in the software itself,
028: * if and wherever such third-party acknowlegements normally appear.
029: *
030: * 4. The names "Rimfaxe", "Rimfaxe Software", "Lars Andersen" and
031: * "Rimfaxe WebServer" must not be used to endorse or promote products
032: * derived from this software without prior written permission. For written
033: * permission, please contact info@rimfaxe.com
034: *
035: * 5. Products derived from this software may not be called "Rimfaxe"
036: * nor may "Rimfaxe" appear in their names without prior written
037: * permission of the Rimfaxe ApS.
038: *
039: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
040: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
041: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
042: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
043: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
044: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
045: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
046: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
047: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
048: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
049: * SUCH DAMAGE.
050: *
051: */
052:
053: package com.rimfaxe.util;
054:
055: import java.util.*;
056:
057: /**
058: *
059: * @author Lars A
060: */
061: public class Template extends Object {
062: String tag = "";
063: String valuetoken = "";
064: String templ = "";
065: float rehash = (float) 0.90;
066: Map replace = Collections.synchronizedMap(new HashMap(30, rehash));
067:
068: StringTokenizer tkz = null;
069: boolean tkz_inside = false;
070:
071: /**
072: * Creates new template
073: */
074: public Template(String str) {
075: this .templ = new String(str);
076: }
077:
078: public Template(String str, String tag) {
079: this .tag = tag;
080: this .templ = new String(str);
081: }
082:
083: public void replace(String from, String to) {
084: this .replace.put(from.toLowerCase(), to);
085: }
086:
087: public String getNextValue(String token) {
088: String val = null;
089: String tmp = new String("");
090:
091: if (!token.equalsIgnoreCase(valuetoken)) {
092: valuetoken = token;
093: tkz = new StringTokenizer(templ, "<=>", true);
094: tkz_inside = false;
095: }
096: if (!tkz_inside) {
097: if (tag.equalsIgnoreCase(""))
098: tkz_inside = true;
099: else
100: while (tkz.hasMoreElements()
101: && (!tmp.equalsIgnoreCase(tag)))
102: tmp = tkz.nextToken();
103: tkz_inside = true;
104: }
105:
106: boolean found = false;
107:
108: while (tkz.hasMoreElements() && (!found)) {
109: tmp = tkz.nextToken();
110: if (tmp.equalsIgnoreCase(token)) {
111: tkz.nextToken();
112: val = tkz.nextToken();
113:
114: if (this .replace.containsKey(tmp + "=" + val)) {
115: val = null;
116: found = false;
117: } else
118: found = true;
119:
120: }
121: if (tmp.equalsIgnoreCase("/" + tag)) {
122: tkz = new StringTokenizer(templ, "<=>", true);
123: tkz_inside = false;
124: return null;
125: }
126: }
127: return val;
128: }
129:
130: public void reset() {
131: float rehash = (float) 0.90;
132: replace = Collections.synchronizedMap(new HashMap(30, rehash));
133: tkz = new StringTokenizer(templ, "<=>", true);
134: tkz_inside = false;
135: }
136:
137: public String simpleParser() {
138: StringBuffer out = new StringBuffer();
139: StringTokenizer tok = new StringTokenizer(templ, "<>", true);
140: String tmp = "";
141: while (tok.hasMoreElements()) {
142: tmp = tok.nextToken();
143: if (replace.containsKey(tmp)) {
144: out.deleteCharAt(out.length() - 1);
145: out.append("" + this .replace.get(tmp));
146: tok.nextToken();
147: } else
148: out.append(tmp);
149:
150: }
151: return out.toString();
152: }
153:
154: public String tagParser() {
155: boolean inside = false;
156: boolean found = false;
157:
158: StringBuffer out = new StringBuffer();
159: StringTokenizer tok = new StringTokenizer(templ, "<>", true);
160: String tmp = "";
161: while ((tok.hasMoreElements()) && (!found)) {
162: tmp = tok.nextToken();
163:
164: if (tmp.equalsIgnoreCase("/" + tag)) {
165: out.deleteCharAt(out.length() - 1);
166: inside = false;
167: tok.nextToken();
168: found = true;
169: }
170:
171: if (inside) {
172: if (replace.containsKey(tmp)) {
173: out.deleteCharAt(out.length() - 1);
174: out.append("" + this .replace.get(tmp));
175: tok.nextToken();
176: } else
177: out.append(tmp);
178: }
179:
180: if (tmp.equalsIgnoreCase(tag)) {
181: inside = true;
182: tok.nextToken();
183: }
184: }
185: return out.toString();
186: }
187:
188: public String toString() {
189: if (tag.equalsIgnoreCase(""))
190: return simpleParser();
191: else
192: return tagParser();
193: }
194:
195: }
|