Search in sources :

Example 1 with SVGAElement

use of org.w3c.dom.svg.SVGAElement in project scout.rt by eclipse.

the class SVGUtility method removeHyperlink.

/**
 * A link is enclosed to the main element
 * <p>
 * Moves move all inner elements out of the link element and removes the link element
 */
public static void removeHyperlink(SVGAElement e) {
    if (SVGConstants.SVG_A_TAG.equals(e.getTagName())) {
        NodeList nodes = e.getChildNodes();
        for (int i = 0, n = nodes.getLength(); i < n; i++) {
            Node node = nodes.item(i);
            if (node instanceof Element) {
                e.getParentNode().insertBefore(nodes.item(i), e);
            }
        }
        e.getParentNode().removeChild(e);
    }
}
Also used : NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) SVGTSpanElement(org.w3c.dom.svg.SVGTSpanElement) SVGTextContentElement(org.w3c.dom.svg.SVGTextContentElement) SVGElement(org.w3c.dom.svg.SVGElement) Element(org.w3c.dom.Element) SVGAElement(org.w3c.dom.svg.SVGAElement) SVGPoint(org.w3c.dom.svg.SVGPoint)

Example 2 with SVGAElement

use of org.w3c.dom.svg.SVGAElement in project scout.rt by eclipse.

the class SVGUtility method addHyperlink.

/**
 * Enclose the element with a link to an url
 * <p>
 * Bug fix: batik sometimes creates a new namespace for xlink even though the namespace is already defined. This
 * utility method fixes that behaviour.
 * <p>
 * Bug:
 * <xmp><a xlink:actuate="onRequest" xlink:type="simple" xlink:show="replace" xmlns:ns3="http://www.w3.org/1999/xlink"
 * ns3:href="http://local/info-prev">....</a></xmp>
 * <p>
 * Fixed:
 * <xmp><a xlink:actuate="onRequest" xlink:type="simple" xlink:show="replace" xlink:href="http://local/info-prev">....
 * </a></xmp>
 */
public static void addHyperlink(Element e, String url) {
    SVGAElement aElem = (SVGAElement) e.getOwnerDocument().createElementNS(SVG_NS, "a");
    e.getParentNode().insertBefore(aElem, e);
    e.getParentNode().removeChild(e);
    aElem.appendChild(e);
    aElem.getHref().setBaseVal(url);
    // bug fix: remove xmlns:xlink=... attributes, change attributes of ns xlink to have name prefixed with 'xlink'
    NamedNodeMap nnmap = aElem.getAttributes();
    for (int i = 0, n = nnmap.getLength(); i < n; i++) {
        Node node = nnmap.item(i);
        if (node instanceof Attr) {
            Attr a = (Attr) node;
            if (XLINK_NS.equals(a.getNamespaceURI()) && !"xlink".equals(a.getPrefix())) {
                nnmap.removeNamedItemNS(a.getNamespaceURI(), a.getLocalName());
                a.setPrefix("xlink");
                nnmap.setNamedItemNS(a);
            }
        }
    }
    for (int i = 0, n = nnmap.getLength(); i < n; i++) {
        Node node = nnmap.item(i);
        if (node instanceof Attr) {
            Attr a = (Attr) node;
            if (javax.xml.XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(a.getNamespaceURI())) {
                nnmap.removeNamedItemNS(a.getNamespaceURI(), a.getLocalName());
            }
        }
    }
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) Node(org.w3c.dom.Node) SVGAElement(org.w3c.dom.svg.SVGAElement) SVGPoint(org.w3c.dom.svg.SVGPoint) Attr(org.w3c.dom.Attr)

Example 3 with SVGAElement

use of org.w3c.dom.svg.SVGAElement in project scout.rt by eclipse.

the class SVGUtilityTest method assertHyperlinkAdded.

private void assertHyperlinkAdded(Element e, String url) {
    if (!(e.getParentNode() instanceof SVGAElement)) {
        Assert.fail("parent is not an 'a-tag'");
    }
    SVGAElement parent = (SVGAElement) e.getParentNode();
    Assert.assertEquals(parent.getHref().getBaseVal(), url);
}
Also used : SVGAElement(org.w3c.dom.svg.SVGAElement)

Aggregations

SVGAElement (org.w3c.dom.svg.SVGAElement)3 Node (org.w3c.dom.Node)2 SVGPoint (org.w3c.dom.svg.SVGPoint)2 Attr (org.w3c.dom.Attr)1 Element (org.w3c.dom.Element)1 NamedNodeMap (org.w3c.dom.NamedNodeMap)1 NodeList (org.w3c.dom.NodeList)1 SVGElement (org.w3c.dom.svg.SVGElement)1 SVGTSpanElement (org.w3c.dom.svg.SVGTSpanElement)1 SVGTextContentElement (org.w3c.dom.svg.SVGTextContentElement)1