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.regex.Matcher;
|
|
25 |
import java.util.regex.Pattern;
|
|
26 |
|
|
27 |
import org.apache.tools.ant.BuildException;
|
|
28 |
|
|
29 |
/**
|
|
30 |
* <code>AbstractScriptCheck</code> is an abstract implementation of
|
|
31 |
* {@link Check} and contains some concrete methods related to script.
|
|
32 |
*
|
|
33 |
*/
|
|
34 |
public abstract class AbstractScriptCheck extends AbstractCheck {
|
|
35 |
|
|
36 |
/**
|
|
37 |
* Write a script with the given name and the text.
|
|
38 |
*
|
|
39 |
* @param name
|
|
40 |
* is the name of the script
|
|
41 |
* @param text
|
|
42 |
* is the script text.
|
|
43 |
*/
|
|
44 |
protected void writeJythonFile(String name, String text, File outputDir) {
|
|
45 |
if (outputDir == null) {
|
|
46 |
throw new BuildException("'output' attribute for the checker '"
|
|
47 |
+ this.toString() + "' should be specified.");
|
|
48 |
}
|
|
49 |
if (text.contains("${")) {
|
|
50 |
this.getReporter().report(this.getSeverity(),
|
|
51 |
"${ found in " + name, this.getAntFile(), 0);
|
|
52 |
}
|
|
53 |
try {
|
|
54 |
String heliumpath = outputDir.getCanonicalPath();
|
|
55 |
new File(heliumpath + File.separator + "jep").mkdirs();
|
|
56 |
File file = new File(heliumpath + File.separator + "jep"
|
|
57 |
+ File.separator + name + "_jep.py");
|
|
58 |
PrintWriter output = new PrintWriter(new FileOutputStream(file));
|
|
59 |
output.write("def abc():\n");
|
|
60 |
output.write(" attributes = {} # pylint: disable-msg=C0103\n");
|
|
61 |
output.write(" elements = {} # pylint: disable-msg=C0103\n");
|
|
62 |
output.write(" project = None # pylint: disable-msg=C0103\n");
|
|
63 |
output.write(" self = None # pylint: disable-msg=C0103\n");
|
|
64 |
text = text.replace(" File(", " self.File(");
|
|
65 |
for (String line : text.split("\n")) {
|
|
66 |
output.write(" " + line + "\n");
|
|
67 |
}
|
|
68 |
output.close();
|
|
69 |
|
|
70 |
if (text.contains("import ")) {
|
|
71 |
File file2 = new File(heliumpath + File.separator
|
|
72 |
+ "test_jython.xml");
|
|
73 |
PrintWriter output2 = new PrintWriter(new FileOutputStream(
|
|
74 |
file2, true));
|
|
75 |
output2.write("try:\n");
|
|
76 |
for (String line : text.split("\n")) {
|
|
77 |
if (line.trim().startsWith("import ")
|
628
|
78 |
|| line.trim().startsWith("from ")) {
|
588
|
79 |
output2.write(" " + line + "\n");
|
628
|
80 |
}
|
588
|
81 |
}
|
|
82 |
|
|
83 |
output2.write("except ImportError, e:\n");
|
|
84 |
output2.write(" print '" + name + " failed: ' + str(e)\n");
|
|
85 |
output2.close();
|
|
86 |
}
|
|
87 |
} catch (IOException e) {
|
|
88 |
throw new BuildException("Not able to write JEP File " + name
|
|
89 |
+ "_jep.py");
|
|
90 |
}
|
|
91 |
}
|
|
92 |
|
|
93 |
/**
|
|
94 |
* Check for the properties in the given script text.
|
|
95 |
*
|
|
96 |
* @param text
|
|
97 |
* is the script text to lookup.
|
|
98 |
*/
|
|
99 |
protected void checkJepPropertiesInText(String text) {
|
|
100 |
Pattern p1 = Pattern
|
|
101 |
.compile("getProperty\\([\"']([a-zA-Z0-9\\.]*)[\"']\\)");
|
|
102 |
Matcher m1 = p1.matcher(text);
|
|
103 |
ArrayList<String> props = new ArrayList<String>();
|
|
104 |
while (m1.find()) {
|
|
105 |
props.add(m1.group(1));
|
|
106 |
}
|
|
107 |
/*
|
|
108 |
* for (String group : props) { checkPropertyInModel(group); }
|
|
109 |
*/
|
|
110 |
}
|
|
111 |
|
|
112 |
}
|