Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2015 Saxonica Limited.
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
// If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
// This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
package net.sf.saxon.ma.json;
import net.sf.saxon.event.PipelineConfiguration;
import net.sf.saxon.event.Receiver;
import net.sf.saxon.event.StartTagBuffer;
import net.sf.saxon.expr.parser.Location;
import net.sf.saxon.lib.NamespaceConstant;
import net.sf.saxon.om.NamespaceBinding;
import net.sf.saxon.om.NodeName;
import net.sf.saxon.trans.Err;
import net.sf.saxon.trans.XPathException;
import net.sf.saxon.tree.util.FastStringBuffer;
import net.sf.saxon.type.SchemaType;
import net.sf.saxon.type.SimpleType;
import net.sf.saxon.type.StringConverter;
import net.sf.saxon.value.DoubleValue;
import net.sf.saxon.value.StringToDouble11;
import net.sf.saxon.value.Whitespace;
import net.sf.saxon.z.IntPredicate;
import java.util.HashSet;
import java.util.Set;
import java.util.Stack;
/**
* A Receiver which receives a stream of XML events using the vocabulary defined for the XML representation
* of JSON in XSLT 3.0, and which generates the corresponding JSON text as a string
*/
public class JsonReceiver implements Receiver {
private PipelineConfiguration pipe;
private FastStringBuffer output;
private FastStringBuffer textBuffer = new FastStringBuffer(128);
private Stack stack = new Stack();
private boolean atStart = true;
private StartTagBuffer startTagBuffer;
private boolean indenting = false;
private boolean escaped = false;
private Stack> keyChecker = new Stack>();
private static final String ERR_INPUT = "FOJS0006";
public JsonReceiver(PipelineConfiguration pipe) {
setPipelineConfiguration(pipe);
}
public void setPipelineConfiguration(PipelineConfiguration pipe) {
this.pipe = pipe;
startTagBuffer = (StartTagBuffer)pipe.getComponent(StartTagBuffer.class.getName());
}
public PipelineConfiguration getPipelineConfiguration() {
return pipe;
}
public void setSystemId(String systemId) {
// no action
}
public void setIndenting(boolean indenting) {
this.indenting = indenting;
}
public boolean isIndenting() {
return indenting;
}
public void open() throws XPathException {
output = new FastStringBuffer(2048);
}
public void startDocument(int properties) throws XPathException {
if (output == null) {
output = new FastStringBuffer(2048);
}
}
public void endDocument() throws XPathException {
// no action
}
public void setUnparsedEntity(String name, String systemID, String publicID) throws XPathException {
// no action
}
public void startElement(NodeName elemName, SchemaType typeCode, Location location, int properties) throws XPathException {
String parent = stack.empty() ? null : stack.peek().getLocalPart();
boolean inMap = "map".equals(parent);
stack.push(elemName);
//started.push(false);
if (!elemName.hasURI(NamespaceConstant.JSON)) {
throw new XPathException("xml-to-json: element found in wrong namespace: " +
elemName.getStructuredQName().getEQName(), ERR_INPUT);
}
if (!atStart) {
output.append(',');
if (indenting) {
indent(stack.size());
}
}
if (inMap) {
if (startTagBuffer == null) {
startTagBuffer = (StartTagBuffer)pipe.getComponent(StartTagBuffer.class.getName());
}
String key = startTagBuffer.getAttribute("", "key");
boolean added = keyChecker.peek().add(key);
if (!added) {
throw new XPathException("xml-to-json: duplicate key value \"" + Err.wrap(key) + "\"", ERR_INPUT);
}
if (key == null) {
throw new XPathException("xml-to-json: Child elements of