Skip to content

Commit

Permalink
Add fj-xml-to-json dep
Browse files Browse the repository at this point in the history
  • Loading branch information
fugerit79 committed Oct 1, 2023
1 parent 873a369 commit 76c6be2
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 211 deletions.
10 changes: 5 additions & 5 deletions fj-doc-base-json/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<groupId>org.fugerit.java</groupId>
<artifactId>fj-xml-to-json</artifactId>
</dependency>

</dependencies>

<organization>
Expand All @@ -54,4 +54,4 @@

<url>https://www.fugerit.org/perm/venus/</url>

</project>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,78 +2,31 @@

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;
import com.fasterxml.jackson.databind.ObjectMapper;

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<JsonNode> 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<String> 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 {
Expand All @@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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<String> 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<String> fieldsNames = node.fieldNames();
Properties props = new Properties();
String qName = null;
String text = null;
Iterator<JsonNode> 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);
Expand All @@ -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;
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ObjectNode> kids = new ArrayList<>();
StringBuilder textBuffer = new StringBuilder();
for ( int k=0; k<list.getLength(); k++ ) {
Node currentTagChild = list.item( k );
if ( currentTagChild instanceof Element ) {
kids.add( this.create((Element)currentTagChild, this.mapper.createObjectNode()) );
} else if ( currentTagChild instanceof Text ) {
textBuffer.append( ((Text)currentTagChild).getTextContent() );
}
}
if ( !kids.isEmpty() ) {
ArrayNode kidsNode = this.mapper.createArrayNode();
for ( ObjectNode currentKid : kids ) {
kidsNode.add( currentKid );
}
currentNode.set( DocObjectMapperHelper.PROPERTY_ELEMENTS, kidsNode );
}
String text = textBuffer.toString();
if ( StringUtils.isNotEmpty( text ) ) {
currentNode.put( DocObjectMapperHelper.PROPERTY_TEXT, text );
}
}
return currentNode;

public DocXmlToJson(XmlToJsonHandler handler) {
super();
this.hanlder = handler;
}

public JsonNode convertToJsonNode( Reader xml ) throws ConfigException {
Expand All @@ -78,7 +37,7 @@ public JsonNode convertToJsonNode( Reader xml ) throws ConfigException {
}

public JsonNode convert( Element root ) throws ConfigException {
return ConfigException.get( () -> this.create( root, this.mapper.createObjectNode() ) );
return ConfigException.get( () -> this.hanlder.convert( root ) );
}

}
Loading

0 comments on commit 76c6be2

Please sign in to comment.