-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
399 additions
and
34 deletions.
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
File renamed without changes.
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,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" | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
fj-doc-base-json/src/main/java/org/fugerit/java/doc/json/ng/DocJsonParserNG.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,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() ) ); | ||
} | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
fj-doc-base-json/src/main/java/org/fugerit/java/doc/json/ng/DocXmlToJsonNG.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,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() ) ); | ||
} | ||
|
||
|
||
|
||
} |
85 changes: 85 additions & 0 deletions
85
fj-doc-base-json/src/main/java/org/fugerit/java/doc/json/ng/XmlToJsonConverterNG.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,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; | ||
} | ||
|
||
|
||
|
||
} |
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
72 changes: 72 additions & 0 deletions
72
fj-doc-base-json/src/test/java/test/org/fugerit/java/doc/json/ng/TestJsonParserNG.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,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" ) ); | ||
} | ||
|
||
|
||
} |
Oops, something went wrong.