Example usage for org.w3c.dom Element setAttributeNS

List of usage examples for org.w3c.dom Element setAttributeNS

Introduction

In this page you can find the example usage for org.w3c.dom Element setAttributeNS.

Prototype

public void setAttributeNS(String namespaceURI, String qualifiedName, String value) throws DOMException;

Source Link

Document

Adds a new attribute.

Usage

From source file:com.esofthead.mycollab.community.ui.chart.JFreeChartWrapper.java

@Override
public Resource getSource() {
    if (res == null) {
        StreamSource streamSource = new StreamResource.StreamSource() {
            private ByteArrayInputStream bytestream = null;

            ByteArrayInputStream getByteStream() {
                if (chart != null && bytestream == null) {
                    int width = getGraphWidth();
                    int height = getGraphHeight();

                    if (mode == RenderingMode.SVG) {

                        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
                        DocumentBuilder docBuilder = null;
                        try {
                            docBuilder = docBuilderFactory.newDocumentBuilder();
                        } catch (ParserConfigurationException e1) {
                            throw new RuntimeException(e1);
                        }/*from www.j  a  v  a  2  s  .c o m*/
                        Document document = docBuilder.newDocument();
                        Element svgelem = document.createElement("svg");
                        document.appendChild(svgelem);

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

                        // draw the chart in the SVG generator
                        chart.draw(svgGenerator, new Rectangle(width, height));
                        Element el = svgGenerator.getRoot();
                        el.setAttributeNS(null, "viewBox", "0 0 " + width + " " + height + "");
                        el.setAttributeNS(null, "style", "width:100%;height:100%;");
                        el.setAttributeNS(null, "preserveAspectRatio", getSvgAspectRatio());

                        // Write svg to buffer
                        ByteArrayOutputStream baoutputStream = new ByteArrayOutputStream();
                        Writer out;
                        try {
                            OutputStream outputStream = gzipEnabled ? new GZIPOutputStream(baoutputStream)
                                    : baoutputStream;
                            out = new OutputStreamWriter(outputStream, "UTF-8");
                            /*
                            * don't use css, FF3 can'd deal with the result
                            * perfectly: wrong font sizes
                            */
                            boolean useCSS = false;
                            svgGenerator.stream(el, out, useCSS, false);
                            outputStream.flush();
                            outputStream.close();
                            bytestream = new ByteArrayInputStream(baoutputStream.toByteArray());
                        } catch (Exception e) {
                            log.error("Error while generating SVG chart", e);
                        }
                    } else {
                        // Draw png to bytestream
                        try {
                            byte[] bytes = ChartUtilities.encodeAsPNG(chart.createBufferedImage(width, height));
                            bytestream = new ByteArrayInputStream(bytes);
                        } catch (Exception e) {
                            log.error("Error while generating PNG chart", e);
                        }

                    }

                } else {
                    bytestream.reset();
                }
                return bytestream;
            }

            @Override
            public InputStream getStream() {
                return getByteStream();
            }
        };

        res = new StreamResource(streamSource, String.format("graph%d", System.currentTimeMillis())) {

            @Override
            public int getBufferSize() {
                if (getStreamSource().getStream() != null) {
                    try {
                        return getStreamSource().getStream().available();
                    } catch (IOException e) {
                        log.warn("Error while get stream info", e);
                        return 0;
                    }
                } else {
                    return 0;
                }
            }

            @Override
            public long getCacheTime() {
                return 0;
            }

            @Override
            public String getFilename() {
                if (mode == RenderingMode.PNG) {
                    return super.getFilename() + ".png";
                } else {
                    return super.getFilename() + (gzipEnabled ? ".svgz" : ".svg");
                }
            }

            @Override
            public DownloadStream getStream() {
                DownloadStream downloadStream = new DownloadStream(getStreamSource().getStream(), getMIMEType(),
                        getFilename());
                if (gzipEnabled && mode == RenderingMode.SVG) {
                    downloadStream.setParameter("Content-Encoding", "gzip");
                }
                return downloadStream;
            }

            @Override
            public String getMIMEType() {
                if (mode == RenderingMode.PNG) {
                    return "image/png";
                } else {
                    return "image/svg+xml";
                }
            }
        };
    }
    return res;
}

From source file:de.topobyte.livecg.core.painting.backend.svg.SvgPainter.java

private void fill(StringBuilder strb) {
    Element path = doc.createElementNS(svgNS, "path");
    path.setAttributeNS(null, "style",
            "fill:" + getCurrentColor() + ";fill-rule:evenodd;stroke:none;fill-opacity:" + color.getAlpha());
    path.setAttributeNS(null, "d", strb.toString());

    append(path);/*from w  ww  .ja v  a2s .c  o m*/
}

From source file:de.topobyte.livecg.core.painting.backend.svg.SvgPainter.java

@Override
public void fillCircle(double x, double y, double radius) {
    Element circle = doc.createElementNS(svgNS, "circle");
    circle.setAttributeNS(null, "cx", Double.toString(x));
    circle.setAttributeNS(null, "cy", Double.toString(y));
    circle.setAttributeNS(null, "r", Double.toString(radius));
    circle.setAttributeNS(null, "fill", getCurrentColor());

    append(circle);/*from   w  w w.  j  a v a2 s.c  om*/
}

From source file:de.topobyte.livecg.core.painting.backend.svg.SvgPainter.java

@Override
public void drawString(String text, double x, double y) {
    Element element = doc.createElementNS(svgNS, "text");
    element.setAttributeNS(null, "style", "fill:" + getCurrentColor() + ";stroke:none;fill-opacity:"
            + color.getAlpha() + ";font-family:Sans;font-size:12px");
    element.setAttributeNS(null, "x", Double.toString(x));
    element.setAttributeNS(null, "y", Double.toString(y));
    element.setTextContent(text);/*ww w. ja  va2s  . co  m*/

    append(element);
}

From source file:de.topobyte.livecg.core.painting.backend.svg.SvgPainter.java

@Override
public void fillRect(int x, int y, int width, int height) {
    Element rectangle = doc.createElementNS(svgNS, "rect");
    rectangle.setAttributeNS(null, "x", Integer.toString(x));
    rectangle.setAttributeNS(null, "y", Integer.toString(y));
    rectangle.setAttributeNS(null, "width", Integer.toString(width));
    rectangle.setAttributeNS(null, "height", Integer.toString(height));
    rectangle.setAttributeNS(null, "fill", getCurrentColor());

    append(rectangle);/*from w  w  w.j  a  va 2  s  .  c  o m*/
}

From source file:de.topobyte.livecg.core.painting.backend.svg.SvgPainter.java

@Override
public void fillRect(double x, double y, double width, double height) {
    Element rectangle = doc.createElementNS(svgNS, "rect");
    rectangle.setAttributeNS(null, "x", Double.toString(x));
    rectangle.setAttributeNS(null, "y", Double.toString(y));
    rectangle.setAttributeNS(null, "width", Double.toString(width));
    rectangle.setAttributeNS(null, "height", Double.toString(height));
    rectangle.setAttributeNS(null, "fill", getCurrentColor());

    append(rectangle);//from ww  w.ja v a 2s . c  o  m
}

From source file:de.topobyte.livecg.core.painting.backend.svg.SvgPainter.java

@Override
public void drawCircle(double x, double y, double radius) {
    Element circle = doc.createElementNS(svgNS, "circle");
    circle.setAttributeNS(null, "cx", Double.toString(x));
    circle.setAttributeNS(null, "cy", Double.toString(y));
    circle.setAttributeNS(null, "r", Double.toString(radius));
    circle.setAttributeNS(null, "fill", "none");
    addStrokeAttributes(circle);// w w w.  j  a  va  2s.co  m
    circle.setAttributeNS(null, "fill", "none");

    append(circle);
}

From source file:de.topobyte.livecg.core.painting.backend.svg.SvgPainter.java

@Override
public void drawRect(int x, int y, int width, int height) {
    Element rectangle = doc.createElementNS(svgNS, "rect");
    rectangle.setAttributeNS(null, "x", Integer.toString(x));
    rectangle.setAttributeNS(null, "y", Integer.toString(y));
    rectangle.setAttributeNS(null, "width", Integer.toString(width));
    rectangle.setAttributeNS(null, "height", Integer.toString(height));
    addStrokeAttributes(rectangle);/*from  w  w  w .j  av a 2  s. c o  m*/
    rectangle.setAttributeNS(null, "fill", "none");

    append(rectangle);
}

From source file:de.topobyte.livecg.core.painting.backend.svg.SvgPainter.java

@Override
public void drawRect(double x, double y, double width, double height) {
    Element rectangle = doc.createElementNS(svgNS, "rect");
    rectangle.setAttributeNS(null, "x", Double.toString(x));
    rectangle.setAttributeNS(null, "y", Double.toString(y));
    rectangle.setAttributeNS(null, "width", Double.toString(width));
    rectangle.setAttributeNS(null, "height", Double.toString(height));
    addStrokeAttributes(rectangle);/*w  w  w . jav a2  s .  c o m*/
    rectangle.setAttributeNS(null, "fill", "none");

    append(rectangle);
}

From source file:com.iontorrent.vaadin.utils.JFreeChartWrapper.java

@Override
public Resource getSource() {
    if (res == null) {
        res = new ApplicationResource() {

            private ByteArrayInputStream bytestream = null;

            ByteArrayInputStream getByteStream() {
                if (chart != null && bytestream == null) {
                    int widht = getGraphWidth();
                    int height = getGraphHeight();
                    info = new ChartRenderingInfo();

                    if (mode == RenderingMode.SVG) {

                        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
                        DocumentBuilder docBuilder = null;
                        try {
                            docBuilder = docBuilderFactory.newDocumentBuilder();
                        } catch (ParserConfigurationException e1) {
                            throw new RuntimeException(e1);
                        }//from  w ww . j  a  va2 s.  c  o m
                        Document document = docBuilder.newDocument();
                        Element svgelem = document.createElement("svg");
                        document.appendChild(svgelem);

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

                        // draw the chart in the SVG generator

                        chart.draw(svgGenerator, new Rectangle(widht, height), info);
                        Element el = svgGenerator.getRoot();
                        el.setAttributeNS(null, "viewBox", "0 0 " + widht + " " + height + "");
                        el.setAttributeNS(null, "style", "width:100%;height:100%;");
                        el.setAttributeNS(null, "preserveAspectRatio", getSvgAspectRatio());

                        // Write svg to buffer
                        ByteArrayOutputStream baoutputStream = new ByteArrayOutputStream();
                        Writer out;
                        try {
                            OutputStream outputStream = gzipEnabled ? new GZIPOutputStream(baoutputStream)
                                    : baoutputStream;
                            out = new OutputStreamWriter(outputStream, "UTF-8");
                            /*
                             * don't use css, FF3 can'd deal with the result
                             * perfectly: wrong font sizes
                             */
                            boolean useCSS = false;
                            svgGenerator.stream(el, out, useCSS, false);
                            outputStream.flush();
                            outputStream.close();
                            bytestream = new ByteArrayInputStream(baoutputStream.toByteArray());
                        } catch (UnsupportedEncodingException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (SVGGraphics2DIOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    } else {
                        // Draw png to bytestream
                        try {

                            byte[] bytes = ChartUtilities.encodeAsPNG(chart.createBufferedImage(widht, height));
                            bytestream = new ByteArrayInputStream(bytes);
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }

                    }

                } else {
                    bytestream.reset();
                }
                return bytestream;
            }

            public Application getApplication() {
                return JFreeChartWrapper.this.getApplication();
            }

            public int getBufferSize() {
                if (getByteStream() != null) {
                    return getByteStream().available();
                } else {
                    return 0;
                }
            }

            public long getCacheTime() {
                return 0;
            }

            public String getFilename() {
                if (mode == RenderingMode.PNG) {
                    return "graph.png";
                } else {
                    return gzipEnabled ? "graph.svgz" : "graph.svg";
                }
            }

            public DownloadStream getStream() {
                DownloadStream downloadStream = new DownloadStream(getByteStream(), getMIMEType(),
                        getFilename());
                if (gzipEnabled && mode == RenderingMode.SVG) {
                    downloadStream.setParameter("Content-Encoding", "gzip");
                }
                return downloadStream;
            }

            public String getMIMEType() {
                if (mode == RenderingMode.PNG) {
                    return "image/png";
                } else {
                    return "image/svg+xml";
                }
            }
        };
    }
    return res;
}