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.ArrayList;
|
|
21 |
import java.util.List;
|
|
22 |
|
|
23 |
import org.apache.tools.ant.Target;
|
|
24 |
import org.dom4j.Document;
|
|
25 |
import org.dom4j.DocumentException;
|
|
26 |
import org.dom4j.Element;
|
|
27 |
import org.dom4j.io.SAXReader;
|
|
28 |
|
|
29 |
import com.nokia.helium.antlint.ant.AntlintException;
|
|
30 |
|
|
31 |
/**
|
628
|
32 |
* <code>CheckAntCall</code> is used to check whether antcall is used with no param elements and
|
|
33 |
* calls the target with no dependencies
|
588
|
34 |
*
|
|
35 |
* <pre>
|
|
36 |
* Usage:
|
|
37 |
*
|
|
38 |
* <antlint>
|
|
39 |
* <fileset id="antlint.files" dir="${antlint.test.dir}/data">
|
|
40 |
* <include name="*.ant.xml"/>
|
|
41 |
* <include name="*build.xml"/>
|
|
42 |
* <include name="*.antlib.xml"/>
|
|
43 |
* </fileset>
|
|
44 |
* <CheckAntCall" severity="error" enabled="true"/>
|
|
45 |
* </antlint>
|
|
46 |
* </pre>
|
|
47 |
*
|
|
48 |
* @ant.task name="CheckAntCall" category="AntLint"
|
|
49 |
*/
|
|
50 |
public class CheckAntCall extends AbstractCheck {
|
|
51 |
|
|
52 |
private File antFile;
|
|
53 |
|
|
54 |
/**
|
|
55 |
* {@inheritDoc}
|
|
56 |
*/
|
|
57 |
public void run(Element node) {
|
|
58 |
if (node.getName().equals("target")) {
|
|
59 |
checkAntCalls(node);
|
|
60 |
}
|
|
61 |
}
|
|
62 |
|
|
63 |
/**
|
|
64 |
* Check against the given node.
|
|
65 |
*
|
628
|
66 |
* @param node is the node to check.
|
588
|
67 |
*/
|
|
68 |
@SuppressWarnings("unchecked")
|
|
69 |
private void checkAntCalls(Element node) {
|
|
70 |
if (node.elements("antcall") != null) {
|
|
71 |
List<Element> antcallList = node.elements("antcall");
|
|
72 |
for (Element antcallElement : antcallList) {
|
|
73 |
String antcallName = antcallElement.attributeValue("target");
|
628
|
74 |
if (((node.elements("param") == null) || (node.elements("param") != null && node.elements("param").isEmpty()))
|
|
75 |
&& !checkTargetDependency(antcallName)) {
|
|
76 |
this.getReporter().report(this.getSeverity(), "<antcall> is used with no param elements and calls the target "
|
|
77 |
+ antcallName
|
|
78 |
+ " that has no dependencies! (<runtarget> could be used instead.)", this.getAntFile(), 0);
|
588
|
79 |
}
|
|
80 |
}
|
|
81 |
}
|
|
82 |
}
|
|
83 |
|
|
84 |
/*
|
|
85 |
* (non-Javadoc)
|
|
86 |
*
|
|
87 |
* @see com.nokia.helium.antlint.ant.types.Check#run(java.io.File)
|
|
88 |
*/
|
|
89 |
public void run(File antFilename) throws AntlintException {
|
|
90 |
|
|
91 |
this.antFile = antFilename;
|
|
92 |
SAXReader saxReader = new SAXReader();
|
|
93 |
Document doc;
|
|
94 |
List<Element> targetNodes = new ArrayList<Element>();
|
|
95 |
|
|
96 |
try {
|
|
97 |
doc = saxReader.read(antFilename);
|
|
98 |
elementTreeWalk(doc.getRootElement(), "target", targetNodes);
|
628
|
99 |
}
|
|
100 |
catch (DocumentException e) {
|
588
|
101 |
throw new AntlintException("Invalid XML file " + e.getMessage());
|
|
102 |
}
|
|
103 |
|
|
104 |
for (Element targetNode : targetNodes) {
|
|
105 |
run(targetNode);
|
|
106 |
}
|
|
107 |
}
|
|
108 |
|
|
109 |
/*
|
|
110 |
* (non-Javadoc)
|
|
111 |
*
|
|
112 |
* @see org.apache.tools.ant.types.DataType#toString()
|
|
113 |
*/
|
|
114 |
public String toString() {
|
|
115 |
return "CheckAntCall";
|
|
116 |
}
|
|
117 |
|
|
118 |
/**
|
|
119 |
* Check the availability dependent targets of the given target.
|
|
120 |
*
|
628
|
121 |
* @param targetName is the target for which dependent targets to be loked up.
|
588
|
122 |
* @return true, if the dependant targets are available; otherwise false
|
|
123 |
*/
|
|
124 |
private boolean checkTargetDependency(String targetName) {
|
|
125 |
boolean dependencyCheck = false;
|
628
|
126 |
Target targetDependency = (Target) getProject().getTargets().get(targetName);
|
588
|
127 |
dependencyCheck = targetDependency != null
|
628
|
128 |
&& targetDependency.getDependencies().hasMoreElements();
|
588
|
129 |
return dependencyCheck;
|
|
130 |
}
|
|
131 |
|
|
132 |
/*
|
|
133 |
* (non-Javadoc)
|
|
134 |
*
|
|
135 |
* @see com.nokia.helium.antlint.ant.types.Check#getAntFile()
|
|
136 |
*/
|
|
137 |
public File getAntFile() {
|
|
138 |
return this.antFile;
|
|
139 |
}
|
|
140 |
|
|
141 |
}
|