Skip to content

Commit

Permalink
Update XML DTD URL
Browse files Browse the repository at this point in the history
  • Loading branch information
giabao committed Feb 10, 2020
1 parent b36c2b0 commit e84b995
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 44 deletions.
77 changes: 53 additions & 24 deletions src/main/scala/com/etsy/sbt/checkstyle/Checkstyle.scala
Expand Up @@ -15,6 +15,7 @@ import sbt._
* @author Joseph Earl <joe@josephearl.co.uk>
*/
object Checkstyle {

/**
* Runs checkstyle
*
Expand All @@ -24,24 +25,45 @@ object Checkstyle {
* @param xsltTransformations XSLT transformations to apply.
* @param severityLevel The severity level used to fail the build.
*/
def checkstyle(javaSource: File, resources: Seq[File], outputFile: File, configLocation: CheckstyleConfigLocation,
xsltTransformations: Option[Set[CheckstyleXSLTSettings]], severityLevel: Option[CheckstyleSeverityLevel], streams: TaskStreams): Unit = {
def checkstyle(
javaSource: File,
resources: Seq[File],
outputFile: File,
configLocation: CheckstyleConfigLocation,
xsltTransformations: Option[Set[CheckstyleXSLTSettings]],
severityLevel: Option[CheckstyleSeverityLevel],
streams: TaskStreams
): Unit = {
val outputLocation = outputFile.absolutePath
val targetFolder = outputFile.getParentFile
val configFile = targetFolder + "/checkstyle-config.xml"
val targetFolder = outputFile.getParentFile
val configFile = targetFolder + "/checkstyle-config.xml"

targetFolder.mkdirs()

val config = scala.xml.XML.loadString(configLocation.read(resources))
scala.xml.XML.save(configFile, config, "UTF-8", xmlDecl = true,
scala.xml.dtd.DocType("module", scala.xml.dtd.PublicID("-//Puppy Crawl//DTD Check Configuration 1.3//EN",
"http://www.puppycrawl.com/dtds/configuration_1_3.dtd"), Nil))
scala.xml.XML.save(
configFile,
config,
"UTF-8",
xmlDecl = true,
scala.xml.dtd.DocType(
"module",
scala.xml.dtd.PublicID(
"-//Puppy Crawl//DTD Check Configuration 1.3//EN",
"https://checkstyle.org/dtds/configuration_1_3.dtd"
),
Nil
)
)

val checkstyleArgs = Array(
"-c", configFile, // checkstyle configuration file
"-c",
configFile, // checkstyle configuration file
javaSource.absolutePath, // location of Java source file
"-f", "xml", // output format
"-o", outputLocation // output file
"-f",
"xml", // output format
"-o",
outputLocation // output file
)

// Checkstyle calls System.exit which would exit SBT
Expand All @@ -52,12 +74,12 @@ object Checkstyle {
}

xsltTransformations match {
case None => // Nothing to do
case None => // Nothing to do
case Some(xslt) => applyXSLT(file(outputLocation), xslt)
}

if (file(outputLocation).exists && severityLevel.isDefined) {
val log = streams.log
val log = streams.log
val issuesFound = processIssues(log, outputLocation, severityLevel.get)

if (issuesFound > 0) {
Expand All @@ -75,21 +97,28 @@ object Checkstyle {
* @param severityLevel The severity level at which to fail the build if style issues exist at that level
* @return A count of the total number of issues processed
*/
private def processIssues(log: Logger, outputLocation: String, severityLevel: CheckstyleSeverityLevel): Int = {
val report = scala.xml.XML.loadFile(file(outputLocation))
private def processIssues(
log: Logger,
outputLocation: String,
severityLevel: CheckstyleSeverityLevel
): Int = {
val report = scala.xml.XML.loadFile(file(outputLocation))
val checkstyleSeverityLevelIndex = CheckstyleSeverityLevel.values.toArray.indexOf(severityLevel)
val appliedCheckstyleSeverityLevels = CheckstyleSeverityLevel.values.drop(checkstyleSeverityLevelIndex)

val appliedCheckstyleSeverityLevels =
CheckstyleSeverityLevel.values.drop(checkstyleSeverityLevelIndex)

(report \ "file").flatMap { file =>
(file \ "error").map { error =>
val severity = CheckstyleSeverityLevel.withName(error.attribute("severity").get.head.text)
appliedCheckstyleSeverityLevels.contains(severity) match {
case false => 0
case true => val lineNumber = error.attribute("line").get.head.text
val filename = file.attribute("name").get.head.text
case true =>
val lineNumber = error.attribute("line").get.head.text
val filename = file.attribute("name").get.head.text
val errorMessage = error.attribute("message").get.head.text
log.error("Checkstyle " + severity + " found in " + filename + ":" + lineNumber + ": " + errorMessage)
log.error(
"Checkstyle " + severity + " found in " + filename + ":" + lineNumber + ": " + errorMessage
)
1
}
}
Expand All @@ -104,12 +133,12 @@ object Checkstyle {
*/
private def applyXSLT(input: File, transformations: Set[CheckstyleXSLTSettings]): Unit = {
val processor = new Processor(false)
val source = processor.newDocumentBuilder().build(input)
val source = processor.newDocumentBuilder().build(input)

transformations foreach { transform: CheckstyleXSLTSettings =>
val output = processor.newSerializer(transform.output)
val compiler = processor.newXsltCompiler()
val executor = compiler.compile(new StreamSource(transform.xslt))
val output = processor.newSerializer(transform.output)
val compiler = processor.newXsltCompiler()
val executor = compiler.compile(new StreamSource(transform.xslt))
val transformer = executor.load()
transformer.setInitialContextNode(source)
transformer.setDestination(output)
Expand All @@ -134,7 +163,7 @@ object Checkstyle {
block
} catch {
case _: NoExitException =>
case e: Throwable => throw e
case e: Throwable => throw e
} finally {
System.setSecurityManager(original)
}
Expand Down
4 changes: 2 additions & 2 deletions src/sbt-test/checkstyle/checkstyle-auto/checkstyle-config.xml
@@ -1,7 +1,7 @@
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
"-//Puppy Crawl//DTD Check Configuration 1.3//EN"
"http://www.puppycrawl.com/dtds/configuration_1_3.dtd">
"-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
"https://checkstyle.org/dtds/configuration_1_3.dtd">

<module name="Checker">

Expand Down
@@ -1,7 +1,7 @@
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
"-//Puppy Crawl//DTD Check Configuration 1.3//EN"
"http://www.puppycrawl.com/dtds/configuration_1_3.dtd">
"-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
"https://checkstyle.org/dtds/configuration_1_3.dtd">

<module name="Checker">

Expand Down
@@ -1,7 +1,7 @@
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
"-//Puppy Crawl//DTD Check Configuration 1.3//EN"
"http://www.puppycrawl.com/dtds/configuration_1_3.dtd">
"-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
"https://checkstyle.org/dtds/configuration_1_3.dtd">

<module name="Checker">

Expand Down
@@ -1,7 +1,7 @@
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
"-//Puppy Crawl//DTD Check Configuration 1.3//EN"
"http://www.puppycrawl.com/dtds/configuration_1_3.dtd">
"-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
"https://checkstyle.org/dtds/configuration_1_3.dtd">

<!--
Expand Down
@@ -1,7 +1,7 @@
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
"-//Puppy Crawl//DTD Check Configuration 1.3//EN"
"http://www.puppycrawl.com/dtds/configuration_1_3.dtd">
"-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
"https://checkstyle.org/dtds/configuration_1_3.dtd">

<module name="Checker">

Expand Down
@@ -1,7 +1,7 @@
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
"-//Puppy Crawl//DTD Check Configuration 1.3//EN"
"http://www.puppycrawl.com/dtds/configuration_1_3.dtd">
"-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
"https://checkstyle.org/dtds/configuration_1_3.dtd">

<module name="Checker">

Expand Down
@@ -1,7 +1,7 @@
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
"-//Puppy Crawl//DTD Check Configuration 1.3//EN"
"http://www.puppycrawl.com/dtds/configuration_1_3.dtd">
"-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
"https://checkstyle.org/dtds/configuration_1_3.dtd">

<module name="Checker">

Expand Down
4 changes: 2 additions & 2 deletions src/sbt-test/checkstyle/checkstyle-test/checkstyle-config.xml
@@ -1,7 +1,7 @@
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
"-//Puppy Crawl//DTD Check Configuration 1.3//EN"
"http://www.puppycrawl.com/dtds/configuration_1_3.dtd">
"-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
"https://checkstyle.org/dtds/configuration_1_3.dtd">

<module name="Checker">

Expand Down
4 changes: 2 additions & 2 deletions src/sbt-test/checkstyle/checkstyle-xslt/checkstyle-config.xml
@@ -1,7 +1,7 @@
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
"-//Puppy Crawl//DTD Check Configuration 1.3//EN"
"http://www.puppycrawl.com/dtds/configuration_1_3.dtd">
"-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
"https://checkstyle.org/dtds/configuration_1_3.dtd">

<module name="Checker">

Expand Down
4 changes: 2 additions & 2 deletions src/sbt-test/checkstyle/checkstyle/checkstyle-config.xml
@@ -1,7 +1,7 @@
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
"-//Puppy Crawl//DTD Check Configuration 1.3//EN"
"http://www.puppycrawl.com/dtds/configuration_1_3.dtd">
"-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
"https://checkstyle.org/dtds/configuration_1_3.dtd">

<module name="Checker">

Expand Down

0 comments on commit e84b995

Please sign in to comment.