Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create function to clean xml white nodes (8.4.0) #56

Merged
merged 3 commits into from
Sep 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [8.4.0] - 2023-09-30

### Added

- transformer config facility
- xml white space remove utility
- StreamHelper.resolveReader() method

### Changed

- fj-bom version set to 1.4.7
Expand Down
2 changes: 1 addition & 1 deletion fj-core-jvfs/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>org.fugerit.java</groupId>
<artifactId>fj-lib</artifactId>
<version>8.4.0-SNAPSHOT</version>
<version>8.4.0</version>
</parent>

<name>fj-core-jvfs</name>
Expand Down
2 changes: 1 addition & 1 deletion fj-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.fugerit.java</groupId>
<artifactId>fj-lib</artifactId>
<version>8.4.0-SNAPSHOT</version>
<version>8.4.0</version>
</parent>

<name>fj-core</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.io.Flushable;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
Expand Down Expand Up @@ -63,6 +64,10 @@ private StreamHelper() {}

public static final String PATH_JNDI = MODE_JNDI+URL_HELPER;

public static Reader resolveReader( String path ) throws IOException {
return new InputStreamReader( resolveStream( path, null ) );
}

public static InputStream resolveStream( String path ) throws IOException {
return resolveStream( path, null );
}
Expand Down
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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@ public static TransformerFactory newSafeTransformerFactory( Properties features
public static TransformerFactory newSafeTransformerFactory() {
return newSafeTransformerFactory( NO_FEATURES );
}

public static Transformer newTransformerWithConfig(TransformerConfig config) throws XMLException {
Transformer transformer = newTransformer();
config.setAll(transformer);
return transformer;
}

public static Transformer newTransformer(Source source, TransformerConfig config) throws XMLException {
Transformer transformer = newTransformer( source );
config.setAll(transformer);
return transformer;
}

public static Transformer newTransformer(Source source) throws XMLException {
Transformer transformer = null;
Expand All @@ -61,7 +73,7 @@ public static Transformer newTransformer(Source source) throws XMLException {
}

public static Transformer newTransformer() throws XMLException {
return newTransformer(null);
return newTransformer( null );
}

}
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 fj-core/src/main/resources/core/xml/remove_whitespace_nodes.xslt
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>
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import org.fugerit.java.core.cfg.ConfigRuntimeException;
import org.fugerit.java.core.lang.helpers.BooleanUtils;
import org.fugerit.java.core.xml.TransformerConfig;
import org.fugerit.java.core.xml.TransformerXML;
import org.junit.Assert;
import org.junit.Test;
Expand Down Expand Up @@ -67,6 +68,19 @@ public void newTransformer() {
Assert.assertTrue( ok );
}

@Test
public void newTransformerConfig() {
boolean ok = false;
try {
Transformer transformer = TransformerXML.newTransformerWithConfig( TransformerConfig.newIndentConfig(null) );
log.info( "factory -> {}", transformer );
ok = transformer != null;
} catch (Exception e) {
this.failEx(e);
}
Assert.assertTrue( ok );
}

@Test
public void newTransformerSource() {
boolean ok = false;
Expand Down
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() );
}

}
27 changes: 27 additions & 0 deletions fj-core/src/test/resources/core/xml/dom/dom_clean1.xml
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>
2 changes: 1 addition & 1 deletion fj-tool/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.fugerit.java</groupId>
<artifactId>fj-lib</artifactId>
<version>8.4.0-SNAPSHOT</version>
<version>8.4.0</version>
</parent>

<name>fj-tool</name>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<relativePath></relativePath>
</parent>

<version>8.4.0-SNAPSHOT</version>
<version>8.4.0</version>
<packaging>pom</packaging>

<name>fj-lib</name>
Expand Down
Loading