Example usage for org.w3c.dom DOMImplementation createDocument

List of usage examples for org.w3c.dom DOMImplementation createDocument

Introduction

In this page you can find the example usage for org.w3c.dom DOMImplementation createDocument.

Prototype

public Document createDocument(String namespaceURI, String qualifiedName, DocumentType doctype)
        throws DOMException;

Source Link

Document

Creates a DOM Document object of the specified type with its document element.

Usage

From source file:Main.java

/**
 * /*from   ww w  . j  a v a  2  s  .  c o  m*/
 * <B>Purpose:</B> Creates an Empty XML Document object
 * 
 * @return
 * @throws ParserConfigurationException
 */
public static Document createEmptyDocument() throws ParserConfigurationException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    DOMImplementation impl = builder.getDOMImplementation();
    Document doc = impl.createDocument(null, null, null);
    return doc;
}

From source file:Main.java

/**
 * Creates an XML {@link Document} with the provided root element tag.
 * We're using DOM for now - memory-intensive, but OK for these uses
 * @param ns The namespace URI of the document element to create, or null for none.
 * @param rootElement The name of the XML root element tag.
 * @return An XML {@link Document}.// www  .  jav a 2s .c  om
 */
public static Document createXmlDoc(String ns, String rootElement) {
    // Create XML DOM document (Memory consuming).
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = null;
    try {
        builder = factory.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
    }
    DOMImplementation impl = builder.getDOMImplementation();
    // Document.
    return impl.createDocument(ns, rootElement, null);
}

From source file:Main.java

/**
 * Creates the dom./*from  w w  w.ja  va 2s .  c  om*/
 *
 * @param documentElementName
 *            the document element name
 * @return the document
 */
public static Document createDom(String documentElementName) {
    try {
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        DOMImplementation domImpl = documentBuilder.getDOMImplementation();
        Document document = domImpl.createDocument(NS_PIPELINE_DATA, documentElementName, null);

        return document;

    } catch (ParserConfigurationException e) {
        e.printStackTrace();
        return null;
    }

}

From source file:Main.java

/**
 * Returns a namespaced root element of a document
 * Useful to create a namespace holder element
 * @param namespace/*from   ww w.  j a v a  2s  . c  om*/
 * @return the root Element
 */
public static Element getRootElement(String elementName, String namespace, String prefix)
        throws TransformerException {
    Element rootNS = null;
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        DocumentBuilder builder = factory.newDocumentBuilder();
        DOMImplementation impl = builder.getDOMImplementation();
        Document namespaceHolder = impl.createDocument(namespace,
                (prefix == null ? "" : prefix + ":") + elementName, null);
        rootNS = namespaceHolder.getDocumentElement();
        rootNS.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:" + prefix, namespace);
    } catch (Exception e) {
        String err = "Error creating a namespace holder document: " + e.getLocalizedMessage();
        throw new TransformerException(err);
    }
    return rootNS;
}

From source file:org.jfreechart.SVGExporter.java

public static void exportChartAsSVG(JFreeChart chart, Rectangle bounds, File svgFile) throws IOException {
    // Get a DOMImplementation and create an XML document
    DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
    Document document = domImpl.createDocument(null, "svg", null);

    // Create an instance of the SVG Generator
    SVGGraphics2D svgGenerator = new SVGGraphics2D(document);

    // draw the chart in the SVG generator
    chart.draw(svgGenerator, bounds);/*  ww  w .ja v  a2s. c o m*/

    // Write svg file
    OutputStream outputStream = new FileOutputStream(svgFile);
    Writer out = new OutputStreamWriter(outputStream, "UTF-8");
    svgGenerator.stream(out, true /* use css */);
    outputStream.flush();
    outputStream.close();
}

From source file:org.dkpro.lab.reporting.ChartUtil.java

/**
 * Exports a JFreeChart to a SVG file./*from  w ww . ja v  a 2 s .co  m*/
 *
 * @param chart JFreeChart to export
 * @param aOS stream to write to.
 * @param aWidth width of the chart in pixels
 * @param aHeight height of the chart in pixels
 * @throws IOException if writing the svgFile fails.
 * @see <a href="http://dolf.trieschnigg.nl/jfreechart/">Saving JFreeChart as SVG vector images
 *      using Batik</a>
 */
public static void writeChartAsSVG(OutputStream aOS, JFreeChart chart, int aWidth, int aHeight)
        throws IOException {
    // Get a DOMImplementation and create an XML document
    DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
    Document document = domImpl.createDocument(null, "svg", null);

    // Create an instance of the SVG Generator
    SVGGraphics2D svgGenerator = new SVGGraphics2D(document);

    // draw the chart in the SVG generator
    chart.draw(svgGenerator, new Rectangle(aWidth, aHeight));

    // Write svg file
    Writer out = new OutputStreamWriter(aOS, "UTF-8");
    svgGenerator.stream(out, true /* use css */);
    out.flush();
    out.close();
}

From source file:net.bioclipse.model.ImageWriter.java

/**
 * Save image to svg format/*  w ww .  j  a va2s .c o  m*/
 * @param path the path including filename where the image is to be stored
 * @param chart TODO
 */
public static void saveImageSVG(String path, JFreeChart chart) {
    //First check that we have a valid chart
    if (chart != null) {
        //Create DOM objects
        DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
        Document document = domImpl.createDocument(null, "svg", null);

        //Create an instance of the SVG generator
        SVGGraphics2D svgGenerator = new SVGGraphics2D(document);

        //Setting the precision apparently avoids a NullPointerException in Batik 1.5
        svgGenerator.getGeneratorContext().setPrecision(6);

        //Render chart into SVG Graphics2D impl.
        chart.draw(svgGenerator, new Rectangle2D.Double(0, 0, 400, 300), null);

        //Write to file
        boolean useCSS = true;
        try {
            Writer out = new OutputStreamWriter(new FileOutputStream(new File(path)), "UTF-8");
            svgGenerator.stream(out, useCSS);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (SVGGraphics2DIOException e) {
            e.printStackTrace();
        }
    }

}

From source file:com.redhat.thermostat.byteman.plot.impl.TestPlotRenderer.java

private static void chartToSvg(JFreeChart chart, int width, int height, String filename) throws Exception {
    Writer writer = null;/*from  www  .j  ava 2s  .  co  m*/
    try {
        DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
        Document document = domImpl.createDocument(null, "svg", null);
        SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
        svgGenerator.setSVGCanvasSize(new Dimension(width, height));
        chart.draw(svgGenerator, new Rectangle(width, height));
        OutputStream os = new FileOutputStream(filename + ".svg");
        writer = new BufferedWriter(new OutputStreamWriter(os, Charset.forName("UTF-8")));
        svgGenerator.stream(writer);
    } finally {
        closeQuietly(writer);
    }
}

From source file:org.encog.workbench.util.graph.DocumentSVG.java

public static void saveSVG(File filename, JFreeChart chart, int width, int height) {
    try {/*  w  ww . j  a  va  2  s . com*/
        // THE FOLLOWING CODE BASED ON THE EXAMPLE IN THE BATIK DOCUMENTATION...
        // Get a DOMImplementation
        DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
        // Create an instance of org.w3c.dom.Document
        Document document = domImpl.createDocument(null, "svg", null);
        // Create an instance of the SVG Generator
        SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
        // set the precision to avoid a null pointer exception in Batik 1.5
        svgGenerator.getGeneratorContext().setPrecision(6);
        // Ask the chart to render into the SVG Graphics2D implementation
        chart.draw(svgGenerator, new Rectangle2D.Double(0, 0, width, height), null);
        // Finally, stream out SVG to a file using UTF-8 character to
        // byte encoding
        boolean useCSS = true;
        Writer out = new OutputStreamWriter(new FileOutputStream(filename), "UTF-8");
        svgGenerator.stream(out, useCSS);
    } catch (IOException ex) {
        throw new WorkBenchError(ex);
    }
}

From source file:de.bund.bfr.knime.pmm.common.chart.ChartUtilities.java

public static void saveChartAs(JFreeChart chart, String fileName, int width, int height) {
    if (fileName.toLowerCase().endsWith(".png")) {
        try {// w ww  . j a  v  a2 s .com
            org.jfree.chart.ChartUtilities.writeChartAsPNG(new FileOutputStream(fileName), chart, width,
                    height);
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else if (fileName.toLowerCase().endsWith(".svg")) {
        try {
            DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
            Document document = domImpl.createDocument(null, "svg", null);
            SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
            Writer outsvg = new OutputStreamWriter(new FileOutputStream(fileName), StandardCharsets.UTF_8);

            chart.draw(svgGenerator, new Rectangle2D.Double(0, 0, width, height));
            svgGenerator.stream(outsvg, true);
            outsvg.close();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (SVGGraphics2DIOException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}