com.genexus.gxserver.client.helpers.XmlHelper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gxserver-client Show documentation
Show all versions of gxserver-client Show documentation
Java client for GeneXus Server services
The newest version!
/*
* The MIT License
*
* Copyright 2020 GeneXus S.A..
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.genexus.gxserver.client.helpers;
import com.genexus.gxserver.client.clients.common.WithLocalContextClassLoader;
import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.JAXBException;
import jakarta.xml.bind.Marshaller;
import jakarta.xml.bind.Unmarshaller;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
/**
*
* @author jlr
*/
public class XmlHelper {
public static T parse(File file, Class tClass) throws FileNotFoundException, IOException, ParserConfigurationException, SAXException, JAXBException {
try (FileInputStream fileStream = new FileInputStream(file)) {
return parse(fileStream, tClass);
}
}
public static T parse(InputStream stream, Class tClass) throws IOException, ParserConfigurationException, SAXException, JAXBException {
try (Reader reader = new InputStreamReader(stream, StandardCharsets.UTF_8.name())) {
InputSource source = new InputSource(reader);
return XmlHelper.parse(source, tClass);
}
}
private static T parse(InputSource source, Class tClass) throws IOException, ParserConfigurationException, SAXException, JAXBException {
DocumentBuilder builder = DocumentBuilders.createSaferDocumentBuilder(factory -> {
factory.setNamespaceAware(true);
});
Document doc = builder.parse(source);
return XmlHelper.parse(doc, tClass);
}
// private so as to ensure it comes through a secure parser
private static T parse(Document doc, Class tClass) throws JAXBException {
JAXBContext jaxbContext = WithLocalContextClassLoader.call(() -> {
return JAXBContext.newInstance(tClass);
});
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
T result = (T) jaxbUnmarshaller.unmarshal(doc);
return result;
}
public static String createXml(T instance) throws JAXBException {
return createXml(instance, false);
}
public static String createXml(T instance, boolean normalized) throws JAXBException {
StringWriter writer = new StringWriter();
writeXml(instance, writer);
String xmlString = writer.toString();
if (normalized) {
try {
xmlString = normalizeXmlString(xmlString);
} catch (IOException | ParserConfigurationException | TransformerException | SAXException ex) {
throw new JAXBException("Error normalizing string", ex);
}
}
return xmlString;
}
public static void writeXml(T instance, File file) throws FileNotFoundException, IOException, JAXBException {
try (
FileOutputStream outputStream = new FileOutputStream(file);
OutputStreamWriter writer = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8.name())) {
writeXml(instance, writer);
}
}
public static void writeXml(T instance, Writer writer) throws JAXBException {
Marshaller jaxbMarshaller = createMarshaller(instance);
jaxbMarshaller.marshal(instance, writer);
}
private static Marshaller createMarshaller(T instance) throws JAXBException {
JAXBContext jaxbContext = JAXBContext.newInstance(instance.getClass());
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_ENCODING, StandardCharsets.UTF_8.name());
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
return jaxbMarshaller;
}
public static String normalizeXmlString(String inputString) throws ParserConfigurationException, SAXException, IOException, TransformerConfigurationException, TransformerException {
DocumentBuilder db = DocumentBuilders.createSaferDocumentBuilder(dbf -> {
dbf.setNamespaceAware(true);
dbf.setCoalescing(true);
dbf.setIgnoringElementContentWhitespace(true);
dbf.setIgnoringComments(true);
});
Document doc = db.parse(new ByteArrayInputStream(inputString.getBytes(StandardCharsets.UTF_8)));
trimWhitespace(doc.getDocumentElement());
doc.setXmlStandalone(true);
doc.normalizeDocument();
StringWriter sw = new StringWriter();
TransformerFactory tf = TransformerFactory.newInstance();
tf.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
tf.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "");
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.transform(new DOMSource(doc), new StreamResult(sw));
return sw.toString().trim();
}
private static void trimWhitespace(Node node) {
NodeList children = node.getChildNodes();
for (int i = 0; i < children.getLength(); ++i) {
Node child = children.item(i);
if (child.getNodeType() == Node.TEXT_NODE) {
child.setTextContent(child.getTextContent().trim());
}
trimWhitespace(child);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy