xmlgraphics-batik-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sugaw...@humane-systems.co.jp (sugawara)
Subject RE: modifying css in live svg document
Date Tue, 11 Apr 2006 05:17:57 GMT
Hi Roger,

To hack the style sheet;
---- following must run under a RunnableQueue thread, needless to say ----
SVGSVGElement root = document.getRootElement();
SVGOMStyleElement style =
(SVGOMStyleElement)getFirstChildByNodeName(root,"style");
for (Node node=style.getFirstChild(); node!=null;
node=node.getNextSibling()) {
    if (node.getNodeType()==Node.CDATA_SECTION_NODE) {
        CDATASection cdata = (CDATASection)node;
        String value = cdata.getNodeValue();
        int indx=0,indx1=0,indx2;
        switch (textType) {
        case GText.BLOCKNAME:	indx = value.indexOf(".bname");  break;
        case GText.PORTNAME:	indx = value.indexOf(".pname");  break;
        case GText.RESULT:		indx = value.indexOf(".result"); break;
        }
        indx1 = value.indexOf("fill:", indx);
        if (indx1 < 0) {
            indx1 = value.indexOf('}', indx);
            cdata.insertData(indx1, ";fill:"+color);
        } else {
            indx1 += 5;
            indx2 = value.indexOf('}', indx1);
            cdata.replaceData(indx1, (indx2-indx1), color);
        }
        return;
    }
}

public static Element getFirstChildByNodeName(Element parent, String
nodeName) {
    for (Node node=parent.getFirstChild(); node!=null;
node=node.getNextSibling()) {
        if (node.getNodeType()==Node.ELEMENT_NODE &&
nodeName.equals(node.getNodeName()))
            return (Element)node;
        }
    return null;
}

Also, to get the modified CSS document as a string;

    public static String serialize( SVGDocument doc ) {
        String svg = null;
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            OutputFormat format = new OutputFormat(doc, "UTF-8", true);
            format.setLineWidth(256);
            DOMSerializer serializer = new XMLSerializer(baos,
format).asDOMSerializer();
            serializer.serialize(doc);
            svg = baos.toString("UTF-8");
        } catch (Exception e) { e.printStackTrace(); }
        return svg;
    }

Hope this will help.

Shin

> -----Original Message-----
> From: rogercritchlow@gmail.com
> [mailto:rogercritchlow@gmail.com]On Behalf Of Roger Critchlow
> Sent: Sunday, April 09, 2006 4:10 PM
> To: batik-users@xmlgraphics.apache.org
> Subject: modifying css in live svg document
>
>
> Hi --
>
> Anyone have some examples of modifying the css stylesheet for a live
> svg document?
>
> I want to edit the styles for classes of svg elements, see the results
> in the JSVGCanvas, and then get the modified style sheet as a string
> to save to a file.
>
> I've found out how to get the original style sheet and convert it to a
> string.  I can see how to hack the attributes of the SVG document and
> get the modified SVG document as a string.  But it isn't obvious how
> to hack the style sheet and get the modified CSS document as a string.
>
> -- rec --
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
> For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org
>
>



---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org


Mime
View raw message