org.ow2.petals.binding.rest.exchange.JSONHelper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of petals-bc-rest Show documentation
Show all versions of petals-bc-rest Show documentation
petals-bc-rest Binding Component description
/**
* Copyright (c) 2007-2012 EBM WebSourcing, 2012-2020 Linagora
*
* This program/library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 2.1 of the License, or (at your
* option) any later version.
*
* This program/library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program/library; If not, see http://www.gnu.org/licenses/
* for the GNU Lesser General Public License version 2.1.
*/
package org.ow2.petals.binding.rest.exchange;
import java.io.InputStream;
import java.io.OutputStream;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.stax.StAXResult;
import javax.xml.transform.stax.StAXSource;
import com.ebmwebsourcing.easycommons.lang.reflect.ReflectionHelper;
import com.ebmwebsourcing.easycommons.xml.Transformers;
import de.odysseus.staxon.base.AbstractXMLStreamReader;
public final class JSONHelper {
private JSONHelper() {
// Utility class --> No constructor
}
/**
* Convert the given JSON {@link InputStream} into XML contained in the resulting stream
*
* @param input
* the JSON to convert
* @param output
* the resulting output in which the JSON converted into XML will be written
* @param inputFactory
* the JSON to XML convertion configuration
* @throws XMLStreamException
* if a XML unexpected processing error occurs (XML well-formedness errors as well as unexpected
* processing conditions)
* @throws TransformerException
* if an unrecoverable error occurs during the course of the transformation
*/
public static void convertJSONToXML(final InputStream input, final Result output,
final XMLInputFactory inputFactory) throws XMLStreamException, TransformerException {
final Transformer transformer = Transformers.takeTransformer();
try {
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.transform(getSourceFromJSON(input, inputFactory), output);
} finally {
Transformers.releaseTransformer(transformer);
}
}
public static Source getSourceFromJSON(final InputStream input, final XMLInputFactory inputFactory)
throws XMLStreamException {
XMLStreamReader reader = inputFactory.createXMLStreamReader(input);
// because of https://bugs.openjdk.java.net/browse/JDK-8016914, we need to set the version
// TODO when https://github.com/beckchr/staxon/issues/39 is resolved, we can use it instead of reflection
if (reader instanceof AbstractXMLStreamReader) {
ReflectionHelper.setFieldValue(AbstractXMLStreamReader.class, reader, "version", "1.0", false);
}
return new StAXSource(reader);
}
/**
* Convert XML from the specified source to the specified output stream.
*
* @param source
* a XML source
* @param output
* an output stream for the converted JSON
* @param outputFactory
* the XML/JSON mapping convention
* @throws TransformerException
* if an unrecoverable error occurs during the course of the transformation
* @throws XMLStreamException
*
*/
public static final void convertXMLToJSON(final Source source, final OutputStream output,
final XMLOutputFactory outputFactory) throws TransformerException, XMLStreamException {
final Transformer transformer = Transformers.takeTransformer();
try {
transformer.transform(source, new StAXResult(outputFactory.createXMLStreamWriter(output)));
} finally {
Transformers.releaseTransformer(transformer);
}
}
}