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:canreg.client.analysis.Tools.java

/**
 * Exports a JFreeChart to a SVG file.//from  w w w.j av  a  2  s. c om
 *
 * @param chart JFreeChart to export
 * @param bounds the dimensions of the viewport
 * @param svgFile the output file.
 * @throws IOException if writing the svgFile fails.
 */
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);

    // 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:eu.planets_project.tb.impl.chart.ExperimentChartServlet.java

public static void writeChartAsSVG(OutputStream outstream, JFreeChart chart, int width, int height)
        throws UnsupportedEncodingException, SVGGraphics2DIOException {

    Writer out = new OutputStreamWriter(outstream, "UTF-8");

    // 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(3);

    // Other opts:
    svgGenerator.getGeneratorContext().setEmbeddedFontsOn(true);
    //        svgGenerator.setFont( new Font("SansSerif", Font.PLAIN, 8) );

    // 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;

    svgGenerator.stream(out, useCSS);//from  w w  w  . j  a  v  a2s.co m

}

From source file:Main.java

/**
 * Creates an empty DOM document. E.g.:/* w  w  w . j a  va 2s. c  o m*/
 * <pre>
 * Document doc = createDocument("book", null, null, null);
 * </pre> creates new DOM of a well-formed document with root element named
 * book.
 *
 * @param rootQName       qualified name of root element, for example
 *                        <code>myroot</code> or <code>ns:myroot</code>
 * @param namespaceURI    URI of root element namespace or <code>null</code>
 * @param doctypePublicID public ID of DOCTYPE or <code>null</code>
 * @param doctypeSystemID system ID of DOCTYPE or <code>null</code> if no
 *                        DOCTYPE required and doctypePublicID is also
 *                        <code>null</code>
 *
 * @throws DOMException              if new DOM with passed parameters can
 *                                   not be created
 * @throws FactoryConfigurationError Application developers should never
 *                                   need to directly catch errors of this
 *                                   type.
 *
 * @return new DOM Document
 */
public static Document createDocument(String rootQName, String namespaceURI, String doctypePublicID,
        String doctypeSystemID) throws DOMException {
    DOMImplementation impl = getDOMImplementation();

    if ((doctypePublicID != null) && (doctypeSystemID == null)) {
        throw new IllegalArgumentException("System ID cannot be null if public ID specified. "); //NOI18N
    }

    DocumentType dtd = null;

    if (doctypeSystemID != null) {
        dtd = impl.createDocumentType(rootQName, doctypePublicID, doctypeSystemID);
    }

    return impl.createDocument(namespaceURI, rootQName, dtd);
}

From source file:com.amalto.webapp.core.util.Util.java

/**
 * Returns a namespaced root element of a document Useful to create a namespace holder element
 * /*from  w  w  w .j a v  a  2 s  .com*/
 * @return the root Element
 */
public static Element getRootElement(String elementName, String namespace, String prefix) throws Exception {
    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); //$NON-NLS-1$ //$NON-NLS-2$
        Element rootNS = namespaceHolder.getDocumentElement();
        rootNS.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:" + prefix, namespace); //$NON-NLS-1$ //$NON-NLS-2$
        return rootNS;
    } catch (Exception e) {
        String err = "Error creating a namespace holder document: " + e.getLocalizedMessage(); //$NON-NLS-1$
        throw new Exception(err);
    }
}

From source file:delphsim.model.Resultado.java

/**
 * Mtodo esttico que exporta una grfica <CODE>JFreeChart</CODE> al
 * formato de imagen vectorial SVG./*from   ww  w.  ja  v a 2  s .  c  o  m*/
 * @param destino El fichero de destino.
 * @param chart La grfica a exportar.
 * @param anchura Anchura de la imagen final.
 * @param altura Altura de la imagen final.
 * @throws java.io.IOException Si hubiera algn problema al crear el archivo en disco.
 */
public static void exportarComoSVG(File destino, JFreeChart chart, int anchura, int altura) throws IOException {
    // Para SVG utilizamos la librera Batik
    // Obtener un DOMImplementation
    DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
    // Crear una instancia de org.w3c.dom.Document
    String svgNS = "http://www.w3.org/2000/svg";
    Document document = domImpl.createDocument(svgNS, "svg", null);
    // Crear una instancia del Generador de SVGs
    SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
    // Dibujar la grfica en el Generador de SVGs
    chart.draw(svgGenerator, new Rectangle(anchura, altura));
    // Escribir el archivo SVG
    OutputStream outputStream = new FileOutputStream(destino);
    Writer out = new OutputStreamWriter(outputStream, "UTF-8");
    svgGenerator.stream(out, true /* utilizar CSS */);
    outputStream.flush();
    outputStream.close();
}

From source file:eu.europa.esig.dss.DSSXMLUtils.java

/**
 * Creates a DOM document without document element.
 *
 * @param namespaceURI  the namespace URI of the document element to create or null
 * @param qualifiedName the qualified name of the document element to be created or null
 * @return {@code Document}/* w  ww. j  a  v  a  2s  .  co  m*/
 */
public static Document createDocument(final String namespaceURI, final String qualifiedName) {
    ensureDocumentBuilder();

    DOMImplementation domImpl;
    try {
        domImpl = dbFactory.newDocumentBuilder().getDOMImplementation();
    } catch (ParserConfigurationException e) {
        throw new DSSException(e);
    }

    return domImpl.createDocument(namespaceURI, qualifiedName, null);
}

From source file:org.exist.xquery.modules.jfreechart.render.SVGrenderer.java

@Override
public void render(JFreeChart chart, Configuration config, OutputStream os) throws IOException {

    Rectangle bounds = new Rectangle(config.getImageWidth(), config.getImageHeight());

    // 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);//from  w ww .  j  a  v  a  2 s . c o m

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

From source file:de.cebitec.readXplorer.plotting.ChartExporter.java

@Override
public void run() {
    notifyObservers(ChartExportStatus.RUNNING);
    Rectangle bounds = new Rectangle(1920, 1080);
    DOMImplementation dom = GenericDOMImplementation.getDOMImplementation();
    Document document = dom.createDocument(null, "svg", null);
    SVGGraphics2D generator = new SVGGraphics2D(document);
    chart.draw(generator, bounds);//from   w  ww .j av a2  s .  com
    try (OutputStream outputStream = Files.newOutputStream(file, StandardOpenOption.CREATE)) {
        Writer out = new OutputStreamWriter(outputStream, "UTF-8");
        generator.stream(out, true);
        outputStream.flush();
        notifyObservers(ChartExportStatus.FINISHED);
    } catch (IOException ex) {
        notifyObservers(ChartExportStatus.FAILED);
    }
}

From source file:net.sf.jasperreports.charts.util.SvgChartRendererFactory.java

@Override
public Renderable getRenderable(JasperReportsContext jasperReportsContext, JFreeChart chart,
        ChartHyperlinkProvider chartHyperlinkProvider, Rectangle2D rectangle) {
    DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
    Document document = domImpl.createDocument(null, "svg", null);
    SVGGraphics2D grx = new SVGGraphics2D(document);

    grx.setSVGCanvasSize(rectangle.getBounds().getSize());

    List<JRPrintImageAreaHyperlink> areaHyperlinks = null;

    if (chartHyperlinkProvider != null && chartHyperlinkProvider.hasHyperlinks()) {
        areaHyperlinks = ChartUtil.getImageAreaHyperlinks(chart, chartHyperlinkProvider, grx, rectangle);
    } else {//from   w w  w . j a  v  a2 s . co  m
        chart.draw(grx, rectangle);
    }

    try {
        StringWriter swriter = new StringWriter();
        grx.stream(swriter);
        byte[] svgData = null;
        try {
            svgData = swriter.getBuffer().toString().getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new JRRuntimeException(e);
        }
        return new SimpleRenderToImageAwareDataRenderer(svgData, areaHyperlinks);
    } catch (SVGGraphics2DIOException e) {
        throw new JRRuntimeException(e);
    }
}

From source file:eu.europa.esig.dss.DSSXMLUtils.java

/**
 * Creates a DOM Document object of the specified type with its document element.
 *
 * @param namespaceURI  the namespace URI of the document element to create or null
 * @param qualifiedName the qualified name of the document element to be created or null
 * @param element       document {@code Element}
 * @return {@code Document}/*from w ww  .j  a  v a 2s.  com*/
 */
public static Document createDocument(final String namespaceURI, final String qualifiedName,
        final Element element) {
    ensureDocumentBuilder();

    DOMImplementation domImpl;
    try {
        domImpl = dbFactory.newDocumentBuilder().getDOMImplementation();
    } catch (ParserConfigurationException e) {
        throw new DSSException(e);
    }
    final Document newDocument = domImpl.createDocument(namespaceURI, qualifiedName, null);
    final Element newElement = newDocument.getDocumentElement();
    newDocument.adoptNode(element);
    newElement.appendChild(element);

    return newDocument;
}