d1fa47a2b9efb15e2436ac0830291d9bb9061ab3
1 /**
2 * This is a simple mechanism to bind Inkscape to Java, and thence
3 * to all of the nice things that can be layered upon that.
4 *
5 * Authors:
6 * Bob Jamison
7 *
8 * Copyright (C) 2007-2008 Bob Jamison
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 3 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 */
25 package org.inkscape.cmn;
27 import java.util.List;
28 import java.io.FileReader;
29 import java.io.PrintStream;
30 import java.io.OutputStream;
31 import java.io.IOException;
32 import javax.swing.JOptionPane;
34 //####for xml
35 //read
36 import org.w3c.dom.Document;
37 import java.io.ByteArrayInputStream;
38 import javax.xml.parsers.DocumentBuilder;
39 import javax.xml.parsers.DocumentBuilderFactory;
40 //write
41 import java.io.ByteArrayOutputStream;
42 import javax.xml.transform.TransformerFactory;
43 import javax.xml.transform.Transformer;
44 import javax.xml.transform.dom.DOMSource;
45 import javax.xml.transform.stream.StreamResult;
47 import org.inkscape.script.ScriptConsole;
51 /**
52 * Provide a gateway from C to Java, to simplify adding
53 * interfaces.
54 */
55 public class Gateway
56 {
57 /**
58 * Pointer back to the BinderyImpl C++ object that launched me
59 */
60 long backPtr;
63 //########################################################################
64 //# MESSSAGES
65 //########################################################################
66 void err(String message)
67 {
68 ScriptConsole console = ScriptConsole.getInstance();
69 if (console != null)
70 console.err("Gateway err:" + message);
71 else
72 log("Gateway err:" + message);
73 }
75 void msg(String message)
76 {
77 ScriptConsole console = ScriptConsole.getInstance();
78 if (console != null)
79 console.msg("Gateway err:" + message);
80 else
81 log("Gateway:" + message);
82 }
84 void trace(String message)
85 {
86 ScriptConsole console = ScriptConsole.getInstance();
87 if (console != null)
88 console.trace("Gateway:" + message);
89 else
90 log("Gateway:" + message);
91 }
94 //########################################################################
95 //# U T I L I T Y
96 //########################################################################
98 /**
99 * Parse a String to an XML Document
100 */
101 public Document stringToDoc(String xmlStr)
102 {
103 if (xmlStr == null || xmlStr.length()==0)
104 return null;
105 Document doc = null;
106 try
107 {
108 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
109 DocumentBuilder parser = factory.newDocumentBuilder();
110 doc = parser.parse(new ByteArrayInputStream(xmlStr.getBytes()));
111 }
112 catch (java.io.IOException e)
113 {
114 err("stringToDoc:" + e);
115 return null;
116 }
117 catch (javax.xml.parsers.ParserConfigurationException e)
118 {
119 err("stringToDoc:" + e);
120 return null;
121 }
122 catch (org.xml.sax.SAXException e)
123 {
124 err("stringToDoc:" + e);
125 return null;
126 }
127 return doc;
128 }
132 /**
133 * Serialize an XML Document to a string
134 */
135 String docToString(Document doc)
136 {
137 if (doc == null)
138 return "";
139 String buf = "";
140 try
141 {
142 ByteArrayOutputStream baos = new ByteArrayOutputStream();
143 TransformerFactory factory = TransformerFactory.newInstance();
144 Transformer tf = factory.newTransformer();
145 tf.transform(new DOMSource(doc), new StreamResult(baos));
146 baos.close();
147 buf = baos.toString();
148 }
149 catch (java.io.IOException e)
150 {
151 err("docToString:" + e);
152 return null;
153 }
154 catch (javax.xml.transform.TransformerConfigurationException e)
155 {
156 err("docToString:" + e);
157 return null;
158 }
159 catch (javax.xml.transform.TransformerException e)
160 {
161 err("docToString:" + e);
162 return null;
163 }
164 return buf;
165 }
168 //########################################################################
169 //# R E P R (inkscape's xml tree)
170 //########################################################################
172 private native String documentGet(long backPtr);
174 public String documentGet()
175 {
176 return documentGet(backPtr);
177 }
180 public Document documentGetXml()
181 {
182 String xmlStr = documentGet();
183 return stringToDoc(xmlStr);
184 }
186 private native boolean documentSet(long backPtr, String xmlStr);
188 public boolean documentSet(String xmlStr)
189 {
190 return documentSet(backPtr, xmlStr);
191 }
193 public boolean documentSetXml(Document doc)
194 {
195 String xmlStr = docToString(doc);
196 return documentSet(xmlStr);
197 }
200 //########################################################################
201 //# LOGGING STREAM
202 //########################################################################
204 public native void logWrite(long backptr, int ch);
206 class LogStream extends OutputStream
207 {
209 public void write(int ch)
210 {
211 logWrite(backPtr, ch);
212 }
214 }
216 PrintStream log = null;
218 /**
219 * printf-style logging
220 */
221 void log(String fmt, Object... args)
222 {
223 log.printf("Gateway:" + fmt, args);
224 }
227 //########################################################################
228 //# RUN
229 //########################################################################
232 /**
233 * Run a script buffer
234 *
235 * @param backPtr pointer back to the C context that called this
236 * @param lang the scripting language to run
237 * @param str the script buffer to execute
238 * @return true if successful, else false
239 */
240 public boolean scriptRun(String lang, String str)
241 {
242 //wrap whole thing in try/catch, since this will
243 //likely be called from C
244 try
245 {
246 ScriptConsole console = ScriptConsole.getInstance();
247 if (console == null)
248 {
249 err("ScriptConsole not initialized");
250 return false;
251 }
252 return console.doRun(lang, str);
253 }
254 catch (Exception e)
255 {
256 err("run :" + e);
257 e.printStackTrace();
258 return false;
259 }
260 }
263 /**
264 * Run a script file
265 *
266 * @param backPtr pointer back to the C context that called this
267 * @param lang the scripting language to run
268 * @param fname the script file to execute
269 * @return true if successful, else false
270 */
271 public boolean scriptRunFile(String lang, String fname)
272 {
273 //wrap whole thing in try/catch, since this will
274 //likely be called from C
275 try
276 {
277 {
278 ScriptConsole console = ScriptConsole.getInstance();
279 if (console == null)
280 {
281 err("ScriptConsole not initialized");
282 return false;
283 }
284 return console.doRun(lang, fname);
285 }
286 }
287 catch (Exception e)
288 {
289 err("scriptRunFile :" + e);
290 return false;
291 }
292 }
298 //########################################################################
299 //# C O N S O L E
300 //########################################################################
303 public boolean showConsole()
304 {
305 ScriptConsole.getInstance().setVisible(true);
306 return true;
307 }
310 //########################################################################
311 //# CONSTRUCTOR
312 //########################################################################
317 /**
318 * Constructor
319 * @param backPtr pointer back to the C context that called this
320 */
321 public Gateway(long backPtr)
322 {
323 /**
324 * Set up the logging stream
325 */
326 log = new PrintStream(new LogStream());
328 //Point back to C++ object
329 this.backPtr = backPtr;
331 _instance = this;
332 }
334 private static Gateway _instance = null;
336 public static Gateway getInstance()
337 {
338 return _instance;
339 }
341 }
342 //########################################################################
343 //# E N D O F F I L E
344 //########################################################################