All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.saxon.ma.json.JsonReceiver Maven / Gradle / Ivy

There is a newer version: 12.5
Show newest version
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 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  must have a key attribute", ERR_INPUT);
            }
            String keyEscaped = startTagBuffer.getAttribute("", "escaped-key");
            boolean alreadyEscaped = false;
            if (keyEscaped != null) {
                try {
                    alreadyEscaped = StringConverter.STRING_TO_BOOLEAN.convertString(keyEscaped).asAtomic().effectiveBooleanValue();
                } catch (XPathException e) {
                    throw new XPathException("xml-to-json: Value of escaped-key attribute '" + Err.wrap(keyEscaped) +
                            "' is not a valid xs:boolean", ERR_INPUT);
                }
            }
            output.append('"');
            if (alreadyEscaped) {
                key = unescape(key);
            }
            output.append(escape(key, false, new ControlChar()));
            output.append('"');
            output.append(indenting ? " : " : ":");
        }
        String local = elemName.getLocalPart();
        escaped = false;
        if (local.equals("array")) {
            if (indenting) {
                indent(stack.size());
                output.append("[ ");
            } else {
                output.append('[');
            }
            atStart = true;
        } else if (local.equals("map")) {
            if (indenting) {
                indent(stack.size());
                output.append("{ ");
            } else {
                output.append('{');
            }
            atStart = true;
            keyChecker.push(new HashSet());
        } else if (local.equals("null")) {
            checkParent(local, parent);
            output.append("null");
            atStart = false;
        } else if (local.equals("string")) {
            if (startTagBuffer == null) {
                startTagBuffer = (StartTagBuffer) pipe.getComponent(StartTagBuffer.class.getName());
            }
            String escapeAtt = startTagBuffer.getAttribute("", "escaped");
            if (escapeAtt != null) {
                try {
                    escaped = StringConverter.STRING_TO_BOOLEAN.convertString(escapeAtt).asAtomic().effectiveBooleanValue();
                } catch (XPathException e) {
                    throw new XPathException("xml-to-json: value of escaped attribute (" +
                        escapeAtt + ") is not a valid xs:boolean", ERR_INPUT);
                }
            }
            checkParent(local, parent);
            atStart = false;
        } else if (local.equals("boolean") || local.equals("number")) {
            checkParent(local, parent);
            atStart = false;
        } else {
            throw new XPathException("xml-to-json: unknown element <" + local + ">", ERR_INPUT);
        }
        textBuffer.setLength(0);
    }

    private void checkParent(String child, String parent) throws XPathException {
        if ("null".equals(parent) || "string".equals(parent) || "number".equals(parent) || "boolean".equals(parent)) {
            throw new XPathException("xml-to-json: A " + Err.wrap(child, Err.ELEMENT) +
                " element cannot appear as a child of " + Err.wrap(parent, Err.ELEMENT), ERR_INPUT);
        }
    }

    public void namespace(NamespaceBinding namespaceBinding, int properties) throws XPathException {
        // no action
    }

    public void attribute(NodeName attName, SimpleType typeCode, CharSequence value, Location locationId, int properties) throws XPathException {
        if (attName.hasURI("") && (attName.getLocalPart().equals("key") || attName.getLocalPart().equals("escaped-key"))) {
            boolean inMap = stack.size() >= 2 && stack.get(stack.size() - 2).getLocalPart().equals("map");
            if (!inMap) {
                throw new XPathException("xml-to-json: The " + attName.getLocalPart() +
                    " attribute is allowed only on elements within a map", ERR_INPUT);
            }
        } else if (attName.hasURI("") && attName.getLocalPart().equals("escaped")) {
                boolean inString = !stack.empty() && stack.peek().getLocalPart().equals("string");
                if (!inString) {
                    throw new XPathException("xml-to-json: The escaped" +
                        " attribute is allowed only on the  element", ERR_INPUT);
                }
        } else if (attName.hasURI("") || attName.hasURI(NamespaceConstant.JSON)) {
            throw new XPathException("xml-to-json: Disallowed attribute in input: " + attName.getDisplayName(), ERR_INPUT);
        }
        // Attributes in other namespaces are ignored
    }

    public void startContent() throws XPathException {
        // no action
    }

    public void endElement() throws XPathException {
        NodeName name = stack.pop();
        String local = name.getLocalPart();
        if (local.equals("boolean")) {
            try {
                boolean b = StringConverter.STRING_TO_BOOLEAN.convertString(textBuffer).asAtomic().effectiveBooleanValue();
                output.append(b ? "true" : "false");
            } catch (XPathException e) {
                throw new XPathException("xml-to-json: Value of  element is not a valid xs:boolean", ERR_INPUT);
            }
        } else if (local.equals("number")) {
            double d = StringToDouble11.getInstance().stringToNumber(textBuffer);
            if (Double.isNaN(d) || Double.isInfinite(d)) {
                throw new XPathException("xml-to-json: Infinity and NaN are not allowed", ERR_INPUT);
            }
            output.append(new DoubleValue(d).getStringValueCS());
        } else if (local.equals("string")) {
            output.append('"');
            String unescaped = escaped ? unescape(textBuffer.toString()) : textBuffer.toString();
            output.append(escape(unescaped, false, new ControlChar()));
            output.append('"');
        } else if (!Whitespace.isWhite(textBuffer)) {
            throw new XPathException("xml-to-json: Element " + name + " must have no text content", ERR_INPUT);
        }
        textBuffer.setLength(0);
        escaped = false;
        if (local.equals("array")) {
            output.append(indenting ? " ]" : "]");
        } else if (local.equals("map")) {
            keyChecker.pop();
            output.append(indenting ? " }" : "}");
        }
    }

    public static CharSequence escape(CharSequence in, boolean forXml, IntPredicate hexEscapes) throws XPathException {
        FastStringBuffer out = new FastStringBuffer(in.length());
        for (int i=0; i= 127 && c <= 159);
        }
    }

    public void characters(CharSequence chars, Location locationId, int properties) throws XPathException {
        textBuffer.append(chars);
    }

    public void processingInstruction(String name, CharSequence data, Location locationId, int properties) throws XPathException {
        // no action
    }

    public void comment(CharSequence content, Location locationId, int properties) throws XPathException {
        // no action
    }

    public void close() throws XPathException {
        // no action
    }

    public boolean usesTypeAnnotations() {
        return false;
    }

    public String getSystemId() {
        return null;
    }

    /**
     * On completion, get the assembled JSON string
     * @return the JSON string representing the supplied XML content.
     */

    public String getJsonString() {
        return output.toString();
    }

    /**
     * Add indentation whitespace to the buffer
     * @param depth the level of indentation
     */

    private void indent(int depth) {
        output.append('\n');
        for (int i=0; i




© 2015 - 2025 Weber Informatics LLC | Privacy Policy