diff --git a/fj-doc-base-json/pom.xml b/fj-doc-base-json/pom.xml
index 87fd5d2b7..7e9618bc5 100644
--- a/fj-doc-base-json/pom.xml
+++ b/fj-doc-base-json/pom.xml
@@ -41,10 +41,10 @@
- com.fasterxml.jackson.core
- jackson-databind
-
-
+ org.fugerit.java
+ fj-xml-to-json
+
+
@@ -54,4 +54,4 @@
https://www.fugerit.org/perm/venus/
-
+
\ No newline at end of file
diff --git a/fj-doc-base-json/src/main/java/org/fugerit/java/doc/json/parse/DocJsonParser.java b/fj-doc-base-json/src/main/java/org/fugerit/java/doc/json/parse/DocJsonParser.java
index 472f705ad..bca11be66 100644
--- a/fj-doc-base-json/src/main/java/org/fugerit/java/doc/json/parse/DocJsonParser.java
+++ b/fj-doc-base-json/src/main/java/org/fugerit/java/doc/json/parse/DocJsonParser.java
@@ -7,16 +7,19 @@
import org.fugerit.java.doc.base.model.DocBase;
import org.fugerit.java.doc.base.parser.AbstractDocParser;
import org.fugerit.java.doc.base.parser.DocValidationResult;
-
-import com.fasterxml.jackson.databind.ObjectMapper;
+import org.fugerit.java.xml2json.XmlToJsonHandler;
public class DocJsonParser extends AbstractDocParser {
private DocObjectMapperHelper helper;
- public DocJsonParser() {
+ public DocJsonParser( XmlToJsonHandler handler ) {
super( DocFacadeSource.SOURCE_TYPE_JSON );
- this.helper = new DocObjectMapperHelper( new ObjectMapper() );
+ this.helper = new DocObjectMapperHelper( handler );
+ }
+
+ public DocJsonParser() {
+ this( new XmlToJsonHandler() );
}
private DocObjectMapperHelper getHelper() {
diff --git a/fj-doc-base-json/src/main/java/org/fugerit/java/doc/json/parse/DocJsonToXml.java b/fj-doc-base-json/src/main/java/org/fugerit/java/doc/json/parse/DocJsonToXml.java
index 3f1f1bf23..704271f93 100644
--- a/fj-doc-base-json/src/main/java/org/fugerit/java/doc/json/parse/DocJsonToXml.java
+++ b/fj-doc-base-json/src/main/java/org/fugerit/java/doc/json/parse/DocJsonToXml.java
@@ -2,16 +2,11 @@
import java.io.Reader;
import java.io.Writer;
-import java.util.Iterator;
-
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
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.facade.DocFacade;
-import org.fugerit.java.doc.base.parser.DocParserContext;
-import org.w3c.dom.Document;
+import org.fugerit.java.xml2json.XmlToJsonHandler;
import org.w3c.dom.Element;
import com.fasterxml.jackson.databind.JsonNode;
@@ -19,61 +14,19 @@
public class DocJsonToXml {
+ private XmlToJsonHandler handler;
+
public DocJsonToXml() {
this( new ObjectMapper() );
}
public DocJsonToXml(ObjectMapper mapper) {
- super();
- this.mapper = mapper;
+ this( new XmlToJsonHandler( mapper ) );
}
- private ObjectMapper mapper;
-
- private void iterateElement( JsonNode current, Document doc, Element tag ) throws ConfigException {
- JsonNode elementsNode = current.get( DocObjectMapperHelper.PROPERTY_ELEMENTS );
- if ( elementsNode != null ) {
- if ( elementsNode.isArray() ) {
- Iterator itElements = elementsNode.elements();
- while ( itElements.hasNext() ) {
- JsonNode currentElement = itElements.next();
- this.create(doc, tag, currentElement);
- }
- } else {
- throw new ConfigException( "Property must be an array : "+elementsNode );
- }
- }
- }
-
- private void iterateAttribute( JsonNode current, Element tag ) {
- Iterator itNames = current.fieldNames();
- while ( itNames.hasNext() ) {
- String currentName = itNames.next();
- if ( !DocObjectMapperHelper.isSpecialProperty( currentName ) ) {
- tag.setAttribute( currentName , current.get( currentName ).asText() );
- }
- }
- }
-
- private Element create( Document doc, Element parent, JsonNode current ) throws ConfigException {
- Element tag = null;
- JsonNode tagNode = current.get( DocObjectMapperHelper.PROPERTY_TAG );
- if ( tagNode == null ) {
- throw new ConfigException( "Tag node is null : "+DocObjectMapperHelper.PROPERTY_TAG );
- } else {
- String tagName = tagNode.asText();
- tag = doc.createElement( tagName );
- if ( parent != null ) {
- parent.appendChild( tag );
- }
- JsonNode textNode = current.get( DocObjectMapperHelper.PROPERTY_TEXT );
- if ( textNode != null ) {
- tag.appendChild( doc.createTextNode( textNode.asText() ) );
- }
- this.iterateElement(current, doc, tag);
- this.iterateAttribute(current, tag);
- }
- return tag;
+ public DocJsonToXml(XmlToJsonHandler handler) {
+ super();
+ this.handler = handler;
}
public void writerAsXml( Reader jsonReader, Writer writer ) throws ConfigException {
@@ -85,26 +38,18 @@ public void writerAsXml( Reader jsonReader, Writer writer ) throws ConfigExcepti
public Element convertToElement( Reader jsonReader ) throws ConfigException {
return ConfigException.get( () -> {
- JsonNode node = this.mapper.readTree( jsonReader );
+ JsonNode node = this.handler.getMapper().readTree( jsonReader );
return this.convert(node);
} );
}
public Element convert( JsonNode json ) throws ConfigException {
- return ConfigException.get( () -> {
- DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
- dbf.setNamespaceAware( true );
- dbf.setValidating( false );
- DocumentBuilder builder = dbf.newDocumentBuilder();
- Document doc = builder.newDocument();
- Element root = this.create(doc, null, json);
- root.setAttribute( "xmlns" , DocFacade.SYSTEM_ID );
- root.setAttribute( "xmlns:xsi" , "http://www.w3.org/2001/XMLSchema-instance" );
- String xsdVersion = DocObjectMapperHelper.findVersion(json, DocFacade.CURRENT_VERSION) ;
- root.setAttribute( "xsi:schemaLocation" , DocParserContext.createXsdVersionXmlns(xsdVersion) );
- return root;
- } );
-
+ Element root = this.handler.convert(json);
+ String xsdVersion = root.getAttribute( DocObjectMapperHelper.PROPERTY_XSD_VERSION );
+ if ( StringUtils.isNotEmpty( xsdVersion ) ) {
+ root.removeAttribute( DocObjectMapperHelper.PROPERTY_XSD_VERSION );
+ }
+ return root;
}
}
diff --git a/fj-doc-base-json/src/main/java/org/fugerit/java/doc/json/parse/DocObjectMapperHelper.java b/fj-doc-base-json/src/main/java/org/fugerit/java/doc/json/parse/DocObjectMapperHelper.java
index 2779416ce..11cef6646 100644
--- a/fj-doc-base-json/src/main/java/org/fugerit/java/doc/json/parse/DocObjectMapperHelper.java
+++ b/fj-doc-base-json/src/main/java/org/fugerit/java/doc/json/parse/DocObjectMapperHelper.java
@@ -4,100 +4,57 @@
import java.io.ByteArrayOutputStream;
import java.io.InputStreamReader;
import java.io.Reader;
+import java.io.StringReader;
+import java.io.StringWriter;
+import java.util.Arrays;
import java.util.HashSet;
-import java.util.Iterator;
-import java.util.Properties;
import java.util.Set;
-import org.fugerit.java.core.lang.helpers.StringUtils;
import org.fugerit.java.core.xml.dom.DOMIO;
import org.fugerit.java.doc.base.config.DocException;
-import org.fugerit.java.doc.base.facade.DocFacade;
import org.fugerit.java.doc.base.model.DocBase;
-import org.fugerit.java.doc.base.parser.DocParserContext;
import org.fugerit.java.doc.base.parser.DocValidationResult;
import org.fugerit.java.doc.base.xml.DocXmlParser;
+import org.fugerit.java.xml2json.XmlToJsonConverter;
+import org.fugerit.java.xml2json.XmlToJsonHandler;
import org.w3c.dom.Element;
-import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class DocObjectMapperHelper {
+
+ private XmlToJsonHandler handler;
- public DocObjectMapperHelper(ObjectMapper mapper) {
+ public DocObjectMapperHelper(XmlToJsonHandler handler) {
super();
- this.mapper = mapper;
+ this.handler = handler;
}
-
- private ObjectMapper mapper;
- public static final String PROPERTY_TAG = "_t";
+ public DocObjectMapperHelper(ObjectMapper mapper) {
+ this( new XmlToJsonHandler( mapper ) );
+ }
+
+ public static final String PROPERTY_TAG = XmlToJsonConverter.DEF_PROPERTY_TAG;
- public static final String PROPERTY_TEXT = "_v";
+ public static final String PROPERTY_TEXT = XmlToJsonConverter.DEF_PROPERTY_TEXT;
- public static final String PROPERTY_ELEMENTS = "_e";
+ public static final String PROPERTY_ELEMENTS = XmlToJsonConverter.DEF_PROPERTY_ELEMENTS;
public static final String PROPERTY_XSD_VERSION = "xsd-version";
- private static final Set SPECIAL_PROPERTY_NAMES = new HashSet<>();
- static {
- SPECIAL_PROPERTY_NAMES.add( PROPERTY_TAG );
- SPECIAL_PROPERTY_NAMES.add( PROPERTY_TEXT );
- SPECIAL_PROPERTY_NAMES.add( PROPERTY_ELEMENTS );
- SPECIAL_PROPERTY_NAMES.add( PROPERTY_XSD_VERSION );
- }
-
+ private static final Set SPECIAL_PROPERTY_NAMES = new HashSet<>( Arrays.asList( PROPERTY_TAG, PROPERTY_ELEMENTS, PROPERTY_TAG, PROPERTY_XSD_VERSION ) );
+
public static boolean isSpecialProperty( String propertyName ) {
return SPECIAL_PROPERTY_NAMES.contains(propertyName);
}
-
- public static String findVersion( JsonNode root, String def ) {
- String res = def;
- JsonNode xsdVersion = root.get( PROPERTY_XSD_VERSION );
- if ( xsdVersion != null ) {
- res = xsdVersion.asText();
- }
- return res;
- }
-
- private void handleElement( JsonNode node, DocParserContext context ) {
- Iterator fieldsNames = node.fieldNames();
- Properties props = new Properties();
- String qName = null;
- String text = null;
- Iterator elements = null;
- while ( fieldsNames.hasNext() ) {
- String currentName = fieldsNames.next();
- JsonNode currentValue = node.get( currentName );
- if ( PROPERTY_TEXT.equalsIgnoreCase( currentName ) ) {
- text = currentValue.asText();
- } else if ( PROPERTY_TAG.equalsIgnoreCase( currentName ) ) {
- qName = currentValue.asText();
- } else if ( PROPERTY_ELEMENTS.equalsIgnoreCase( currentName ) ) {
- elements = currentValue.elements();
- } else {
- props.setProperty( currentName , currentValue.asText() );
- }
- }
- context.handleStartElement(qName, props);
- if ( StringUtils.isNotEmpty( text ) ) {
- context.handleText(text);
- }
- if ( elements != null ) {
- while ( elements.hasNext() ) {
- this.handleElement( elements.next() , context );
- }
- }
- context.handleEndElement(qName);
- }
public DocValidationResult validateWorkerResult(Reader reader, boolean parseVersion) throws DocException {
return DocException.get( () -> {
DocValidationResult result = DocValidationResult.newDefaultNotDefinedResult();
- DocJsonToXml convert = new DocJsonToXml( this.mapper );
+ DocJsonToXml convert = new DocJsonToXml( this.handler.getMapper() );
Element root = convert.convertToElement( reader );
try ( ByteArrayOutputStream buffer = new ByteArrayOutputStream() ) {
DOMIO.writeDOMIndent(root, buffer);
@@ -120,14 +77,15 @@ public DocValidationResult validateWorkerResult(Reader reader, boolean parseVers
public DocBase parse(Reader reader) throws DocException {
return DocException.get( () -> {
- DocParserContext context = new DocParserContext();
- context.startDocument();
- JsonNode root = this.mapper.readTree( reader );
- this.handleElement(root, context);
- context.endDocument();
+ DocBase docBase = null;
+ try ( StringWriter writer = new StringWriter() ) {
+ this.handler.writerAsXml( reader , writer );
+ try ( StringReader xml = new StringReader( writer.toString() ) ) {
+ DocXmlParser parser = new DocXmlParser();
+ docBase = parser.parse( xml );
+ }
+ }
log.debug( "Parse done!" );
- DocBase docBase = context.getDocBase();
- docBase.setXsdVersion( findVersion(root, DocFacade.CURRENT_VERSION) );
return docBase;
});
}
diff --git a/fj-doc-base-json/src/main/java/org/fugerit/java/doc/json/parse/DocXmlToJson.java b/fj-doc-base-json/src/main/java/org/fugerit/java/doc/json/parse/DocXmlToJson.java
index d2f8d8659..8fdca16e7 100644
--- a/fj-doc-base-json/src/main/java/org/fugerit/java/doc/json/parse/DocXmlToJson.java
+++ b/fj-doc-base-json/src/main/java/org/fugerit/java/doc/json/parse/DocXmlToJson.java
@@ -1,72 +1,31 @@
package org.fugerit.java.doc.json.parse;
import java.io.Reader;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Properties;
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.core.xml.dom.DOMUtils;
+import org.fugerit.java.xml2json.XmlToJsonHandler;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
-import org.w3c.dom.Node;
-import org.w3c.dom.NodeList;
-import org.w3c.dom.Text;
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;
public class DocXmlToJson {
+ private XmlToJsonHandler hanlder;
+
public DocXmlToJson() {
this( new ObjectMapper() );
}
public DocXmlToJson(ObjectMapper mapper) {
- super();
- this.mapper = mapper;
+ this( new XmlToJsonHandler( mapper ) );
}
-
- private ObjectMapper mapper;
-
- private ObjectNode create( Element currentTag, ObjectNode currentNode ) throws Exception {
- // mapping normal properties
- Properties props = DOMUtils.attributesToProperties( currentTag );
- for ( Object k : props.keySet() ) {
- String key = String.valueOf( k );
- currentNode.put( key , props.getProperty( key ));
- }
- // adding special properties
- currentNode.put( DocObjectMapperHelper.PROPERTY_TAG , currentTag.getTagName() );
- if ( currentTag.hasChildNodes() ) {
- NodeList list = currentTag.getChildNodes();
- List kids = new ArrayList<>();
- StringBuilder textBuffer = new StringBuilder();
- for ( int k=0; k this.create( root, this.mapper.createObjectNode() ) );
+ return ConfigException.get( () -> this.hanlder.convert( root ) );
}
}
diff --git a/fj-doc-base-json/src/test/java/test/org/fugerit/java/doc/json/parse/TestJsonParser.java b/fj-doc-base-json/src/test/java/test/org/fugerit/java/doc/json/parse/TestJsonParser.java
index 71d272c9b..679139fc0 100644
--- a/fj-doc-base-json/src/test/java/test/org/fugerit/java/doc/json/parse/TestJsonParser.java
+++ b/fj-doc-base-json/src/test/java/test/org/fugerit/java/doc/json/parse/TestJsonParser.java
@@ -16,11 +16,14 @@
import org.fugerit.java.doc.base.typehandler.markdown.SimpleMarkdownExtTypeHandler;
import org.fugerit.java.doc.json.parse.DocJsonFacade;
import org.fugerit.java.doc.json.parse.DocJsonParser;
+import org.fugerit.java.doc.json.parse.DocObjectMapperHelper;
import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
public class TestJsonParser {
public static final boolean VALID = true;
@@ -75,6 +78,11 @@ private boolean parseWorker( String path ) {
public void testParse01() {
Assert.assertTrue( this.parseWorker( "doc_test_01" ) );
}
+
+ @Test
+ public void testParse02() {
+ Assert.assertTrue( this.parseWorker( "doc_test_02" ) );
+ }
@Test
public void testValidateOk01() {
@@ -90,7 +98,7 @@ public void testValidateOk01ParseVersion() {
public void testValidateOk02ParseVersion() {
Assert.assertTrue( this.validateWorker( "doc_test_02", VALID, NO_EXCEPTION, true ) );
}
-
+
@Test
public void testValidateKo02() {
Assert.assertFalse( this.validateWorker( "doc_test_02_ko", NOT_VALID, NO_EXCEPTION ) );
@@ -106,4 +114,11 @@ public void testFacade() {
} );
}
+ @Test
+ public void testHelper() {
+ Assert.assertTrue( DocObjectMapperHelper.isSpecialProperty( DocObjectMapperHelper.PROPERTY_TAG ) );
+ Assert.assertFalse( DocObjectMapperHelper.isSpecialProperty( "aaa" ) );
+ Assert.assertNotNull( new DocObjectMapperHelper( new ObjectMapper() ) );
+ }
+
}
diff --git a/fj-doc-base-json/xml_conversion.md b/fj-doc-base-json/xml_conversion.md
index 905315c50..200255856 100644
--- a/fj-doc-base-json/xml_conversion.md
+++ b/fj-doc-base-json/xml_conversion.md
@@ -1,12 +1,7 @@
# XML conversion conventions
-When converting to json (or yaml) format, some conventions has been assumed.
+As of version fj-doc-base-json 3.1.0 the conversion is made through the project [fj-xml-to-json](https://github.com/fugerit-org/fj-xml-to-json).
-A) Three special properties are used to handle some conditions:
-1. "_t" : property is used for storing the original element name (for instance if we are storing a `` element, we will have a `"_t":"para"` property)
-2. "_v" : property is used for storing text context (for instance if we are storing a `my text` element, we will have a `"_v":"my text"` property
-3. "_e" : property is used for storing child elements as an array. (for instance if we are storing a ` | |
` we will have a `"_e":[{"_t":"cell"}.{"_t":"cell"}]` property.
+see [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.
-B) All other xml attributes are stored using the corresponding property name. (for instance `my text` will convert to `{"_t":"para","_v":"my text","style":"bold"}`
-
-C) The property `xsd-version` may be used in the root of the three to store the reference to xsd version to use `{"xsd-version":"1-10","_t":"doc","_e":[...]}` (alternatively the standard properties `"xmlns:xsi"`, `"xsi:schemaLocation"`, `"xmlns"` can be used)
+It should be backward compatible.
\ No newline at end of file
diff --git a/fj-doc-base-yaml/src/main/java/org/fugerit/java/doc/yaml/parse/DocYamlParser.java b/fj-doc-base-yaml/src/main/java/org/fugerit/java/doc/yaml/parse/DocYamlParser.java
index 5ee5999dd..e139ccb6b 100644
--- a/fj-doc-base-yaml/src/main/java/org/fugerit/java/doc/yaml/parse/DocYamlParser.java
+++ b/fj-doc-base-yaml/src/main/java/org/fugerit/java/doc/yaml/parse/DocYamlParser.java
@@ -8,17 +8,21 @@
import org.fugerit.java.doc.base.parser.AbstractDocParser;
import org.fugerit.java.doc.base.parser.DocValidationResult;
import org.fugerit.java.doc.json.parse.DocObjectMapperHelper;
+import org.fugerit.java.xml2json.XmlToJsonHandler;
-import com.fasterxml.jackson.databind.ObjectMapper;
-import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
+import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
public class DocYamlParser extends AbstractDocParser {
private DocObjectMapperHelper helper;
- public DocYamlParser() {
+ public DocYamlParser( XmlToJsonHandler handler ) {
super( DocFacadeSource.SOURCE_TYPE_YAML );
- this.helper = new DocObjectMapperHelper( new ObjectMapper( new YAMLFactory() ) );
+ this.helper = new DocObjectMapperHelper( handler );
+ }
+
+ public DocYamlParser() {
+ this( new XmlToJsonHandler( new YAMLMapper() ) );
}
private DocObjectMapperHelper getHelper() {
diff --git a/pom.xml b/pom.xml
index ea81da926..ded96801a 100644
--- a/pom.xml
+++ b/pom.xml
@@ -27,6 +27,7 @@
8.4.1
${project.version}
+ 0.1.0
4.0.1
${poi-version}
@@ -203,6 +204,12 @@
fj-doc-sample
${fj-doc-version}
+
+
+ org.fugerit.java
+ fj-xml-to-json
+ ${fj-xml-to-json-version}
+
org.apache.poi