This repository has been archived by the owner on Apr 6, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FIXED JENKINS-17788] Use JS-Lint parser when parsing CSS-Lint files.
- Loading branch information
Showing
9 changed files
with
314 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/main/java/hudson/plugins/warnings/parser/CssLintParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package hudson.plugins.warnings.parser; | ||
|
||
import hudson.Extension; | ||
|
||
/** | ||
* A parser for CSS-Lint checks warnings. | ||
* | ||
* @author Ulli Hafner | ||
*/ | ||
@Extension | ||
public class CssLintParser extends LintParser { | ||
private static final long serialVersionUID = 8613418992526753095L; | ||
|
||
/** | ||
* Creates a new instance of {@link CssLintParser}. | ||
*/ | ||
public CssLintParser() { | ||
super(Messages._Warnings_CssLint_ParserName(), | ||
Messages._Warnings_CssLint_LinkName(), | ||
Messages._Warnings_CssLint_TrendName()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
src/main/java/hudson/plugins/warnings/parser/JSLintXMLSaxParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package hudson.plugins.warnings.parser; | ||
|
||
import java.util.List; | ||
import java.util.logging.Logger; | ||
|
||
import org.apache.commons.lang.StringUtils; | ||
import org.xml.sax.Attributes; | ||
import org.xml.sax.SAXException; | ||
import org.xml.sax.helpers.DefaultHandler; | ||
|
||
import hudson.plugins.analysis.util.model.FileAnnotation; | ||
import hudson.plugins.analysis.util.model.Priority; | ||
|
||
/** | ||
* Handles parsing. | ||
*/ | ||
public class JSLintXMLSaxParser extends DefaultHandler { | ||
private static final Logger LOGGER = Logger.getLogger(JSLintXMLSaxParser.class.toString()); | ||
private final List<FileAnnotation> warnings; | ||
private String fileName; | ||
private final String type; | ||
|
||
/** Categories. */ | ||
private static final String CATEGORY_PARSING = "Parsing"; | ||
private static final String CATEGORY_UNDEFINED_VARIABLE = "Undefined Variable"; | ||
private static final String CATEGORY_FORMATTING = "Formatting"; | ||
|
||
/** | ||
* Creates a new instance of {@link JSLintXMLSaxParser}. | ||
* | ||
* @param type | ||
* type of the parser | ||
* @param warnings | ||
* the warnings output | ||
*/ | ||
public JSLintXMLSaxParser(final String type, final List<FileAnnotation> warnings) { | ||
super(); | ||
|
||
this.type = type; | ||
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 (isLintDerivate(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 = CATEGORY_PARSING; | ||
} | ||
else if (message.endsWith(" is not defined.")) { | ||
priority = Priority.HIGH; | ||
category = CATEGORY_UNDEFINED_VARIABLE; | ||
} | ||
else if (message.contains("Mixed spaces and tabs")) { | ||
priority = Priority.LOW; | ||
category = CATEGORY_FORMATTING; | ||
} | ||
else if (message.contains("Unnecessary semicolon")) { | ||
category = CATEGORY_FORMATTING; | ||
} | ||
else if (message.contains("is better written in dot notation")) { | ||
category = CATEGORY_FORMATTING; | ||
} | ||
|
||
int lineNumber = AbstractWarningsParser.convertLineNumber(atts.getValue("line")); | ||
Warning warning = new Warning(fileName, lineNumber, type, category, message, priority); | ||
|
||
warnings.add(warning); | ||
return; | ||
} | ||
else { | ||
LOGGER.info("Unknown jslint xml tag: " + key); | ||
} | ||
} | ||
|
||
private boolean isLintDerivate(final String key) { | ||
return key != null && key.contains("lint"); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
src/main/java/hudson/plugins/warnings/parser/LintParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package hudson.plugins.warnings.parser; | ||
|
||
import java.io.IOException; | ||
import java.io.Reader; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
import javax.xml.parsers.ParserConfigurationException; | ||
import javax.xml.parsers.SAXParser; | ||
import javax.xml.parsers.SAXParserFactory; | ||
|
||
import org.apache.tools.ant.util.ReaderInputStream; | ||
import org.jvnet.localizer.Localizable; | ||
import org.kohsuke.stapler.framework.io.IOException2; | ||
import org.xml.sax.SAXException; | ||
|
||
import hudson.plugins.analysis.util.model.FileAnnotation; | ||
|
||
/** | ||
* Base class for parsers based on {@link JSLintXMLSaxParser}. | ||
* | ||
* @author Ulli Hafner | ||
*/ | ||
public abstract class LintParser extends AbstractWarningsParser { | ||
private static final long serialVersionUID = 3341424685245834156L; | ||
|
||
/** | ||
* Creates a new instance of {@link LintParser}. | ||
* | ||
* @param parserName | ||
* name of the parser | ||
* @param linkName | ||
* name of the project action link | ||
* @param trendName | ||
* name of the trend graph | ||
*/ | ||
protected LintParser(final Localizable parserName, final Localizable linkName, final Localizable trendName) { | ||
super(parserName, linkName, 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(getGroup(), warnings)); | ||
|
||
return warnings; | ||
} | ||
catch (SAXException exception) { | ||
throw new IOException2(exception); | ||
} | ||
catch (ParserConfigurationException exception) { | ||
throw new IOException2(exception); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/test/java/hudson/plugins/warnings/parser/CssLintParserTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package hudson.plugins.warnings.parser; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
import java.io.IOException; | ||
import java.util.Collection; | ||
|
||
import org.junit.Test; | ||
|
||
import hudson.plugins.analysis.util.model.FileAnnotation; | ||
|
||
/** | ||
* Tests the class {@link JSLintParser}. | ||
* | ||
* @author Ulli Hafner | ||
*/ | ||
public class CssLintParserTest extends ParserTester { | ||
/** | ||
* Tests parsing of CSS-Lint files. | ||
* | ||
* @throws IOException | ||
* in case of an error | ||
*/ | ||
@Test | ||
public void testCssLint() throws IOException { | ||
Collection<FileAnnotation> results = createParser().parse(openFile()); | ||
assertEquals(WRONG_NUMBER_OF_WARNINGS_DETECTED, 51, results.size()); | ||
} | ||
|
||
/** | ||
* Creates the parser. | ||
* | ||
* @return the warnings parser | ||
*/ | ||
protected AbstractWarningsParser createParser() { | ||
return new CssLintParser(); | ||
} | ||
|
||
@Override | ||
protected String getWarningsFile() { | ||
return "jslint/csslint.xml"; | ||
} | ||
} |
Oops, something went wrong.