588
|
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 |
package com.nokia.helium.antlint.ant.types;
|
|
18 |
|
|
19 |
import java.io.File;
|
|
20 |
import java.io.FileOutputStream;
|
628
|
21 |
import java.io.IOException;
|
588
|
22 |
import java.io.PrintWriter;
|
|
23 |
import java.util.ArrayList;
|
|
24 |
import java.util.List;
|
|
25 |
|
|
26 |
import org.apache.tools.ant.BuildException;
|
|
27 |
import org.dom4j.Document;
|
|
28 |
import org.dom4j.DocumentException;
|
|
29 |
import org.dom4j.Element;
|
|
30 |
import org.dom4j.io.SAXReader;
|
|
31 |
|
|
32 |
import com.nokia.helium.antlint.ant.AntlintException;
|
|
33 |
|
|
34 |
/**
|
|
35 |
* <code>CheckPythonTasks</code> is used to the check the coding convention of
|
|
36 |
* python tasks.
|
|
37 |
*
|
|
38 |
* <pre>
|
|
39 |
* Usage:
|
|
40 |
*
|
|
41 |
* <antlint>
|
|
42 |
* <fileset id="antlint.files" dir="${antlint.test.dir}/data">
|
|
43 |
* <include name="*.ant.xml"/>
|
|
44 |
* <include name="*build.xml"/>
|
|
45 |
* <include name="*.antlib.xml"/>
|
|
46 |
* </fileset>
|
|
47 |
* <CheckPythonTasks" severity="error" enabled="true" outputDir="${antlint.test.dir}/output"/>
|
|
48 |
* </antlint>
|
|
49 |
* </pre>
|
|
50 |
*
|
|
51 |
* @ant.task name="CheckPythonTasks" category="AntLint"
|
|
52 |
*/
|
|
53 |
public class CheckPythonTasks extends AbstractCheck {
|
|
54 |
|
|
55 |
private File outputDir;
|
|
56 |
private File antFile;
|
|
57 |
|
|
58 |
/**
|
|
59 |
* {@inheritDoc}
|
|
60 |
*/
|
|
61 |
@SuppressWarnings("unchecked")
|
|
62 |
public void run(Element node) {
|
|
63 |
if (node.getName().equals("target")) {
|
|
64 |
String target = node.attributeValue("name");
|
|
65 |
List<Element> pythonList = node.selectNodes("//target[@name='"
|
|
66 |
+ target + "']/descendant::*[name()=\"hlm:python\"]");
|
|
67 |
int i = 0;
|
|
68 |
for (Element pythonElement : pythonList) {
|
|
69 |
writePythonFile(i + "_" + target, pythonElement.getText());
|
|
70 |
i++;
|
|
71 |
}
|
|
72 |
}
|
|
73 |
}
|
|
74 |
|
|
75 |
/**
|
|
76 |
* Writes the given text to a python file.
|
|
77 |
*
|
|
78 |
* @param name
|
|
79 |
* is the name of the file to be written.
|
|
80 |
* @param text
|
|
81 |
* is the text to be written inside the file.
|
|
82 |
*/
|
|
83 |
private void writePythonFile(String name, String text) {
|
|
84 |
if (getOutputDir() == null) {
|
|
85 |
throw new BuildException("'output' attribute for the checker '"
|
|
86 |
+ this.toString() + "' should be specified.");
|
|
87 |
}
|
|
88 |
try {
|
|
89 |
String heliumpath = getOutputDir().getCanonicalPath();
|
|
90 |
new File(heliumpath + File.separator + "python").mkdirs();
|
|
91 |
File file = new File(heliumpath + File.separator + "python"
|
|
92 |
+ File.separator + "target" + name + ".py");
|
|
93 |
PrintWriter output = new PrintWriter(new FileOutputStream(file));
|
|
94 |
if (!text.equals("")) {
|
|
95 |
output.write("def abc():");
|
628
|
96 |
for (String line : text.split("\n")) {
|
588
|
97 |
output.write(" " + line + "\n");
|
628
|
98 |
}
|
588
|
99 |
}
|
|
100 |
output.close();
|
|
101 |
} catch (IOException e) {
|
|
102 |
throw new BuildException(
|
|
103 |
"IOException:Not able to write python file " + name + ".py");
|
|
104 |
}
|
|
105 |
}
|
|
106 |
|
|
107 |
/*
|
|
108 |
* (non-Javadoc)
|
|
109 |
*
|
|
110 |
* @see com.nokia.helium.antlint.ant.types.Check#run(java.io.File)
|
|
111 |
*/
|
|
112 |
public void run(File antFilename) throws AntlintException {
|
|
113 |
List<Element> targetNodes = new ArrayList<Element>();
|
|
114 |
|
|
115 |
this.antFile = antFilename;
|
|
116 |
SAXReader saxReader = new SAXReader();
|
|
117 |
Document doc;
|
|
118 |
try {
|
|
119 |
doc = saxReader.read(antFilename);
|
|
120 |
elementTreeWalk(doc.getRootElement(), "target", targetNodes);
|
|
121 |
} catch (DocumentException e) {
|
|
122 |
throw new AntlintException("Invalid XML file " + e.getMessage());
|
|
123 |
}
|
|
124 |
for (Element targetNode : targetNodes) {
|
|
125 |
run(targetNode);
|
|
126 |
}
|
|
127 |
|
|
128 |
}
|
|
129 |
|
|
130 |
/**
|
|
131 |
* @param outputDir
|
|
132 |
* the outputDir to set
|
|
133 |
*/
|
|
134 |
public void setOutputDir(File outputDir) {
|
|
135 |
this.outputDir = outputDir;
|
|
136 |
}
|
|
137 |
|
|
138 |
/**
|
|
139 |
* @return the outputDir
|
|
140 |
*/
|
|
141 |
public File getOutputDir() {
|
|
142 |
return outputDir;
|
|
143 |
}
|
|
144 |
|
|
145 |
/*
|
|
146 |
* (non-Javadoc)
|
|
147 |
*
|
|
148 |
* @see org.apache.tools.ant.types.DataType#toString()
|
|
149 |
*/
|
|
150 |
public String toString() {
|
|
151 |
return "CheckPythonTasks";
|
|
152 |
}
|
|
153 |
|
|
154 |
/*
|
|
155 |
* (non-Javadoc)
|
|
156 |
*
|
|
157 |
* @see com.nokia.helium.antlint.ant.types.Check#getAntFile()
|
|
158 |
*/
|
|
159 |
public File getAntFile() {
|
|
160 |
return this.antFile;
|
|
161 |
}
|
|
162 |
}
|