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

io.gravitee.el.spel.function.xml.XmlPayloadConverter Maven / Gradle / Ivy

There is a newer version: 3.2.3
Show newest version
/**
 * Copyright (C) 2015 The Gravitee team (http://gravitee.io)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.gravitee.el.spel.function.xml;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.sax.SAXSource;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;

/**
 * @author David BRASSELY (david.brassely at graviteesource.com)
 * @author GraviteeSource Team
 */
public class XmlPayloadConverter {

    private final DocumentBuilderFactory documentBuilderFactory;

    public XmlPayloadConverter() {
        this(DocumentBuilderFactoryUtils.newInstance());
        this.documentBuilderFactory.setNamespaceAware(true);
    }

    public XmlPayloadConverter(DocumentBuilderFactory documentBuilderFactory) {
        this.documentBuilderFactory = documentBuilderFactory;
    }

    public Document convertToDocument(Object object) {
        try {
            if (object instanceof Document) {
                return (Document) object;
            } else if (object instanceof Node) {
                return nodeToDocument((Node) object);
            } else if (object instanceof DOMSource) {
                Node node = ((DOMSource) object).getNode();
                return nodeToDocument(node);
            } else if (object instanceof Source) {
                InputSource inputSource = sourceToInputSource((Source) object);
                return getDocumentBuilder().parse(inputSource);
            } else if (object instanceof File) {
                return getDocumentBuilder().parse((File) object);
            } else if (object instanceof String) {
                return getDocumentBuilder().parse(new InputSource(new StringReader((String) object)));
            } else if (object instanceof InputStream) {
                return getDocumentBuilder().parse((InputStream) object);
            } else if (object instanceof byte[]) {
                return getDocumentBuilder().parse(new ByteArrayInputStream((byte[]) object));
            }
        } catch (Exception e) {
            throw new XPathException("failed to parse " + object.getClass() + " payload '" + object + "'", e);
        }

        throw new XPathException("unsupported payload type [" + object.getClass().getName() + "]");
    }

    private static InputSource sourceToInputSource(Source source) {
        InputSource inputSource = SAXSource.sourceToInputSource(source);
        if (inputSource == null) {
            inputSource = new InputSource(source.getSystemId());
        }
        return inputSource;
    }

    protected Document nodeToDocument(Node node) {
        if (node instanceof Document) {
            return (Document) node;
        }
        Document document = getDocumentBuilder().newDocument();
        document.appendChild(document.importNode(node, true));
        return document;
    }

    public Node convertToNode(Object object) {
        Node node;
        if (object instanceof Node) {
            node = (Node) object;
        } else if (object instanceof DOMSource) {
            node = ((DOMSource) object).getNode();
        } else {
            node = convertToDocument(object);
        }
        return node;
    }

    protected synchronized DocumentBuilder getDocumentBuilder() {
        try {
            return this.documentBuilderFactory.newDocumentBuilder();
        } catch (ParserConfigurationException e) {
            throw new XPathException("failed to create a new DocumentBuilder", e);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy