Debian Codegraph @

src/main/java/net/sf/antcontrib/inifile/IniFileTask.java

raw symbols SCIP
1 /*
2 * Copyright (c) 2001-2004 Ant-Contrib project. All rights reserved.
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 net.sf.antcontrib.inifile;
17
18 import java.io.File;
19 import java.io.FileReader;
20 import java.io.FileWriter;
21 import java.io.IOException;
22 import java.util.Iterator;
23 import java.util.Vector;
24
25 import org.apache.tools.ant.BuildException;
26 import org.apache.tools.ant.Project;
27 import org.apache.tools.ant.Task;
28 import org.apache.tools.ant.taskdefs.Property;
29
30
31 /****************************************************************************
32 * Place class description here.
33 *
34 * @author <a href='mailto:[email protected]'>Matthew Inger</a>
35 * @author <additional author>
36 *
37 * @since
38 *
39 ****************************************************************************/
40
41
42 public class IniFileTask
43 extends Task
44 {
45 public static abstract class IniOperation
46 {
47 private String section;
48 private String property;
49
50 public IniOperation()
51 {
52 super();
53 }
54
55 public String getSection()
56 {
57 return section;
58 }
59
60
61 public void setSection(String section)
62 {
63 this.section = section;
64 }
65
66
67 public String getProperty()
68 {
69 return property;
70 }
71
72
73 public void setProperty(String property)
74 {
75 this.property = property;
76 }
77
78 public void execute(Project project, IniFile iniFile)
79 {
80 operate(iniFile);
81 }
82
83 protected abstract void operate(IniFile file);
84 }
85
86 public static abstract class IniOperationConditional extends IniOperation
87 {
88 private String ifCond;
89 private String unlessCond;
90
91 public IniOperationConditional()
92 {
93 super();
94 }
95
96 public void setIf(String ifCond)
97 {
98 this.ifCond = ifCond;
99 }
100
101 public void setUnless(String unlessCond)
102 {
103 this.unlessCond = unlessCond;
104 }
105
106 /**
107 * Returns true if the define's if and unless conditions
108 * (if any) are satisfied.
109 */
110 public boolean isActive(org.apache.tools.ant.Project p)
111 {
112 if (ifCond != null && p.getProperty(ifCond) == null)
113 {
114 return false;
115 }
116 else if (unlessCond != null && p.getProperty(unlessCond) != null)
117 {
118 return false;
119 }
120
121 return true;
122 }
123
124 public void execute(Project project, IniFile iniFile)
125 {
126 if (isActive(project))
127 operate(iniFile);
128 }
129 }
130
131 public static abstract class IniOperationPropertySetter extends IniOperation
132 {
133 private boolean override;
134 private String resultproperty;
135
136 public IniOperationPropertySetter()
137 {
138 super();
139 }
140
141 public void setOverride(boolean override)
142 {
143 this.override = override;
144 }
145
146 public void setResultProperty(String resultproperty)
147 {
148 this.resultproperty = resultproperty;
149 }
150
151 protected final void setResultPropertyValue(Project project, String value)
152 {
153 if (value != null)
154 {
155 if (override)
156 {
157 if (project.getUserProperty(resultproperty) == null)
158 project.setProperty(resultproperty, value);
159 else
160 project.setUserProperty(resultproperty, value);
161 }
162 else
163 {
164 Property p = (Property)project.createTask("property");
165 p.setName(resultproperty);
166 p.setValue(value);
167 p.execute();
168 }
169 }
170 }
171 }
172
173 public static final class Remove
174 extends IniOperationConditional
175 {
176 public Remove()
177 {
178 super();
179 }
180
181 protected void operate(IniFile file)
182 {
183 String secName = getSection();
184 String propName = getProperty();
185
186 if (propName == null)
187 {
188 file.removeSection(secName);
189 }
190 else
191 {
192 IniSection section = file.getSection(secName);
193 if (section != null)
194 section.removeProperty(propName);
195 }
196 }
197 }
198
199
200 public final class Set
201 extends IniOperationConditional
202 {
203 private String value;
204 private String operation;
205
206 public Set()
207 {
208 super();
209 }
210
211
212 public void setValue(String value)
213 {
214 this.value = value;
215 }
216
217
218 public void setOperation(String operation)
219 {
220 this.operation = operation;
221 }
222
223
224 protected void operate(IniFile file)
225 {
226 String secName = getSection();
227 String propName = getProperty();
228
229 IniSection section = file.getSection(secName);
230 if (section == null)
231 {
232 section = new IniSection(secName);
233 file.setSection(section);
234 }
235
236 if (propName != null)
237 {
238 if (operation != null)
239 {
240 if ("+".equals(operation))
241 {
242 IniProperty prop = section.getProperty(propName);
243 value = prop.getValue();
244 int intVal = Integer.parseInt(value) + 1;
245 value = String.valueOf(intVal);
246 }
247 else if ("-".equals(operation))
248 {
249 IniProperty prop = section.getProperty(propName);
250 value = prop.getValue();
251 int intVal = Integer.parseInt(value) - 1;
252 value = String.valueOf(intVal);
253 }
254 }
255 section.setProperty(new IniProperty(propName, value));
256 }
257 }
258 }
259
260 public final class Exists
261 extends IniOperationPropertySetter
262 {
263 public Exists()
264 {
265 super();
266 }
267
268 protected void operate(IniFile file)
269 {
270 boolean exists = false;
271 String secName = getSection();
272 String propName = getProperty();
273
274 if (secName == null)
275 throw new BuildException("You must supply a section to search for.");
276
277 if (propName == null)
278 exists = (file.getSection(secName) != null);
279 else
280 exists = (file.getProperty(secName, propName) != null);
281
282 setResultPropertyValue(getProject(), Boolean.valueOf(exists).toString());
283 }
284 }
285
286 public final class Get
287 extends IniOperationPropertySetter
288 {
289 public Get()
290 {
291 super();
292 }
293
294 protected void operate(IniFile file)
295 {
296 String secName = getSection();
297 String propName = getProperty();
298
299 if (secName == null)
300 throw new BuildException("You must supply a section to search for.");
301
302 if (propName == null)
303 throw new BuildException("You must supply a property name to search for.");
304
305 setResultPropertyValue(getProject(), file.getProperty(secName, propName));
306 }
307 }
308
309 private File source;
310 private File dest;
311 private Vector operations;
312
313 public IniFileTask()
314 {
315 super();
316 this.operations = new Vector();
317 }
318
319 public Set createSet()
320 {
321 Set set = new Set();
322 operations.add(set);
323 return set;
324 }
325
326 public Remove createRemove()
327 {
328 Remove remove = new Remove();
329 operations.add(remove);
330 return remove;
331 }
332
333 public Exists createExists()
334 {
335 Exists exists = new Exists();
336 operations.add(exists);
337 return exists;
338 }
339
340 public Get createGet()
341 {
342 Get get = new Get();
343 operations.add(get);
344 return get;
345 }
346
347 public void setSource(File source)
348 {
349 this.source = source;
350 }
351
352
353 public void setDest(File dest)
354 {
355 this.dest = dest;
356 }
357
358
359 public void execute()
360 throws BuildException
361 {
362 if (dest == null)
363 throw new BuildException("You must supply a dest file to write to.");
364
365 IniFile iniFile = null;
366
367 try
368 {
369 iniFile = readIniFile(source);
370 }
371 catch (IOException e)
372 {
373 throw new BuildException(e);
374 }
375
376 Iterator it = operations.iterator();
377 IniOperation operation = null;
378 while (it.hasNext())
379 {
380 operation = (IniOperation)it.next();
381 operation.execute(getProject(), iniFile);
382 }
383
384 FileWriter writer = null;
385
386 try
387 {
388 try
389 {
390 writer = new FileWriter(dest);
391 iniFile.write(writer);
392 }
393 finally
394 {
395 try
396 {
397 if (writer != null)
398 writer.close();
399 }
400 catch (IOException e)
401 {
402 ; // gulp
403 }
404 }
405 }
406 catch (IOException e)
407 {
408 throw new BuildException(e);
409 }
410
411 }
412
413
414 private IniFile readIniFile(File source)
415 throws IOException
416 {
417 FileReader reader = null;
418 IniFile iniFile = new IniFile();
419
420 if (source == null)
421 return iniFile;
422
423 try
424 {
425 reader = new FileReader(source);
426 iniFile.read(reader);
427 }
428 finally
429 {
430 try
431 {
432 if (reader != null)
433 reader.close();
434 }
435 catch (IOException e)
436 {
437 ; // gulp
438 }
439 }
440
441 return iniFile;
442 }
443 }