587
|
1 |
/*
|
|
2 |
* Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of the License "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description:
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
|
|
18 |
package com.nokia.ant.taskdefs;
|
|
19 |
|
|
20 |
import java.io.File;
|
|
21 |
import java.util.ArrayList;
|
|
22 |
import java.util.Iterator;
|
628
|
23 |
|
|
24 |
import org.apache.commons.configuration.Configuration;
|
|
25 |
import org.apache.commons.configuration.ConfigurationException;
|
|
26 |
import org.apache.commons.configuration.PropertiesConfiguration;
|
|
27 |
import org.apache.commons.configuration.XMLConfiguration;
|
587
|
28 |
import org.apache.tools.ant.BuildException;
|
|
29 |
import org.apache.tools.ant.Task;
|
|
30 |
import org.apache.tools.ant.types.FileSet;
|
|
31 |
import org.apache.tools.ant.types.ResourceCollection;
|
|
32 |
import org.apache.tools.ant.types.resources.FileResource;
|
|
33 |
|
|
34 |
/**
|
|
35 |
* Can load ant configuration file both in .xml and .txt format.
|
|
36 |
* In .txt file configuration could be defined like -
|
|
37 |
* text.a = text.value.A
|
|
38 |
* text.b : text.value.B
|
|
39 |
* text.c : ${text.a}
|
|
40 |
* In .xml file configuration could be defined like -
|
|
41 |
* <config>
|
|
42 |
* <foo>bar</foo>
|
|
43 |
* <interpolated>foo value = ${foo}</interpolated>
|
|
44 |
* <xml>
|
|
45 |
* <c>C</c>
|
|
46 |
* <d>D</d>
|
|
47 |
* </xml>
|
|
48 |
* <array>
|
|
49 |
* <value>one</value>
|
|
50 |
* <value>two</value>
|
|
51 |
* </array>
|
|
52 |
*</config>
|
|
53 |
* @ant.task name="configuration"
|
|
54 |
*/
|
|
55 |
public class AntConfigurationTask extends Task
|
|
56 |
{
|
|
57 |
private File filepath;
|
|
58 |
|
|
59 |
private ArrayList<ResourceCollection> rcs = new ArrayList<ResourceCollection>();
|
|
60 |
|
|
61 |
public final void setFile(final File file)
|
|
62 |
{
|
|
63 |
this.filepath = file;
|
|
64 |
}
|
|
65 |
|
|
66 |
public final void addFileset(final FileSet set)
|
|
67 |
{
|
|
68 |
add(set);
|
|
69 |
}
|
|
70 |
|
|
71 |
public final void add( final ResourceCollection res)
|
|
72 |
{
|
|
73 |
rcs.add(res);
|
|
74 |
}
|
|
75 |
|
|
76 |
public final void execute()
|
|
77 |
{
|
|
78 |
if (filepath != null)
|
|
79 |
{
|
|
80 |
importFile(filepath);
|
|
81 |
}
|
|
82 |
else
|
|
83 |
{
|
|
84 |
Iterator resourceCollectionIter = rcs.iterator();
|
|
85 |
|
|
86 |
while (resourceCollectionIter.hasNext())
|
|
87 |
{
|
|
88 |
ResourceCollection resourceCollection = (ResourceCollection) resourceCollectionIter
|
|
89 |
.next();
|
|
90 |
Iterator resourceIter = resourceCollection.iterator();
|
|
91 |
while (resourceIter.hasNext())
|
|
92 |
{
|
|
93 |
FileResource filepath = (FileResource) resourceIter.next();
|
|
94 |
importFile(filepath.getFile());
|
|
95 |
}
|
|
96 |
}
|
|
97 |
}
|
|
98 |
}
|
|
99 |
|
|
100 |
private void importFile(final File file)
|
|
101 |
{
|
|
102 |
try
|
|
103 |
{
|
|
104 |
String filename = file.getName();
|
|
105 |
Configuration config = null;
|
|
106 |
if (filename.endsWith(".txt"))
|
|
107 |
{
|
|
108 |
config = new PropertiesConfiguration(file);
|
|
109 |
}
|
|
110 |
else if (filename.endsWith(".xml"))
|
|
111 |
{
|
|
112 |
config = new XMLConfiguration(file);
|
|
113 |
}
|
|
114 |
Iterator keysIter = config.getKeys();
|
|
115 |
while (keysIter.hasNext())
|
|
116 |
{
|
|
117 |
String key = (String) keysIter.next();
|
|
118 |
getProject().setProperty(key, config.getString(key));
|
|
119 |
}
|
|
120 |
}
|
|
121 |
catch (ConfigurationException e)
|
|
122 |
{
|
|
123 |
throw new BuildException("Not able to import the ANT file " + e.getMessage());
|
|
124 |
}
|
|
125 |
}
|
|
126 |
}
|