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 |
import java.util.regex.Matcher;
|
|
23 |
import java.util.regex.Pattern;
|
|
24 |
|
|
25 |
import org.dom4j.Document;
|
|
26 |
import org.dom4j.DocumentException;
|
|
27 |
import org.dom4j.Element;
|
|
28 |
import org.dom4j.io.SAXReader;
|
|
29 |
|
|
30 |
import com.nokia.helium.antlint.ant.AntlintException;
|
|
31 |
|
|
32 |
/**
|
|
33 |
* <code>CheckPropertyName</code> is used to check the naming convention of
|
|
34 |
* property names.
|
|
35 |
*
|
|
36 |
* <pre>
|
|
37 |
* Usage:
|
|
38 |
*
|
|
39 |
* <antlint>
|
|
40 |
* <fileset id="antlint.files" dir="${antlint.test.dir}/data">
|
|
41 |
* <include name="*.ant.xml"/>
|
|
42 |
* <include name="*build.xml"/>
|
|
43 |
* <include name="*.antlib.xml"/>
|
|
44 |
* </fileset>
|
|
45 |
* <CheckPropertyName" severity="error" enabled="true" regexp="([a-z0-9[\\d\\-]]*)" />
|
|
46 |
* </antlint>
|
|
47 |
* </pre>
|
|
48 |
*
|
|
49 |
* @ant.task name="CheckPropertyName" category="AntLint"
|
|
50 |
*
|
|
51 |
*/
|
|
52 |
public class CheckPropertyName extends AbstractCheck {
|
|
53 |
|
|
54 |
private ArrayList<String> propertiesVisited = new ArrayList<String>();
|
|
55 |
private String regExp;
|
|
56 |
private File antFile;
|
|
57 |
|
|
58 |
/**
|
|
59 |
* {@inheritDoc}
|
|
60 |
*/
|
|
61 |
public void run(Element node) {
|
|
62 |
if (node.getName().equals("property")) {
|
|
63 |
String text = node.attributeValue("name");
|
|
64 |
if (text != null && !text.isEmpty()) {
|
|
65 |
checkPropertyName(text);
|
|
66 |
}
|
|
67 |
}
|
|
68 |
}
|
|
69 |
|
|
70 |
/**
|
|
71 |
* Check the given property name.
|
|
72 |
*
|
|
73 |
* @param propertyName
|
|
74 |
* is the property name to check.
|
|
75 |
*/
|
|
76 |
private void checkPropertyName(String propertyName) {
|
|
77 |
Pattern p1 = Pattern.compile(getRegExp());
|
|
78 |
Matcher m1 = p1.matcher(propertyName);
|
|
79 |
if (!m1.matches() && !isPropertyAlreadyVisited(propertyName)) {
|
|
80 |
this.getReporter().report(this.getSeverity(),
|
|
81 |
"INVALID Property Name: " + propertyName,
|
|
82 |
this.getAntFile(), 0);
|
|
83 |
markPropertyAsVisited(propertyName);
|
|
84 |
}
|
|
85 |
}
|
|
86 |
|
|
87 |
/*
|
|
88 |
* (non-Javadoc)
|
|
89 |
*
|
|
90 |
* @see com.nokia.helium.antlint.ant.types.Check#run(java.io.File)
|
|
91 |
*/
|
|
92 |
public void run(File antFilename) throws AntlintException {
|
|
93 |
List<Element> propertyNodes = new ArrayList<Element>();
|
|
94 |
|
|
95 |
this.antFile = antFilename;
|
|
96 |
SAXReader saxReader = new SAXReader();
|
|
97 |
Document doc;
|
|
98 |
try {
|
|
99 |
doc = saxReader.read(antFilename);
|
|
100 |
elementTreeWalk(doc.getRootElement(), "property", propertyNodes);
|
|
101 |
} catch (DocumentException e) {
|
|
102 |
throw new AntlintException("Invalid XML file " + e.getMessage());
|
|
103 |
}
|
|
104 |
for (Element propertyNode : propertyNodes) {
|
|
105 |
run(propertyNode);
|
|
106 |
}
|
|
107 |
}
|
|
108 |
|
|
109 |
/**
|
|
110 |
* Check whether the property is already visited or not.
|
|
111 |
*
|
|
112 |
* @param propertyName
|
|
113 |
* is the property to be checked.
|
|
114 |
* @return true, if already been visited; otherwise false
|
|
115 |
*/
|
|
116 |
private boolean isPropertyAlreadyVisited(String propertyName) {
|
|
117 |
return propertiesVisited.contains(propertyName);
|
|
118 |
}
|
|
119 |
|
|
120 |
/**
|
|
121 |
* Mark the given property as visited.
|
|
122 |
*
|
|
123 |
* @param propertyName
|
|
124 |
* is the property to be marked.
|
|
125 |
*/
|
|
126 |
private void markPropertyAsVisited(String propertyName) {
|
|
127 |
propertiesVisited.add(propertyName);
|
|
128 |
}
|
|
129 |
|
|
130 |
/**
|
|
131 |
* @param regExp
|
|
132 |
* the regExp to set
|
|
133 |
*/
|
|
134 |
public void setRegExp(String regExp) {
|
|
135 |
this.regExp = regExp;
|
|
136 |
}
|
|
137 |
|
|
138 |
/**
|
|
139 |
* @return the regExp
|
|
140 |
*/
|
|
141 |
public String getRegExp() {
|
|
142 |
return regExp;
|
|
143 |
}
|
|
144 |
|
|
145 |
/*
|
|
146 |
* (non-Javadoc)
|
|
147 |
*
|
|
148 |
* @see org.apache.tools.ant.types.DataType#toString()
|
|
149 |
*/
|
|
150 |
public String toString() {
|
|
151 |
return "CheckPropertyName";
|
|
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 |
}
|