From: ishmal Date: Wed, 2 Apr 2008 21:57:44 +0000 (+0000) Subject: Improve buf<-->xml doc conversion X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=bdab8bcc2c214b47d1ed29a977c54851a32f05d4;p=inkscape.git Improve buf<-->xml doc conversion --- diff --git a/src/bind/java/org/inkscape/cmn/Gateway.java b/src/bind/java/org/inkscape/cmn/Gateway.java index bba9cdf0b..d1fa47a2b 100644 --- a/src/bind/java/org/inkscape/cmn/Gateway.java +++ b/src/bind/java/org/inkscape/cmn/Gateway.java @@ -31,11 +31,18 @@ import java.io.OutputStream; import java.io.IOException; import javax.swing.JOptionPane; -//for xml +//####for xml +//read import org.w3c.dom.Document; import java.io.ByteArrayInputStream; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; +//write +import java.io.ByteArrayOutputStream; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.Transformer; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; import org.inkscape.script.ScriptConsole; @@ -56,33 +63,43 @@ long backPtr; //######################################################################## //# MESSSAGES //######################################################################## -static void err(String message) +void err(String message) { - System.err.println("Gateway err:" + message); + ScriptConsole console = ScriptConsole.getInstance(); + if (console != null) + console.err("Gateway err:" + message); + else + log("Gateway err:" + message); } -static void msg(String message) +void msg(String message) { - System.out.println("Gateway:" + message); + ScriptConsole console = ScriptConsole.getInstance(); + if (console != null) + console.msg("Gateway err:" + message); + else + log("Gateway:" + message); } - +void trace(String message) +{ + ScriptConsole console = ScriptConsole.getInstance(); + if (console != null) + console.trace("Gateway:" + message); + else + log("Gateway:" + message); +} //######################################################################## -//# R E P R (inkscape's xml tree) +//# U T I L I T Y //######################################################################## -private native String documentGet(long backPtr); - -public String documentGet() -{ - return documentGet(backPtr); -} - -public Document documentGetXml() +/** + * Parse a String to an XML Document + */ +public Document stringToDoc(String xmlStr) { - String xmlStr = documentGet(); if (xmlStr == null || xmlStr.length()==0) return null; Document doc = null; @@ -94,19 +111,78 @@ public Document documentGetXml() } catch (java.io.IOException e) { - err("getReprXml:" + e); + err("stringToDoc:" + e); + return null; } catch (javax.xml.parsers.ParserConfigurationException e) { - err("getReprXml:" + e); + err("stringToDoc:" + e); + return null; } catch (org.xml.sax.SAXException e) { - err("getReprXml:" + e); + err("stringToDoc:" + e); + return null; } return doc; } + + +/** + * Serialize an XML Document to a string + */ +String docToString(Document doc) +{ + if (doc == null) + return ""; + String buf = ""; + try + { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + TransformerFactory factory = TransformerFactory.newInstance(); + Transformer tf = factory.newTransformer(); + tf.transform(new DOMSource(doc), new StreamResult(baos)); + baos.close(); + buf = baos.toString(); + } + catch (java.io.IOException e) + { + err("docToString:" + e); + return null; + } + catch (javax.xml.transform.TransformerConfigurationException e) + { + err("docToString:" + e); + return null; + } + catch (javax.xml.transform.TransformerException e) + { + err("docToString:" + e); + return null; + } + return buf; +} + + +//######################################################################## +//# R E P R (inkscape's xml tree) +//######################################################################## + +private native String documentGet(long backPtr); + +public String documentGet() +{ + return documentGet(backPtr); +} + + +public Document documentGetXml() +{ + String xmlStr = documentGet(); + return stringToDoc(xmlStr); +} + private native boolean documentSet(long backPtr, String xmlStr); public boolean documentSet(String xmlStr) @@ -114,6 +190,13 @@ public boolean documentSet(String xmlStr) return documentSet(backPtr, xmlStr); } +public boolean documentSetXml(Document doc) +{ + String xmlStr = docToString(doc); + return documentSet(xmlStr); +} + + //######################################################################## //# LOGGING STREAM //######################################################################## diff --git a/src/bind/java/org/inkscape/script/ScriptConsole.java b/src/bind/java/org/inkscape/script/ScriptConsole.java index c7d71b575..8241e2797 100644 --- a/src/bind/java/org/inkscape/script/ScriptConsole.java +++ b/src/bind/java/org/inkscape/script/ScriptConsole.java @@ -78,17 +78,17 @@ JComboBox engineBox; //######################################################################## //# MESSSAGES //######################################################################## -void err(String fmt, Object... arguments) +public void err(String fmt, Object... arguments) { terminal.errorf("ScriptConsole err:" + fmt + "\n", arguments); } -void msg(String fmt, Object... arguments) +public void msg(String fmt, Object... arguments) { terminal.outputf("ScriptConsole:" + fmt, arguments); } -void trace(String fmt, Object... arguments) +public void trace(String fmt, Object... arguments) { terminal.outputf("ScriptConsole:" + fmt + "\n", arguments); }