From ffdfa5e14c88e3b86fc5525583b0f2cb1012b27e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A1udio=20Gomes?= Date: Sun, 17 Mar 2024 09:24:26 -0300 Subject: [PATCH] add some singleton --- .../pirilampo/core/compilers/Compiler.java | 6 ------ .../core/compilers/FolderToHTMLCompiler.java | 12 ++++-------- .../pirilampo/core/parsers/ImageParser.java | 5 +++++ .../pirilampo/core/parsers/IndexParser.java | 4 ++-- .../pirilampo/core/parsers/JsonParser.java | 14 ++++++++++++++ .../pirilampo/core/parsers/MarkdownParser.java | 6 +++++- .../pirilampo/core/parsers/TextParser.java | 6 ++---- .../pirilampo/core/parsers/ImageParserTest.java | 4 +--- .../pirilampo/core/parsers/MarkdownParserTest.java | 4 +--- 9 files changed, 34 insertions(+), 27 deletions(-) create mode 100644 core/src/main/java/com/github/clagomess/pirilampo/core/parsers/JsonParser.java diff --git a/core/src/main/java/com/github/clagomess/pirilampo/core/compilers/Compiler.java b/core/src/main/java/com/github/clagomess/pirilampo/core/compilers/Compiler.java index e927b5c..532e81b 100644 --- a/core/src/main/java/com/github/clagomess/pirilampo/core/compilers/Compiler.java +++ b/core/src/main/java/com/github/clagomess/pirilampo/core/compilers/Compiler.java @@ -1,7 +1,5 @@ package com.github.clagomess.pirilampo.core.compilers; -import com.fasterxml.jackson.core.JsonGenerator; -import com.fasterxml.jackson.databind.ObjectMapper; import com.github.clagomess.pirilampo.core.dto.FeatureMetadataDto; import com.github.clagomess.pirilampo.core.dto.ParametersDto; import com.github.clagomess.pirilampo.core.fi.UIProgressFI; @@ -29,10 +27,6 @@ public abstract class Compiler { @Setter protected float progressCount = 0; - protected final ObjectMapper mapper = new ObjectMapper() {{ - disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET); - }}; - private final Pattern pFeatureExt = Pattern.compile("\\.feature$", Pattern.CASE_INSENSITIVE); public String getFeatureExtension(File f){ Matcher matcher = pFeatureExt.matcher(f.getName()); diff --git a/core/src/main/java/com/github/clagomess/pirilampo/core/compilers/FolderToHTMLCompiler.java b/core/src/main/java/com/github/clagomess/pirilampo/core/compilers/FolderToHTMLCompiler.java index 75f7146..81dac59 100644 --- a/core/src/main/java/com/github/clagomess/pirilampo/core/compilers/FolderToHTMLCompiler.java +++ b/core/src/main/java/com/github/clagomess/pirilampo/core/compilers/FolderToHTMLCompiler.java @@ -5,10 +5,7 @@ import com.github.clagomess.pirilampo.core.dto.MenuDto; import com.github.clagomess.pirilampo.core.dto.ParametersDto; import com.github.clagomess.pirilampo.core.enums.DiffEnum; -import com.github.clagomess.pirilampo.core.parsers.GherkinDocumentParser; -import com.github.clagomess.pirilampo.core.parsers.ImageParser; -import com.github.clagomess.pirilampo.core.parsers.IndexParser; -import com.github.clagomess.pirilampo.core.parsers.MenuParser; +import com.github.clagomess.pirilampo.core.parsers.*; import lombok.extern.slf4j.Slf4j; import org.apache.commons.io.FileUtils; @@ -27,7 +24,6 @@ @Slf4j public class FolderToHTMLCompiler extends Compiler implements ArtifactCompiler { private final PropertiesCompiler propertiesCompiler = new PropertiesCompiler(); - private final ImageParser imageParser = new ImageParser(); private final ParametersDto parameters; private final IndexParser indexParser = new IndexParser(); protected final MenuParser menuParser; @@ -104,7 +100,7 @@ protected void buildSidebar(PrintWriter out){ if(parameters.getProjectLogo() != null){ out.print(""); }else{ out.print(String.format( @@ -134,12 +130,12 @@ protected void buildMenu(PrintWriter out) throws IOException { if(menuParser.getMenu().getChildren().isEmpty()) { out.print("createMenuItem(document.getElementsByClassName('sidebar-nav')[0], "); - mapper.writeValue(out, menuParser.getMenu()); + JsonParser.getInstance().writeValue(out, menuParser.getMenu()); out.println(");"); } else { for(MenuDto menu : menuParser.getMenu().getChildren()) { out.print("createMenuItem(document.getElementsByClassName('sidebar-nav')[0], "); - mapper.writeValue(out, menu); + JsonParser.getInstance().writeValue(out, menu); out.println(");"); } } diff --git a/core/src/main/java/com/github/clagomess/pirilampo/core/parsers/ImageParser.java b/core/src/main/java/com/github/clagomess/pirilampo/core/parsers/ImageParser.java index 7b94d54..eb5cfa7 100644 --- a/core/src/main/java/com/github/clagomess/pirilampo/core/parsers/ImageParser.java +++ b/core/src/main/java/com/github/clagomess/pirilampo/core/parsers/ImageParser.java @@ -2,6 +2,7 @@ import com.github.clagomess.pirilampo.core.compilers.Compiler; import com.github.clagomess.pirilampo.core.dto.ParametersDto; +import lombok.Getter; import lombok.extern.slf4j.Slf4j; import java.io.*; @@ -11,6 +12,10 @@ @Slf4j public class ImageParser extends Compiler { + @Getter + private static final ImageParser instance = new ImageParser(); + private ImageParser(){} + public void parse(PrintWriter out, ParametersDto parameters, File feature, String fileName){ File file = getAbsolutePathFile(parameters, feature, fileName); diff --git a/core/src/main/java/com/github/clagomess/pirilampo/core/parsers/IndexParser.java b/core/src/main/java/com/github/clagomess/pirilampo/core/parsers/IndexParser.java index 0e53224..9e7d9c5 100644 --- a/core/src/main/java/com/github/clagomess/pirilampo/core/parsers/IndexParser.java +++ b/core/src/main/java/com/github/clagomess/pirilampo/core/parsers/IndexParser.java @@ -62,12 +62,12 @@ public void putFeaturePhrase(String featureId, String rawPhrase){ public void buildIndex(PrintWriter out) throws IOException { out.print("let indexPhrases = "); - mapper.writeValue(out, phrases.entrySet().stream() + JsonParser.getInstance().writeValue(out, phrases.entrySet().stream() .collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey))); out.println(";"); out.print("let indexMap = "); - mapper.writeValue(out, index); + JsonParser.getInstance().writeValue(out, index); out.println(";"); } } diff --git a/core/src/main/java/com/github/clagomess/pirilampo/core/parsers/JsonParser.java b/core/src/main/java/com/github/clagomess/pirilampo/core/parsers/JsonParser.java new file mode 100644 index 0000000..7801f01 --- /dev/null +++ b/core/src/main/java/com/github/clagomess/pirilampo/core/parsers/JsonParser.java @@ -0,0 +1,14 @@ +package com.github.clagomess.pirilampo.core.parsers; + +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.databind.ObjectMapper; +import lombok.Getter; + +public class JsonParser extends ObjectMapper { + @Getter + private static final JsonParser instance = new JsonParser(); + + private JsonParser() { + disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET); + } +} diff --git a/core/src/main/java/com/github/clagomess/pirilampo/core/parsers/MarkdownParser.java b/core/src/main/java/com/github/clagomess/pirilampo/core/parsers/MarkdownParser.java index 934274e..0580beb 100644 --- a/core/src/main/java/com/github/clagomess/pirilampo/core/parsers/MarkdownParser.java +++ b/core/src/main/java/com/github/clagomess/pirilampo/core/parsers/MarkdownParser.java @@ -1,5 +1,6 @@ package com.github.clagomess.pirilampo.core.parsers; +import lombok.Getter; import lombok.extern.slf4j.Slf4j; import org.commonmark.node.Document; import org.commonmark.node.Node; @@ -17,7 +18,10 @@ public class MarkdownParser { private final Parser parser = Parser.builder().build(); private final HtmlRenderer renderer; - public MarkdownParser() { + @Getter + private static final MarkdownParser instance = new MarkdownParser(); + + private MarkdownParser() { this.renderer = HtmlRenderer.builder() .nodeRendererFactory(SkipParentWrapperParagraphsRenderer::new) .build(); diff --git a/core/src/main/java/com/github/clagomess/pirilampo/core/parsers/TextParser.java b/core/src/main/java/com/github/clagomess/pirilampo/core/parsers/TextParser.java index c21b9d5..ec9f285 100644 --- a/core/src/main/java/com/github/clagomess/pirilampo/core/parsers/TextParser.java +++ b/core/src/main/java/com/github/clagomess/pirilampo/core/parsers/TextParser.java @@ -13,8 +13,6 @@ import java.util.regex.Pattern; public class TextParser extends Compiler { - private final MarkdownParser markdownParser = new MarkdownParser(); - private final ImageParser imageParser = new ImageParser(); private final IndexParser indexParser; private final ParametersDto parameters; @@ -44,7 +42,7 @@ public void format(PrintWriter out, String txtRaw, boolean makdown){ return; } - if(makdown) txt = markdownParser.build(txt); + if(makdown) txt = MarkdownParser.getInstance().build(txt); String img = "

"; txt = txt.replaceAll("", img); @@ -96,7 +94,7 @@ protected void findAndReplaceImagesSrcAndWriteOut(PrintWriter out, String txt){ out.write(txt, position, groupPosition - position); out.write("src=\""); - imageParser.parse(out, parameters, feature, mImgSrc.group(1)); + ImageParser.getInstance().parse(out, parameters, feature, mImgSrc.group(1)); out.write("\""); position = groupPosition + group.length(); diff --git a/core/src/test/java/com/github/clagomess/pirilampo/core/parsers/ImageParserTest.java b/core/src/test/java/com/github/clagomess/pirilampo/core/parsers/ImageParserTest.java index 41316ad..31d2d9d 100644 --- a/core/src/test/java/com/github/clagomess/pirilampo/core/parsers/ImageParserTest.java +++ b/core/src/test/java/com/github/clagomess/pirilampo/core/parsers/ImageParserTest.java @@ -14,8 +14,6 @@ @Slf4j public class ImageParserTest extends Common { - private final ImageParser imageParser = new ImageParser(); - @ParameterizedTest @CsvSource(value = { "true$smallest.png$data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQAAAAA3bvkkAAAACklEQVR4AWNgAAAAAgABc3UBGAAAAABJRU5ErkJggg==", @@ -33,7 +31,7 @@ public void parse(Boolean embedded, String filename, String expected){ StringWriter sw = new StringWriter(); try (PrintWriter out = new PrintWriter(sw)){ - imageParser.parse(out, parameters, featureFile, filename); + ImageParser.getInstance().parse(out, parameters, featureFile, filename); assertEquals(expected, sw.toString()); } diff --git a/core/src/test/java/com/github/clagomess/pirilampo/core/parsers/MarkdownParserTest.java b/core/src/test/java/com/github/clagomess/pirilampo/core/parsers/MarkdownParserTest.java index eb86c82..47bdcd2 100644 --- a/core/src/test/java/com/github/clagomess/pirilampo/core/parsers/MarkdownParserTest.java +++ b/core/src/test/java/com/github/clagomess/pirilampo/core/parsers/MarkdownParserTest.java @@ -7,13 +7,11 @@ @Slf4j public class MarkdownParserTest { - private final MarkdownParser markdownParser = new MarkdownParser(); - @Test public void build(){ assertEquals( "a: xx_a", - markdownParser.build("a: [xx_a](xx_a.html)") + MarkdownParser.getInstance().build("a: [xx_a](xx_a.html)") ); } }