001: /*
002: * Copyright 2005 Paul Hinds
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: package org.tp23.antinstaller.input;
017:
018: import java.io.File;
019: import java.io.IOException;
020: import java.util.HashMap;
021: import java.util.Map;
022: import java.util.Properties;
023:
024: import org.tp23.antinstaller.InstallerContext;
025:
026: /**
027: * <p>Data Holder for results of the data collection and convenience methods for
028: * obtaining default values containing ${prop.name}/blah syntax </p>
029: * @todo Ensure in the validator (and Docs) that developers only add ${refs} for properties set on earlier pages
030: * @author Paul Hinds
031: * @version $Id: ResultContainer.java,v 1.6 2007/01/28 10:25:48 teknopaul Exp $
032: */
033: public class ResultContainer {
034:
035: private HashMap properties = new HashMap();
036: private Properties environment = InstallerContext.getEnvironment();
037: private File installRoot;
038:
039: public ResultContainer() {
040: }
041:
042: /**
043: * fetch a string for File and Directory inputs that expands ${refs} and
044: * also creates absolute paths from relative paths in the default value
045: * @param defaultString String
046: * @return String
047: */
048: public String getDefaultFileRef(String defaultString) {
049: if (defaultString == null) {
050: return null;
051: }
052:
053: String expandedRefs = getDefaultValue(defaultString);
054: File ref = new File(expandedRefs);
055: if (!ref.isAbsolute()) {
056: String path = null;
057: try {
058: path = new File(installRoot, expandedRefs)
059: .getCanonicalPath();
060: } catch (IOException ex) {
061: // this is a bugger, but it should not happen it implies . or ..
062: // can not be resolved, all we can do is return the . or .. and hope
063: // it works later
064: path = new File(installRoot, expandedRefs)
065: .getAbsolutePath();
066: }
067: return path;
068: } else {
069: String path = ref.getAbsolutePath();
070: return path;
071: }
072: }
073:
074: /**
075: *
076: * Handles dereferenceing ${propName} syntax in default value fields
077: * @param defaultString String a plain String or a String with ${ref} references
078: * @return String
079: */
080: public String getDefaultValue(String defaultString) {
081: if (defaultString == null) {
082: return null;
083: }
084:
085: char[] characters = defaultString.toCharArray();
086: char c;
087: StringBuffer result = new StringBuffer();
088:
089: StringBuffer propertyNameBuffer = new StringBuffer();
090: boolean inProp = false; // state flag indicating parsing a propertyName
091: for (int i = 0; i < characters.length;) {
092: c = characters[i];
093: if (c == '$'
094: && (characters.length > i + 1 && characters[i + 1] == '{')) {
095: if (inProp) {
096: //Nested property
097: int endIndex = defaultString.indexOf('}', i + 1);
098: if (endIndex != -1) {
099: ++endIndex;
100: propertyNameBuffer
101: .append(getDefaultValue(defaultString
102: .substring(i, endIndex)));
103: i = endIndex;
104: continue;
105: } else {
106: result.append(propertyNameBuffer.toString());
107: propertyNameBuffer = new StringBuffer();
108: }
109: } else {
110: inProp = true;
111: propertyNameBuffer.append(c);
112: ++i;
113: continue;
114: }
115: } else if (c == '{') {
116: if (inProp) {
117: propertyNameBuffer.append(c);
118: if (characters[i - 1] != '$') {
119: inProp = false;
120: result.append(propertyNameBuffer.toString());
121: propertyNameBuffer = new StringBuffer();
122: }
123: ++i;
124: continue;
125: }
126: } else if (c == '}') {
127: if (inProp) {
128: appendProperty(propertyNameBuffer, result);
129: propertyNameBuffer = new StringBuffer();
130: inProp = false;
131: ++i;
132: continue;
133: }
134: }
135: if (!inProp)
136: result.append(c);
137: else
138: propertyNameBuffer.append(c);
139: ++i;
140: }
141: if (propertyNameBuffer.length() != 0) {
142: result.append(propertyNameBuffer.toString());
143: }
144: return result.toString();
145: }
146:
147: public HashMap getResults() {
148: return properties;
149: }
150:
151: public void setProperty(String key, String value) {
152: properties.put(key, value);
153: }
154:
155: public String getProperty(String key) {
156: return (String) properties.get(key);
157: }
158:
159: public void setInstallRoot(File installRoot) {
160: this .installRoot = installRoot;
161: }
162:
163: /**
164: * @since 0.7.1 to support installs from readonly media
165: * @return Map
166: */
167: public Map getAllProperties() {
168: return properties;
169: }
170:
171: /**
172: * Appends the property if found or inserts an empty string.
173: * This method now supports loading environment variables.
174: * @param propertyNameBuffer StringBuffer
175: * @param result StringBuffer
176: */
177: private void appendProperty(StringBuffer propertyNameBuffer,
178: StringBuffer result) {
179: String propertyName = propertyNameBuffer.toString();
180: String key = propertyName.substring(2);
181: String value = (String) properties.get(key);
182: if (value == null
183: && key.startsWith(InstallerContext.ENV_PREFIX)) {
184: value = environment.getProperty(key);
185: }
186: if (value == null
187: && key.startsWith(InstallerContext.JAVA_PREFIX)) {
188: value = environment.getProperty(key);
189: }
190: if (value != null) {
191: result.append(value);
192: }
193: }
194: }
|