diff --git a/src/runtime/java/Makefile b/src/runtime/java/Makefile new file mode 100644 index 000000000..596735f11 --- /dev/null +++ b/src/runtime/java/Makefile @@ -0,0 +1,7 @@ +all: + libtool --mode=compile gcc -I /usr/lib/jvm/java-6-openjdk/include -std=c99 -c jpgf.c + libtool --mode=link gcc -g -O -o libjpgf.la jpgf.lo -rpath /usr/lib -lpgf -lgu + libtool --mode=install cp libjpgf.la /usr/lib/libjpgf.la + +headers: + javah org.grammaticalframework.PGF diff --git a/src/runtime/java/jpgf.c b/src/runtime/java/jpgf.c new file mode 100644 index 000000000..4796e32dd --- /dev/null +++ b/src/runtime/java/jpgf.c @@ -0,0 +1,31 @@ +#include +#include +#include "org_grammaticalframework_PGF.h" + +JNIEXPORT jobject JNICALL +Java_org_grammaticalframework_PGF_readPGF(JNIEnv *env, jclass cls, jstring s) +{ + const char *fpath = (*env)->GetStringUTFChars(env, s, 0); + + GuPool* pool = gu_new_pool(); + GuPool* tmp_pool = gu_local_pool(); + + // Create an exception frame that catches all errors. + GuExn* err = gu_new_exn(NULL, gu_kind(type), tmp_pool); + + // Read the PGF grammar. + PgfPGF* pgf = pgf_read(fpath, pool, err); + if (!gu_ok(err)) { + gu_pool_free(pool); + gu_pool_free(tmp_pool); + return NULL; + } + + gu_pool_free(tmp_pool); + + (*env)->ReleaseStringUTFChars(env, s, fpath); + + jmethodID constrId = (*env)->GetMethodID(env, cls, "", "(II)V"); + + return (*env)->NewObject(env, cls, constrId, (int) pool, (int) pgf); +} diff --git a/src/runtime/java/org/grammaticalframework/PGF.java b/src/runtime/java/org/grammaticalframework/PGF.java new file mode 100644 index 000000000..777edbd98 --- /dev/null +++ b/src/runtime/java/org/grammaticalframework/PGF.java @@ -0,0 +1,25 @@ +package org.grammaticalframework; + +public class PGF { + static native PGF readPGF(String path); + + private int pool; + private int gr; + + private PGF(int pool, int gr) { + this.pool = pool; + this.gr = gr; + } + + static { + System.loadLibrary("jpgf"); + } + + public void test() { + System.out.println("pool="+pool+", gr="+gr); + } + + public static void main(String[] args) { + readPGF("/home/krasimir/www.grammaticalframework.org/treebanks/PennTreebank/ParseEngAbs.pgf").test(); + } +}