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:de.topobyte.livecg.core.painting.backend.svg.SvgPainter.java

private void append(Element element) {
    Element e = root;/* ww  w. j  ava2 s  .  c  om*/
    if (clipIds != null) {
        for (int id : clipIds) {
            Element g = doc.createElementNS(svgNS, "g");
            g.setAttributeNS(null, "clip-path", "url(#" + CLIP_PATH_PREFIX + id + ")");
            e.appendChild(g);
            e = g;
        }
    }
    if (transform != null && !transform.isIdentity()) {
        Element g = doc.createElementNS(svgNS, "g");
        g.setAttributeNS(null, "transform", transformValue());
        e.appendChild(g);
        e = g;
    }
    e.appendChild(element);
}

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

private void addStrokeAttributes(Element element) {
    if (dash == null) {
        element.setAttributeNS(null, "stroke", getCurrentColor());
        element.setAttributeNS(null, "stroke-width", width + "px");
        element.setAttributeNS(null, "stroke-linecap", "round");
    } else {//  www .  j a va 2  s .  co m
        element.setAttributeNS(null, "stroke", getCurrentColor());
        element.setAttributeNS(null, "stroke-width", width + "px");
        element.setAttributeNS(null, "stroke-linejoin", "round");
        element.setAttributeNS(null, "stroke-linecap", "round");
        StringBuilder strb = new StringBuilder();
        for (int i = 0; i < dash.length; i++) {
            strb.append(dash[i]);
            if (i < dash.length - 1) {
                strb.append(",");
            }
        }
        element.setAttributeNS(null, "stroke-dasharray", strb.toString());
        element.setAttributeNS(null, "stroke-dashoffset", "" + phase);
        element.setAttributeNS(null, "stroke-opacity", "" + color.getAlpha());
    }

}

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

private void addToDefs(int index, Shape shape) {
    Element clipPath = doc.createElementNS(svgNS, "clipPath");
    clipPath.setAttributeNS(null, "id", CLIP_PATH_PREFIX + index);

    SvgPathBuilder pb = new SvgPathBuilder();
    StringBuilder strb = pb.buildPath(shape);

    Element path = doc.createElementNS(svgNS, "path");
    path.setAttributeNS(null, "d", strb.toString());

    if (transform != null) {
        path.setAttributeNS(null, "transform", transformValue());
    }//from  ww  w .  j a  v a  2 s  .c  om

    clipPath.appendChild(path);
    defs.appendChild(clipPath);
}

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

@Override
public void drawImage(Image image, int x, int y) {
    BufferedImage im = ImageUtil.convert(image);
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    try {//  w w  w  .  j  ava2s.co  m
        boolean written = ImageIO.write(im, "png", output);
        if (!written) {
            logger.error("unable to draw image: no writer found");
        }
    } catch (IOException e) {
        logger.error("unable to draw image: " + e.getMessage());
        return;
    }
    byte[] bytes = output.toByteArray();
    String base64 = Base64.encodeBase64String(bytes);

    Element element = doc.createElementNS(svgNS, "image");
    element.setAttributeNS(null, "x", Integer.toString(x));
    element.setAttributeNS(null, "y", Integer.toString(y));
    element.setAttributeNS(null, "width", Integer.toString(image.getWidth()));
    element.setAttributeNS(null, "height", Integer.toString(image.getHeight()));
    element.setAttributeNS(null, "xlink:href", "data:image/png;base64," + base64);

    append(element);
}

From source file:org.vaadin.addon.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 widht = 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);
                        }//w w  w.jav  a 2s  . 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));
                        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;
            }

            @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) {
                        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:at.gv.egovernment.moa.id.auth.validator.parep.client.szrgw.SZRGWClient.java

public Document buildGetIdentityLinkRequest(String PEPSIdentifier, String PEPSFirstname, String PEPSFamilyname,
        String PEPSDateOfBirth, String signature, String representative, String represented,
        String mandateContent) throws SZRGWClientException {

    String SZRGW_NS = "http://reference.e-government.gv.at/namespace/szrgw/20070807#";
    try {/*  ww  w. java 2s. com*/
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.newDocument();

        Element getIdentityLink = doc.createElementNS(SZRGW_NS, "szrgw:GetIdentityLinkRequest");
        getIdentityLink.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:szrgw", SZRGW_NS);
        doc.appendChild(getIdentityLink);

        if ((PEPSIdentifier != null) || (PEPSFirstname != null) || (PEPSFamilyname != null)
                || (PEPSDateOfBirth != null)) {

            Element pepsDataElem = doc.createElementNS(SZRGW_NS, "szrgw:PEPSData");
            getIdentityLink.appendChild(pepsDataElem);

            if (PEPSIdentifier != null) {
                Element elem = doc.createElementNS(SZRGW_NS, "szrgw:Identifier");
                pepsDataElem.appendChild(elem);
                Text text = doc.createTextNode(PEPSIdentifier);
                elem.appendChild(text);
            }
            if (PEPSFirstname != null) {
                Element elem = doc.createElementNS(SZRGW_NS, "szrgw:Firstname");
                pepsDataElem.appendChild(elem);
                Text text = doc.createTextNode(PEPSFirstname);
                elem.appendChild(text);
            }

            if (PEPSFamilyname != null) {
                Element elem = doc.createElementNS(SZRGW_NS, "szrgw:Familyname");
                pepsDataElem.appendChild(elem);
                Text text = doc.createTextNode(PEPSFamilyname);
                elem.appendChild(text);
            }

            if (PEPSDateOfBirth != null) {
                Element elem = doc.createElementNS(SZRGW_NS, "szrgw:DateOfBirth");
                pepsDataElem.appendChild(elem);
                Text text = doc.createTextNode(PEPSDateOfBirth);
                elem.appendChild(text);
            }

            if (representative != null) {
                Element elem = doc.createElementNS(SZRGW_NS, "szrgw:Representative");
                pepsDataElem.appendChild(elem);
                Text text = doc.createTextNode(representative);
                elem.appendChild(text);
            }

            if (represented != null) {
                Element elem = doc.createElementNS(SZRGW_NS, "szrgw:Represented");
                pepsDataElem.appendChild(elem);
                Text text = doc.createTextNode(represented);
                elem.appendChild(text);
            }

            if (mandateContent != null) {
                Element elem = doc.createElementNS(SZRGW_NS, "szrgw:MandateContent");
                pepsDataElem.appendChild(elem);
                Text text = doc.createTextNode(mandateContent);
                elem.appendChild(text);
            }
        }

        if (signature == null)
            throw new SZRGWClientException("Signature element must not be null!");
        else {
            Element sig = doc.createElementNS(SZRGW_NS, "szrgw:Signature");
            Element base64content = doc.createElementNS(SZRGW_NS, "szrgw:Base64Content");
            sig.appendChild(base64content);
            getIdentityLink.appendChild(sig);
            Text text = doc.createTextNode(signature);
            base64content.appendChild(text);
        }

        if (representative != null && represented != null && mandateContent != null) {
            Element mis = doc.createElementNS(SZRGW_NS, "szrgw:MIS");
            Element filters = doc.createElementNS(SZRGW_NS, "szrgw:Filters");
            mis.appendChild(filters);
            Element target = doc.createElementNS(SZRGW_NS, "szrgw:Target");
            mis.appendChild(target);
            Element friendlyName = doc.createElementNS(SZRGW_NS, "szrgw:OAFriendlyName");
            mis.appendChild(friendlyName);
            getIdentityLink.appendChild(mis);

            //            TODO fetch data from oa params
            //             String moasessionid = req.getParameter(MOAIDAuthConstants.PARAM_SESSIONID);
            //             moasessionid = StringEscapeUtils.escapeHtml(moasessionid);
            //              AuthenticationSession moasession = AuthenticationSessionStoreage.getSession(moasessionid);
            //             OAAuthParameter oaParam = AuthConfigurationProvider.getInstance().getOnlineApplicationParameter(moasession.getPublicOAURLPrefix());
            //             if (oaParam == null)
            //                   throw new AuthenticationException("auth.00", new Object[] { moasession.getPublicOAURLPrefix() });
            //              Text text = doc.createTextNode(oaParam.getFriendlyName());
        }

        return doc;
    } catch (ParserConfigurationException e) {
        throw new SZRGWClientException(e);
    } /*catch (CertificateEncodingException e) {
       throw new SZRGWClientException(e);
      }*/

}

From source file:org.openmeetings.app.data.record.BatikMethods.java

public void drawText(SVGGraphics2D g2d, int x, int y, int width, int height, String text,
        String default_export_font, int style, int size, Color fontColor, Document document) throws Exception {

    //      g2d.setClip(x, y, width, height);
    //      g2d.setColor(Color.black);
    //      g2d.drawString(text, x, y+20);

    String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;

    // Get the root element (the 'svg' element).
    Element svgRoot = document.getDocumentElement();

    log.debug("svgNS DEBUG: " + svgNS);

    //Element textElement = g2d.getDOMFactory().createElementNS(svgNS,"text");
    Element rectangle = document.createElementNS(svgNS, "rect");
    rectangle.setAttributeNS(svgNS, "x", "10");
    rectangle.setAttributeNS(svgNS, "y", "20");
    rectangle.setAttributeNS(svgNS, "width", "100");
    rectangle.setAttributeNS(svgNS, "height", "50");
    rectangle.setAttributeNS(svgNS, "fill", "red");

    // Attach the rectangle to the root 'svg' element.
    svgRoot.appendChild(rectangle);/*w w  w.  j  ava2s .  c om*/

}

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

private void stroke(StringBuilder strb) {
    Element path = doc.createElementNS(svgNS, "path");
    addStrokeAttributes(path);//www  .  ja va2s .  co  m
    path.setAttributeNS(null, "fill", "none");
    path.setAttributeNS(null, "d", strb.toString());

    append(path);
}

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

@Override
public void drawLine(double x1, double y1, double x2, double y2) {
    Element path = doc.createElementNS(svgNS, "path");
    addStrokeAttributes(path);/*from w ww.  j  av a  2s  .co  m*/
    path.setAttributeNS(null, "fill", "none");
    path.setAttributeNS(null, "d", String.format(Locale.US, "M %f,%f %f,%f", x1, y1, x2, y2));

    append(path);
}

From source file:Sax2Dom.java

public void startElement(String namespace, String localName, String qName, Attributes attrs) {
    final Element tmp = (Element) _document.createElementNS(namespace, qName);

    // Add namespace declarations first
    if (_namespaceDecls != null) {
        final int nDecls = _namespaceDecls.size();
        for (int i = 0; i < nDecls; i++) {
            final String prefix = (String) _namespaceDecls.elementAt(i++);

            if (prefix == null || prefix.equals(EMPTYSTRING)) {
                tmp.setAttributeNS(XMLNS_URI, XMLNS_PREFIX, (String) _namespaceDecls.elementAt(i));
            } else {
                tmp.setAttributeNS(XMLNS_URI, XMLNS_STRING + prefix, (String) _namespaceDecls.elementAt(i));
            }/*from w  w  w  .jav  a  2s .com*/
        }
        _namespaceDecls.clear();
    }

    // Add attributes to element
    final int nattrs = attrs.getLength();
    for (int i = 0; i < nattrs; i++) {
        if (attrs.getLocalName(i) == null) {
            tmp.setAttribute(attrs.getQName(i), attrs.getValue(i));
        } else {
            tmp.setAttributeNS(attrs.getURI(i), attrs.getQName(i), attrs.getValue(i));
        }
    }

    // Append this new node onto current stack node
    Node last = (Node) _nodeStk.peek();
    last.appendChild(tmp);

    // Push this node onto stack
    _nodeStk.push(tmp);
}