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.util.List;
|
|
21 |
import java.io.IOException;
|
|
22 |
|
|
23 |
import javax.xml.parsers.SAXParser;
|
|
24 |
import javax.xml.parsers.SAXParserFactory;
|
|
25 |
import javax.xml.parsers.ParserConfigurationException;
|
|
26 |
import org.xml.sax.SAXException;
|
|
27 |
import org.dom4j.Element;
|
|
28 |
import org.dom4j.Node;
|
|
29 |
|
|
30 |
import com.nokia.helium.antlint.AntLintHandler;
|
|
31 |
import com.nokia.helium.antlint.ant.AntlintException;
|
|
32 |
|
|
33 |
/**
|
|
34 |
* <code>CheckTabCharacter</code> is used to check the tab characters inside the
|
|
35 |
* ant files.
|
|
36 |
*
|
|
37 |
* <pre>
|
|
38 |
* Usage:
|
|
39 |
*
|
|
40 |
* <antlint>
|
|
41 |
* <fileset id="antlint.files" dir="${antlint.test.dir}/data">
|
|
42 |
* <include name="*.ant.xml"/>
|
|
43 |
* <include name="*build.xml"/>
|
|
44 |
* <include name="*.antlib.xml"/>
|
|
45 |
* </fileset>
|
|
46 |
* <CheckTabCharacter" severity="error" enabled="true" />
|
|
47 |
* </antlint>
|
|
48 |
* </pre>
|
|
49 |
*
|
|
50 |
* @ant.task name="CheckTabCharacter" category="AntLint"
|
|
51 |
*
|
|
52 |
*/
|
|
53 |
public class CheckTabCharacter extends AbstractCheck {
|
|
54 |
|
|
55 |
private File antFile;
|
|
56 |
|
|
57 |
/**
|
|
58 |
* {@inheritDoc}
|
|
59 |
*/
|
|
60 |
public void run(Element node) {
|
|
61 |
checkTabsInScript(node);
|
|
62 |
}
|
|
63 |
|
|
64 |
/**
|
|
65 |
* Check against the given node.
|
|
66 |
*
|
|
67 |
* @param node
|
|
68 |
* is the node to check.
|
|
69 |
*/
|
|
70 |
@SuppressWarnings("unchecked")
|
|
71 |
private void checkTabsInScript(Element node) {
|
|
72 |
if (node.getName().equals("target")) {
|
|
73 |
String target = node.attributeValue("name");
|
|
74 |
|
|
75 |
List<Node> statements = node.selectNodes("//target[@name='"
|
|
76 |
+ target + "']/script | //target[@name='" + target
|
|
77 |
+ "']/*[name()=\"hlm:python\"]");
|
|
78 |
|
|
79 |
for (Node statement : statements) {
|
|
80 |
if (statement.getText().contains("\t")) {
|
|
81 |
this.getReporter().report(getSeverity(),
|
|
82 |
"Target " + target + " has a script with tabs",
|
|
83 |
getAntFile(), 0);
|
|
84 |
}
|
|
85 |
}
|
|
86 |
}
|
|
87 |
}
|
|
88 |
|
|
89 |
/*
|
|
90 |
* (non-Javadoc)
|
|
91 |
*
|
|
92 |
* @see com.nokia.helium.antlint.ant.types.Check#run(java.io.File)
|
|
93 |
*/
|
|
94 |
public void run(File antFilename) throws AntlintException {
|
|
95 |
try {
|
|
96 |
this.antFile = antFilename;
|
|
97 |
SAXParserFactory saxFactory = SAXParserFactory.newInstance();
|
|
98 |
saxFactory.setNamespaceAware(true);
|
|
99 |
saxFactory.setValidating(true);
|
|
100 |
SAXParser parser = saxFactory.newSAXParser();
|
|
101 |
AntLintHandler handler = new AntLintHandler(this);
|
|
102 |
handler.setTabCharacterCheck(true);
|
|
103 |
parser.parse(antFilename, handler);
|
|
104 |
} catch (ParserConfigurationException e) {
|
|
105 |
throw new AntlintException("Not able to parse XML file "
|
|
106 |
+ e.getMessage());
|
|
107 |
} catch (SAXException e) {
|
|
108 |
throw new AntlintException("Not able to parse XML file "
|
|
109 |
+ e.getMessage());
|
|
110 |
} catch (IOException e) {
|
|
111 |
throw new AntlintException("Not able to find XML file "
|
|
112 |
+ e.getMessage());
|
|
113 |
}
|
|
114 |
}
|
|
115 |
|
|
116 |
/*
|
|
117 |
* (non-Javadoc)
|
|
118 |
*
|
|
119 |
* @see org.apache.tools.ant.types.DataType#toString()
|
|
120 |
*/
|
|
121 |
public String toString() {
|
|
122 |
return "CheckTabCharacter";
|
|
123 |
}
|
|
124 |
|
|
125 |
/*
|
|
126 |
* (non-Javadoc)
|
|
127 |
*
|
|
128 |
* @see com.nokia.helium.antlint.ant.types.Check#getAntFile()
|
|
129 |
*/
|
|
130 |
public File getAntFile() {
|
|
131 |
return this.antFile;
|
|
132 |
}
|
|
133 |
|
|
134 |
}
|