0
我想從一個源文件的元素組成一個svg與蠟染。只要元素不引用defs部分中定義的東西(比如漸變或過濾器),這就可以工作。但是當過濾器被引用時,我得到一個異常。我試圖複製defs部分,但沒有幫助。蠟染複製從一個文件到另一個文件的元素
String parser = XMLResourceDescriptor.getXMLParserClassName();
SAXSVGDocumentFactory factory = new SAXSVGDocumentFactory(parser);
Document source = factory.createDocument("source", getClass().getResourceAsStream("/artwork/source.svg"));
SVGOMDocument target = (SVGOMDocument) domImpl.createDocument(svgNS, "svg", null);
Node defs = source.getElementsByTagName("defs").item(0).cloneNode(true);
target.adoptNode(defs);
target.getRootElement().appendChild(defs);
BridgeContext ctx = new BridgeContext(new UserAgentAdapter());
GVTBuilder builder = new GVTBuilder();
builder.build(ctx, target);
Element sourceEl = getElementByXPath(source,
"//*[@id='IMAGES']/*[@id='" + selection.getImageSet() + "']/*[@id='"
+ suit.abbreviation() + value + "-" + selection.getImageSet() + "']");
SVGOMElement complete = (SVGOMElement) sourceEl.cloneNode(true);
target.adoptNode(complete);
target.getDocumentElement().appendChild(complete);
Rectangle2D completeBBox = builder.build(ctx, complete).getSensitiveBounds();
最後一行給出例外。任何幫助,將不勝感激。
編輯:
如果我保存目標SVG添加DEFS部分後,然後再重新裝入它的工作原理。但我想避免這個冒險步驟。
你會得到什麼例外? –
引起:org.apache.batik.bridge.BridgeException:null:-1 找不到引用的元素: 「#filter18051」 – gerrit
我想你也必須克隆引用的元素。或者修復引用是遠程引用(假設Batik支持遠程引用)。 –