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 |
import java.util.regex.Matcher;
|
|
26 |
import java.util.regex.Pattern;
|
|
27 |
|
|
28 |
import org.apache.tools.ant.BuildException;
|
|
29 |
import org.dom4j.Document;
|
|
30 |
import org.dom4j.DocumentException;
|
|
31 |
import org.dom4j.Element;
|
|
32 |
import org.dom4j.Node;
|
|
33 |
import org.dom4j.io.SAXReader;
|
|
34 |
|
|
35 |
import com.nokia.helium.antlint.ant.AntlintException;
|
|
36 |
|
|
37 |
/**
|
|
38 |
* <code>CheckScriptDef</code> is used to check the coding convention in
|
|
39 |
* scriptdef.
|
|
40 |
*
|
|
41 |
* <pre>
|
|
42 |
* Usage:
|
|
43 |
*
|
|
44 |
* <antlint>
|
|
45 |
* <fileset id="antlint.files" dir="${antlint.test.dir}/data">
|
|
46 |
* <include name="*.ant.xml"/>
|
|
47 |
* <include name="*build.xml"/>
|
|
48 |
* <include name="*.antlib.xml"/>
|
|
49 |
* </fileset>
|
|
50 |
* <CheckScriptDef" severity="error" enabled="true" outputDir="${antlint.test.dir}/output"/>
|
|
51 |
* </antlint>
|
|
52 |
* </pre>
|
|
53 |
*
|
|
54 |
* @ant.task name="CheckScriptDef" category="AntLint"
|
|
55 |
*
|
|
56 |
*/
|
|
57 |
public class CheckScriptDef extends AbstractScriptCheck {
|
|
58 |
|
|
59 |
private File outputDir;
|
|
60 |
private File antFile;
|
|
61 |
|
|
62 |
/**
|
|
63 |
* {@inheritDoc}
|
|
64 |
*/
|
|
65 |
public void run(Element node) {
|
|
66 |
if (node.getName().equals("scriptdef")) {
|
|
67 |
String scriptdefname = node.attributeValue("name");
|
|
68 |
String language = node.attributeValue("language");
|
|
69 |
|
|
70 |
checkScriptDef(scriptdefname, node);
|
|
71 |
|
|
72 |
if (language.equals("beanshell")) {
|
|
73 |
writeBeanshellFile(scriptdefname, node.getText());
|
|
74 |
}
|
|
75 |
}
|
|
76 |
}
|
|
77 |
|
|
78 |
/**
|
|
79 |
* Check against the given node.
|
|
80 |
*
|
|
81 |
* @param node
|
|
82 |
* is the node to check.
|
|
83 |
*/
|
|
84 |
@SuppressWarnings("unchecked")
|
|
85 |
public void checkScriptDef(String name, Node node) {
|
|
86 |
List<Node> statements = node.selectNodes("//scriptdef[@name='" + name
|
|
87 |
+ "']/attribute");
|
|
88 |
Pattern p1 = Pattern.compile("attributes.get\\([\"']([^\"']*)[\"']\\)");
|
|
89 |
Matcher m1 = p1.matcher(node.getText());
|
|
90 |
ArrayList<String> props = new ArrayList<String>();
|
|
91 |
while (m1.find()) {
|
|
92 |
props.add(m1.group(1));
|
|
93 |
}
|
|
94 |
|
|
95 |
if (!statements.isEmpty() && !props.isEmpty()) {
|
|
96 |
for (Node statement : statements) {
|
|
97 |
if (!props.contains(statement.valueOf("@name"))) {
|
|
98 |
this.getReporter().report(
|
|
99 |
this.getSeverity(),
|
|
100 |
"Scriptdef " + name + " does not use "
|
|
101 |
+ statement.valueOf("@name"),
|
|
102 |
this.getAntFile(), 0);
|
|
103 |
}
|
|
104 |
}
|
|
105 |
}
|
|
106 |
}
|
|
107 |
|
|
108 |
/**
|
|
109 |
* Write a bean shell file with the given text.
|
|
110 |
*
|
|
111 |
* @param scriptdefname
|
|
112 |
* is the name of the file to be written.
|
|
113 |
* @param text
|
|
114 |
* is the text to be written inside the file.
|
|
115 |
*/
|
|
116 |
private void writeBeanshellFile(String scriptdefname, String text) {
|
|
117 |
if (getOutputDir() == null) {
|
|
118 |
throw new BuildException("'output' attribute for the checker '"
|
|
119 |
+ this.toString() + "' should be specified.");
|
|
120 |
}
|
|
121 |
scriptdefname = "Beanshell" + scriptdefname;
|
|
122 |
try {
|
|
123 |
String heliumpath = getOutputDir().getCanonicalPath();
|
|
124 |
new File(heliumpath + File.separator + "beanshell").mkdirs();
|
|
125 |
File file = new File(heliumpath + File.separator + "beanshell"
|
|
126 |
+ File.separator + scriptdefname + ".java");
|
|
127 |
PrintWriter output = new PrintWriter(new FileOutputStream(file));
|
|
128 |
|
|
129 |
for (String line : text.split("\n")) {
|
628
|
130 |
if (line.trim().startsWith("import")) {
|
588
|
131 |
output.write(line + "\n");
|
628
|
132 |
}
|
588
|
133 |
}
|
|
134 |
|
|
135 |
output.write("/**\n * x\n */\npublic final class " + scriptdefname
|
|
136 |
+ " {\n");
|
628
|
137 |
output.write(" private " + scriptdefname + "() { }\n");
|
|
138 |
output.write(" public static void main(String[] args) {\n");
|
588
|
139 |
for (String line : text.split("\n")) {
|
628
|
140 |
if (!line.trim().startsWith("import")) {
|
|
141 |
output.write(" " + line + "\n");
|
|
142 |
}
|
588
|
143 |
}
|
628
|
144 |
output.write(" }\n");
|
|
145 |
output.write("}\n");
|
588
|
146 |
output.close();
|
|
147 |
} catch (IOException e) {
|
|
148 |
throw new BuildException("Not able to write Beanshell File "
|
|
149 |
+ scriptdefname + ".java");
|
|
150 |
}
|
|
151 |
}
|
|
152 |
|
|
153 |
/*
|
|
154 |
* (non-Javadoc)
|
|
155 |
*
|
|
156 |
* @see com.nokia.helium.antlint.ant.types.Check#run(java.io.File)
|
|
157 |
*/
|
|
158 |
public void run(File antFilename) throws AntlintException {
|
|
159 |
|
|
160 |
List<Element> scriptDefNodes = new ArrayList<Element>();
|
|
161 |
|
|
162 |
this.antFile = antFilename;
|
|
163 |
SAXReader saxReader = new SAXReader();
|
|
164 |
Document doc;
|
|
165 |
try {
|
|
166 |
doc = saxReader.read(antFilename);
|
|
167 |
elementTreeWalk(doc.getRootElement(), "scriptdef", scriptDefNodes);
|
|
168 |
} catch (DocumentException e) {
|
|
169 |
throw new AntlintException("Invalid XML file " + e.getMessage());
|
|
170 |
}
|
|
171 |
for (Element scriptDefNode : scriptDefNodes) {
|
|
172 |
run(scriptDefNode);
|
|
173 |
}
|
|
174 |
}
|
|
175 |
|
|
176 |
/**
|
|
177 |
* @param outputDir
|
|
178 |
* the outputDir to set
|
|
179 |
*/
|
|
180 |
public void setOutputDir(File outputDir) {
|
|
181 |
this.outputDir = outputDir;
|
|
182 |
}
|
|
183 |
|
|
184 |
/**
|
|
185 |
* @return the outputDir
|
|
186 |
*/
|
|
187 |
public File getOutputDir() {
|
|
188 |
return outputDir;
|
|
189 |
}
|
|
190 |
|
|
191 |
/*
|
|
192 |
* (non-Javadoc)
|
|
193 |
*
|
|
194 |
* @see org.apache.tools.ant.types.DataType#toString()
|
|
195 |
*/
|
|
196 |
public String toString() {
|
|
197 |
return "CheckScriptDef";
|
|
198 |
}
|
|
199 |
|
|
200 |
/*
|
|
201 |
* (non-Javadoc)
|
|
202 |
*
|
|
203 |
* @see com.nokia.helium.antlint.ant.types.Check#getAntFile()
|
|
204 |
*/
|
|
205 |
public File getAntFile() {
|
|
206 |
return this.antFile;
|
|
207 |
}
|
|
208 |
}
|