org.leibnizcenter.xml.TerseJson Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of xml-to-json Show documentation
Show all versions of xml-to-json Show documentation
Library for converting XML to JSON
The newest version!
package org.leibnizcenter.xml;
import com.google.gson.Gson;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import org.apache.xerces.dom.*;
import org.leibnizcenter.xml.helpers.XmlNodeToJsonElement;
import org.w3c.dom.*;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Maarten on 19/11/2015.
*/
public class TerseJson {
private final Options options;
public TerseJson() {
this.options = new Options();
}
public TerseJson(Options options) {
this.options = options;
}
/**
* First element must be a document node or element node
*
* @param reader JSON stream
* @return XML document
*/
public static Document parse(JsonReader reader) throws IOException, NotImplemented {
reader.beginArray();
int nodeType = reader.nextInt();
CoreDocumentImpl doc = new DocumentImpl();
if (nodeType == Node.ELEMENT_NODE) {
addInitialElement(reader, doc);
} else if (nodeType == Node.DOCUMENT_NODE) {
addInitialDocNode(doc, reader);
} else {
throw new IllegalStateException("Don't know how to handle root node with type " + nodeType);
}
reader.endArray();
return doc;
}
protected static void addInitialElement(JsonReader reader, CoreDocumentImpl doc) throws IOException, NotImplemented {
// [nodeType,tagName[,children,[attributes]]
String tagName = reader.nextString();
Element e = new ElementImpl(doc, tagName);
doc.appendChild(e);
if (!reader.peek().equals(JsonToken.END_ARRAY)) {
setChildren(doc, reader, e);
if (!reader.peek().equals(JsonToken.END_ARRAY)) {
setAttributes(reader, e);
}
}
}
protected static void addInitialDocNode(CoreDocumentImpl doc, JsonReader reader) throws IOException, NotImplemented {
reader.beginArray(); // Children nodes
while (!reader.peek().equals(JsonToken.END_ARRAY)) {
Node child = parseNextNode(
doc, reader
);
doc.appendChild(child);
}
reader.endArray();
}
public static Node parseNextNode(CoreDocumentImpl doc, JsonReader reader) throws IOException, NotImplemented {
if (reader.peek().equals(JsonToken.STRING)) {
return doc.createTextNode(reader.nextString());
} else {
reader.beginArray();
int nodeType = reader.nextInt();
Node node;
switch (nodeType) {
case Node.CDATA_SECTION_NODE:
// [nodeType, nodeVal]
String data = reader.nextString();
node = new CDATASectionImpl(doc, data);
break;
case Node.COMMENT_NODE:
// [nodeType, nodeVal]
String comment = reader.nextString();
node = new CommentImpl(doc, comment);
break;
case Node.PROCESSING_INSTRUCTION_NODE:
// [nodeType, nodeTarget,nodeVal]
String target = reader.nextString();
String val = reader.nextString();
node = new ProcessingInstructionImpl(doc, target, val);
break;
case Node.TEXT_NODE:
// Return just the string
node = new TextImpl(doc, reader.nextString());
break;
case Node.DOCUMENT_NODE:
// [nodeType, nodeChildren]
case Node.ATTRIBUTE_NODE:
// [key, value]
case Node.DOCUMENT_FRAGMENT_NODE: // XML doc without root node
throw new IllegalStateException();
case Node.ELEMENT_NODE:
// [nodeType,tagName[,children,[attributes]]
String tagName = reader.nextString();
Element e = new ElementImpl(doc, tagName);
node = e;
if (!reader.peek().equals(JsonToken.END_ARRAY)) {
setChildren(doc, reader, e);
if (!reader.peek().equals(JsonToken.END_ARRAY)) {
setAttributes(reader, e);
}
}
break;
case Node.DOCUMENT_TYPE_NODE:
case Node.NOTATION_NODE:
case Node.ENTITY_NODE:
case Node.ENTITY_REFERENCE_NODE:
//todo
// [nodeType,tagName[,attributes,[children]]
default:
throw new NotImplemented();
}
reader.endArray();
return node;
}
}
protected static void setChildren(CoreDocumentImpl doc, JsonReader reader, Element e) throws IOException, NotImplemented {
//Children
reader.beginArray();
while (!reader.peek().equals(JsonToken.END_ARRAY)) {
e.appendChild(parseNextNode(doc, reader));
}
reader.endArray();
}
protected static void setAttributes(JsonReader reader, Element e) throws IOException {
reader.beginArray();
while (!reader.peek().equals(JsonToken.END_ARRAY)) {
reader.beginArray();
String key = reader.nextString();
String value = reader.nextString();
e.setAttribute(key, value);
reader.endArray();
}
reader.endArray();
}
protected Object[] getChildren(Node n) throws NotImplemented {
switch (n.getNodeType()) {
case Node.CDATA_SECTION_NODE:
case Node.COMMENT_NODE:
case Node.TEXT_NODE:
case Node.PROCESSING_INSTRUCTION_NODE:
case Node.ATTRIBUTE_NODE:
//These nodes only contain text
// PIs only have a target and a content
// attributes only have a key and a value
throw new IllegalStateException("Should not try to get children of node " + n.toString());
case Node.DOCUMENT_NODE:
case Node.ELEMENT_NODE:
case Node.DOCUMENT_FRAGMENT_NODE:
case Node.DOCUMENT_TYPE_NODE:
List