From 1506fcdc292fa2f4bcf31da921de25a7b2a4925a Mon Sep 17 00:00:00 2001 From: ishmal Date: Wed, 2 Apr 2008 20:39:29 +0000 Subject: [PATCH] Initial capability to load current document into script --- src/bind/java/org/inkscape/cmn/Gateway.java | 57 ++++++++++++++++++-- src/bind/javabind.cpp | 60 ++++++++++++++++----- 2 files changed, 99 insertions(+), 18 deletions(-) diff --git a/src/bind/java/org/inkscape/cmn/Gateway.java b/src/bind/java/org/inkscape/cmn/Gateway.java index 00e720e32..bba9cdf0b 100644 --- a/src/bind/java/org/inkscape/cmn/Gateway.java +++ b/src/bind/java/org/inkscape/cmn/Gateway.java @@ -31,6 +31,12 @@ import java.io.OutputStream; import java.io.IOException; import javax.swing.JOptionPane; +//for xml +import org.w3c.dom.Document; +import java.io.ByteArrayInputStream; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; + import org.inkscape.script.ScriptConsole; @@ -63,6 +69,51 @@ static void msg(String message) +//######################################################################## +//# 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(); + if (xmlStr == null || xmlStr.length()==0) + return null; + Document doc = null; + try + { + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + DocumentBuilder parser = factory.newDocumentBuilder(); + doc = parser.parse(new ByteArrayInputStream(xmlStr.getBytes())); + } + catch (java.io.IOException e) + { + err("getReprXml:" + e); + } + catch (javax.xml.parsers.ParserConfigurationException e) + { + err("getReprXml:" + e); + } + catch (org.xml.sax.SAXException e) + { + err("getReprXml:" + e); + } + return doc; +} + +private native boolean documentSet(long backPtr, String xmlStr); + +public boolean documentSet(String xmlStr) +{ + return documentSet(backPtr, xmlStr); +} + //######################################################################## //# LOGGING STREAM //######################################################################## @@ -199,12 +250,8 @@ public Gateway(long backPtr) private static Gateway _instance = null; -public static Gateway getInstance(long backPtr) +public static Gateway getInstance() { - if (_instance == null) - { - _instance = new Gateway(backPtr); - } return _instance; } diff --git a/src/bind/javabind.cpp b/src/bind/javabind.cpp index 0262aee54..6bffe0cd7 100644 --- a/src/bind/javabind.cpp +++ b/src/bind/javabind.cpp @@ -53,6 +53,10 @@ #include #include +//For repr and document +#include +#include +#include /** * Note: We must limit Java or JVM-specific code to this file @@ -118,65 +122,70 @@ String getExceptionString(JNIEnv *env) return buf; } -jint getInt(JNIEnv *env, jobject obj, const char *name) +jint getObjInt(JNIEnv *env, jobject obj, const char *name) { jfieldID fid = env->GetFieldID(env->GetObjectClass(obj), name, "I"); return env->GetIntField(obj, fid); } -void setInt(JNIEnv *env, jobject obj, const char *name, jint val) +void setObjInt(JNIEnv *env, jobject obj, const char *name, jint val) { jfieldID fid = env->GetFieldID(env->GetObjectClass(obj), name, "I"); env->SetIntField(obj, fid, val); } -jlong getLong(JNIEnv *env, jobject obj, const char *name) +jlong getObjLong(JNIEnv *env, jobject obj, const char *name) { jfieldID fid = env->GetFieldID(env->GetObjectClass(obj), name, "J"); return env->GetLongField(obj, fid); } -void setLong(JNIEnv *env, jobject obj, const char *name, jlong val) +void setObjLong(JNIEnv *env, jobject obj, const char *name, jlong val) { jfieldID fid = env->GetFieldID(env->GetObjectClass(obj), name, "J"); env->SetLongField(obj, fid, val); } -jfloat getFloat(JNIEnv *env, jobject obj, const char *name) +jfloat getObjFloat(JNIEnv *env, jobject obj, const char *name) { jfieldID fid = env->GetFieldID(env->GetObjectClass(obj), name, "F"); return env->GetFloatField(obj, fid); } -void setFloat(JNIEnv *env, jobject obj, const char *name, jfloat val) +void setObjFloat(JNIEnv *env, jobject obj, const char *name, jfloat val) { jfieldID fid = env->GetFieldID(env->GetObjectClass(obj), name, "F"); env->SetFloatField(obj, fid, val); } -jdouble getDouble(JNIEnv *env, jobject obj, const char *name) +jdouble getObjDouble(JNIEnv *env, jobject obj, const char *name) { jfieldID fid = env->GetFieldID(env->GetObjectClass(obj), name, "D"); return env->GetDoubleField(obj, fid); } -void setDouble(JNIEnv *env, jobject obj, const char *name, jdouble val) +void setObjDouble(JNIEnv *env, jobject obj, const char *name, jdouble val) { jfieldID fid = env->GetFieldID(env->GetObjectClass(obj), name, "D"); env->SetDoubleField(obj, fid, val); } -String getString(JNIEnv *env, jobject obj, const char *name) +String getString(JNIEnv *env, jstring jstr) { - jfieldID fid = env->GetFieldID(env->GetObjectClass(obj), name, "Ljava/lang/String;"); - jstring jstr = (jstring)env->GetObjectField(obj, fid); const char *chars = env->GetStringUTFChars(jstr, JNI_FALSE); String str = chars; env->ReleaseStringUTFChars(jstr, chars); return str; } -void setString(JNIEnv *env, jobject obj, const char *name, const String &val) +String getObjString(JNIEnv *env, jobject obj, const char *name) +{ + jfieldID fid = env->GetFieldID(env->GetObjectClass(obj), name, "Ljava/lang/String;"); + jstring jstr = (jstring)env->GetObjectField(obj, fid); + return getString(env, jstr); +} + +void setObjString(JNIEnv *env, jobject obj, const char *name, const String &val) { jstring jstr = env->NewStringUTF(val.c_str()); jfieldID fid = env->GetFieldID(env->GetObjectClass(obj), name, "Ljava/lang/String;"); @@ -635,6 +644,29 @@ static void populateClassPath(const String &javaroot, //======================================================================== // Gateway //======================================================================== +/** + * This is provided to scripts can grab the current copy or the + * repr tree. If anyone has a smarter way of doing this, please implement. + */ +jstring JNICALL documentGet(JNIEnv *env, jobject /*obj*/, jlong ptr) +{ + JavaBinderyImpl *bind = (JavaBinderyImpl *)ptr; + String buf = sp_repr_save_buf((SP_ACTIVE_DOCUMENT)->rdoc); + jstring jstr = env->NewStringUTF(buf.c_str()); + return jstr; +} + +/** + * This is provided to scripts can load an XML tree into Inkscape. + * If anyone has a smarter way of doing this, please implement. + */ +jboolean JNICALL documentSet(JNIEnv *env, jobject /*obj*/, jlong ptr, jstring jstr) +{ + JavaBinderyImpl *bind = (JavaBinderyImpl *)ptr; + String s = getString(env, jstr); + SPDocument *doc = sp_document_new_from_mem(s.c_str(), s.size(), true); +} + /** * This method is used to allow the gateway class to * redirect its logging stream here. @@ -649,7 +681,9 @@ void JNICALL logWrite(JNIEnv */*env*/, jobject /*obj*/, jlong ptr, jint ch) static JNINativeMethod gatewayMethods[] = { -{ (char *)"logWrite", (char *)"(JI)V", (void *)logWrite }, +{ (char *)"documentGet", (char *)"(J)Ljava/lang/String;", (void *)documentGet }, +{ (char *)"documentSet", (char *)"(JLjava/lang/String;)Z", (void *)documentSet }, +{ (char *)"logWrite", (char *)"(JI)V", (void *)logWrite }, { NULL, NULL, NULL } }; -- 2.30.2