You are viewing a plain text version of this content. The canonical link for it is here.
Posted to batik-users@xmlgraphics.apache.org by Daniel Siino <DS...@Intellitrans.com> on 2004/07/14 21:44:25 UTC
Adding a proccessing instruction
Hi,
This has been mentioned in 2 previous posts:
http://mail-archives.apache.org/eyebrowse/ReadMsg?listId=53
<http://mail-archives.apache.org/eyebrowse/ReadMsg?listId=53&msgNo=2188>
&msgNo=2188
http://mail-archives.apache.org/eyebrowse/ReadMsg?listId=53
<http://mail-archives.apache.org/eyebrowse/ReadMsg?listId=53&msgNo=1435>
&msgNo=1435
but it still doesn't work for me. Here's the code
DOMImplementation domImpl = SVGDOMImplementation.getDOMImplementation();
String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
Document document = domImpl.createDocument(svgNS, "svg", null);
String strPI = ("type=\"text/css\" href=\"css/svgstyle.css\" ");
ProcessingInstruction pi =
document.createProcessingInstruction("xml-stylesheet", strPI);
document.insertBefore(pi, document.getDocumentElement() );
and here's the result as displayed by view SVG source in IE (with Adobe
plug-in version 3 and 6)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.0//EN'
'http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd'>
<svg style="stroke-dasharray:none; .... >
<defs id="genericDefs" />
<g>
<g style="fill:aqua; stroke:aqua;">
<rect x="5" y="5" width="600" style="fill:none;" height="400" />
</g>
</g>
</svg>
The content is generated using
svggen.stream(svgOutput, true); but I also tried passing the root
svggen.stream(root, svgOutput, true);
I also tried setting the use css param to false.
The debugger does show that the first child is a SVGStyleSheetInstruction
but it most be removed at some point.
Any help would be greatly appreciated.
Thanks
Daniel
Re: Adding a proccessing instruction
Posted by Cameron McCormack <ca...@aka.mcc.id.au>.
I wrote:
> It seems that the SVG generator doesn't actually output any processing
> instructions it comes across. You could add this support pretty easily
> by modifying org/apache/batik/svggen/XmlWriter.java to add a method to
> write the PIs:
Ok, so it seems it's not so simple. The SVGGraphics2D object will just
copy over the document element from the generated SVG tree to be
serialized. You could, as well as the other changes I mentioned, change
the line in SVGGraphics2D.stream(Element, Writer, boolean) that says
this:
svgDocument.appendChild(svgRoot);
with this:
if (rootParent == null) {
for (Node n = svgRoot.getOwnerDocument().getFirstChild();
n != null; n = n.getNextSibling()) {
int t = n.getNodeType();
if (t == Node.ELEMENT_NODE) {
svgDocument.appendChild(n);
}
if (t == Node.PROCESSING_INSTRUCTION_NODE) {
svgDocument.appendChild(n.cloneNode(false));
}
}
} else {
svgDocument.appendChild(svgRoot);
}
It's a bit hackish, but should work. If you were serializing only a
subtree of the generated document, though, the PIs wouldn't get written
out. (But I'm not sure that you would want them there anyway.)
Cameron
--
Cameron McCormack
| Web: http://mcc.id.au/
| ICQ: 26955922
---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-users-help@xml.apache.org
Re: Adding a proccessing instruction
Posted by Cameron McCormack <ca...@aka.mcc.id.au>.
Hi Daniel.
Daniel Siino:
> DOMImplementation domImpl = SVGDOMImplementation.getDOMImplementation();
>
> String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
>
> Document document = domImpl.createDocument(svgNS, "svg", null);
>
> String strPI = ("type=\"text/css\" href=\"css/svgstyle.css\" ");
>
> ProcessingInstruction pi =
> document.createProcessingInstruction("xml-stylesheet", strPI);
>
> document.insertBefore(pi, document.getDocumentElement() );
...
> The content is generated using
>
> svggen.stream(svgOutput, true); but I also tried passing the root
>
> svggen.stream(root, svgOutput, true);
It seems that the SVG generator doesn't actually output any processing
instructions it comes across. You could add this support pretty easily
by modifying org/apache/batik/svggen/XmlWriter.java to add a method to
write the PIs:
private static void writeXml(ProcessingInstruction pi, IndentWriter out)
throws IOException, SVGGraphics2DIOException {
out.write("<?");
out.write(pi.getTarget());
String data = pi.getData();
if (data.length() > 0) {
out.write(" ");
out.write(data);
}
out.write("?>");
out.write(EOL);
}
and add a case to the switch statement in writeXml(Node, Writer):
case Node.PROCESSING_INSTRUCTION:
writeXml((ProcessingInstruction)node, out);
break;
and add an import line at the top:
import org.w3c.dom.ProcessingInstruction;
Cameron
--
Cameron McCormack
| Web: http://mcc.id.au/
| ICQ: 26955922
---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-users-help@xml.apache.org