List of usage examples for org.w3c.dom DOMImplementation createDocument
public Document createDocument(String namespaceURI, String qualifiedName, DocumentType doctype) throws DOMException;
From source file:fr.aliasource.webmail.server.proxy.client.http.DOMUtils.java
public static Document createDoc(String namespace, String rootElement) throws ParserConfigurationException, FactoryConfigurationError { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true);/*ww w . j a va 2 s.c o m*/ DocumentBuilder builder = dbf.newDocumentBuilder(); DOMImplementation di = builder.getDOMImplementation(); return di.createDocument(namespace, rootElement, null); }
From source file:org.objectweb.proactive.extensions.timitspmd.util.charts.Utilities.java
/** * Exports a JFreeChart to a SVG file.//from w ww. ja v a 2s.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 saveChartAsSVG(JFreeChart chart, Rectangle bounds, File svgFile) throws IOException { try { Class<?> GDI = Class.forName("org.apache.batik.dom.GenericDOMImplementation"); // Get a DOMImplementation and create an XML document Method getDOMImplementation = GDI.getMethod("getDOMImplementation", new Class<?>[0]); DOMImplementation domImpl = (DOMImplementation) getDOMImplementation.invoke(null, new Object[0]); org.w3c.dom.Document document = domImpl.createDocument(null, "svg", null); // Create an instance of the SVG Generator Class<?> SG2D = Class.forName("org.apache.batik.svggen.SVGGraphics2D"); Method streamMethod = SG2D.getMethod("stream", new Class<?>[] { Writer.class, boolean.class }); Constructor<?> SG2DConstr = SG2D.getConstructor(new Class<?>[] { org.w3c.dom.Document.class }); Object svgGenerator = SG2DConstr.newInstance(document); // draw the chart in the SVG generator chart.draw((Graphics2D) svgGenerator, bounds); // Write svg file OutputStream outputStream = new FileOutputStream(svgFile); Writer out = new OutputStreamWriter(outputStream, "UTF-8"); streamMethod.invoke(svgGenerator, new Object[] { out, true /* use css */ }); outputStream.flush(); outputStream.close(); out.close(); } catch (Exception e) { e.printStackTrace(); } }
From source file:de.atomfrede.tools.evalutation.tools.plot.util.PlotUtil.java
public static void saveChartAsSVG(File file, JFreeChart chart, int width, int height) throws Exception { Writer out = null;//from ww w. ja v a2s. c o m try { DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation(); String svgNS = "http://www.w3.org/2000/svg"; org.w3c.dom.Document document = domImpl.createDocument(svgNS, "svg", null); SVGGraphics2D svgGenerator = new SVGGraphics2D(document); svgGenerator.getGeneratorContext().setPrecision(6); chart.draw(svgGenerator, new Rectangle2D.Double(0, 0, width, height), null); boolean useCSS = true; out = new OutputStreamWriter(new FileOutputStream(file), "UTF-8"); svgGenerator.stream(out, useCSS); } catch (UnsupportedEncodingException enc) { log.error(enc); } catch (FileNotFoundException fnf) { log.error(fnf); } catch (SVGGraphics2DIOException e) { log.error(e); } catch (DOMException dome) { log.error(dome); } finally { if (out != null) out.close(); } }
From source file:electroStaticUI.Save.java
/** * Save chart as SVG file.//from ww w . j a va2 s . c o m * Required libs: Apache Batik (batik-svggen.jar, batik-dom.jar, dom.jar). * * @param chart JFreeChart to save. * @param fileName Name of file to save chart in. * @param width Width of chart graphic. * @param height Height of chart graphic. * @return Final file name used. * @throws IOException if failed. * * To Do: Add a save/open method for saving a file native to this application. It should save the users current charge distribution * to be re-loaded at a later time. */ static public final String saveChartToSVG(final JFreeChart chart, String fileName, final int width, final int height) throws IOException { String result = null; if (chart != null) { if (fileName == null) { final String chartTitle = chart.getTitle().getText(); if (chartTitle != null) { fileName = chartTitle; } else { fileName = "chart"; } } result = fileName + ".svg"; final DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation(); final Document document = domImpl.createDocument(null, "svg", null); final SVGGraphics2D svgGenerator = new SVGGraphics2D(document); // svgGenerator.getGeneratorContext().setEmbeddedFontsOn(true); //embed fonts //set anti-aliasing bug fix for SVG generator: //setting rendering hints of svgGenerator doesn't help as it seems to use the rendering hints from the chart final boolean antiAlias = chart.getAntiAlias(); final RenderingHints rh = chart.getRenderingHints(); if (antiAlias) { rh.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); rh.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF); //fix } else { rh.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF); rh.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); //fix } // svgGenerator.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // svgGenerator.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF); // svgGenerator.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); // svgGenerator.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY); // svgGenerator.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); // svgGenerator.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY); chart.draw(svgGenerator, new Rectangle2D.Double(0, 0, width, height), new ChartRenderingInfo()); //undo anti-aliasing bugfix settings for chart to use correct settings: if (antiAlias) { rh.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); rh.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); } else { rh.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF); rh.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF); } final boolean useCSS = true; Writer out = null; try { out = new OutputStreamWriter( new BufferedOutputStream(new FileOutputStream(new File(result), false)), "iso-8859-1"); //UTF-8 svgGenerator.stream(out, useCSS); } finally { svgGenerator.dispose(); IOUtils.closeQuietly(out); } } //else: input unavailable return result; }
From source file:fr.romainf.QRCode.java
/** * Renders the QRCode data in an SVG document. * * @param matrix BitMatrix BitMatrix of the encoded QRCode * @param pixelColour Color The colour of pixels representing bits * @return SVGGraphics2D/*from w w w.j av a 2 s . c om*/ */ private static SVGGraphics2D renderSVG(BitMatrix matrix, Color pixelColour) { DOMImplementation implementation = SVGDOMImplementation.getDOMImplementation(); String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI; Document document = implementation.createDocument(svgNS, "svg", null); SVGGraphics2D svgGenerator = new SVGGraphics2D(document); int width = matrix.getWidth(); int height = matrix.getHeight(); Area area = new Area(); for (int x = 0; x < width; x += 1) { for (int y = 0; y < height; y += 1) { if (matrix.get(x, y)) { area.add(new Area(new Rectangle(x, y, 1, 1))); } } } svgGenerator.setPaint(pixelColour); svgGenerator.fill(area); return svgGenerator; }
From source file:de.laures.cewolf.util.Renderer.java
/** * Handles rendering a chart as a SVG. Currently this method is synchronized * because of concurrency issues with JFreeChart. * * @param baos/*from w w w . j av a 2 s . c o m*/ * @param chart * @param width * @param height * @throws IOException */ private static synchronized void handleSVG(ByteArrayOutputStream baos, JFreeChart chart, int width, int height) throws IOException { OutputStreamWriter writer = new OutputStreamWriter(baos, "UTF-8"); DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation(); Document document = domImpl.createDocument("cewolf-svg", "svg", null); SVGGeneratorContext ctx = SVGGeneratorContext.createDefault(document); ctx.setComment("Generated by Cewolf using JFreeChart and Apache Batik SVG Generator"); SVGGraphics2D svgGenerator = new SVGGraphics2D(ctx, false); svgGenerator.setSVGCanvasSize(new Dimension(width, height)); chart.draw(svgGenerator, new Rectangle2D.Double(0, 0, width, height), null); svgGenerator.stream(writer, false); writer.close(); }
From source file:Main.java
/** * Creates empty DOM Document using JAXP factoring. E.g.: * <p><pre>// w w w .j a v a 2 s . c o m * Document doc = createDocument("book", null, null, null); * </pre><p> * creates new DOM of a well-formed document with root element named book. * * @param rootQName qualified name of root element. e.g. <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:DomUtil.java
/** * Creates a new document./*from ww w . j a v a 2s. c o m*/ * * @param qualifiedName the qualified name of the document type to be * created * @param publicId the external subset public identifier * @param systemId the external subset system identifier * @param namespaceUri the namespace URI of the document element to create */ public static Document createDocument(String qualifiedName, String publicId, String systemId, String namespaceUri) { DOMImplementation dom = DomUtil.getDocumentBuilder().getDOMImplementation(); DocumentType docType = dom.createDocumentType(qualifiedName, publicId, systemId); Document document = dom.createDocument(namespaceUri, qualifiedName, docType); if (namespaceUri != null) { document.getDocumentElement().setAttribute("xmlns", namespaceUri); } return document; }
From source file:net.sf.mzmine.chartbasics.graphicsexport.ChartExportUtil.java
public static void writeChartToSVG(JFreeChart chart, int width, int height, File name) throws Exception { // Get a DOMImplementation DOMImplementation domImpl = SVGDOMImplementation.getDOMImplementation(); org.w3c.dom.Document document = domImpl.createDocument(null, "svg", null); SVGGraphics2D svgGenerator = new SVGGraphics2D(document); svgGenerator.setSVGCanvasSize(new Dimension(width, height)); chart.draw(svgGenerator, new Rectangle2D.Double(0, 0, width, height)); boolean useCSS = true; // we want to use CSS style attribute Writer out = null;//from w w w. jav a 2 s. c om try { out = new OutputStreamWriter(new FileOutputStream(name), "UTF-8"); svgGenerator.stream(out, useCSS); } catch (UnsupportedEncodingException | FileNotFoundException e) { e.printStackTrace(); throw e; } catch (SVGGraphics2DIOException e) { e.printStackTrace(); throw e; } finally { try { out.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); throw e; } } }
From source file:at.ac.dbisinformatik.snowprofile.web.svgcreator.SVGCreator.java
/** * creates a SVG-Graph for given jsonDocument which includes ExtJS-SVG-Objects. The created SVG-Document will be converted in the given export type. * //w ww. j ava 2s. c o m * @param jsonDocument * @param exportType * @param profileID * @return * @throws TransformerException * @throws URISyntaxException * @throws TranscoderException * @throws IOException */ public static ByteArrayOutputStream svgDocument(JsonArray jsonDocument, String exportType, String profileID) throws TransformerException, URISyntaxException, TranscoderException, IOException { ByteArrayOutputStream ret = null; DOMImplementation impl = SVGDOMImplementation.getDOMImplementation(); String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI; Document doc = impl.createDocument(svgNS, "svg", null); // Get the root element (the 'svg' element). Element svgRoot = doc.getDocumentElement(); // Set the width and height attributes on the root 'svg' element. svgRoot.setAttributeNS(null, "width", "1500"); svgRoot.setAttributeNS(null, "height", "1500"); JsonArray items = jsonDocument; for (int i = 0; i < items.size(); ++i) { String type = items.get(i).getAsJsonObject().get("type").getAsString(); Element element = null; org.w3c.dom.Text test = null; String path = ""; String width = ""; String height = ""; String x = ""; String y = ""; String fill = ""; String text = ""; String font = ""; String fontFamily = ""; String fontSize = ""; String degrees = ""; String stroke = ""; String opacity = ""; String src = ""; switch (type) { case "rect": width = items.get(i).getAsJsonObject().get("width").getAsString(); height = items.get(i).getAsJsonObject().get("height").getAsString(); x = items.get(i).getAsJsonObject().get("x").getAsString(); y = items.get(i).getAsJsonObject().get("y").getAsString(); fill = items.get(i).getAsJsonObject().get("fill").getAsString(); stroke = items.get(i).getAsJsonObject().get("stroke").getAsString(); opacity = items.get(i).getAsJsonObject().get("opacity").getAsString(); // Create the rectangle. element = doc.createElementNS(svgNS, "rect"); element.setAttributeNS(null, "width", width); element.setAttributeNS(null, "height", height); element.setAttributeNS(null, "x", x); element.setAttributeNS(null, "y", y); element.setAttributeNS(null, "fill", fill); element.setAttributeNS(null, "stroke", stroke); element.setAttributeNS(null, "opacity", opacity); break; case "path": path = items.get(i).getAsJsonObject().get("path").getAsString(); fill = items.get(i).getAsJsonObject().get("fill").getAsString(); stroke = items.get(i).getAsJsonObject().get("stroke").getAsString(); // Create the path. element = doc.createElementNS(svgNS, "path"); element.setAttributeNS(null, "d", path); element.setAttributeNS(null, "fill", fill); element.setAttributeNS(null, "stroke", stroke); break; case "image": x = items.get(i).getAsJsonObject().get("x").getAsString(); y = items.get(i).getAsJsonObject().get("y").getAsString(); width = items.get(i).getAsJsonObject().get("width").getAsString(); height = items.get(i).getAsJsonObject().get("height").getAsString(); src = System.class.getResource("/at/ac/dbisinformatik/snowprofile/web/resources/" + items.get(i).getAsJsonObject().get("src").getAsString()).toString(); // Create the path. element = doc.createElementNS(svgNS, "image"); element.setAttributeNS(null, "x", x); element.setAttributeNS(null, "y", y); element.setAttributeNS(null, "width", width); element.setAttributeNS(null, "height", height); element.setAttributeNS(null, "xlink:href", src); break; case "text": text = items.get(i).getAsJsonObject().get("text").getAsString(); fill = items.get(i).getAsJsonObject().get("fill").getAsString(); font = items.get(i).getAsJsonObject().get("font").getAsString(); x = items.get(i).getAsJsonObject().get("x").getAsString(); y = items.get(i).getAsJsonObject().get("y").getAsString(); // Transformation if (items.get(i).getAsJsonObject().get("rotate") != null) { JsonObject temp = items.get(i).getAsJsonObject().get("rotate").getAsJsonObject(); degrees = temp.get("degrees").getAsString(); } fontSize = font.split(" ")[0]; fontFamily = font.split(" ")[1]; // Create the text. test = doc.createTextNode(text); element = doc.createElementNS(svgNS, "text"); element.setAttributeNS(null, "text", text); element.setAttributeNS(null, "fill", fill); element.setAttributeNS(null, "font-family", fontFamily); element.setAttributeNS(null, "font-size", fontSize); element.setAttributeNS(null, "x", x); element.setAttributeNS(null, "y", y); if (!degrees.equals("")) { // element.setAttributeNS(null, "transform", // "rotate(270 "+500+","+80+")"); } element.appendChild(test); break; default: break; } // Attach the rectangle to the root 'svg' element. svgRoot.appendChild(element); } TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); DOMSource source = new DOMSource(doc); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); StreamResult result = new StreamResult(outputStream); transformer.transform(source, result); InputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray()); switch (exportType) { case "png": ret = createPNG(inputStream); break; case "jpg": ret = createJPG(inputStream); break; case "pdf": ret = createPDF(inputStream); break; default: break; } return ret; }