This repository has been archived by the owner on Apr 6, 2022. It is now read-only.
/
JSLintParser.java
127 lines (109 loc) · 4.47 KB
/
JSLintParser.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package hudson.plugins.warnings.parser;
import hudson.Extension;
import hudson.plugins.analysis.util.model.FileAnnotation;
import hudson.plugins.analysis.util.model.Priority;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.logging.Logger;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.apache.commons.lang.StringUtils;
import org.apache.tools.ant.util.ReaderInputStream;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
/**
* A parser for JSLint checks warnings.
*
* @author Gavin Mogan <gavin@kodekoan.com>
*/
@Extension
public class JSLintParser extends AbstractWarningsParser {
private static final long serialVersionUID = 8613418992526753095L;
private static final Logger LOGGER = Logger.getLogger(JSLintParser.class.toString());
/** Categories. */
static final String CATEGORY_PARSING = "Parsing";
static final String CATEGORY_UNDEFINED_VARIABLE = "Undefined Variable";
static final String CATEGORY_FORMATTING = "Formatting";
/**
* Creates a new instance of {@link JSLintParser}.
*/
public JSLintParser() {
super(Messages._Warnings_JSLint_ParserName(),
Messages._Warnings_JSLint_LinkName(),
Messages._Warnings_JSLint_TrendName());
}
@Override
public Collection<FileAnnotation> parse(final Reader file) throws IOException, ParsingCanceledException {
try {
List<FileAnnotation> warnings = new ArrayList<FileAnnotation>();
SAXParserFactory parserFactory = SAXParserFactory.newInstance();
SAXParser parser = parserFactory.newSAXParser();
parser.parse(new ReaderInputStream(file, "UTF-8"), new JSLintXMLSaxParser(warnings));
return warnings;
}
catch (SAXException exception) {
throw new IOException(exception);
}
catch (ParserConfigurationException exception) {
throw new IOException(exception);
}
}
/**
* Handles parsing.
*/
private class JSLintXMLSaxParser extends DefaultHandler {
private final List<FileAnnotation> warnings;
private String fileName;
public JSLintXMLSaxParser(final List<FileAnnotation> warnings) {
super();
this.warnings = warnings;
}
@Override
public void startElement(final String namespaceURI, final String localName,
final String qName, final Attributes atts) throws SAXException {
String key = qName;
if ("jslint".equals(key)) {
return; // Start element, good to skip
}
if ("file".equals(key)) {
fileName = atts.getValue("name");
return;
}
if ("issue".equals(key)) {
String category = StringUtils.EMPTY;
Priority priority = Priority.NORMAL;
String message = atts.getValue("reason");
if (message.startsWith("Expected")) {
priority = Priority.HIGH;
category = JSLintParser.CATEGORY_PARSING;
}
else if (message.endsWith(" is not defined.")) {
priority = Priority.HIGH;
category = JSLintParser.CATEGORY_UNDEFINED_VARIABLE;
}
else if (message.contains("Mixed spaces and tabs")) {
priority = Priority.LOW;
category = JSLintParser.CATEGORY_FORMATTING;
}
else if (message.contains("Unnecessary semicolon")) {
category = JSLintParser.CATEGORY_FORMATTING;
}
else if (message.contains("is better written in dot notation")) {
category = JSLintParser.CATEGORY_FORMATTING;
}
int lineNumber = getLineNumber(atts.getValue("line"));
Warning warning = createWarning( fileName, lineNumber, category, message, priority);
warnings.add(warning);
return;
}
else {
LOGGER.info("Unknown jslint xml tag: " + key);
}
}
}
}