1 | /*
|
---|
2 | * Copyright 2005 Paul Hinds
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | */
|
---|
16 | package org.tp23.antinstaller.input;
|
---|
17 |
|
---|
18 | import java.io.File;
|
---|
19 | import java.io.IOException;
|
---|
20 | import java.util.HashMap;
|
---|
21 | import java.util.Map;
|
---|
22 | import java.util.Properties;
|
---|
23 |
|
---|
24 | import org.tp23.antinstaller.InstallerContext;
|
---|
25 |
|
---|
26 |
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * <p>Data Holder for results of the data collection and convenience methods for
|
---|
30 | * obtaining default values containing ${prop.name}/blah syntax </p>
|
---|
31 | * @todo Ensure in the validator (and Docs) that developers only add ${refs} for properties set on earlier pages
|
---|
32 | * @author Paul Hinds
|
---|
33 | * @version $Id: ResultContainer.java,v 1.6 2007/01/28 10:25:48 teknopaul Exp $
|
---|
34 | */
|
---|
35 | public class ResultContainer {
|
---|
36 |
|
---|
37 | private HashMap properties = new HashMap();
|
---|
38 | private Properties environment = InstallerContext.getEnvironment();
|
---|
39 | private File installRoot;
|
---|
40 |
|
---|
41 | public ResultContainer() {
|
---|
42 | }
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * fetch a string for File and Directory inputs that expands ${refs} and
|
---|
46 | * also creates absolute paths from relative paths in the default value
|
---|
47 | * @param defaultString String
|
---|
48 | * @return String
|
---|
49 | */
|
---|
50 | public String getDefaultFileRef(String defaultString){
|
---|
51 | if(defaultString == null) {
|
---|
52 | return null;
|
---|
53 | }
|
---|
54 |
|
---|
55 | String expandedRefs = getDefaultValue(defaultString);
|
---|
56 | File ref = new File(expandedRefs);
|
---|
57 | if(!ref.isAbsolute()){
|
---|
58 | String path = null;
|
---|
59 | try {
|
---|
60 | path = new File(installRoot, expandedRefs).getCanonicalPath();
|
---|
61 | }
|
---|
62 | catch (IOException ex) {
|
---|
63 | // this is a bugger, but it should not happen it implies . or ..
|
---|
64 | // can not be resolved, all we can do is return the . or .. and hope
|
---|
65 | // it works later
|
---|
66 | path = new File(installRoot, expandedRefs).getAbsolutePath();
|
---|
67 | }
|
---|
68 | return path;
|
---|
69 | } else {
|
---|
70 | String path = ref.getAbsolutePath();
|
---|
71 | return path;
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | /**
|
---|
76 | *
|
---|
77 | * Handles dereferenceing ${propName} syntax in default value fields
|
---|
78 | * @param defaultString String a plain String or a String with ${ref} references
|
---|
79 | * @return String
|
---|
80 | */
|
---|
81 | public String getDefaultValue(String defaultString) {
|
---|
82 | if(defaultString == null) {
|
---|
83 | return null;
|
---|
84 | }
|
---|
85 |
|
---|
86 | char[] characters = defaultString.toCharArray();
|
---|
87 | char c;
|
---|
88 | StringBuffer result = new StringBuffer();
|
---|
89 |
|
---|
90 | StringBuffer propertyNameBuffer = new StringBuffer();
|
---|
91 | boolean inProp = false; // state flag indicating parsing a propertyName
|
---|
92 | for (int i = 0; i < characters.length;) {
|
---|
93 | c = characters[i];
|
---|
94 | if ( c == '$' && ( characters.length > i + 1 && characters[i + 1] == '{' ) ){
|
---|
95 | if(inProp){
|
---|
96 | //Nested property
|
---|
97 | int endIndex = defaultString.indexOf( '}', i + 1 );
|
---|
98 | if( endIndex != -1 ) {
|
---|
99 | ++endIndex;
|
---|
100 | propertyNameBuffer.append( getDefaultValue( defaultString.substring( i, endIndex ) ) );
|
---|
101 | i = endIndex;
|
---|
102 | continue;
|
---|
103 | }
|
---|
104 | else {
|
---|
105 | result.append(propertyNameBuffer.toString());
|
---|
106 | propertyNameBuffer = new StringBuffer();
|
---|
107 | }
|
---|
108 | }
|
---|
109 | else{
|
---|
110 | inProp = true;
|
---|
111 | propertyNameBuffer.append(c);
|
---|
112 | ++i;
|
---|
113 | continue;
|
---|
114 | }
|
---|
115 | }
|
---|
116 | else if (c == '{') {
|
---|
117 | if (inProp) {
|
---|
118 | propertyNameBuffer.append(c);
|
---|
119 | if(characters[i - 1] != '$') {
|
---|
120 | inProp=false;
|
---|
121 | result.append(propertyNameBuffer.toString());
|
---|
122 | propertyNameBuffer = new StringBuffer();
|
---|
123 | }
|
---|
124 | ++i;
|
---|
125 | continue;
|
---|
126 | }
|
---|
127 | }
|
---|
128 | else if (c == '}') {
|
---|
129 | if (inProp) {
|
---|
130 | appendProperty(propertyNameBuffer, result);
|
---|
131 | propertyNameBuffer = new StringBuffer();
|
---|
132 | inProp = false;
|
---|
133 | ++i;
|
---|
134 | continue;
|
---|
135 | }
|
---|
136 | }
|
---|
137 | if (!inProp) result.append(c);
|
---|
138 | else propertyNameBuffer.append(c);
|
---|
139 | ++i;
|
---|
140 | }
|
---|
141 | if(propertyNameBuffer.length() != 0) {
|
---|
142 | result.append(propertyNameBuffer.toString());
|
---|
143 | }
|
---|
144 | return result.toString();
|
---|
145 | }
|
---|
146 |
|
---|
147 |
|
---|
148 |
|
---|
149 | public HashMap getResults() {
|
---|
150 | return properties;
|
---|
151 | }
|
---|
152 | public void setProperty(String key, String value){
|
---|
153 | properties.put(key, value);
|
---|
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 | * @since 0.7.1 to support installs from readonly media
|
---|
164 | * @return Map
|
---|
165 | */
|
---|
166 | public Map getAllProperties(){
|
---|
167 | return properties;
|
---|
168 | }
|
---|
169 |
|
---|
170 | /**
|
---|
171 | * Appends the property if found or inserts an empty string.
|
---|
172 | * This method now supports loading environment variables.
|
---|
173 | * @param propertyNameBuffer StringBuffer
|
---|
174 | * @param result StringBuffer
|
---|
175 | */
|
---|
176 | private void appendProperty(StringBuffer propertyNameBuffer, StringBuffer result) {
|
---|
177 | String propertyName = propertyNameBuffer.toString();
|
---|
178 | String key = propertyName.substring(2);
|
---|
179 | String value = (String)properties.get(key);
|
---|
180 | if(value == null && key.startsWith(InstallerContext.ENV_PREFIX)) {
|
---|
181 | value = environment.getProperty(key);
|
---|
182 | }
|
---|
183 | if(value == null && key.startsWith(InstallerContext.JAVA_PREFIX)) {
|
---|
184 | value = environment.getProperty(key);
|
---|
185 | }
|
---|
186 | if (value != null) {
|
---|
187 | result.append(value);
|
---|
188 | }
|
---|
189 | }
|
---|
190 | }
|
---|