-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added - transformer config facility - xml white space remove utility - StreamHelper.resolveReader() method
- Loading branch information
Showing
9 changed files
with
261 additions
and
1 deletion.
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
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
74 changes: 74 additions & 0 deletions
74
fj-core/src/main/java/org/fugerit/java/core/xml/TransformerConfig.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,74 @@ | ||
package org.fugerit.java.core.xml; | ||
|
||
import java.util.Map; | ||
import java.util.Properties; | ||
import java.util.stream.Stream; | ||
|
||
import javax.xml.transform.OutputKeys; | ||
import javax.xml.transform.Transformer; | ||
|
||
public final class TransformerConfig { | ||
|
||
public static final String YES = "yes"; | ||
public static final String NO = "no"; | ||
|
||
private Properties outputProperties; | ||
|
||
private TransformerConfig() { | ||
this.outputProperties = new Properties(); | ||
} | ||
|
||
public static TransformerConfig newConfig() { | ||
return new TransformerConfig(); | ||
} | ||
|
||
public void setAll( Transformer t ) { | ||
t.setOutputProperties( this.outputProperties ); | ||
} | ||
|
||
public Stream<Map.Entry<Object, Object>> stream() { | ||
return this.outputProperties.entrySet().stream(); | ||
} | ||
|
||
public TransformerConfig add( String key, String value ) { | ||
this.outputProperties.setProperty(key, value); | ||
return this; | ||
} | ||
|
||
public TransformerConfig omitXmlDeclarationYes() { | ||
return this.add( OutputKeys.OMIT_XML_DECLARATION, YES ); | ||
} | ||
|
||
public TransformerConfig omitXmlDeclarationNo() { | ||
return this.add( OutputKeys.OMIT_XML_DECLARATION, NO ); | ||
} | ||
|
||
public TransformerConfig indentYes() { | ||
return this.add( OutputKeys.INDENT, YES ); | ||
} | ||
|
||
public TransformerConfig indentNo() { | ||
return this.add( OutputKeys.INDENT, NO ); | ||
} | ||
|
||
public TransformerConfig methodXml() { | ||
return this.method( "xml" ); | ||
} | ||
|
||
public TransformerConfig method( String value ) { | ||
return this.add( OutputKeys.METHOD, value ); | ||
} | ||
|
||
public TransformerConfig indentAmount( int indentAmount ) { | ||
return this.add( "{http://xml.apache.org/xslt}indent-amount", String.valueOf( indentAmount ) ); | ||
} | ||
|
||
public static TransformerConfig newIndentConfig( Integer indentAmount ) { | ||
TransformerConfig config = newConfig().indentYes().methodXml().omitXmlDeclarationYes(); | ||
if ( indentAmount != null ) { | ||
config = config.indentAmount( indentAmount.intValue() ); | ||
} | ||
return config; | ||
} | ||
|
||
} |
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
47 changes: 47 additions & 0 deletions
47
fj-core/src/main/java/org/fugerit/java/core/xml/XMLWhiteSpaceRemove.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,47 @@ | ||
package org.fugerit.java.core.xml; | ||
|
||
import java.io.Reader; | ||
import java.io.Writer; | ||
|
||
import javax.xml.transform.Result; | ||
import javax.xml.transform.Source; | ||
import javax.xml.transform.Transformer; | ||
import javax.xml.transform.dom.DOMSource; | ||
import javax.xml.transform.stream.StreamResult; | ||
import javax.xml.transform.stream.StreamSource; | ||
|
||
import org.fugerit.java.core.io.helper.StreamHelper; | ||
import org.w3c.dom.Node; | ||
|
||
public class XMLWhiteSpaceRemove { | ||
|
||
public static final String PATH_REMOVE_WHITESPACE_NODES_XSLT = StreamHelper.PATH_CLASSLOADER+"core/xml/remove_whitespace_nodes.xslt"; | ||
|
||
private XMLWhiteSpaceRemove() {} | ||
|
||
public static void cleanBlankNodesIndent2( Node node, Writer writer ) throws XMLException { | ||
cleanBlankNodes( node, writer, TransformerConfig.newIndentConfig( 2 )); | ||
} | ||
|
||
public static void cleanBlankNodes( Node node, Writer writer, TransformerConfig config ) throws XMLException { | ||
cleanBlankNodes( node, new StreamResult( writer ), config); | ||
} | ||
|
||
public static void cleanBlankNodes( Node node, Result result, TransformerConfig config ) throws XMLException { | ||
cleanBlankNodes( new DOMSource( node ), result, config); | ||
} | ||
|
||
public static void cleanBlankNodes( Source source, Result result, TransformerConfig config ) throws XMLException { | ||
cleanBlankNodes(source, result, config, PATH_REMOVE_WHITESPACE_NODES_XSLT); | ||
} | ||
|
||
public static void cleanBlankNodes( Source source, Result result, TransformerConfig config, String xlstPath ) throws XMLException { | ||
XMLException.apply( () -> { | ||
try ( Reader xlstClean = StreamHelper.resolveReader(xlstPath) ) { | ||
Transformer t = TransformerXML.newTransformer( new StreamSource( xlstClean ), config ); | ||
t.transform( source , result ); | ||
} | ||
}); | ||
} | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
fj-core/src/main/resources/core/xml/remove_whitespace_nodes.xslt
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,11 @@ | ||
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> | ||
<xsl:strip-space elements="*"/> | ||
<xsl:output method="xml" encoding="UTF-8"/> | ||
|
||
<xsl:template match="@*|node()"> | ||
<xsl:copy> | ||
<xsl:apply-templates select="@*|node()"/> | ||
</xsl:copy> | ||
</xsl:template> | ||
|
||
</xsl:stylesheet> |
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
62 changes: 62 additions & 0 deletions
62
fj-core/src/test/java/test/org/fugerit/java/core/xml/dom/TestDOMClean.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,62 @@ | ||
package test.org.fugerit.java.core.xml.dom; | ||
|
||
import java.io.File; | ||
import java.io.FileWriter; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.Writer; | ||
|
||
import org.fugerit.java.core.lang.helpers.ClassHelper; | ||
import org.fugerit.java.core.xml.TransformerConfig; | ||
import org.fugerit.java.core.xml.XMLException; | ||
import org.fugerit.java.core.xml.XMLWhiteSpaceRemove; | ||
import org.fugerit.java.core.xml.dom.DOMIO; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.w3c.dom.Document; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
public class TestDOMClean { | ||
|
||
private static final String INPUT_FILE_XML = "dom_clean1.xml"; | ||
|
||
private void testWorker( String testFile, File outFile, TransformerConfig config) throws IOException, XMLException { | ||
try ( InputStream is = ClassHelper.loadFromDefaultClassLoader( "core/xml/dom/"+testFile ); | ||
Writer writer = new FileWriter( outFile ) ) { | ||
Document doc = DOMIO.loadDOMDoc( is ); | ||
config.stream().forEach( e -> log.info( "outputProperty {} -> {}", e.getKey(), e.getValue() ) ); | ||
XMLWhiteSpaceRemove.cleanBlankNodes(doc, writer, TransformerConfig.newIndentConfig( 5 )); | ||
} | ||
} | ||
|
||
@Test | ||
public void testClean1() throws IOException, XMLException { | ||
String testFile = INPUT_FILE_XML; | ||
File outFile = new File( "target", "cleaned_a_"+testFile ); | ||
try ( InputStream is = ClassHelper.loadFromDefaultClassLoader( "core/xml/dom/"+testFile ); | ||
Writer writer = new FileWriter( outFile ) ) { | ||
Document doc = DOMIO.loadDOMDoc( is ); | ||
XMLWhiteSpaceRemove.cleanBlankNodesIndent2(doc, writer); | ||
} | ||
Assert.assertTrue( outFile.exists() ); | ||
} | ||
|
||
@Test | ||
public void testClean2() throws IOException, XMLException { | ||
String testFile = "dom_clean1.xml"; | ||
File outFile = new File( "target", "cleaned_b_"+testFile ); | ||
this.testWorker(testFile, outFile, TransformerConfig.newIndentConfig( 5 )); | ||
Assert.assertTrue( outFile.exists() ); | ||
} | ||
|
||
@Test | ||
public void testClean3() throws IOException, XMLException { | ||
String testFile = "dom_clean1.xml"; | ||
File outFile = new File( "target", "cleaned_c_"+testFile ); | ||
this.testWorker(testFile, outFile, TransformerConfig.newConfig().indentNo().omitXmlDeclarationNo()); | ||
Assert.assertTrue( outFile.exists() ); | ||
} | ||
|
||
} |
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,27 @@ | ||
<config> | ||
|
||
<bean id="4.1"> | ||
|
||
<field>field21</field> | ||
|
||
|
||
<labelKey>sec_41_Testlabelwith</labelKey> | ||
</bean> | ||
|
||
<bean id="4.2"> | ||
|
||
<field>field22</field> | ||
|
||
|
||
<labelKey>sec_42_Testlabel2</labelKey> | ||
</bean> | ||
|
||
<bean id="4.2"> | ||
|
||
<field>field23</field> | ||
|
||
|
||
<labelKey>sec_42_Testlabel4</labelKey> | ||
</bean> | ||
|
||
</config> |