Skip to content

Commit

Permalink
[3.1.0] - 2023-10-01
Browse files Browse the repository at this point in the history
  • Loading branch information
fugerit79 committed Oct 1, 2023
1 parent 76c6be2 commit ba8813a
Show file tree
Hide file tree
Showing 17 changed files with 399 additions and 34 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [3.1.0] - 2023-10-01

### Added

- new config attribute 'fop-suppress-events' top mod-fop
- fj-xml-to-json 0.1.1 dependency
- [next generation json format support](fj-doc-base-json/src/main/docs/xml_conversion_ng.md) [experimental]

### Changed

- refactor of json and yaml format to use [fj-xml-to-json](https://github.com/fugerit-org/fj-xml-to-json)
- fj-bom set to 1.4.7
- jackon and opencsv version set in fj-bom
- fj-version set to 8.4.1
Expand Down
7 changes: 6 additions & 1 deletion fj-doc-base-json/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@ All basic features are implemented (json parsing, conversion from and to xml)
Disabled, native support will be added in a future release.

*Doc Json format*
The xml and json format have inherent differences. this is why is important to read the [conversion conventions used](xml_conversion.md)
The xml and json format have inherent differences. this is why is important to read the [conversion conventions used](src/main/docs/xml_conversion.md)

*Doc Json format NG*
A alternate json format, defined Next Generation (NG) is under development.
See the [conversion conventions used](src/main/docs/xml_conversion_ng.md)

Examples :
* [sample json doc](src/test/resources/sample/doc_test_01.json)
* [sample json ng doc](src/test/resources/sample/doc_test_01_ng.json)
* [sample xml doc](src/test/resources/sample/doc_test_01.xml)
File renamed without changes.
42 changes: 42 additions & 0 deletions fj-doc-base-json/src/main/docs/xml_conversion_ng.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# XML conversion conventions Next Generation

As of version fj-doc-base-json 3.1.0 the json ng format is under development.

It add some customization to standard [xml_conversion on fj-xml-to-json](https://github.com/fugerit-org/fj-xml-to-json/blob/main/src/main/docs/xml_conversion.md) project.

## Version 1.0.0-rc.1

Here is a summary of the differences :

### medata info

info elements that were usually written in this verbose way :

{
"_t" : "metadata",
"_e" : [ {
"name" : "margins",
"_t" : "info",
"_v" : "10;10;10;30"
}, {
"name" : "excel-table-id",
"_t" : "info",
"_v" : "excel-table=print"
}, {
"name" : "excel-width-multiplier",
"_t" : "info",
"_v" : "450"
} ]
}


can now be summarized with a single json object node :

{
"_t" : "metadata",
"info" : {
"margins" : "10;10;10;30",
"excel-table-id" : "excel-table=print",
"excel-width-multiplier" : "450"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.fugerit.java.doc.json.ng;

import org.fugerit.java.doc.base.facade.DocFacadeSource;
import org.fugerit.java.doc.json.parse.DocJsonParser;
import org.fugerit.java.xml2json.XmlToJsonHandler;

public class DocJsonParserNG extends DocJsonParser {

public DocJsonParserNG() {
super(DocFacadeSource.SOURCE_TYPE_JSON_NG, new XmlToJsonHandler( new XmlToJsonConverterNG() ) );
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.fugerit.java.doc.json.ng;

import org.fugerit.java.doc.json.parse.DocXmlToJson;
import org.fugerit.java.xml2json.XmlToJsonHandler;

public class DocXmlToJsonNG extends DocXmlToJson {

public DocXmlToJsonNG() {
super(new XmlToJsonHandler( new XmlToJsonConverterNG() ) );
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package org.fugerit.java.doc.json.ng;

import java.util.Iterator;

import org.fugerit.java.core.cfg.ConfigException;
import org.fugerit.java.doc.base.model.DocContainer;
import org.fugerit.java.doc.base.model.DocInfo;
import org.fugerit.java.xml2json.XmlToJsonConverter;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.TextNode;

public class XmlToJsonConverterNG extends XmlToJsonConverter {

private boolean isMetaSection( String tagName ) {
return DocContainer.TAG_NAME_META.equals( tagName ) || DocContainer.TAG_NAME_METADATA.equals( tagName );
}

@Override
public ObjectNode handleTag(ObjectMapper mapper, Element currentTag, ObjectNode currentNode) {
String tagName = currentTag.getTagName();
// special handling of metadata section
if ( this.isMetaSection(tagName) ) {
currentNode.set( this.getPropertyTag() , new TextNode( tagName ) );
NodeList list = currentTag.getChildNodes();
ArrayNode kidsNode = mapper.createArrayNode();
ObjectNode infoNode = mapper.createObjectNode();
for ( int k=0; k<list.getLength(); k++ ) {
Node kidNode = list.item(k);
if ( kidNode instanceof Element ) {
Element kidTag = (Element)kidNode;
if ( DocInfo.TAG_NAME.equals( kidTag.getTagName() ) ) {
infoNode.set( kidTag.getAttribute( DocInfo.ATT_NAME ) , new TextNode( kidTag.getTextContent() ) );
} else {
kidsNode.add( super.handleTag(mapper, kidTag, mapper.createObjectNode() ) );
}
}
}
if ( !infoNode.isEmpty() ) {
currentNode.set( DocInfo.TAG_NAME , infoNode );
}
currentNode.set( this.getPropertyElements(), kidsNode );
} else {
currentNode = super.handleTag(mapper, currentTag, currentNode);
}
return currentNode;
}

@Override
public Element handleNode(Document doc, Element parent, JsonNode current) throws ConfigException {
Element tag = null;
String tagName = this.getTagNameOrThrowException(current);
if ( this.isMetaSection( tagName ) ) {
tag = doc.createElement( tagName );
ObjectNode infoNode = (ObjectNode)current.get( DocInfo.TAG_NAME );
Iterator<String> itFields = infoNode.fieldNames();
while ( itFields.hasNext() ) {
String currentInfoKey = itFields.next();
String currentInfoValue = ((TextNode)infoNode.get( currentInfoKey )).asText();
Element infoTag = doc.createElement(DocInfo.TAG_NAME);
infoTag.setAttribute( DocInfo.ATT_NAME , currentInfoKey );
infoTag.setTextContent( currentInfoValue );
tag.appendChild( infoTag );
}
if ( parent != null ) {
parent.appendChild( tag );
}
this.iterateElement(current, doc, tag);
this.addAttributes(current, tag);
} else {
tag = super.handleNode(doc, parent, current);
}
return tag;
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@ public class DocJsonParser extends AbstractDocParser {

private DocObjectMapperHelper helper;

public DocJsonParser( XmlToJsonHandler handler ) {
super( DocFacadeSource.SOURCE_TYPE_JSON );
protected DocJsonParser(int sourceType, XmlToJsonHandler handler ) {
super(sourceType);
this.helper = new DocObjectMapperHelper( handler );
}

public DocJsonParser( XmlToJsonHandler handler ) {
this( DocFacadeSource.SOURCE_TYPE_JSON, handler );
}

public DocJsonParser() {
this( new XmlToJsonHandler() );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import org.fugerit.java.core.cfg.ConfigException;
import org.fugerit.java.core.lang.helpers.StringUtils;
import org.fugerit.java.core.xml.dom.DOMIO;
import org.fugerit.java.doc.base.config.DocVersion;
import org.fugerit.java.doc.base.facade.DocFacade;
import org.fugerit.java.doc.base.parser.DocParserContext;
import org.fugerit.java.xml2json.XmlToJsonHandler;
import org.w3c.dom.Element;

Expand Down Expand Up @@ -43,12 +46,29 @@ public Element convertToElement( Reader jsonReader ) throws ConfigException {
} );
}

private static final String ATT_XMLNS = "xmlns";

private static final String ATT_XMLNS_XSI = "xmlns:xsi";

private static final String ATT_XSD_LOC = "xsi:schemaLocation";

private void setIfNotFound( Element tag, String name, String value ) {
if ( StringUtils.isEmpty( tag.getAttribute( name ) ) ) {
tag.setAttribute( name , value );
}
}

public Element convert( JsonNode json ) throws ConfigException {
Element root = this.handler.convert(json);
String xsdVersion = root.getAttribute( DocObjectMapperHelper.PROPERTY_XSD_VERSION );
if ( StringUtils.isNotEmpty( xsdVersion ) ) {
root.removeAttribute( DocObjectMapperHelper.PROPERTY_XSD_VERSION );
}
// finishing touches
xsdVersion = StringUtils.valueWithDefault( xsdVersion , DocVersion.CURRENT_VERSION.stringVersion() );
this.setIfNotFound(root, ATT_XMLNS, DocFacade.SYSTEM_ID);
this.setIfNotFound(root, ATT_XMLNS_XSI, "http://www.w3.org/2001/XMLSchema-instance");
this.setIfNotFound(root, ATT_XSD_LOC, DocParserContext.createXsdVersionXmlns( xsdVersion ));
return root;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package test.org.fugerit.java.doc.json.ng;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.fugerit.java.core.function.SafeFunction;
import org.fugerit.java.core.lang.helpers.ClassHelper;
import org.fugerit.java.doc.base.config.DocInput;
import org.fugerit.java.doc.base.config.DocOutput;
import org.fugerit.java.doc.base.config.DocTypeHandler;
import org.fugerit.java.doc.base.model.DocBase;
import org.fugerit.java.doc.base.model.DocInfo;
import org.fugerit.java.doc.base.typehandler.markdown.SimpleMarkdownExtTypeHandler;
import org.fugerit.java.doc.json.ng.DocJsonParserNG;
import org.fugerit.java.doc.json.ng.DocXmlToJsonNG;
import org.fugerit.java.doc.json.parse.DocJsonParser;
import org.junit.Assert;
import org.junit.Test;

import com.fasterxml.jackson.databind.JsonNode;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class TestJsonParserNG {

private boolean parseWorker( String path, String checkInfoKey, String checkinfoValue ) {
return SafeFunction.get( () -> {
DocTypeHandler handler = SimpleMarkdownExtTypeHandler.HANDLER;
try ( InputStream is = ClassHelper.loadFromDefaultClassLoader( "sample/"+path+".json" );
FileOutputStream fos = new FileOutputStream( "target/"+path+"."+handler.getType() ) ) {
DocJsonParser parser = new DocJsonParserNG();
DocBase docBase = parser.parse(is);
log.info( "docBase -> {}", docBase );
DocInput input = DocInput.newInput( handler.getType(), docBase, null );
DocOutput output = DocOutput.newOutput( fos );
handler.handle( input, output );
log.info( "info : {}", docBase.getInfo() );
String infoValue = docBase.getStableInfo().getProperty( checkInfoKey );
return checkinfoValue.equals( infoValue );
}
} );
}

@Test
public void testParse01() {
Assert.assertTrue( this.parseWorker( "doc_test_01_ng", DocInfo.INFO_NAME_MARGINS, "10;10;10;30" ) );
}

private boolean worker( String path ) {
return SafeFunction.get( () -> {
File outputFile = new File( "target/"+path+"_ng.json" );
try ( InputStream is = ClassHelper.loadFromDefaultClassLoader( "sample/"+path+".xml" );
FileOutputStream fos = new FileOutputStream( outputFile ) ) {
DocXmlToJsonNG converter = new DocXmlToJsonNG();
JsonNode tree = converter.convertToJsonNode( new InputStreamReader( is ) );
log.info( "xml -> {}", tree);
fos.write( tree.toPrettyString().getBytes() );
return outputFile.exists();
}
} );
}

@Test
public void test01() {
Assert.assertTrue( this.worker( "doc_test_01" ) );
}


}
Loading

0 comments on commit ba8813a

Please sign in to comment.