hello list,
i have now looked into my old problem again, the one where changes in
the dom tree are not reflected in a saved jpeg. now, it seems that using
createElementNS instead of createElement did resolve parts of the
problem (although the resulting svg looks exactly the same). however, if
i use document.importNode(), to which there is no alternative other than
cloning the entire thing by hand, the tree is again not updated.
what i want to do is this: create an empty svg document. based on a
client's request, select various nodes from other svg documents and copy
them to the current one. (yes, i need to copy, because the documents
have to be self-contained. i cannot just <use> things). now, if
importNode() doesn't update the GVT tree, i have to serialize the thing
and create a new svgdocument from the serialized from, which i obviously
want to avoid. any way around this?
.rm
import java.io.FileOutputStream;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.apache.batik.bridge.BridgeContext;
import org.apache.batik.bridge.DocumentLoader;
import org.apache.batik.bridge.GVTBuilder;
import org.apache.batik.bridge.UserAgent;
import org.apache.batik.bridge.UserAgentAdapter;
import org.apache.batik.dom.svg.SVGDOMImplementation;
import org.apache.batik.gvt.GraphicsNode;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.batik.transcoder.image.JPEGTranscoder;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Node;
import org.w3c.dom.svg.SVGDocument;
public class TestDomTreeUpdate {
private static void addAttribute(Node n, String attname, String
attValue) {
Node attNode = n.getOwnerDocument().createAttribute(attname);
attNode.setNodeValue(attValue);
n.getAttributes().setNamedItem(attNode);
}
public static void main(String[] args) {
try {
DOMImplementation impl = SVGDOMImplementation
.getDOMImplementation();
String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
// SETUP THE SVG DOCUMENT
SVGDocument d = (SVGDocument)
impl.createDocument(svgNS, "svg", null);
Node svgNode = d.getDocumentElement();
Node textNode = d.createElement("text");
svgNode.appendChild(textNode);
addAttribute(svgNode, "width", "400");
addAttribute(svgNode, "height", "400");
// INITIALIZE THE GVTBUILDER
UserAgent userAgent;
DocumentLoader loader;
BridgeContext ctx;
GVTBuilder builder;
GraphicsNode rootGN;
userAgent = new UserAgentAdapter();
loader = new DocumentLoader(userAgent);
ctx = new BridgeContext(userAgent, loader);
ctx.setDynamicState(BridgeContext.DYNAMIC);
builder = new GVTBuilder();
rootGN = builder.build(ctx, d);
// ADD SOME TEXT
Node tspanNode = d.createElement("tspan");
textNode.appendChild(tspanNode);
addAttribute(textNode, "transform", "translate(200
200)");
Node theText = d.createTextNode("FOO BAR");
tspanNode.appendChild(theText);
// SAVING AS JPEG
System.err.println("saving jpeg ... ");
try {
JPEGTranscoder t = new JPEGTranscoder();
t.addTranscodingHint(JPEGTranscoder.KEY_QUALITY, new
Float(1));
TranscoderInput input = new TranscoderInput(d);
TranscoderOutput output = new
TranscoderOutput(new FileOutputStream("test.jpg"));
t.transcode(input,output);
} catch (Exception e) {
e.printStackTrace();
}
// SAVING AS SVG
System.err.println("saving svg ... ");
try {
Source xmlSource = new DOMSource(d);
Result outputTarget = new StreamResult(new
FileOutputStream("test.svg"));
TransformerFactory tf =
TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.transform(xmlSource, outputTarget);
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]