xmlgraphics-batik-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Roger Critchlow" <...@elf.org>
Subject Re: modifying css in live svg document
Date Thu, 13 Apr 2006 14:44:50 GMT
Hi Shin --

This is helpful.

I was keeping the stylesheet in an external file included with an
 <?xml-stylesheet href=... /> processing instruction.  So I was able
to get the StyleSheet interface object with:

    /*
     * Get the stylesheet for our document.
     * This fetches an external stylesheet.
     */
    protected StyleSheet getStyleSheet(SVGDocument doc) {
    	NodeList list = doc.getChildNodes();
    	for (int i = 0; i < list.getLength(); i += 1) {
    		if (list.item(i) instanceof SVGStyleSheetProcessingInstruction)
    			return ((SVGStyleSheetProcessingInstruction)list.item(i)).getCSSStyleSheet();
    	}
    	return null;
    }

I was then serializing the style sheet with:

    /*
     * Write the stylesheet to out.
     */
    protected void writeCSS(PrintStream out) {
    	CSSEngine eng = ((SVGOMDocument)svgDoc).getCSSEngine();
    	StyleSheet css = getStyleSheet(svgDoc);
    	if (css != null && eng != null)
    		out.print(css.toString(eng));
    }

Using the <style></style> element, as you suggest, I find that the
style sheet is serialized with the svg when I use:

    /*
     * Write the svg document to out.
     */
    protected void writeSVG(PrintStream out) {
        StringWriter sw = new StringWriter();
        try {
            DOMUtilities.writeDocument(svgDoc, sw);
        } catch (IOException e) {
            throw new Error("IO");
        }
        out.print(sw.toString());
    }

I guess I was hoping for some clues to using the CSS DOM to get at and
modify the parsed rules for the style sheet.  I'll keep looking to see
if I can make that work.

Thank you,

-- rec --

On 4/10/06, sugawara <sugawara@humane-systems.co.jp> wrote:
> 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
>
>

---------------------------------------------------------------------
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