|
@@ -11,99 +11,88 @@ |
11 |
11 |
|
import static com.esotericsoftware.scar.support.Parameter.*; |
12 |
12 |
|
|
13 |
13 |
|
@SuppressWarnings({"UnusedDeclaration"}) |
14 |
|
- |
public class ProjectParameters extends Util |
|
14 |
+ |
public class ProjectParameters extends FileUtil |
15 |
15 |
|
{ |
16 |
16 |
|
public static final Parameter NAME = def( "name", Form.STRING, "The name of the project. Used to name the JAR.", // |
17 |
17 |
|
"Default:\n" + // |
18 |
18 |
|
" The name of the directory containing the project YAML file, or\n" + // |
19 |
19 |
|
" the name of the YAML file if it is not 'build'." ); |
20 |
20 |
|
|
21 |
|
- |
public static final Parameter TARGET = def( "target", Form.STRING, "The directory to output build artifacts.", // |
22 |
|
- |
"Default: The directory containing the project YAML file, plus '../target/name'." ); |
23 |
|
- |
|
24 |
|
- |
public static final Parameter VERSION = def( "version", Form.STRING, "The version of the project. If available, used to name the JAR." ); |
|
21 |
+ |
public static final Parameter VERSION = def( "version", Form.STRING, "The version of the project. If available, used to name the 'default' named JAR." ); |
25 |
22 |
|
|
26 |
|
- |
public static final Parameter RESOURCES = def( "resources", Form.PATHS, "Wildcard patterns for the file(s) to include in the JAR.", // |
27 |
|
- |
"Default: 'resources' or 'src/main/resources'." ); |
|
23 |
+ |
public static final Parameter MAIN = def( "main", Form.STRING, "Name of the main class." ); |
28 |
24 |
|
|
29 |
|
- |
public static final Parameter DIST = def( "dist", Form.PATHS, "Wildcard patterns for the file(s) to include in the distribution, outside the JAR.", // |
30 |
|
- |
"Default: 'dist'." ); |
|
25 |
+ |
public static final Parameter TARGET = def( "target", Form.STRING, "The directory to output build artifacts.", // |
|
26 |
+ |
"Default: The directory containing the project YAML file, plus 'build'." ); |
31 |
27 |
|
|
32 |
|
- |
public static final Parameter SOURCE = def( "source", Form.PATHS, "Wildcard patterns for the Java file(s) to compile.", // |
33 |
|
- |
"Default: 'src|**/*.java' or 'src/main/java|**/*.java'." ); |
|
28 |
+ |
public static final Parameter DEPENDENCIES = def( "dependencies", Form.STRING_LIST, "Relative or absolute path(s) to dependency project directories or YAML files." ); |
34 |
29 |
|
|
35 |
30 |
|
public static final Parameter CLASSPATH = def( "classpath", Form.PATHS, "Wildcard patterns for the file(s) to include on the classpath.", // |
36 |
|
- |
"Default: 'lib|**/*.jar'." ); |
|
31 |
+ |
"Default: 'lib|**.jar'." ); |
37 |
32 |
|
|
38 |
|
- |
public static final Parameter DEPENDENCIES = def( "dependencies", Form.STRING_LIST, "Relative or absolute path(s) to dependency project directories or YAML files." ); |
|
33 |
+ |
public static final Parameter SOURCE = def( "source", Form.PATHS, "Wildcard patterns for the Java file(s) to compile.", // |
|
34 |
+ |
"Default: 'src|**.java' or 'src/main/java|**.java'." ); |
39 |
35 |
|
|
40 |
|
- |
public static final Parameter INCLUDE = def( "include", Form.STRING_LIST, "Relative or absolute path(s) to project files to inherit properties from." ); |
|
36 |
+ |
public static final Parameter RESOURCES = def( "resources", Form.PATHS, "Wildcard patterns for the file(s) to include in the JAR.", // |
|
37 |
+ |
"Default: 'resources' or 'src/main/resources'." ); |
41 |
38 |
|
|
42 |
|
- |
public static final Parameter MAIN = def( "main", Form.STRING, "Name of the main class." ); |
|
39 |
+ |
public static final Parameter JAR = def( "jar", Form.STRING, "JAR name w/ optional path for the JAR ('.jar' added to the end if does not end with 'jar', case insensitive).", // |
|
40 |
+ |
"Default: '$target$[-$version$]'." ); |
43 |
41 |
|
|
44 |
|
- |
protected final String mName; |
45 |
|
- |
protected final File mCanonicalProjectDir; |
46 |
|
- |
protected final Map<Object, Object> mData = new HashMap<Object, Object>(); |
|
42 |
+ |
public static final Parameter DIST = def( "dist", Form.PATHS, "Wildcard patterns for the file(s) to include in the distribution, outside the JAR." ); |
47 |
43 |
|
|
48 |
|
- |
public ProjectParameters( String pName, File pCanonicalProjectDir, Map<Object, Object> pData ) |
49 |
|
- |
{ |
50 |
|
- |
mCanonicalProjectDir = pCanonicalProjectDir; |
51 |
|
- |
if ( pData != null ) |
52 |
|
- |
{ |
53 |
|
- |
for ( Object key : pData.keySet() ) |
54 |
|
- |
{ |
55 |
|
- |
Object zValue = pData.get( key ); |
56 |
|
- |
if ( key instanceof String ) |
57 |
|
- |
{ |
58 |
|
- |
key = key.toString().toLowerCase(); |
59 |
|
- |
} |
60 |
|
- |
mData.put( key, zValue ); |
61 |
|
- |
} |
62 |
|
- |
} |
63 |
|
- |
Object zObjName = mData.get( NAME.getName() ); |
64 |
|
- |
String zName = (zObjName == null) ? null : Util.noEmpty( zObjName.toString() ); |
65 |
|
- |
mData.put( NAME.getName(), mName = (zName != null) ? zName : pName ); |
66 |
|
- |
} |
|
44 |
+ |
public static final Parameter APPDIR = def( "appdir", Form.STRING, "Directory path to bring together all files (both JARs and 'dist', for this project " + "and all dependencies, recursively) the application needs to be run from JAR files." ); |
67 |
45 |
|
|
68 |
|
- |
@Override |
69 |
|
- |
public String toString() |
|
46 |
+ |
// ------------------------------------------------ Default Support ------------------------------------------------ |
|
47 |
+ |
|
|
48 |
+ |
protected synchronized void applyDefaults() |
70 |
49 |
|
{ |
71 |
|
- |
return getName(); |
|
50 |
+ |
defaultTARGET(); |
|
51 |
+ |
defaultCLASSPATH(); |
|
52 |
+ |
defaultSOURCE(); |
|
53 |
+ |
defaultRESOURCES(); |
|
54 |
+ |
defaultJAR(); |
72 |
55 |
|
} |
73 |
56 |
|
|
74 |
|
- |
protected Object valueFor( Object pKey ) |
|
57 |
+ |
protected void defaultJAR() |
75 |
58 |
|
{ |
76 |
|
- |
if ( pKey instanceof String ) |
|
59 |
+ |
String zJar = getJar(); |
|
60 |
+ |
if ( null == zJar ) |
77 |
61 |
|
{ |
78 |
|
- |
pKey = Util.noEmpty( pKey.toString().toLowerCase() ); |
|
62 |
+ |
mManager.put( JAR.getName(), "$target$/$name$" + (hasVersion() ? "-$version$" : "") + ".jar" ); |
79 |
63 |
|
} |
80 |
|
- |
Util.assertNotNull( "key", pKey ); |
81 |
|
- |
synchronized ( this ) |
|
64 |
+ |
else |
82 |
65 |
|
{ |
83 |
|
- |
return mData.get( pKey ); |
|
66 |
+ |
int at = zJar.lastIndexOf( '.' ); |
|
67 |
+ |
if ( at == -1 || !".jar".equalsIgnoreCase( zJar.substring( at ) ) ) |
|
68 |
+ |
{ |
|
69 |
+ |
mManager.put( JAR.getName(), zJar + ".jar" ); |
|
70 |
+ |
} |
84 |
71 |
|
} |
85 |
72 |
|
} |
86 |
73 |
|
|
87 |
|
- |
public synchronized Object[] keys() |
|
74 |
+ |
protected void defaultTARGET() |
88 |
75 |
|
{ |
89 |
|
- |
return mData.keySet().toArray(); |
|
76 |
+ |
defaultKey( TARGET, "build" ); |
90 |
77 |
|
} |
91 |
78 |
|
|
92 |
|
- |
public synchronized File getCanonicalProjectDir() |
|
79 |
+ |
private void defaultCLASSPATH() |
93 |
80 |
|
{ |
94 |
|
- |
return mCanonicalProjectDir; |
|
81 |
+ |
defaultSubDirOptional( CLASSPATH, "lib|**.jar" ); |
95 |
82 |
|
} |
96 |
83 |
|
|
97 |
|
- |
public synchronized String getName() |
|
84 |
+ |
private void defaultRESOURCES() |
98 |
85 |
|
{ |
99 |
|
- |
return mName; |
|
86 |
+ |
defaultSubDirOptional( RESOURCES, "src/main/resources", "resources" ); |
100 |
87 |
|
} |
101 |
88 |
|
|
102 |
|
- |
public String getTarget() |
|
89 |
+ |
private void defaultSOURCE() |
103 |
90 |
|
{ |
104 |
|
- |
return get( TARGET.getName() ); |
|
91 |
+ |
defaultSubDirOptional( SOURCE, "src/main/java|**.java", "src|**.java" ); |
105 |
92 |
|
} |
106 |
93 |
|
|
|
94 |
+ |
// ------------------------------------------- Special Property Accessors ------------------------------------------ |
|
95 |
+ |
|
107 |
96 |
|
public boolean hasVersion() |
108 |
97 |
|
{ |
109 |
98 |
|
return (null != getVersion()); |
|
@@ -111,63 +100,69 @@ |
111 |
100 |
|
|
112 |
101 |
|
public String getVersion() |
113 |
102 |
|
{ |
114 |
|
- |
return get( VERSION.getName() ); |
|
103 |
+ |
return get( JAR.getName() ); |
115 |
104 |
|
} |
116 |
105 |
|
|
117 |
|
- |
public Paths getResources() |
118 |
|
- |
throws IOException |
|
106 |
+ |
public boolean hasMain() |
119 |
107 |
|
{ |
120 |
|
- |
return getPaths( RESOURCES.getName() ); |
|
108 |
+ |
return (null != getMain()); |
121 |
109 |
|
} |
122 |
110 |
|
|
123 |
|
- |
public Paths getDist() |
124 |
|
- |
throws IOException |
|
111 |
+ |
public String getMain() |
125 |
112 |
|
{ |
126 |
|
- |
return getPaths( DIST.getName() ); |
|
113 |
+ |
return get( MAIN.getName() ); |
127 |
114 |
|
} |
128 |
115 |
|
|
129 |
|
- |
public Paths getSource() |
130 |
|
- |
throws IOException |
|
116 |
+ |
public String getTarget() |
131 |
117 |
|
{ |
132 |
|
- |
return getPaths( SOURCE.getName() ); |
|
118 |
+ |
return get( TARGET.getName() ); |
|
119 |
+ |
} |
|
120 |
+ |
|
|
121 |
+ |
public List<String> getDependencies() |
|
122 |
+ |
{ |
|
123 |
+ |
return getListNotNull( DEPENDENCIES.getName() ); |
133 |
124 |
|
} |
134 |
125 |
|
|
135 |
126 |
|
public Paths getClasspath() |
136 |
|
- |
throws IOException |
137 |
127 |
|
{ |
138 |
128 |
|
return getPaths( CLASSPATH.getName() ); |
139 |
129 |
|
} |
140 |
130 |
|
|
141 |
|
- |
public List<String> getDependencies() |
|
131 |
+ |
public Paths getSource() |
142 |
132 |
|
{ |
143 |
|
- |
return getList( DEPENDENCIES.getName() ); |
|
133 |
+ |
return getPaths( SOURCE.getName() ); |
144 |
134 |
|
} |
145 |
135 |
|
|
146 |
|
- |
public List<String> getInclude() |
|
136 |
+ |
public Paths getResources() |
147 |
137 |
|
{ |
148 |
|
- |
return getList( INCLUDE.getName() ); |
|
138 |
+ |
return getPaths( RESOURCES.getName() ); |
149 |
139 |
|
} |
150 |
140 |
|
|
151 |
|
- |
public boolean hasMain() |
|
141 |
+ |
public String getJar() |
152 |
142 |
|
{ |
153 |
|
- |
return (null != getMain()); |
|
143 |
+ |
return get( JAR.getName() ); |
154 |
144 |
|
} |
155 |
145 |
|
|
156 |
|
- |
public String getMain() |
|
146 |
+ |
public Paths getDist() |
157 |
147 |
|
{ |
158 |
|
- |
return get( MAIN.getName() ); |
|
148 |
+ |
return getPaths( DIST.getName() ); |
159 |
149 |
|
} |
160 |
150 |
|
|
161 |
|
- |
// Below here are the accessors for the underlying Data (map) |
|
151 |
+ |
public String getAppDir() |
|
152 |
+ |
{ |
|
153 |
+ |
return get( APPDIR.getName() ); |
|
154 |
+ |
} |
|
155 |
+ |
|
|
156 |
+ |
// ---------------------------------- Generic accessors for the underlying Data (map) ------------------------------ |
162 |
157 |
|
|
163 |
158 |
|
public boolean has( Object key ) |
164 |
159 |
|
{ |
165 |
|
- |
return null != valueFor( key ); |
|
160 |
+ |
return null != getObject( key ); |
166 |
161 |
|
} |
167 |
162 |
|
|
168 |
163 |
|
public Object getObject( Object key ) |
169 |
164 |
|
{ |
170 |
|
- |
return valueFor( key ); |
|
165 |
+ |
return mManager.get( mManager.normalizeKey( key ) ); |
171 |
166 |
|
} |
172 |
167 |
|
|
173 |
168 |
|
public Object getObject( Object key, Object defaultValue ) |
|
@@ -177,166 +172,156 @@ |
177 |
172 |
|
|
178 |
173 |
|
public String get( Object key ) |
179 |
174 |
|
{ |
180 |
|
- |
return get( key, null ); |
|
175 |
+ |
Object zValue = getObject( key ); |
|
176 |
+ |
return (zValue != null) ? zValue.toString() : null; |
181 |
177 |
|
} |
182 |
178 |
|
|
183 |
179 |
|
public String get( Object key, String defaultValue ) |
184 |
180 |
|
{ |
185 |
|
- |
Object value = getObject( key ); |
186 |
|
- |
return (value != null) ? value.toString() : defaultValue; |
|
181 |
+ |
return Util.deNull( get( key ), defaultValue ); |
187 |
182 |
|
} |
188 |
183 |
|
|
189 |
|
- |
public int getInt( Object key ) |
|
184 |
+ |
public int get_int( Object key ) |
190 |
185 |
|
{ |
191 |
|
- |
return getInt( key, 0 ); |
|
186 |
+ |
return get_int( key, 0 ); |
192 |
187 |
|
} |
193 |
188 |
|
|
194 |
|
- |
public int getInt( Object key, int defaultValue ) |
|
189 |
+ |
public int get_int( Object key, int defaultValue ) |
195 |
190 |
|
{ |
196 |
|
- |
Object value = getObject( key ); |
197 |
|
- |
if ( value == null ) |
198 |
|
- |
{ |
199 |
|
- |
return defaultValue; |
200 |
|
- |
} |
201 |
|
- |
if ( value instanceof Number ) |
202 |
|
- |
{ |
203 |
|
- |
return ((Number) value).intValue(); |
204 |
|
- |
} |
205 |
|
- |
return Integer.parseInt( value.toString() ); |
|
191 |
+ |
return getInteger( key, defaultValue ); |
206 |
192 |
|
} |
207 |
193 |
|
|
208 |
|
- |
public float getFloat( Object key ) |
|
194 |
+ |
public Integer getInteger( Object key, int defaultValue ) |
209 |
195 |
|
{ |
210 |
|
- |
return getFloat( key, 0 ); |
|
196 |
+ |
return Util.deNull( getInteger( key ), defaultValue ); |
211 |
197 |
|
} |
212 |
198 |
|
|
213 |
|
- |
public float getFloat( Object key, float defaultValue ) |
|
199 |
+ |
public Integer getInteger( Object key ) |
214 |
200 |
|
{ |
215 |
|
- |
Object value = getObject( key ); |
216 |
|
- |
if ( value == null ) |
217 |
|
- |
{ |
218 |
|
- |
return defaultValue; |
219 |
|
- |
} |
220 |
|
- |
if ( value instanceof Number ) |
221 |
|
- |
{ |
222 |
|
- |
return ((Number) value).floatValue(); |
223 |
|
- |
} |
224 |
|
- |
return Float.parseFloat( value.toString() ); |
|
201 |
+ |
return getCachedWithConvertion( key, INTEGER ); |
225 |
202 |
|
} |
226 |
203 |
|
|
227 |
|
- |
public boolean getBoolean( Object key ) |
|
204 |
+ |
public float get_float( Object key ) |
228 |
205 |
|
{ |
229 |
|
- |
return getBoolean( key, false ); |
|
206 |
+ |
return get_float( key, 0 ); |
230 |
207 |
|
} |
231 |
208 |
|
|
232 |
|
- |
public boolean getBoolean( Object key, boolean defaultValue ) |
|
209 |
+ |
public float get_float( Object key, float defaultValue ) |
233 |
210 |
|
{ |
234 |
|
- |
Object value = getObject( key ); |
235 |
|
- |
if ( value == null ) |
236 |
|
- |
{ |
237 |
|
- |
return defaultValue; |
238 |
|
- |
} |
239 |
|
- |
if ( value instanceof Boolean ) |
240 |
|
- |
{ |
241 |
|
- |
return (Boolean) value; |
242 |
|
- |
} |
243 |
|
- |
return Boolean.parseBoolean( value.toString() ); |
|
211 |
+ |
return getFloat( key, defaultValue ); |
|
212 |
+ |
} |
|
213 |
+ |
|
|
214 |
+ |
public Float getFloat( Object key, float defaultValue ) |
|
215 |
+ |
{ |
|
216 |
+ |
return Util.deNull( getFloat( key ), defaultValue ); |
|
217 |
+ |
} |
|
218 |
+ |
|
|
219 |
+ |
public Float getFloat( Object key ) |
|
220 |
+ |
{ |
|
221 |
+ |
return getCachedWithConvertion( key, FLOAT ); |
|
222 |
+ |
} |
|
223 |
+ |
|
|
224 |
+ |
public boolean get_boolean( Object key ) |
|
225 |
+ |
{ |
|
226 |
+ |
return get_boolean( key, false ); |
|
227 |
+ |
} |
|
228 |
+ |
|
|
229 |
+ |
public boolean get_boolean( Object key, boolean defaultValue ) |
|
230 |
+ |
{ |
|
231 |
+ |
return getBoolean( key, defaultValue ); |
|
232 |
+ |
} |
|
233 |
+ |
|
|
234 |
+ |
public Boolean getBoolean( Object key, boolean defaultValue ) |
|
235 |
+ |
{ |
|
236 |
+ |
return Util.deNull( getBoolean( key ), defaultValue ); |
|
237 |
+ |
} |
|
238 |
+ |
|
|
239 |
+ |
public Boolean getBoolean( Object key ) |
|
240 |
+ |
{ |
|
241 |
+ |
return getCachedWithConvertion( key, BOOLEAN ); |
244 |
242 |
|
} |
245 |
243 |
|
|
246 |
244 |
|
/** |
247 |
|
- |
* Returns a list of strings under the specified key. If the key is a single value, it is placed in a list and returned. If the |
248 |
|
- |
* key does not exist, an empty list is returned. |
|
245 |
+ |
* Returns a list of objects under the specified key. If the key is a single value, it is placed in a list and returned. If the |
|
246 |
+ |
* key does not exist, a list with the defaultValues is returned. |
249 |
247 |
|
*/ |
250 |
|
- |
public List<String> getList( Object key, String... defaultValues ) |
|
248 |
+ |
public List<?> getObjectListNotNull( Object key, Object... defaultValues ) |
251 |
249 |
|
{ |
252 |
|
- |
Object object = getObject( key ); |
253 |
|
- |
if ( object instanceof List ) |
254 |
|
- |
{ |
255 |
|
- |
List src = (List) object; |
256 |
|
- |
List<String> rv = new ArrayList<String>( src.size() ); |
257 |
|
- |
for ( Object entry : src ) |
258 |
|
- |
{ |
259 |
|
- |
rv.add( entry.toString() ); |
260 |
|
- |
} |
261 |
|
- |
return rv; |
262 |
|
- |
} |
263 |
|
- |
if ( object != null ) |
264 |
|
- |
{ |
265 |
|
- |
return Arrays.asList( object.toString() ); |
266 |
|
- |
} |
267 |
|
- |
return (defaultValues == null) ? null : Arrays.asList( defaultValues ); |
|
250 |
+ |
List<?> zList = getObjectList( key ); |
|
251 |
+ |
return (zList != null) ? zList : (defaultValues == null) ? Collections.emptyList() : Arrays.asList( defaultValues ); |
268 |
252 |
|
} |
269 |
253 |
|
|
270 |
254 |
|
/** |
271 |
255 |
|
* Returns a list of objects under the specified key. If the key is a single value, it is placed in a list and returned. If the |
272 |
|
- |
* key does not exist, an empty list is returned. |
|
256 |
+ |
* key does not exist, a 'null' is returned. |
273 |
257 |
|
*/ |
274 |
|
- |
public List getObjectList( Object key, Object... defaultValues ) |
|
258 |
+ |
public List<?> getObjectList( Object key ) |
275 |
259 |
|
{ |
276 |
|
- |
Object object = getObject( key ); |
277 |
|
- |
if ( object instanceof List ) |
278 |
|
- |
{ |
279 |
|
- |
return (List) object; |
280 |
|
- |
} |
281 |
|
- |
if ( object != null ) |
282 |
|
- |
{ |
283 |
|
- |
return Arrays.asList( object ); |
284 |
|
- |
} |
285 |
|
- |
return (defaultValues == null) ? null : Arrays.asList( defaultValues ); |
|
260 |
+ |
return getCachedWithConvertion( key, LIST_OBJECT ); |
|
261 |
+ |
} |
|
262 |
+ |
|
|
263 |
+ |
/** |
|
264 |
+ |
* Returns a list of strings under the specified key. If the key is a single value, it is placed in a list and returned. If the |
|
265 |
+ |
* key does not exist, a list with the defaultValues is returned. |
|
266 |
+ |
*/ |
|
267 |
+ |
public List<String> getListNotNull( Object key, String... defaultValues ) |
|
268 |
+ |
{ |
|
269 |
+ |
List<String> zList = getList( key ); |
|
270 |
+ |
return (zList != null) ? zList : (defaultValues == null) ? Collections.<String>emptyList() : Arrays.asList( defaultValues ); |
|
271 |
+ |
} |
|
272 |
+ |
|
|
273 |
+ |
/** |
|
274 |
+ |
* Returns a list of strings under the specified key. If the key is a single value, it is placed in a list and returned. If the |
|
275 |
+ |
* key does not exist, a 'null' is returned. |
|
276 |
+ |
*/ |
|
277 |
+ |
public List<String> getList( Object key ) |
|
278 |
+ |
{ |
|
279 |
+ |
return getCachedWithConvertion( key, LIST_STRING ); |
286 |
280 |
|
} |
287 |
281 |
|
|
288 |
|
- |
public Map<String, String> getMap( Object key, String... defaultValues ) |
|
282 |
+ |
/** |
|
283 |
+ |
* Returns a Map<Object,Object> under the specified key. If the key does not exist, a Map with the defaultValues (as Key/Value pairs) is returned. |
|
284 |
+ |
*/ |
|
285 |
+ |
public Map<?, ?> getObjectMapNotNull( Object key, Object... defaultValues ) |
289 |
286 |
|
{ |
290 |
287 |
|
Util.assertPairedEntries( "defaultValues", defaultValues ); |
291 |
|
- |
Map<String, String> map = getAsMap( key ); |
292 |
|
- |
if ( map == null ) |
293 |
|
- |
{ |
294 |
|
- |
if ( defaultValues != null ) |
295 |
|
- |
{ |
296 |
|
- |
map = new HashMap<String, String>(); |
297 |
|
- |
for ( int i = 0; i < defaultValues.length; ) |
298 |
|
- |
{ |
299 |
|
- |
String defaultKey = defaultValues[i++]; |
300 |
|
- |
String defaultValue = defaultValues[i++]; |
301 |
|
- |
map.put( defaultKey, defaultValue ); |
302 |
|
- |
} |
303 |
|
- |
} |
304 |
|
- |
} |
305 |
|
- |
return map; |
|
288 |
+ |
Map<?, ?> map = getObjectMap( key ); |
|
289 |
+ |
return (map != null) ? map : createMap( defaultValues ); |
306 |
290 |
|
} |
307 |
291 |
|
|
308 |
|
- |
public Map getObjectMap( Object key, Object... defaultValues ) |
|
292 |
+ |
/** |
|
293 |
+ |
* Returns a Map<Object,Object> under the specified key. If the key does not exist, a 'null' is returned. |
|
294 |
+ |
*/ |
|
295 |
+ |
public Map<?, ?> getObjectMap( Object key ) |
|
296 |
+ |
{ |
|
297 |
+ |
return getCachedWithConvertion( key, MAP_OBJECT ); |
|
298 |
+ |
} |
|
299 |
+ |
|
|
300 |
+ |
/** |
|
301 |
+ |
* Returns a Map<String,String> under the specified key. If the key does not exist, a Map with the defaultValues (as Key/Value pairs) is returned. |
|
302 |
+ |
*/ |
|
303 |
+ |
public Map<String, String> getMapNotNull( Object key, String... defaultValues ) |
309 |
304 |
|
{ |
310 |
305 |
|
Util.assertPairedEntries( "defaultValues", defaultValues ); |
311 |
|
- |
Map<Object, Object> map = getAsMap( key ); |
312 |
|
- |
if ( map == null ) |
313 |
|
- |
{ |
314 |
|
- |
if ( defaultValues != null ) |
315 |
|
- |
{ |
316 |
|
- |
map = new HashMap<Object, Object>(); |
317 |
|
- |
for ( int i = 0; i < defaultValues.length; ) |
318 |
|
- |
{ |
319 |
|
- |
Object defaultKey = defaultValues[i++]; |
320 |
|
- |
Object defaultValue = defaultValues[i++]; |
321 |
|
- |
map.put( defaultKey, defaultValue ); |
322 |
|
- |
} |
323 |
|
- |
} |
324 |
|
- |
} |
325 |
|
- |
return map; |
|
306 |
+ |
Map<String, String> map = getMap( key ); |
|
307 |
+ |
return (map != null) ? map : createMap( defaultValues ); |
|
308 |
+ |
} |
|
309 |
+ |
|
|
310 |
+ |
/** |
|
311 |
+ |
* Returns a Map<String, String> under the specified key. If the key does not exist, a 'null' is returned. |
|
312 |
+ |
*/ |
|
313 |
+ |
public Map<String, String> getMap( Object key ) |
|
314 |
+ |
{ |
|
315 |
+ |
return getCachedWithConvertion( key, MAP_STRING ); |
326 |
316 |
|
} |
327 |
317 |
|
|
328 |
318 |
|
/** |
329 |
319 |
|
* Uses the strings under the specified key to {@link Paths#glob(String, String...) glob} paths. |
330 |
320 |
|
*/ |
331 |
321 |
|
public Paths getPaths( String key ) |
332 |
|
- |
throws IOException |
333 |
322 |
|
{ |
334 |
|
- |
Paths paths = new Paths(); |
335 |
|
- |
for ( String dirPattern : getList( key ) ) |
336 |
|
- |
{ |
337 |
|
- |
paths.glob( path( dirPattern ) ); |
338 |
|
- |
} |
339 |
|
- |
return paths; |
|
323 |
+ |
Paths zPaths = getCachedWithConvertion( key, PATHS ); |
|
324 |
+ |
return (zPaths != null) ? zPaths : new Paths(); |
340 |
325 |
|
} |
341 |
326 |
|
|
342 |
327 |
|
/** |
|
@@ -344,8 +329,11 @@ |
344 |
329 |
|
* If the specified path is a relative path, it is made absolute relative to this project's directory. |
345 |
330 |
|
*/ |
346 |
331 |
|
public String path( String path ) |
347 |
|
- |
throws IOException |
348 |
332 |
|
{ |
|
333 |
+ |
if ( path == null ) |
|
334 |
+ |
{ |
|
335 |
+ |
return null; |
|
336 |
+ |
} |
349 |
337 |
|
path = format( path ); |
350 |
338 |
|
String zSuffix = ""; |
351 |
339 |
|
int pipeIndex = path.indexOf( '|' ); |
|
@@ -391,6 +379,45 @@ |
391 |
379 |
|
return buffer.toString(); |
392 |
380 |
|
} |
393 |
381 |
|
|
|
382 |
+ |
public Object[] keys() |
|
383 |
+ |
{ |
|
384 |
+ |
return mManager.keys(); |
|
385 |
+ |
} |
|
386 |
+ |
|
|
387 |
+ |
public File getCanonicalProjectDir() |
|
388 |
+ |
{ |
|
389 |
+ |
return mCanonicalProjectDir; |
|
390 |
+ |
} |
|
391 |
+ |
|
|
392 |
+ |
public String getName() |
|
393 |
+ |
{ |
|
394 |
+ |
return mName; |
|
395 |
+ |
} |
|
396 |
+ |
|
|
397 |
+ |
public ProjectParameters( String pName, File pCanonicalProjectDir, Map<Object, Object> pData ) |
|
398 |
+ |
{ |
|
399 |
+ |
this( pName, pCanonicalProjectDir, new Manager( pData ) ); |
|
400 |
+ |
} |
|
401 |
+ |
|
|
402 |
+ |
// ---------------------------------------------------- Support ---------------------------------------------------- |
|
403 |
+ |
|
|
404 |
+ |
protected ProjectParameters( ProjectParameters pParameters ) |
|
405 |
+ |
{ |
|
406 |
+ |
this( pParameters.getName(), pParameters.getCanonicalProjectDir(), pParameters.mManager ); |
|
407 |
+ |
} |
|
408 |
+ |
|
|
409 |
+ |
private ProjectParameters( String pName, File pCanonicalProjectDir, Manager pManager ) |
|
410 |
+ |
{ |
|
411 |
+ |
mCanonicalProjectDir = pCanonicalProjectDir; |
|
412 |
+ |
mManager = pManager; |
|
413 |
+ |
Object zName = mManager.get( NAME.getName() ); |
|
414 |
+ |
mManager.put( NAME.getName(), mName = (zName != null) ? zName.toString() : pName ); |
|
415 |
+ |
} |
|
416 |
+ |
|
|
417 |
+ |
protected final String mName; |
|
418 |
+ |
protected final File mCanonicalProjectDir; |
|
419 |
+ |
protected final Manager mManager; |
|
420 |
+ |
|
394 |
421 |
|
static private final java.util.regex.Pattern formatPattern = java.util.regex.Pattern.compile( "([^\\$]*)\\$([^\\$]+)\\$([^\\$]*)" ); |
395 |
422 |
|
|
396 |
423 |
|
@SuppressWarnings({"unchecked"}) |
|
@@ -399,4 +426,166 @@ |
399 |
426 |
|
Object zValue = getObject( pKey ); |
400 |
427 |
|
return (zValue instanceof Map) ? (Map<T, T>) zValue : null; |
401 |
428 |
|
} |
|
429 |
+ |
|
|
430 |
+ |
@Override |
|
431 |
+ |
public String toString() |
|
432 |
+ |
{ |
|
433 |
+ |
return getName(); |
|
434 |
+ |
} |
|
435 |
+ |
|
|
436 |
+ |
protected void deletePath( String pProjectRelativePath ) |
|
437 |
+ |
{ |
|
438 |
+ |
String zPath = path( pProjectRelativePath ); |
|
439 |
+ |
if ( zPath != null ) |
|
440 |
+ |
{ |
|
441 |
+ |
delete( zPath ); |
|
442 |
+ |
} |
|
443 |
+ |
} |
|
444 |
+ |
|
|
445 |
+ |
protected void defaultSubDirOptional( Parameter pParameter, String... pOptions ) |
|
446 |
+ |
{ |
|
447 |
+ |
Object o = mManager.get( pParameter.getName() ); |
|
448 |
+ |
if ( o == null ) |
|
449 |
+ |
{ |
|
450 |
+ |
for ( String zOption : pOptions ) |
|
451 |
+ |
{ |
|
452 |
+ |
if ( dirExists( zOption ) ) |
|
453 |
+ |
{ |
|
454 |
+ |
mManager.put( pParameter.getName(), zOption ); |
|
455 |
+ |
return; |
|
456 |
+ |
} |
|
457 |
+ |
} |
|
458 |
+ |
} |
|
459 |
+ |
} |
|
460 |
+ |
|
|
461 |
+ |
protected boolean dirExists( String pPath ) |
|
462 |
+ |
{ |
|
463 |
+ |
int pipeAt = pPath.indexOf( '|' ); |
|
464 |
+ |
if ( pipeAt != -1 ) |
|
465 |
+ |
{ |
|
466 |
+ |
pPath = pPath.substring( pipeAt ); |
|
467 |
+ |
} |
|
468 |
+ |
return new File( getCanonicalProjectDir(), pPath ).isDirectory(); |
|
469 |
+ |
} |
|
470 |
+ |
|
|
471 |
+ |
protected void defaultKey( Parameter pParameter, String pDefault ) |
|
472 |
+ |
{ |
|
473 |
+ |
if ( null == get( pParameter.getName() ) ) |
|
474 |
+ |
{ |
|
475 |
+ |
mManager.put( pParameter.getName(), pDefault ); |
|
476 |
+ |
} |
|
477 |
+ |
} |
|
478 |
+ |
|
|
479 |
+ |
protected <T> Map<T, T> createMap( T[] defaultValues ) |
|
480 |
+ |
{ |
|
481 |
+ |
Map<T, T> map = new HashMap<T, T>(); |
|
482 |
+ |
if ( defaultValues != null ) |
|
483 |
+ |
{ |
|
484 |
+ |
for ( int i = 0; i < defaultValues.length; ) |
|
485 |
+ |
{ |
|
486 |
+ |
T defaultKey = defaultValues[i++]; |
|
487 |
+ |
T defaultValue = defaultValues[i++]; |
|
488 |
+ |
map.put( defaultKey, defaultValue ); |
|
489 |
+ |
} |
|
490 |
+ |
} |
|
491 |
+ |
return map; |
|
492 |
+ |
} |
|
493 |
+ |
|
|
494 |
+ |
protected <T> T getCachedWithConvertion( Object pKey, DataConverter<T> pConverter ) |
|
495 |
+ |
{ |
|
496 |
+ |
Object zValue = mManager.getCachedResponse( pKey = mManager.normalizeKey( pKey ) ); |
|
497 |
+ |
if ( zValue != null ) |
|
498 |
+ |
{ |
|
499 |
+ |
//noinspection unchecked |
|
500 |
+ |
return (T) zValue; |
|
501 |
+ |
} |
|
502 |
+ |
zValue = mManager.get( pKey ); |
|
503 |
+ |
if ( zValue == null ) |
|
504 |
+ |
{ |
|
505 |
+ |
return null; |
|
506 |
+ |
} |
|
507 |
+ |
T zConverted = pConverter.convert( zValue ); |
|
508 |
+ |
mManager.addCachedResponse( pKey, zConverted ); |
|
509 |
+ |
return zConverted; |
|
510 |
+ |
} |
|
511 |
+ |
|
|
512 |
+ |
private static final DataConverter<Integer> INTEGER = new DataConverter<Integer>() |
|
513 |
+ |
{ |
|
514 |
+ |
@Override |
|
515 |
+ |
public Integer convert( Object pValue ) |
|
516 |
+ |
{ |
|
517 |
+ |
return (pValue instanceof Number) ? ((Number) pValue).intValue() : Integer.parseInt( pValue.toString() ); |
|
518 |
+ |
} |
|
519 |
+ |
}; |
|
520 |
+ |
|
|
521 |
+ |
private static final DataConverter<Float> FLOAT = new DataConverter<Float>() |
|
522 |
+ |
{ |
|
523 |
+ |
@Override |
|
524 |
+ |
public Float convert( Object pValue ) |
|
525 |
+ |
{ |
|
526 |
+ |
return (pValue instanceof Number) ? ((Number) pValue).floatValue() : Float.parseFloat( pValue.toString() ); |
|
527 |
+ |
} |
|
528 |
+ |
}; |
|
529 |
+ |
|
|
530 |
+ |
private static final DataConverter<Boolean> BOOLEAN = new DataConverter<Boolean>() |
|
531 |
+ |
{ |
|
532 |
+ |
@Override |
|
533 |
+ |
public Boolean convert( Object pValue ) |
|
534 |
+ |
{ |
|
535 |
+ |
return (pValue instanceof Boolean) ? (Boolean) pValue : Boolean.parseBoolean( pValue.toString() ); |
|
536 |
+ |
} |
|
537 |
+ |
}; |
|
538 |
+ |
|
|
539 |
+ |
private static final DataConverter<List<?>> LIST_OBJECT = new DataConverter<List<?>>() |
|
540 |
+ |
{ |
|
541 |
+ |
@Override |
|
542 |
+ |
public List<?> convert( Object pValue ) |
|
543 |
+ |
{ |
|
544 |
+ |
return (pValue instanceof List) ? (List<?>) pValue : Arrays.asList( pValue ); |
|
545 |
+ |
} |
|
546 |
+ |
}; |
|
547 |
+ |
|
|
548 |
+ |
private static final DataConverter<List<String>> LIST_STRING = new DataConverter<List<String>>() |
|
549 |
+ |
{ |
|
550 |
+ |
@Override |
|
551 |
+ |
public List<String> convert( Object pValue ) |
|
552 |
+ |
{ |
|
553 |
+ |
//noinspection unchecked |
|
554 |
+ |
return (pValue instanceof List) ? (List<String>) pValue : Arrays.asList( pValue.toString() ); |
|
555 |
+ |
} |
|
556 |
+ |
}; |
|
557 |
+ |
|
|
558 |
+ |
private static final DataConverter<Map<?, ?>> MAP_OBJECT = new DataConverter<Map<?, ?>>() |
|
559 |
+ |
{ |
|
560 |
+ |
@Override |
|
561 |
+ |
public Map<?, ?> convert( Object pValue ) |
|
562 |
+ |
{ |
|
563 |
+ |
return (Map<?, ?>) pValue; |
|
564 |
+ |
} |
|
565 |
+ |
}; |
|
566 |
+ |
|
|
567 |
+ |
private static final DataConverter<Map<String, String>> MAP_STRING = new DataConverter<Map<String, String>>() |
|
568 |
+ |
{ |
|
569 |
+ |
@Override |
|
570 |
+ |
public Map<String, String> convert( Object pValue ) |
|
571 |
+ |
{ |
|
572 |
+ |
//noinspection unchecked |
|
573 |
+ |
return (Map<String, String>) pValue; |
|
574 |
+ |
} |
|
575 |
+ |
}; |
|
576 |
+ |
|
|
577 |
+ |
private final DataConverter<Paths> PATHS = new DataConverter<Paths>() |
|
578 |
+ |
{ |
|
579 |
+ |
@Override |
|
580 |
+ |
public Paths convert( Object pValue ) |
|
581 |
+ |
{ |
|
582 |
+ |
Paths paths = new Paths(); |
|
583 |
+ |
List<String> zList = LIST_STRING.convert( pValue ); |
|
584 |
+ |
for ( String dirPattern : zList ) |
|
585 |
+ |
{ |
|
586 |
+ |
paths.glob( path( dirPattern ) ); |
|
587 |
+ |
} |
|
588 |
+ |
return paths; |
|
589 |
+ |
} |
|
590 |
+ |
}; |
402 |
591 |
|
} |