diff --git a/maven-plugin/src/main/java/net/revelc/code/formatter/FormatterMojo.java b/maven-plugin/src/main/java/net/revelc/code/formatter/FormatterMojo.java index 15174b1b8..1171c49cf 100644 --- a/maven-plugin/src/main/java/net/revelc/code/formatter/FormatterMojo.java +++ b/maven-plugin/src/main/java/net/revelc/code/formatter/FormatterMojo.java @@ -220,6 +220,12 @@ public class FormatterMojo extends AbstractMojo implements ConfigurationSource { @Parameter(defaultValue = "src/config/jsoup/formatter/xml.properties", property = "configxmlfile", required = true) private String configXmlFile; + /** + * File or classpath location of a properties file to use in json formatting. + */ + @Parameter(defaultValue = "src/config/gson/formatter/json.properties", property = "configjsonfile", required = true) + private String configJsonFile; + /** * File or classpath location of a properties file to use in css formatting. */ @@ -633,19 +639,22 @@ private void createCodeFormatter() throws MojoExecutionException { this.jsFormatter.init(jsFormattingOptions, this); } if (configHtmlFile != null) { - this.htmlFormatter.setFilename(configHtmlFile); + this.htmlFormatter.init(getOptionsFromPropertiesFile(configHtmlFile), this); } if (configXmlFile != null) { - this.xmlFormatter.setFilename(configXmlFile); + this.xmlFormatter.init(getOptionsFromPropertiesFile(configXmlFile), this); + } + if (configJsonFile != null) { + this.jsonFormatter.init(getOptionsFromPropertiesFile(configJsonFile), this); } if (configCssFile != null) { - this.cssFormatter.setFilename(configCssFile); + this.cssFormatter.init(getOptionsFromPropertiesFile(configCssFile), this); } // stop the process if not config files where found if (javaFormattingOptions == null && jsFormattingOptions == null && configHtmlFile == null && configXmlFile == null && configCssFile == null) { throw new MojoExecutionException( - "You must provide a Java, Javascript, HTML, XML or CSS configuration file."); + "You must provide a Java, Javascript, HTML, XML, JSON, or CSS configuration file."); } } @@ -695,6 +704,34 @@ private Map getOptionsFromConfigFile(String newConfigFile) throw } } + /** + * Read properties file and return the properties as {@link Map}. + * + * @return the options from properties file or null if not properties file found + * @throws MojoExecutionException the mojo execution exception + */ + private Map getOptionsFromPropertiesFile(String newPropertiesFile) throws MojoExecutionException { + + this.getLog().debug("Using search path at: " + this.basedir.getAbsolutePath()); + this.resourceManager.addSearchPath(FileResourceLoader.ID, this.basedir.getAbsolutePath()); + + Properties properties = new Properties(); + try { + properties.load(this.resourceManager.getResourceAsInputStream(newPropertiesFile)); + } catch (ResourceNotFoundException e) { + getLog().debug("Property file [" + newPropertiesFile + "] cannot be found", e); + return new HashMap<>(); + } catch (IOException e) { + throw new MojoExecutionException("Cannot read config file [" + newPropertiesFile + "]", e); + } + + final Map map = new HashMap<>(); + for (final String name : properties.stringPropertyNames()) { + map.put(name, properties.getProperty(name)); + } + return map; + } + class ResultCollector { int successCount; diff --git a/maven-plugin/src/main/java/net/revelc/code/formatter/JsoupBasedFormatter.java b/maven-plugin/src/main/java/net/revelc/code/formatter/JsoupBasedFormatter.java index 55bb17ef2..92baadea9 100644 --- a/maven-plugin/src/main/java/net/revelc/code/formatter/JsoupBasedFormatter.java +++ b/maven-plugin/src/main/java/net/revelc/code/formatter/JsoupBasedFormatter.java @@ -18,17 +18,14 @@ import org.jsoup.Jsoup; import org.jsoup.nodes.Document; +import org.jsoup.nodes.Document.OutputSettings; import org.jsoup.nodes.Document.OutputSettings.Syntax; import org.jsoup.nodes.Entities.EscapeMode; import org.jsoup.parser.Parser; -import java.io.IOException; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Paths; import java.util.Map; -import java.util.Properties; /** * @author yoshiman @@ -36,18 +33,24 @@ */ public abstract class JsoupBasedFormatter extends AbstractCacheableFormatter implements Formatter { - private String filename; + private OutputSettings formatter; @Override public void init(Map options, ConfigurationSource cfg) { - if (cfg != null) { - super.initCfg(cfg); - } + super.initCfg(cfg); + + formatter = new OutputSettings(); + formatter.charset(Charset.forName(options.getOrDefault("charset", StandardCharsets.UTF_8.name()))); + formatter.escapeMode(EscapeMode.valueOf(options.getOrDefault("escapeMode", EscapeMode.xhtml.name()))); + formatter.indentAmount(Integer.parseInt(options.getOrDefault("indentAmount", "4"))); + formatter.outline(Boolean.parseBoolean(options.getOrDefault("outlineMode", Boolean.TRUE.toString()))); + formatter.prettyPrint(Boolean.parseBoolean(options.getOrDefault("pretty", Boolean.TRUE.toString()))); + formatter.syntax(Syntax.valueOf(options.getOrDefault("syntax", Syntax.html.name()))); } - public String doFormat(String code, Syntax syntax) { + public String doFormat(String code, LineEnding ending) { Document document; - switch (syntax) { + switch (formatter.syntax()) { case html: document = Jsoup.parse(code, "", Parser.htmlParser()); break; @@ -55,27 +58,9 @@ public String doFormat(String code, Syntax syntax) { document = Jsoup.parse(code, "", Parser.xmlParser()); break; default: - throw new IllegalArgumentException(syntax + " is not allowed as syntax"); - } - try { - Properties properties = new Properties(); - properties.load(Files.newInputStream(Paths.get(filename))); - Charset charset = Charset.forName(properties.getProperty("charset", StandardCharsets.UTF_8.name())); - EscapeMode escapeMode = EscapeMode.valueOf(properties.getProperty("escapeMode", EscapeMode.xhtml.name())); - int indentAmount = Integer.parseInt(properties.getProperty("indentAmount", "1")); - boolean outlineMode = Boolean.parseBoolean(properties.getProperty("outlineMode", Boolean.toString(true))); - boolean pretty = Boolean.parseBoolean(properties.getProperty("pretty", Boolean.toString(true))); - document.outputSettings() // - .charset(charset) // - .escapeMode(escapeMode) // - .indentAmount(indentAmount) // - .outline(outlineMode) // - .prettyPrint(pretty) // - .syntax(syntax); - - } catch (IOException e) { - this.log.error(e); + throw new IllegalArgumentException(formatter.syntax() + " is not allowed as syntax"); } + document.outputSettings(formatter); String formattedCode = document.outerHtml(); if (code.equals(formattedCode)) { @@ -86,15 +71,7 @@ public String doFormat(String code, Syntax syntax) { @Override public boolean isInitialized() { - return filename != null; - } - - public String getFilename() { - return filename; - } - - public void setFilename(String filename) { - this.filename = filename; + return formatter != null; } } diff --git a/maven-plugin/src/main/java/net/revelc/code/formatter/css/CssFormatter.java b/maven-plugin/src/main/java/net/revelc/code/formatter/css/CssFormatter.java index a739b8919..eb60ce0b8 100644 --- a/maven-plugin/src/main/java/net/revelc/code/formatter/css/CssFormatter.java +++ b/maven-plugin/src/main/java/net/revelc/code/formatter/css/CssFormatter.java @@ -28,40 +28,23 @@ import java.io.IOException; import java.io.StringReader; -import java.nio.file.Files; -import java.nio.file.Paths; import java.util.Map; -import java.util.Properties; /** * @author yoshiman * */ public class CssFormatter extends AbstractCacheableFormatter implements Formatter { - private String filename; - private CSSFormat format; - @Override - public boolean isInitialized() { - return filename != null && format != null; - } + private CSSFormat formatter; @Override public void init(Map options, ConfigurationSource cfg) { - if (cfg != null) { - super.initCfg(cfg); - } - Properties properties = new Properties(); - try { - properties.load(Files.newInputStream(Paths.get(filename))); - } catch (IOException e) { - this.log.error("error while reading properties file", e); - } - int indent = Integer.parseInt(properties.getProperty("indent", "2")); - boolean rgbAsHex = Boolean.parseBoolean(properties.getProperty("rgbAsHex", Boolean.toString(true))); - format = new CSSFormat(); - format.setPropertiesInSeparateLines(indent); - format.setRgbAsHex(rgbAsHex); + super.initCfg(cfg); + + int indent = Integer.parseInt(options.getOrDefault("indent", "4")); + boolean rgbAsHex = Boolean.parseBoolean(options.getOrDefault("rgbAsHex", Boolean.TRUE.toString())); + formatter = new CSSFormat().setPropertiesInSeparateLines(indent).setRgbAsHex(rgbAsHex); } @Override @@ -70,7 +53,7 @@ protected String doFormat(String code, LineEnding ending) throws IOException { InputSource source = new InputSource(new StringReader(code)); CSSOMParser parser = new CSSOMParser(new SACParserCSS3()); CSSStyleSheetImpl sheet = (CSSStyleSheetImpl) parser.parseStyleSheet(source, null, null); - String formattedCode = sheet.getCssText(format); + String formattedCode = sheet.getCssText(formatter); if (code.equals(formattedCode)) { return null; @@ -78,12 +61,9 @@ protected String doFormat(String code, LineEnding ending) throws IOException { return formattedCode; } - public String getFilename() { - return filename; - } - - public void setFilename(String filename) { - this.filename = filename; + @Override + public boolean isInitialized() { + return formatter != null; } } diff --git a/maven-plugin/src/main/java/net/revelc/code/formatter/html/HTMLFormatter.java b/maven-plugin/src/main/java/net/revelc/code/formatter/html/HTMLFormatter.java index a3c100407..e9e28eb11 100644 --- a/maven-plugin/src/main/java/net/revelc/code/formatter/html/HTMLFormatter.java +++ b/maven-plugin/src/main/java/net/revelc/code/formatter/html/HTMLFormatter.java @@ -19,9 +19,6 @@ import net.revelc.code.formatter.Formatter; import net.revelc.code.formatter.JsoupBasedFormatter; import net.revelc.code.formatter.LineEnding; -import org.jsoup.nodes.Document.OutputSettings.Syntax; - -import java.io.UnsupportedEncodingException; /** * @author yoshiman @@ -30,8 +27,8 @@ public class HTMLFormatter extends JsoupBasedFormatter implements Formatter { @Override - public String doFormat(String code, LineEnding ending) throws UnsupportedEncodingException { - return super.doFormat(code, Syntax.html); + public String doFormat(String code, LineEnding ending) { + return super.doFormat(code, ending); } } diff --git a/maven-plugin/src/main/java/net/revelc/code/formatter/json/JsonFormatter.java b/maven-plugin/src/main/java/net/revelc/code/formatter/json/JsonFormatter.java index 92b75924e..12a57b3fd 100644 --- a/maven-plugin/src/main/java/net/revelc/code/formatter/json/JsonFormatter.java +++ b/maven-plugin/src/main/java/net/revelc/code/formatter/json/JsonFormatter.java @@ -32,30 +32,37 @@ * */ public class JsonFormatter extends AbstractCacheableFormatter implements Formatter { - private Gson gson; - private JsonParser jsonParser; - @Override - public boolean isInitialized() { - return gson != null && jsonParser != null; - } + private Gson formatter; + + private JsonParser jsonParser; @Override public void init(Map options, ConfigurationSource cfg) { - if (cfg != null) { - super.initCfg(cfg); + super.initCfg(cfg); + + boolean printPrinting = Boolean.parseBoolean(options.getOrDefault("prettyPrinting", Boolean.TRUE.toString())); + + if (printPrinting) { + formatter = new GsonBuilder().setPrettyPrinting().create(); + } else { + formatter = new GsonBuilder().create(); } - gson = new GsonBuilder().setPrettyPrinting().create(); jsonParser = new JsonParser(); } @Override protected String doFormat(String code, LineEnding ending) throws IOException { - String formattedCode = gson.toJson(jsonParser.parse(code)); + String formattedCode = formatter.toJson(jsonParser.parse(code)); if (code.equals(formattedCode)) { return null; } return formattedCode; } + @Override + public boolean isInitialized() { + return formatter != null; + } + } diff --git a/maven-plugin/src/main/java/net/revelc/code/formatter/xml/XMLFormatter.java b/maven-plugin/src/main/java/net/revelc/code/formatter/xml/XMLFormatter.java index a8403e746..edb4e128d 100644 --- a/maven-plugin/src/main/java/net/revelc/code/formatter/xml/XMLFormatter.java +++ b/maven-plugin/src/main/java/net/revelc/code/formatter/xml/XMLFormatter.java @@ -19,9 +19,6 @@ import net.revelc.code.formatter.Formatter; import net.revelc.code.formatter.JsoupBasedFormatter; import net.revelc.code.formatter.LineEnding; -import org.jsoup.nodes.Document.OutputSettings.Syntax; - -import java.io.UnsupportedEncodingException; /** * @author yoshiman @@ -30,8 +27,8 @@ public class XMLFormatter extends JsoupBasedFormatter implements Formatter { @Override - public String doFormat(String code, LineEnding ending) throws UnsupportedEncodingException { - return super.doFormat(code, Syntax.xml); + public String doFormat(String code, LineEnding ending) { + return super.doFormat(code, ending); } } diff --git a/maven-plugin/src/test/java/net/revelc/code/formatter/css/CssFormatterTest.java b/maven-plugin/src/test/java/net/revelc/code/formatter/css/CssFormatterTest.java index 41b0067cf..117f6e8f5 100644 --- a/maven-plugin/src/test/java/net/revelc/code/formatter/css/CssFormatterTest.java +++ b/maven-plugin/src/test/java/net/revelc/code/formatter/css/CssFormatterTest.java @@ -21,6 +21,7 @@ import org.junit.Test; import java.io.File; +import java.util.HashMap; /** * @author yoshiman @@ -29,10 +30,14 @@ public class CssFormatterTest extends AbstractFormatterTest { @Test public void testDoFormatFile() throws Exception { - CssFormatter cssFormatter = new CssFormatter(); - cssFormatter.setFilename("src/test/resources/css.properties"); - doTestFormat(cssFormatter, "someFile.css", - "2e3a00647508e528051f607b29e7e2dc5dc7ba12c1b75b746f490676f51ca27c1c0f26c233b94804d7656434d65138249530bf0969cff4a7e9c2baef0f3c9294"); + // FIXME Handle linux vs windows since this formatter does not accept line endings + if (System.lineSeparator().equals("\n")) { + doTestFormat(new CssFormatter(), "someFile.css", + "590c14fa99d8296d7e1c6d4124de96e8fc436ee3f44704445a966befd37488e336b14e60fdb7d26181f6fc0c05848c1cc32701b34f98846f3122d4d057de9605"); + } else { + doTestFormat(new CssFormatter(), "someFile.css", + "c3bdea2e2755c1e773459024ca5114282da1ebf0c46d975d90d2567f39ac16c7c6f227745a80f4912d55049a177699ffe619df444ebb3cffeb8574e41babaf0b"); + } } @Test @@ -41,8 +46,7 @@ public void testIsIntialized() throws Exception { Assert.assertFalse(cssFormatter.isInitialized()); final File targetDir = new File("target/testoutput"); targetDir.mkdirs(); - cssFormatter.setFilename("src/test/resources/css.properties"); - cssFormatter.init(null, null); + cssFormatter.init(new HashMap(), new AbstractFormatterTest.TestConfigurationSource(targetDir)); Assert.assertTrue(cssFormatter.isInitialized()); } diff --git a/maven-plugin/src/test/java/net/revelc/code/formatter/html/HTMLFormatterTest.java b/maven-plugin/src/test/java/net/revelc/code/formatter/html/HTMLFormatterTest.java index 0e875fbd7..ce0eaef54 100644 --- a/maven-plugin/src/test/java/net/revelc/code/formatter/html/HTMLFormatterTest.java +++ b/maven-plugin/src/test/java/net/revelc/code/formatter/html/HTMLFormatterTest.java @@ -17,6 +17,7 @@ package net.revelc.code.formatter.html; import java.io.File; +import java.util.HashMap; import org.junit.Assert; import org.junit.Test; @@ -30,10 +31,14 @@ public class HTMLFormatterTest extends AbstractFormatterTest { @Test public void testDoFormatFile() throws Exception { - HTMLFormatter htmlFormatter = new HTMLFormatter(); - htmlFormatter.setFilename("src/test/resources/html.properties"); - doTestFormat(htmlFormatter, "someFile.html", - "355c8710f25888c803d010fe5e534e5d0d5056954e3d71681a03de5c0334686267188dd43cca1ab5c5b3268f59f663a2f1814e234f31f1b6dc3c50f5879fe421"); + // FIXME Handle linux vs windows since this formatter does not accept line endings + if (System.lineSeparator().equals("\n")) { + doTestFormat(new HTMLFormatter(), "someFile.html", + "a96122af3d92a24300e252fd136b24b1a03814f4e8137411956d2305452c3c1fb1782958be591707adfaa26e9ed8e04b16fcf62c7a8f52b1e80f3d0e709b48ad"); + } else { + doTestFormat(new HTMLFormatter(), "someFile.html", + "8e3d98ef0c4d4578ab4fc5e29ce2ac1b5fdfd12b3aff42ef6c8c838c4daf4ac77b473dadce9b53e13c928835533b0a057269bbd6dce8dc301cd108c8bda56d12"); + } } @Test @@ -42,8 +47,7 @@ public void testIsIntialized() throws Exception { Assert.assertFalse(htmlFormatter.isInitialized()); final File targetDir = new File("target/testoutput"); targetDir.mkdirs(); - htmlFormatter.setFilename("src/test/resources/html.properties"); - htmlFormatter.init(null, null); + htmlFormatter.init(new HashMap(), new AbstractFormatterTest.TestConfigurationSource(targetDir)); Assert.assertTrue(htmlFormatter.isInitialized()); } diff --git a/maven-plugin/src/test/java/net/revelc/code/formatter/json/JsonFormatterTest.java b/maven-plugin/src/test/java/net/revelc/code/formatter/json/JsonFormatterTest.java index f6c690988..bd9d14a15 100644 --- a/maven-plugin/src/test/java/net/revelc/code/formatter/json/JsonFormatterTest.java +++ b/maven-plugin/src/test/java/net/revelc/code/formatter/json/JsonFormatterTest.java @@ -21,6 +21,7 @@ import org.junit.Test; import java.io.File; +import java.util.HashMap; /** * @author yoshiman @@ -29,8 +30,7 @@ public class JsonFormatterTest extends AbstractFormatterTest { @Test public void testDoFormatFile() throws Exception { - JsonFormatter jsonFormatter = new JsonFormatter(); - doTestFormat(jsonFormatter, "someFile.json", + doTestFormat(new JsonFormatter(), "someFile.json", "478edd57b917235d00f16611505060460758e7e0f4b53938941226dca183d09be7e946d9a14dbac492a200592d5a6fa5f463e60fd1c3d3dbf05c08c3c869a36b"); } @@ -40,7 +40,7 @@ public void testIsIntialized() throws Exception { Assert.assertFalse(jsonFormatter.isInitialized()); final File targetDir = new File("target/testoutput"); targetDir.mkdirs(); - jsonFormatter.init(null, null); + jsonFormatter.init(new HashMap(), new AbstractFormatterTest.TestConfigurationSource(targetDir)); Assert.assertTrue(jsonFormatter.isInitialized()); } diff --git a/maven-plugin/src/test/java/net/revelc/code/formatter/xml/XMLFormatterTest.java b/maven-plugin/src/test/java/net/revelc/code/formatter/xml/XMLFormatterTest.java index 408a5cccb..230a3d7c1 100644 --- a/maven-plugin/src/test/java/net/revelc/code/formatter/xml/XMLFormatterTest.java +++ b/maven-plugin/src/test/java/net/revelc/code/formatter/xml/XMLFormatterTest.java @@ -17,6 +17,7 @@ package net.revelc.code.formatter.xml; import java.io.File; +import java.util.HashMap; import org.junit.Assert; import org.junit.Test; @@ -30,10 +31,14 @@ public class XMLFormatterTest extends AbstractFormatterTest { @Test public void testDoFormatFile() throws Exception { - XMLFormatter xmlFormatterFormatter = new XMLFormatter(); - xmlFormatterFormatter.setFilename("src/test/resources/xml.properties"); - doTestFormat(xmlFormatterFormatter, "someFile.xml", - "7066679c7aa8e064c7a1f77e76285759c67d5884bef351cd0aa50e0245abc984612c832cbd4fdbe7a1b303e679a1207c42f5c9fb16094391fa0b7045662b2127"); + // FIXME Handle linux vs windows since this formatter does not accept line endings + if (System.lineSeparator().equals("\n")) { + doTestFormat(new XMLFormatter(), "someFile.xml", + "5b37e98476e050998ecad303cc4a3feaca45eb6966e3a7248964df2e670403939b153b45292074e926c1c22c8264df204f0c0011d6c31102652b732186868563"); + } else { + doTestFormat(new XMLFormatter(), "someFile.xml", + "98f896736377248255739514b27e5cad99df44e5daa37664dc8eeb79cdeb3ec113f390247c5573e0713258e8a5da69f8e4078cf2535235e437db451803c2971c"); + } } @Test @@ -42,8 +47,7 @@ public void testIsIntialized() throws Exception { Assert.assertFalse(xmlFormatter.isInitialized()); final File targetDir = new File("target/testoutput"); targetDir.mkdirs(); - xmlFormatter.setFilename("src/test/resources/xml.properties"); - xmlFormatter.init(null, null); + xmlFormatter.init(new HashMap(), new AbstractFormatterTest.TestConfigurationSource(targetDir)); Assert.assertTrue(xmlFormatter.isInitialized()); } diff --git a/src/config/gson/formatter/json.properties b/src/config/gson/formatter/json.properties new file mode 100644 index 000000000..d778fb7b5 --- /dev/null +++ b/src/config/gson/formatter/json.properties @@ -0,0 +1,18 @@ +# +# Copyright 2010-2017. All work is copyrighted to their respective +# author(s), unless otherwise stated. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +prettyPrinting=true diff --git a/src/config/jsoup/formatter/html.properties b/src/config/jsoup/formatter/html.properties index 3d9b44e47..9c5807feb 100644 --- a/src/config/jsoup/formatter/html.properties +++ b/src/config/jsoup/formatter/html.properties @@ -17,6 +17,7 @@ charset=UTF-8 escapeMode=xhtml -indentAmount=1 +indentAmount=4 outlineMode=true pretty=true +syntax=html diff --git a/src/config/jsoup/formatter/xml.properties b/src/config/jsoup/formatter/xml.properties index 3d9b44e47..e99ff32c2 100644 --- a/src/config/jsoup/formatter/xml.properties +++ b/src/config/jsoup/formatter/xml.properties @@ -17,6 +17,7 @@ charset=UTF-8 escapeMode=xhtml -indentAmount=1 +indentAmount=4 outlineMode=true pretty=true +syntax=xml diff --git a/src/config/ph-css/formatter/css.properties b/src/config/ph-css/formatter/css.properties index 70481c830..492171ee8 100644 --- a/src/config/ph-css/formatter/css.properties +++ b/src/config/ph-css/formatter/css.properties @@ -15,5 +15,5 @@ # limitations under the License. # -indent=2 +indent=4 rgbAsHex=true