001: /*
002: * This file is part of "SnipSnap Radeox Rendering Engine".
003: *
004: * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
005: * All Rights Reserved.
006: *
007: * Please visit http://radeox.org/ for updates and contact.
008: *
009: * --LICENSE NOTICE--
010: * Licensed under the Apache License, Version 2.0 (the "License");
011: * you may not use this file except in compliance with the License.
012: * You may obtain a copy of the License at
013: *
014: * http://www.apache.org/licenses/LICENSE-2.0
015: *
016: * Unless required by applicable law or agreed to in writing, software
017: * distributed under the License is distributed on an "AS IS" BASIS,
018: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019: * See the License for the specific language governing permissions and
020: * limitations under the License.
021: * --LICENSE NOTICE--
022: */
023:
024: package org.radeox.macro.parameter;
025:
026: import java.util.HashMap;
027: import java.util.Map;
028: import java.util.StringTokenizer;
029:
030: import org.radeox.api.engine.context.RenderContext;
031: import org.radeox.api.macro.MacroParameter;
032:
033: /**
034: * @author
035: * @version $Id: BaseMacroParameter.java 7707 2006-04-12 17:30:19Z
036: * ian@caret.cam.ac.uk $
037: */
038:
039: public class BaseMacroParameter implements MacroParameter {
040: private String content;
041:
042: protected Map params;
043:
044: private int size;
045:
046: protected RenderContext context;
047:
048: private int start;
049:
050: private int end;
051:
052: private int contentStart;
053:
054: private int contentEnd;
055:
056: public BaseMacroParameter() {
057: }
058:
059: public BaseMacroParameter(RenderContext context) {
060: this .context = context;
061: }
062:
063: public void setParams(String stringParams) {
064: params = split(stringParams, "|");
065: size = params.size();
066: }
067:
068: public RenderContext getContext() {
069: return context;
070: }
071:
072: public Map getParams() {
073: return params;
074: }
075:
076: public String getContent() {
077: return content;
078: }
079:
080: public void setContent(String content) {
081: this .content = content;
082: }
083:
084: public int getLength() {
085: return size;
086: }
087:
088: public String get(String index, int idx) {
089: String result = get(index);
090: if (result == null) {
091: result = get(idx);
092: }
093: return result;
094: }
095:
096: public String get(String index) {
097: return (String) params.get(index);
098: }
099:
100: public String get(int index) {
101: return get("" + index);
102: }
103:
104: /**
105: * Splits a String on a delimiter to a List. The function works like the
106: * perl-function split.
107: *
108: * @param aString
109: * a String to split
110: * @param delimiter
111: * a delimiter dividing the entries
112: * @return a Array of splittet Strings
113: */
114:
115: private Map split(String aString, String delimiter) {
116: Map result = new HashMap();
117:
118: if (null != aString) {
119: StringTokenizer st = new StringTokenizer(aString, delimiter);
120: int i = 0;
121:
122: while (st.hasMoreTokens()) {
123: String value = st.nextToken();
124: String key = "" + i;
125: if (value.indexOf("=") != -1) {
126: // Store this for
127: result.put(key, insertValue(value));
128: int index = value.indexOf("=");
129: key = value.substring(0, index);
130: value = value.substring(index + 1);
131:
132: result.put(key, insertValue(value));
133: } else {
134: result.put(key, insertValue(value));
135: }
136: i++;
137: }
138: }
139: return result;
140: }
141:
142: private String insertValue(String s) {
143: int idx = s.indexOf('$');
144: if (idx != -1) {
145: StringBuffer tmp = new StringBuffer();
146: Map globals = context.getParameters();
147: String var = s.substring(idx + 1);
148: if (idx > 0)
149: tmp.append(s.substring(0, idx));
150: if (globals.containsKey(var)) {
151: tmp.append(globals.get(var));
152: }
153: return tmp.toString();
154: }
155: return s;
156: }
157:
158: public void setStart(int start) {
159: this .start = start;
160: }
161:
162: public void setEnd(int end) {
163: this .end = end;
164: }
165:
166: public int getStart() {
167: return this .start;
168: }
169:
170: public int getEnd() {
171: return this .end;
172: }
173:
174: public int getContentStart() {
175: return contentStart;
176: }
177:
178: public void setContentStart(int contentStart) {
179: this .contentStart = contentStart;
180: }
181:
182: public int getContentEnd() {
183: return contentEnd;
184: }
185:
186: public void setContentEnd(int contentEnd) {
187: this.contentEnd = contentEnd;
188: }
189:
190: }
|