Subversion Repositories bacoAlunos

Rev

Blame | Compare with Previous | Last modification | View Log | RSS feed

/*
 * Dom4jUtil
 *
 * Copyright 2007 Mitra
 * Jorge Machado
 */

package jomm.dom4j;

import org.xml.sax.InputSource;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.*;

import javax.xml.transform.*;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.net.URL;
import java.net.MalformedURLException;
import java.io.*;
import java.io.Writer;
import java.util.Map;
import java.util.Hashtable;

/**
 * Class Description
 *
 * @author Jorge Machado <machadofisher@gmail.com>
 * @see ;
 */


public class Dom4jUtil
{

    public static 1.5.0/docs/api/java/lang/String.html">String getPathOfResource(1.5.0/docs/api/java/lang/String.html">String resourceName){

        1.5.0/docs/api/java/net/URL.html">URL url = 1.5.0/docs/api/java/lang/Thread.html">Thread.currentThread().getContextClassLoader().getResource(resourceName);
        if (url==null)
            return null;
        1.5.0/docs/api/java/lang/String.html">String ret=null;
        try{
            ret=java.net.1.5.0/docs/api/java/net/URLDecoder.html">URLDecoder.decode(url.getFile(),"iso-8859-1");
        }catch(1.5.0/docs/api/java/io/UnsupportedEncodingException.html">UnsupportedEncodingException e){
            throw new 1.5.0/docs/api/java/lang/RuntimeException.html">RuntimeException(e);
        }
        if (ret==null)
            return null;
        if (ret.startsWith("/"))
            return ret.substring(1);
        return ret;
    }

    public static 5+0%2Fdocs%2Fapi+Document">Document parse(1.5.0/docs/api/java/net/URL.html">URL url) throws DocumentException
    {
        SAXReader reader = new SAXReader();
        return reader.read(url);
    }

    public static 5+0%2Fdocs%2Fapi+Document">Document parse(1.5.0/docs/api/java/lang/String.html">String xml) throws DocumentException
    {
        SAXReader reader = new SAXReader();
        return reader.read(new 1.5.0/docs/api/java/io/StringReader.html">StringReader(xml));
    }
    public static 5+0%2Fdocs%2Fapi+Document">Document parse(1.5.0/docs/api/java/io/File.html">File file) throws DocumentException, 1.5.0/docs/api/java/net/MalformedURLException.html">MalformedURLException {
        SAXReader reader = new SAXReader();
        return reader.read(file);
    }

    public static 5+0%2Fdocs%2Fapi+Document">Document parse(1.5.0/docs/api/org/xml/sax/InputSource.html">InputSource source) throws DocumentException, 1.5.0/docs/api/java/net/MalformedURLException.html">MalformedURLException
    {
        SAXReader reader = new SAXReader();
        return reader.read(source);
    }

    public static 5+0%2Fdocs%2Fapi+Document">Document styleDocument(5+0%2Fdocs%2Fapi+Document">Document document,1.5.0/docs/api/java/lang/String.html">String stylesheet) throws 1.5.0/docs/api/java/lang/Exception.html">Exception
    {

        // load the transformer using JAXP
        1.5.0/docs/api/javax/xml/transform/Transformer.html">Transformer transformer = Dom4jUtil.getTransformer(stylesheet);
        // now lets style the given document
        DocumentSource source = new DocumentSource( document );
        DocumentResult result = new DocumentResult();
        transformer.transform( source, result );

        // return the transformed document
        return result.getDocument();
    }

    public static 5+0%2Fdocs%2Fapi+Document">Document styleDocument(5+0%2Fdocs%2Fapi+Document">Document document,1.5.0/docs/api/javax/xml/transform/Transformer.html">Transformer transformer) throws 1.5.0/docs/api/java/lang/Exception.html">Exception
    {

        // now lets style the given document
        DocumentSource source = new DocumentSource( document );
        DocumentResult result = new DocumentResult();
        transformer.transform( source, result );

        // return the transformed document
        return result.getDocument();
    }


    public static void styleDocument(5+0%2Fdocs%2Fapi+Document">Document document,1.5.0/docs/api/java/lang/String.html">String stylesheet, java.io.1.5.0/docs/api/java/io/Writer.html">Writer writer) throws 1.5.0/docs/api/java/lang/Exception.html">Exception
    {
        // load the transformer using JAXP
        1.5.0/docs/api/javax/xml/transform/Transformer.html">Transformer transformer = Dom4jUtil.getTransformer(stylesheet);
        Dom4jUtil.styleDocument(document,transformer,writer);
    }

    public static void styleDocument(5+0%2Fdocs%2Fapi+Document">Document document,1.5.0/docs/api/javax/xml/transform/Transformer.html">Transformer transformer, 1.5.0/docs/api/java/io/Writer.html">Writer writer) throws 1.5.0/docs/api/java/lang/Exception.html">Exception
    {

        DocumentSource source = new DocumentSource( document );

        transformer.setOutputProperty(1.5.0/docs/api/javax/xml/transform/OutputKeys.html">OutputKeys.OMIT_XML_DECLARATION,"yes");

        1.5.0/docs/api/java/io/ByteArrayOutputStream.html">ByteArrayOutputStream output= new 1.5.0/docs/api/java/io/ByteArrayOutputStream.html">ByteArrayOutputStream();
        1.5.0/docs/api/javax/xml/transform/stream/StreamResult.html">StreamResult sresult = new 1.5.0/docs/api/javax/xml/transform/stream/StreamResult.html">StreamResult(output);
        transformer.transform(source, sresult);
        writer.write(output.toString());
    }

    public static 5+0%2Fdocs%2Fapi+Document">Document styleDocumentFromSystem(5+0%2Fdocs%2Fapi+Document">Document document,1.5.0/docs/api/java/lang/String.html">String stylesheetPathInClassLoader)
            throws 1.5.0/docs/api/java/lang/Exception.html">Exception
    {
        1.5.0/docs/api/javax/xml/transform/Transformer.html">Transformer transformer = Dom4jUtil.getTransformerFromSystem(stylesheetPathInClassLoader);
        return Dom4jUtil.styleDocument(document,transformer);
    }
    public static void styleDocumentFromSystem(5+0%2Fdocs%2Fapi+Document">Document document,1.5.0/docs/api/java/lang/String.html">String stylesheetPathInClassLoader, 1.5.0/docs/api/java/io/Writer.html">Writer writer)
            throws 1.5.0/docs/api/java/lang/Exception.html">Exception
    {
        1.5.0/docs/api/javax/xml/transform/Transformer.html">Transformer transformer = Dom4jUtil.getTransformerFromSystem(stylesheetPathInClassLoader);
        Dom4jUtil.styleDocument(document,transformer,writer);
    }
    public static void styleDocumentFromSystem(5+0%2Fdocs%2Fapi+Document">Document document,1.5.0/docs/api/java/lang/String.html">String stylesheetPathInClassLoader, 1.5.0/docs/api/java/lang/String.html">String fileDest)
            throws 1.5.0/docs/api/java/lang/Exception.html">Exception
    {
        1.5.0/docs/api/javax/xml/transform/Transformer.html">Transformer transformer = Dom4jUtil.getTransformerFromSystem(stylesheetPathInClassLoader);
        Dom4jUtil.styleDocument(document,transformer,fileDest);
    }

    public static 5+0%2Fdocs%2Fapi+Document">Document styleDocument(5+0%2Fdocs%2Fapi+Document">Document document,1.5.0/docs/api/java/lang/String.html">String stylesheet, 1.5.0/docs/api/java/lang/String.html">String fileDest) throws 1.5.0/docs/api/java/lang/Exception.html">Exception {

        1.5.0/docs/api/javax/xml/transform/Transformer.html">Transformer transformer = Dom4jUtil.getTransformer(stylesheet);
        // now lets style the given document
        DocumentSource source = new DocumentSource( document );
        DocumentResult result = new DocumentResult();
        transformer.transform( source, result );
        // return the transformed document
        5+0%2Fdocs%2Fapi+Document">Document transformedDoc = result.getDocument();
        Dom4jUtil.write(transformedDoc,fileDest);
        return transformedDoc;
    }



    private static 5+0%2Fdocs%2Fapi+Document">Document styleDocument(5+0%2Fdocs%2Fapi+Document">Document document, 1.5.0/docs/api/javax/xml/transform/Transformer.html">Transformer transformer, 1.5.0/docs/api/java/lang/String.html">String fileDest)
            throws 1.5.0/docs/api/javax/xml/transform/TransformerException.html">TransformerException, 1.5.0/docs/api/java/io/IOException.html">IOException
    {
        // now lets style the given document
        DocumentSource source = new DocumentSource( document );
        DocumentResult result = new DocumentResult();
        transformer.transform( source, result );

        // return the transformed document
        5+0%2Fdocs%2Fapi+Document">Document transformedDoc = result.getDocument();
        Dom4jUtil.write(transformedDoc,fileDest);
        return transformedDoc;
    }

    private static 1.5.0/docs/api/javax/xml/transform/TransformerFactory.html">TransformerFactory transFact = 1.5.0/docs/api/javax/xml/transform/TransformerFactory.html">TransformerFactory.newInstance();
    private static Map<String,Templates> templates =new Hashtable<String,Templates>();

    private static 1.5.0/docs/api/javax/xml/transform/Transformer.html">Transformer getTransformer(1.5.0/docs/api/java/lang/String.html">String xsltFile) throws 1.5.0/docs/api/javax/xml/transform/TransformerConfigurationException.html">TransformerConfigurationException {
        1.5.0/docs/api/javax/xml/transform/Templates.html">Templates tpl=(1.5.0/docs/api/javax/xml/transform/Templates.html">Templates) Dom4jUtil.templates.get(xsltFile);
        if (tpl==null){
            1.5.0/docs/api/javax/xml/transform/Source.html">Source xsltSource = new 1.5.0/docs/api/javax/xml/transform/stream/StreamSource.html">StreamSource(xsltFile);
            tpl = Dom4jUtil.transFact.newTemplates(xsltSource);
            Dom4jUtil.templates.put(xsltFile,tpl);
        }
        return tpl.newTransformer();
    }


    private static 1.5.0/docs/api/javax/xml/transform/Transformer.html">Transformer getTransformerFromSystem(1.5.0/docs/api/java/lang/String.html">String xsltFile) throws 1.5.0/docs/api/javax/xml/transform/TransformerConfigurationException.html">TransformerConfigurationException, 1.5.0/docs/api/java/io/IOException.html">IOException
    {
        1.5.0/docs/api/javax/xml/transform/Templates.html">Templates tpl=(1.5.0/docs/api/javax/xml/transform/Templates.html">Templates) Dom4jUtil.templates.get(xsltFile);
        if (tpl==null)
        {
            1.5.0/docs/api/java/lang/String.html">String path = getPathOfResource(xsltFile);
            1.5.0/docs/api/javax/xml/transform/Source.html">Source xsltSource = new 1.5.0/docs/api/javax/xml/transform/stream/StreamSource.html">StreamSource(path);
            tpl = Dom4jUtil.transFact.newTemplates(xsltSource);
            Dom4jUtil.templates.put(xsltFile,tpl);
        }
        return tpl.newTransformer();
    }




    public static void write(5+0%2Fdocs%2Fapi+Document">Document document,1.5.0/docs/api/java/lang/String.html">String fileDest) throws 1.5.0/docs/api/java/io/IOException.html">IOException
    {
        // lets write to a file
        XMLWriter writer = new XMLWriter(
                new 1.5.0/docs/api/java/io/FileWriter.html">FileWriter( fileDest )
        );
        writer.write( document );
        writer.close();


        // Pretty print the document to System.out
//        OutputFormat format = OutputFormat.createPrettyPrint();
//        writer = new XMLWriter( System.out, format );
//        writer.write( document );
//
//        // Compact format to System.out
//        format = OutputFormat.createCompactFormat();
//        writer = new XMLWriter( System.out, format );
//        writer.write( document );
    }
}