view src/MPipeline/src/mpipeline/XMLUtils.java @ 49:0bcdf74587d1 database-connection-manager

Cleanup
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Wed, 07 Dec 2011 17:24:36 +0100
parents f0afece42f48
children
line wrap: on
line source

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mpipeline;

/**
 * A set of XML utilities copied from those used in MATLAB.
 * @author hewitson
 */
import java.io.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.w3c.dom.*;
import org.xml.sax.*;

// Referenced classes of package com.mathworks.xml:
//            EncodingParser
/**
 * 
 * @author hewitson
 */
public class XMLUtils {

    /**
     * 
     */
    public XMLUtils() {
    }

    /**
     * Serialize an object to XML.
     * 
     * @param obj
     * @param obj1
     * @throws java.lang.Exception
     */
    public static void serializeXML(Object obj, Object obj1)
            throws Exception {
        serializeXML(obj, obj1, null);
    }

    /**
     * 
     * @param s
     * @return
     */
    public static String modifiedIdentityTransformation(String s) {
        return (new StringBuilder()).append("<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\"><xsl:output method=\"xml\" /><xsl:template match=\"@*|node()\"><xsl:copy><xsl:apply-templates select=\"@*|node()\"/></xsl:copy></xsl:template>").append(s).append("</xsl:stylesheet>").toString();
    }

    /**
     * 
     * @param s
     * @param s1
     * @return
     * @throws java.io.IOException
     */
    public static String transform(String s, String s1)
            throws IOException {
        try {
            TransformerFactory transformerfactory = TransformerFactory.newInstance();
            Transformer transformer = transformerfactory.newTransformer(new StreamSource(new StringReader(s1)));
            StringWriter stringwriter = new StringWriter();
            transformer.transform(new StreamSource(new StringReader(s)), new StreamResult(stringwriter));
            return stringwriter.toString();
        } catch (TransformerException transformerexception) {
            throw new IllegalStateException(transformerexception);
        }
    }

    /**
     * 
     * @param file
     * @param s
     * @param file1
     * @throws java.io.IOException
     */
    public static void transform(File file, String s, File file1)
            throws IOException {
        try {
            TransformerFactory transformerfactory = TransformerFactory.newInstance();
            Transformer transformer = transformerfactory.newTransformer(new StreamSource(new StringReader(s)));
            transformer.transform(new StreamSource(file.getAbsolutePath()), new StreamResult(new FileOutputStream(file1)));
        } catch (TransformerException transformerexception) {
            throw new IllegalStateException(transformerexception);
        }
    }

    /**
     * 
     * @param file
     * @param file1
     * @param file2
     * @throws java.io.IOException
     */
    public static void transform(File file, File file1, File file2)
            throws IOException {
        try {
            TransformerFactory transformerfactory = TransformerFactory.newInstance();
            Transformer transformer = transformerfactory.newTransformer(new StreamSource(file1.getAbsolutePath()));
            transformer.transform(new StreamSource(file.getAbsolutePath()), new StreamResult(new FileOutputStream(file2)));
        } catch (TransformerException transformerexception) {
            throw new IllegalStateException(transformerexception);
        }
    }

    /**
     * 
     * @param obj
     * @param obj1
     * @param s
     * @throws java.lang.Exception
     */
    public static void serializeXML(Object obj, Object obj1, String s)
            throws Exception {
        TransformerFactory transformerfactory = TransformerFactory.newInstance();
        Transformer transformer = transformerfactory.newTransformer();
//        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        if (s != null) {
            transformer.setOutputProperty("encoding", s);
        }
        if (obj instanceof Document) {
            Document document = (Document) obj;
            DocumentType documenttype = document.getDoctype();
            if (documenttype != null) {
                String s1 = documenttype.getSystemId();
                String s2 = documenttype.getPublicId();
                if (s1 != null) {
                    transformer.setOutputProperty("doctype-system", s1);
                }
                if (s2 != null) {
                    transformer.setOutputProperty("doctype-public", s2);
                }
            }
        }
        transformer.transform(transformSourceFactory(obj), transformResultFactory(obj1));
    }

    /**
     * 
     * @param obj
     * @return
     * @throws java.lang.Exception
     */
    public static Source transformSourceFactory(Object obj)
            throws Exception {
        if (obj instanceof Source) {
            return (Source) obj;
        }
        if (obj instanceof String) {
            return new StreamSource((String) obj);
        }
        if (obj instanceof Node) {
            return new DOMSource((Node) obj);
        }
        if (obj instanceof InputSource) {
            return new SAXSource((InputSource) obj);
        }
        if (obj instanceof InputStream) {
            return new StreamSource((InputStream) obj);
        }
        if (obj instanceof Reader) {
            return new StreamSource((Reader) obj);
        }
        if (obj instanceof File) {
            return new StreamSource((File) obj);
        } else {
            throw new Exception("Unrecognized XSLT Source type");
        }
    }

    /**
     * 
     * @param obj
     * @return
     * @throws java.lang.Exception
     */
    public static Result transformResultFactory(Object obj)
            throws Exception {
        if (obj instanceof Result) {
            return (Result) obj;
        }
        if (obj instanceof String) {
            return new StreamResult((String) obj);
        }
        if (obj instanceof Node) {
            return new DOMResult((Node) obj);
        }
        if (obj instanceof ContentHandler) {
            return new SAXResult((ContentHandler) obj);
        }
        if (obj instanceof File) {
            return new StreamResult((File) obj);
        }
        if (obj instanceof OutputStream) {
            return new StreamResult((OutputStream) obj);
        }
        if (obj instanceof Writer) {
            return new StreamResult((Writer) obj);
        } else {
            throw new Exception("Unrecognized XSLT Result type");
        }
    }

    /**
     * 
     * @param s
     * @return
     * @throws java.lang.Exception
     */
    public static Document createDocument(String s)
            throws Exception {
        return createDocument("", s);
    }

    /**
     * 
     * @param s
     * @param s1
     * @return
     * @throws java.lang.Exception
     */
    public static Document createDocument(String s, String s1)
            throws Exception {
        return getDOMImpl().createDocument(s, s1, null);
    }

    /**
     * 
     * @param s
     * @param s1
     * @param documenttype
     * @return
     * @throws java.lang.Exception
     */
    public static Document createDocument(String s, String s1, DocumentType documenttype)
            throws Exception {
        return getDOMImpl().createDocument(s, s1, documenttype);
    }

    /**
     * 
     * @param s
     * @param s1
     * @param s2
     * @param s3
     * @return
     * @throws java.lang.Exception
     */
    public static Document createDocument(String s, String s1, String s2, String s3)
            throws Exception {
        return getDOMImpl().createDocument(s, s1, getDOMImpl().createDocumentType(s1, s2, s3));
    }

    /**
     * 
     * @param s
     * @param s1
     * @param s2
     * @return
     * @throws java.lang.Exception
     */
    public static DocumentType createDocumentType(String s, String s1, String s2)
            throws Exception {
        return getDOMImpl().createDocumentType(s, s1, s2);
    }

    private static DocumentBuilder getDocBuilder()
            throws ParserConfigurationException {
        if (sDocBuilder == null) {
            sDocBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        }
        return sDocBuilder;
    }

    private static DOMImplementation getDOMImpl()
            throws ParserConfigurationException {
        return getDocBuilder().getDOMImplementation();
    }

    /**
     * 
     * @param obj
     * @return
     * @throws org.xml.sax.SAXException
     * @throws java.io.IOException
     * @throws javax.xml.parsers.ParserConfigurationException
     */
    public static Document parse(Object obj)
            throws SAXException, IOException, ParserConfigurationException {
        return parse(obj, getDocBuilder());
    }

    /**
     * 
     * @param obj
     * @param documentbuilder
     * @return
     * @throws org.xml.sax.SAXException
     * @throws java.io.IOException
     * @throws javax.xml.parsers.ParserConfigurationException
     */
    public static Document parse(Object obj, DocumentBuilder documentbuilder)
            throws SAXException, IOException, ParserConfigurationException {
        if (obj instanceof File) {
            return documentbuilder.parse(new InputSource(new FileReader((File) obj)));
        }
        if (obj instanceof String) {
            return documentbuilder.parse((String) obj);
        }
        if (obj instanceof InputSource) {
            return documentbuilder.parse((InputSource) obj);
        }
        if (obj instanceof InputStream) {
            return documentbuilder.parse((InputStream) obj);
        } else {
            throw new SAXException("Invalid parser input type");
        }
    }

    /**
     * 
     * @param s
     * @return
     */
    public static String filePathAsUrl(String s) {
        String s2 = s.substring(0, 2);
        String s1;
        if (s2.equals("\\\\") || s2.equals("//")) {
            s1 = "file:///\\";
        } else if (s.substring(1, 2).equals(":")) {
            s1 = "file:///";
        } else {
            s1 = "file://";
        }
        return (new StringBuilder()).append(s1).append(s).toString();
    }//    public static String getEncoding(InputStream inputstream)
//    {
//        return EncodingParser.getEncoding(inputstream);
//    }
//
//    public static String getEncoding(Reader reader)
//    {
//        return EncodingParser.getEncoding(reader);
//    }
    private static DocumentBuilder sDocBuilder = null;
}