簡體   English   中英

蠟染將元素從一個文檔復制到另一個文檔

[英]Batik copy element from one document to another

我正在嘗試從源文檔中的元素組成一個帶有蠟染的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();

最后一行給出了例外。 任何幫助,將不勝感激。

編輯:

如果我在添加defs部分后保存了目標svg,然后再次重新加載它就可以了。 但我想避免這一附加步驟。

我找到了避免額外保存/加載的解決方案。 使用importNode代替克隆/采用def。

Node defs = target.importNode(source.getElementsByTagName("defs").item(0), true);
target.getRootElement().appendChild(defs);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM