Skip to content

Commit ba8813a

Browse files
committed
[3.1.0] - 2023-10-01
1 parent 76c6be2 commit ba8813a

File tree

17 files changed

+399
-34
lines changed

17 files changed

+399
-34
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
## [3.1.0] - 2023-10-01
11+
1012
### Added
1113

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

1418
### Changed
1519

20+
- refactor of json and yaml format to use [fj-xml-to-json](https://github.com/fugerit-org/fj-xml-to-json)
1621
- fj-bom set to 1.4.7
1722
- jackon and opencsv version set in fj-bom
1823
- fj-version set to 8.4.1

fj-doc-base-json/README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,13 @@ All basic features are implemented (json parsing, conversion from and to xml)
1919
Disabled, native support will be added in a future release.
2020

2121
*Doc Json format*
22-
The xml and json format have inherent differences. this is why is important to read the [conversion conventions used](xml_conversion.md)
22+
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)
23+
24+
*Doc Json format NG*
25+
A alternate json format, defined Next Generation (NG) is under development.
26+
See the [conversion conventions used](src/main/docs/xml_conversion_ng.md)
2327

2428
Examples :
2529
* [sample json doc](src/test/resources/sample/doc_test_01.json)
30+
* [sample json ng doc](src/test/resources/sample/doc_test_01_ng.json)
2631
* [sample xml doc](src/test/resources/sample/doc_test_01.xml)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# XML conversion conventions Next Generation
2+
3+
As of version fj-doc-base-json 3.1.0 the json ng format is under development.
4+
5+
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.
6+
7+
## Version 1.0.0-rc.1
8+
9+
Here is a summary of the differences :
10+
11+
### medata info
12+
13+
info elements that were usually written in this verbose way :
14+
15+
{
16+
"_t" : "metadata",
17+
"_e" : [ {
18+
"name" : "margins",
19+
"_t" : "info",
20+
"_v" : "10;10;10;30"
21+
}, {
22+
"name" : "excel-table-id",
23+
"_t" : "info",
24+
"_v" : "excel-table=print"
25+
}, {
26+
"name" : "excel-width-multiplier",
27+
"_t" : "info",
28+
"_v" : "450"
29+
} ]
30+
}
31+
32+
33+
can now be summarized with a single json object node :
34+
35+
{
36+
"_t" : "metadata",
37+
"info" : {
38+
"margins" : "10;10;10;30",
39+
"excel-table-id" : "excel-table=print",
40+
"excel-width-multiplier" : "450"
41+
}
42+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.fugerit.java.doc.json.ng;
2+
3+
import org.fugerit.java.doc.base.facade.DocFacadeSource;
4+
import org.fugerit.java.doc.json.parse.DocJsonParser;
5+
import org.fugerit.java.xml2json.XmlToJsonHandler;
6+
7+
public class DocJsonParserNG extends DocJsonParser {
8+
9+
public DocJsonParserNG() {
10+
super(DocFacadeSource.SOURCE_TYPE_JSON_NG, new XmlToJsonHandler( new XmlToJsonConverterNG() ) );
11+
}
12+
13+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.fugerit.java.doc.json.ng;
2+
3+
import org.fugerit.java.doc.json.parse.DocXmlToJson;
4+
import org.fugerit.java.xml2json.XmlToJsonHandler;
5+
6+
public class DocXmlToJsonNG extends DocXmlToJson {
7+
8+
public DocXmlToJsonNG() {
9+
super(new XmlToJsonHandler( new XmlToJsonConverterNG() ) );
10+
}
11+
12+
13+
14+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package org.fugerit.java.doc.json.ng;
2+
3+
import java.util.Iterator;
4+
5+
import org.fugerit.java.core.cfg.ConfigException;
6+
import org.fugerit.java.doc.base.model.DocContainer;
7+
import org.fugerit.java.doc.base.model.DocInfo;
8+
import org.fugerit.java.xml2json.XmlToJsonConverter;
9+
import org.w3c.dom.Document;
10+
import org.w3c.dom.Element;
11+
import org.w3c.dom.Node;
12+
import org.w3c.dom.NodeList;
13+
14+
import com.fasterxml.jackson.databind.JsonNode;
15+
import com.fasterxml.jackson.databind.ObjectMapper;
16+
import com.fasterxml.jackson.databind.node.ArrayNode;
17+
import com.fasterxml.jackson.databind.node.ObjectNode;
18+
import com.fasterxml.jackson.databind.node.TextNode;
19+
20+
public class XmlToJsonConverterNG extends XmlToJsonConverter {
21+
22+
private boolean isMetaSection( String tagName ) {
23+
return DocContainer.TAG_NAME_META.equals( tagName ) || DocContainer.TAG_NAME_METADATA.equals( tagName );
24+
}
25+
26+
@Override
27+
public ObjectNode handleTag(ObjectMapper mapper, Element currentTag, ObjectNode currentNode) {
28+
String tagName = currentTag.getTagName();
29+
// special handling of metadata section
30+
if ( this.isMetaSection(tagName) ) {
31+
currentNode.set( this.getPropertyTag() , new TextNode( tagName ) );
32+
NodeList list = currentTag.getChildNodes();
33+
ArrayNode kidsNode = mapper.createArrayNode();
34+
ObjectNode infoNode = mapper.createObjectNode();
35+
for ( int k=0; k<list.getLength(); k++ ) {
36+
Node kidNode = list.item(k);
37+
if ( kidNode instanceof Element ) {
38+
Element kidTag = (Element)kidNode;
39+
if ( DocInfo.TAG_NAME.equals( kidTag.getTagName() ) ) {
40+
infoNode.set( kidTag.getAttribute( DocInfo.ATT_NAME ) , new TextNode( kidTag.getTextContent() ) );
41+
} else {
42+
kidsNode.add( super.handleTag(mapper, kidTag, mapper.createObjectNode() ) );
43+
}
44+
}
45+
}
46+
if ( !infoNode.isEmpty() ) {
47+
currentNode.set( DocInfo.TAG_NAME , infoNode );
48+
}
49+
currentNode.set( this.getPropertyElements(), kidsNode );
50+
} else {
51+
currentNode = super.handleTag(mapper, currentTag, currentNode);
52+
}
53+
return currentNode;
54+
}
55+
56+
@Override
57+
public Element handleNode(Document doc, Element parent, JsonNode current) throws ConfigException {
58+
Element tag = null;
59+
String tagName = this.getTagNameOrThrowException(current);
60+
if ( this.isMetaSection( tagName ) ) {
61+
tag = doc.createElement( tagName );
62+
ObjectNode infoNode = (ObjectNode)current.get( DocInfo.TAG_NAME );
63+
Iterator<String> itFields = infoNode.fieldNames();
64+
while ( itFields.hasNext() ) {
65+
String currentInfoKey = itFields.next();
66+
String currentInfoValue = ((TextNode)infoNode.get( currentInfoKey )).asText();
67+
Element infoTag = doc.createElement(DocInfo.TAG_NAME);
68+
infoTag.setAttribute( DocInfo.ATT_NAME , currentInfoKey );
69+
infoTag.setTextContent( currentInfoValue );
70+
tag.appendChild( infoTag );
71+
}
72+
if ( parent != null ) {
73+
parent.appendChild( tag );
74+
}
75+
this.iterateElement(current, doc, tag);
76+
this.addAttributes(current, tag);
77+
} else {
78+
tag = super.handleNode(doc, parent, current);
79+
}
80+
return tag;
81+
}
82+
83+
84+
85+
}

fj-doc-base-json/src/main/java/org/fugerit/java/doc/json/parse/DocJsonParser.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,15 @@ public class DocJsonParser extends AbstractDocParser {
1313

1414
private DocObjectMapperHelper helper;
1515

16-
public DocJsonParser( XmlToJsonHandler handler ) {
17-
super( DocFacadeSource.SOURCE_TYPE_JSON );
16+
protected DocJsonParser(int sourceType, XmlToJsonHandler handler ) {
17+
super(sourceType);
1818
this.helper = new DocObjectMapperHelper( handler );
1919
}
2020

21+
public DocJsonParser( XmlToJsonHandler handler ) {
22+
this( DocFacadeSource.SOURCE_TYPE_JSON, handler );
23+
}
24+
2125
public DocJsonParser() {
2226
this( new XmlToJsonHandler() );
2327
}

fj-doc-base-json/src/main/java/org/fugerit/java/doc/json/parse/DocJsonToXml.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
import org.fugerit.java.core.cfg.ConfigException;
77
import org.fugerit.java.core.lang.helpers.StringUtils;
88
import org.fugerit.java.core.xml.dom.DOMIO;
9+
import org.fugerit.java.doc.base.config.DocVersion;
10+
import org.fugerit.java.doc.base.facade.DocFacade;
11+
import org.fugerit.java.doc.base.parser.DocParserContext;
912
import org.fugerit.java.xml2json.XmlToJsonHandler;
1013
import org.w3c.dom.Element;
1114

@@ -43,12 +46,29 @@ public Element convertToElement( Reader jsonReader ) throws ConfigException {
4346
} );
4447
}
4548

49+
private static final String ATT_XMLNS = "xmlns";
50+
51+
private static final String ATT_XMLNS_XSI = "xmlns:xsi";
52+
53+
private static final String ATT_XSD_LOC = "xsi:schemaLocation";
54+
55+
private void setIfNotFound( Element tag, String name, String value ) {
56+
if ( StringUtils.isEmpty( tag.getAttribute( name ) ) ) {
57+
tag.setAttribute( name , value );
58+
}
59+
}
60+
4661
public Element convert( JsonNode json ) throws ConfigException {
4762
Element root = this.handler.convert(json);
4863
String xsdVersion = root.getAttribute( DocObjectMapperHelper.PROPERTY_XSD_VERSION );
4964
if ( StringUtils.isNotEmpty( xsdVersion ) ) {
5065
root.removeAttribute( DocObjectMapperHelper.PROPERTY_XSD_VERSION );
5166
}
67+
// finishing touches
68+
xsdVersion = StringUtils.valueWithDefault( xsdVersion , DocVersion.CURRENT_VERSION.stringVersion() );
69+
this.setIfNotFound(root, ATT_XMLNS, DocFacade.SYSTEM_ID);
70+
this.setIfNotFound(root, ATT_XMLNS_XSI, "http://www.w3.org/2001/XMLSchema-instance");
71+
this.setIfNotFound(root, ATT_XSD_LOC, DocParserContext.createXsdVersionXmlns( xsdVersion ));
5272
return root;
5373
}
5474

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package test.org.fugerit.java.doc.json.ng;
2+
3+
import java.io.File;
4+
import java.io.FileOutputStream;
5+
import java.io.InputStream;
6+
import java.io.InputStreamReader;
7+
8+
import org.fugerit.java.core.function.SafeFunction;
9+
import org.fugerit.java.core.lang.helpers.ClassHelper;
10+
import org.fugerit.java.doc.base.config.DocInput;
11+
import org.fugerit.java.doc.base.config.DocOutput;
12+
import org.fugerit.java.doc.base.config.DocTypeHandler;
13+
import org.fugerit.java.doc.base.model.DocBase;
14+
import org.fugerit.java.doc.base.model.DocInfo;
15+
import org.fugerit.java.doc.base.typehandler.markdown.SimpleMarkdownExtTypeHandler;
16+
import org.fugerit.java.doc.json.ng.DocJsonParserNG;
17+
import org.fugerit.java.doc.json.ng.DocXmlToJsonNG;
18+
import org.fugerit.java.doc.json.parse.DocJsonParser;
19+
import org.junit.Assert;
20+
import org.junit.Test;
21+
22+
import com.fasterxml.jackson.databind.JsonNode;
23+
24+
import lombok.extern.slf4j.Slf4j;
25+
26+
@Slf4j
27+
public class TestJsonParserNG {
28+
29+
private boolean parseWorker( String path, String checkInfoKey, String checkinfoValue ) {
30+
return SafeFunction.get( () -> {
31+
DocTypeHandler handler = SimpleMarkdownExtTypeHandler.HANDLER;
32+
try ( InputStream is = ClassHelper.loadFromDefaultClassLoader( "sample/"+path+".json" );
33+
FileOutputStream fos = new FileOutputStream( "target/"+path+"."+handler.getType() ) ) {
34+
DocJsonParser parser = new DocJsonParserNG();
35+
DocBase docBase = parser.parse(is);
36+
log.info( "docBase -> {}", docBase );
37+
DocInput input = DocInput.newInput( handler.getType(), docBase, null );
38+
DocOutput output = DocOutput.newOutput( fos );
39+
handler.handle( input, output );
40+
log.info( "info : {}", docBase.getInfo() );
41+
String infoValue = docBase.getStableInfo().getProperty( checkInfoKey );
42+
return checkinfoValue.equals( infoValue );
43+
}
44+
} );
45+
}
46+
47+
@Test
48+
public void testParse01() {
49+
Assert.assertTrue( this.parseWorker( "doc_test_01_ng", DocInfo.INFO_NAME_MARGINS, "10;10;10;30" ) );
50+
}
51+
52+
private boolean worker( String path ) {
53+
return SafeFunction.get( () -> {
54+
File outputFile = new File( "target/"+path+"_ng.json" );
55+
try ( InputStream is = ClassHelper.loadFromDefaultClassLoader( "sample/"+path+".xml" );
56+
FileOutputStream fos = new FileOutputStream( outputFile ) ) {
57+
DocXmlToJsonNG converter = new DocXmlToJsonNG();
58+
JsonNode tree = converter.convertToJsonNode( new InputStreamReader( is ) );
59+
log.info( "xml -> {}", tree);
60+
fos.write( tree.toPrettyString().getBytes() );
61+
return outputFile.exists();
62+
}
63+
} );
64+
}
65+
66+
@Test
67+
public void test01() {
68+
Assert.assertTrue( this.worker( "doc_test_01" ) );
69+
}
70+
71+
72+
}

0 commit comments

Comments
 (0)