001: /*
002: * Copyright 2006 Luca Garulli (luca.garulli@assetdata.it)
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.romaframework.wizard.helper;
018:
019: import java.text.DateFormat;
020: import java.text.SimpleDateFormat;
021: import java.util.Date;
022:
023: /**
024: * Helper for templating operations.
025: *
026: * @author Luca Garulli (luca.garulli@assetdata.it)
027: */
028: public class TemplateHelper {
029:
030: protected static DateFormat dateFormat = new SimpleDateFormat(
031: "yyyyMMdd");
032: protected static int sequence = 0;
033:
034: protected static final int RANDOM_NUMBERS = 10000;
035:
036: public static final String VAR_SEQUENCE = "sequence";
037: public static final String VAR_RANDOM = "random";
038: public static final String VAR_SYSDATE = "sysdate";
039:
040: public static final String VARIABLE_START_TAG = "#{";
041: public static final String VARIABLE_END_TAG = "}";
042:
043: /**
044: * Get the value by resolving all variables contained in the value passed
045: *
046: * @param iValue
047: * @param iCurrentValue
048: * @return Expanded value
049: */
050: public static String resolveValue(String iValue) {
051: if (iValue == null || iValue.length() == 0)
052: return iValue;
053:
054: StringBuffer value = new StringBuffer();
055: int startVar = 0;
056: int endVar;
057: int startPos = 0;
058: String varName;
059: String varValue;
060:
061: while (startVar > -1) {
062: startVar = iValue.indexOf(VARIABLE_START_TAG, startPos);
063: if (startVar > -1) {
064: // COPY ALL BEFORE START VARIABLE TAG
065: value.append(iValue.substring(startPos, startVar));
066:
067: endVar = iValue.indexOf(VARIABLE_END_TAG, startVar);
068: if (endVar == -1)
069: throw new IllegalArgumentException(
070: "Error on resolving property: Missed '"
071: + VARIABLE_END_TAG + "' tag in "
072: + iValue.substring(startVar));
073:
074: varName = iValue.substring(startVar
075: + VARIABLE_START_TAG.length(), endVar);
076:
077: varValue = resolveVariable(varName);
078:
079: value.append(varValue);
080:
081: startPos = endVar + 1;
082: } else
083: // COPY ALL THE REMAINING STRING
084: value.append(iValue.substring(startPos));
085: }
086: return value.toString();
087: }
088:
089: /**
090: * Resolve a variable with a value
091: *
092: * @param iVarName
093: * Variable name
094: * @param iCurrentValue
095: * Current buffer value
096: * @return The value resolved
097: */
098: public static String resolveVariable(String iVarName) {
099: if (iVarName.equals(VAR_SYSDATE))
100: return dateFormat.format(new Date());
101: else if (iVarName.equals(VAR_SEQUENCE))
102: return String.valueOf(sequence++);
103: else if (iVarName.equals(VAR_RANDOM))
104: return String.valueOf(Math.random() * RANDOM_NUMBERS);
105:
106: String result = System.getProperty(iVarName);
107: if (result != null)
108: return result;
109:
110: throw new IllegalArgumentException("Undefined variable "
111: + iVarName);
112: }
113: }
|