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 |
|
|
19 |
package com.nokia.helium.core.ant.taskdefs;
|
|
20 |
|
628
|
21 |
import java.io.BufferedReader;
|
|
22 |
import java.io.File;
|
|
23 |
import java.io.FileInputStream;
|
|
24 |
import java.io.FileNotFoundException;
|
|
25 |
import java.io.FileOutputStream;
|
|
26 |
import java.io.IOException;
|
|
27 |
import java.io.InputStreamReader;
|
|
28 |
import java.io.OutputStreamWriter;
|
|
29 |
import java.io.UnsupportedEncodingException;
|
587
|
30 |
import java.util.Iterator;
|
628
|
31 |
import java.util.Vector;
|
587
|
32 |
|
628
|
33 |
import org.apache.tools.ant.BuildException;
|
587
|
34 |
import org.apache.tools.ant.Task;
|
|
35 |
import org.apache.tools.ant.types.Path;
|
|
36 |
|
|
37 |
/**
|
|
38 |
* This class implements a Path de/serializer which can dumps the list of files
|
|
39 |
* to a UTF-8 file or retrieve a list path from a file into a path datatype.
|
|
40 |
*
|
|
41 |
* Examples:
|
|
42 |
* <pre>
|
|
43 |
* <target name="serialize">
|
|
44 |
* <hlm:path2file file="output.txt">
|
|
45 |
* <path>
|
|
46 |
* <pathelement path="/temp/filename.ext"/>
|
|
47 |
* </path>
|
|
48 |
* </hlm:path2file>
|
|
49 |
* </target>
|
|
50 |
* </pre>
|
|
51 |
*
|
|
52 |
* The execution of the <code>serialize</code> task will produce a file called output.txt that will contains
|
|
53 |
* a line referencing the /temp/filename.ext.
|
|
54 |
|
|
55 |
* <pre>
|
|
56 |
* <target name="deserialize">
|
|
57 |
* <hlm:path2file reference="output.ref"> file="output.txt"/>
|
|
58 |
* </target>
|
|
59 |
* </pre>
|
|
60 |
*
|
|
61 |
* The execution of the <code>deserialize</code> task will create a path datatype that will
|
|
62 |
* contains a pathelement pointing on <code>/temp/filename.ext.</code>
|
|
63 |
*
|
|
64 |
* @ant.task name="path2file" category="Core"
|
|
65 |
*/
|
|
66 |
public class SerializePathTask extends Task {
|
|
67 |
|
|
68 |
private Vector<Path> paths = new Vector<Path>();
|
|
69 |
private File filename;
|
|
70 |
private String reference;
|
|
71 |
|
|
72 |
/**
|
|
73 |
* Add path datatype to the task.
|
|
74 |
*
|
|
75 |
* @param path
|
|
76 |
*/
|
|
77 |
public void add(Path path) {
|
|
78 |
paths.add(path);
|
|
79 |
}
|
|
80 |
|
|
81 |
/**
|
|
82 |
* Set reference attribute.
|
|
83 |
*
|
|
84 |
* @param reference
|
|
85 |
*/
|
|
86 |
public void setReference(String reference) {
|
|
87 |
this.reference = reference;
|
|
88 |
}
|
|
89 |
|
|
90 |
/**
|
|
91 |
* Set file attribute. It is a mandatory setting.
|
|
92 |
*
|
|
93 |
* @param filename
|
|
94 |
*/
|
|
95 |
public void setFile(File filename) {
|
|
96 |
this.filename = filename;
|
|
97 |
}
|
|
98 |
|
|
99 |
/**
|
|
100 |
* Excecute the task. If filename is defined and reference is not, it will
|
|
101 |
* try to dump the paths content into a file. Else if a filename and a
|
|
102 |
* reference is defined it will covert each line of the file into a
|
|
103 |
* pathelement of a path datatype.
|
|
104 |
*/
|
|
105 |
public void execute() {
|
|
106 |
if (filename == null) {
|
|
107 |
throw new BuildException("'file' attribute must be defined");
|
|
108 |
}
|
|
109 |
if (filename != null && reference == null) {
|
|
110 |
this.log("Dumping paths into file " + filename);
|
|
111 |
try {
|
|
112 |
OutputStreamWriter os = new OutputStreamWriter(
|
|
113 |
new FileOutputStream(filename), "UTF8");
|
|
114 |
for (Iterator<Path> ipath = paths.iterator(); ipath.hasNext();) {
|
|
115 |
Path path = ipath.next();
|
|
116 |
String[] plist = path.list();
|
|
117 |
for (int i = 0; i < plist.length; i++) {
|
|
118 |
os.write(plist[i] + "\n");
|
|
119 |
}
|
|
120 |
}
|
|
121 |
os.close();
|
|
122 |
} catch (FileNotFoundException exc) {
|
|
123 |
throw new BuildException(exc);
|
|
124 |
} catch (UnsupportedEncodingException exc) {
|
|
125 |
throw new BuildException(exc);
|
|
126 |
} catch (IOException exc) {
|
|
127 |
throw new BuildException(exc);
|
|
128 |
}
|
|
129 |
} else if (filename != null && reference != null) {
|
|
130 |
this.log("Converting " + filename + " content into path.");
|
|
131 |
try {
|
|
132 |
Path path = new Path(getProject());
|
|
133 |
BufferedReader is = new BufferedReader(new InputStreamReader(
|
|
134 |
new FileInputStream(filename), "UTF-8"));
|
|
135 |
String line = null;
|
|
136 |
while ((line = is.readLine()) != null) {
|
|
137 |
path.createPathElement().setPath(line);
|
|
138 |
}
|
|
139 |
this.log("Creating reference " + reference + ".");
|
|
140 |
getProject().addReference(reference, path);
|
|
141 |
} catch (FileNotFoundException exc) {
|
|
142 |
throw new BuildException(exc);
|
|
143 |
} catch (UnsupportedEncodingException exc) {
|
|
144 |
throw new BuildException(exc);
|
|
145 |
} catch (IOException exc) {
|
|
146 |
throw new BuildException(exc);
|
|
147 |
}
|
|
148 |
} else {
|
|
149 |
new BuildException("The task is not configured properly.");
|
|
150 |
}
|
|
151 |
|
|
152 |
}
|
|
153 |
}
|