diff --git a/src/Makefile b/src/Makefile index d9faaeb..4bfff6a 100644 --- a/src/Makefile +++ b/src/Makefile @@ -9,6 +9,8 @@ $(JAR): org/pEp/jniadapter/pEpException.java org/pEp/jniadapter/AbstractEngine.j javac org/pEp/jniadapter/*.java jar cf $@ org/pEp/jniadapter/*.class +org_pEp_jniadapter_AbstractEngine.o: org_pEp_jniadapter_AbstractEngine.h org_pEp_jniadapter_AbstractEngine.cc throw_pEp_exception.hh jniutils.hh + org_pEp_jniadapter_AbstractEngine.h: org/pEp/jniadapter/AbstractEngine.java javah org.pEp.jniadapter.AbstractEngine @@ -18,8 +20,6 @@ org_pEp_jniadapter_Engine.h: org/pEp/jniadapter/Engine.java org_pEp_jniadapter_MimeMessage.h: org/pEp/jniadapter/MimeMessage.java javah org.pEp.jniadapter.MimeMessage -org_pEp_jniadapter_AbstractEngine.o: org_pEp_jniadapter_AbstractEngine.h org_pEp_jniadapter_AbstractEngine.cc throw_pEp_exception.hh jniutils.hh - %.o: %.cc %.h jniutils.hh $(LIBRARY): org_pEp_jniadapter_AbstractEngine.o org_pEp_jniadapter_Engine.o org_pEp_jniadapter_MimeMessage.o throw_pEp_exception.o jniutils.o @@ -28,11 +28,8 @@ $(LIBRARY): org_pEp_jniadapter_AbstractEngine.o org_pEp_jniadapter_Engine.o org_ org/pEp/jniadapter/pEpException.java: pEp.yml2 gen_java_exceptions.ysl2 yml2proc -y gen_java_exceptions.ysl2 $< -o $@ -org/pEp/jniadapter/Engine.java : pEp.yml2 gen_java_Engine.ysl2 types_java.ysl2 - yml2proc -y gen_java_Engine.ysl2 $< -o $@ - -org/pEp/jniadapter/MimeMessage.java : pEp.yml2 gen_java_Message.ysl2 types_java.ysl2 - yml2proc -y gen_java_Message.ysl2 $< -o $@ +org/pEp/jniadapter/Engine.java org/pEp/jniadapter/MimeMessage.java : pEp.yml2 gen_java_Message.ysl2 types_java.ysl2 + yml2proc -y gen_java_Message.ysl2 $< throw_pEp_exception.cc throw_pEp_exception.hh: pEp.yml2 gen_throw_pEp_exception.ysl2 textutils.ysl2 yml2proc -y gen_throw_pEp_exception.ysl2 $< -o throw_pEp_exception.cc diff --git a/src/gen_java_Engine.ysl2 b/src/gen_java_Engine.ysl2 deleted file mode 100644 index c5409be..0000000 --- a/src/gen_java_Engine.ysl2 +++ /dev/null @@ -1,32 +0,0 @@ -include yslt.yml2 - -tstylesheet { - include ./types_java.ysl2 - - template "/" - || - package org.pEp.jniadapter; - - final public class Engine extends AbstractEngine { - public Engine() throws pEpException { } - - `` apply "namespace/interface/method"; - } - || - - template "method" - || - public native void «@name»( - `` apply "parm", 2 - ) throws pEpException;`if "position()!=last()" > \n` - || - - template "parm" { - const "dir", "name(*[1])"; - const "type", "name(*[2])"; - const "name", "name(*[3])"; - - | `call "toJava" with "type", "$type"` «translate($name,'-','_')»`if "position()!=last()" > ,` - } -} - diff --git a/src/gen_java_Message.ysl2 b/src/gen_java_Message.ysl2 index fc3a2c2..862fd9d 100644 --- a/src/gen_java_Message.ysl2 +++ b/src/gen_java_Message.ysl2 @@ -3,61 +3,88 @@ include yslt.yml2 tstylesheet { include ./types_java.ysl2 - template "/namespace[@name='pEp']" apply "struct", 0; + template "/namespace[@name='pEp']" apply "struct|interface", 0; - template "struct[@name='message']" -|| -package org.pEp.jniadapter; + template "interface" { + const "cname" call "toJava" with "type", "@name"; + document("org/pEp/jniadapter/{$cname}.java", "text") + || + package org.pEp.jniadapter; -import java.util.Date; + import java.util.ArrayList; -public class MimeMessage { - public enum TextFormat { - plain (0), - html (1), - other (255); + final public class «$cname» extends AbstractEngine { + public «$cname»() throws pEpException { } + `` apply "namespace/interface/method"; + } + || + } - public final int value; + template "struct" { + const "cname" call "toJava" with "type", "@name"; + document("org/pEp/jniadapter/{$cname}.java", "text") + || + package org.pEp.jniadapter; - TextFormat(int value) { - this.value = value; - } - } + import java.util.ArrayList; + import java.util.Date; + import java.util.HashMap; - public enum Direction { - incoming (0), - outgoing (1); + public class «$cname» { + `` apply "enum" + `` apply "*[name(.)!='enum']", mode=entry - public final int value; + } + || + } - Direction(int value) { - this.value = value; + template "enum" + || + public enum «@name» { + `` apply "*", mode=value + ; + + public final int value; + public static final HashMap tag = + new HashMap(); + + «@name»(int value) { + this.value = value; + fill(value); + } + + private void fill(int value) { + tag.put(value, this); + } } - } - public enum EncFormat { - none (0), - pieces (1), - S_MIME (2), - PGP_MIME (3), - PEP (4); + || - public final int value; + template "method" + || + public native void «@name»( + `` apply "parm", 2 + ) throws pEpException;`if "position()!=last()" > \n` + || - EncFormat(int value) { - this.value = value; - } + template "parm" { + const "dir", "name(*[1])"; + const "type", "name(*[2])"; + const "name", "name(*[3])"; + + | `call "toJava" with "type", "$type"` «translate($name,'-','_')»`if "position()!=last()" > ,` } - ``apply "entry"; -} -|| + template "*", mode=entry { + const "type", "name(.)"; + const "name", "name(*[position()=1])"; - template "entry" || - public native †@type ©@name(); - public native void ©@name(†@type value); + private native †$type _©$name(); + private native void _©$name(†$type value); || + } + template "*", mode=value | «name(.)» («.»)`if "position()!=last()" > , ` } diff --git a/src/org/pEp/jniadapter/AbstractEngine.java b/src/org/pEp/jniadapter/AbstractEngine.java index f0a5612..abd8cb2 100644 --- a/src/org/pEp/jniadapter/AbstractEngine.java +++ b/src/org/pEp/jniadapter/AbstractEngine.java @@ -1,15 +1,19 @@ package org.pEp.jniadapter; -abstract class AbstractEngine implements AutoCloseable { - static { - System.loadLibrary("pEpJNI"); - } +import java.util.ArrayList; +import java.io.UnsupportedEncodingException; import java.text.Normalizer; + +abstract class AbstractEngine implements AutoCloseable { static { + System.loadLibrary("pEpJNI"); } - private native void init() throws pEpException; - private native void release(); + private native void init() throws pEpException; private native void + release(); private long handle; - final protected long getHandle() { return handle; } + + final protected long getHandle() { + return handle; + } public AbstractEngine() throws pEpException { init(); @@ -18,5 +22,96 @@ abstract class AbstractEngine implements AutoCloseable { final public void close() { release(); } + + public static byte[] toUTF8(String str) { + try { + String _str = Normalizer.normalize(str, Normalizer.Form.NFC); + return _str.getBytes("UTF-8"); + } + catch (UnsupportedEncodingException e) { + assert false; + return new byte[0]; + } + } + + public static ArrayList toUTF8(ArrayList list) { + ArrayList result = new ArrayList(list.size()); + + for (int i=0; i toUTF8(Pair pair) { + Pair result = new Pair(); + + result.first = toUTF8(pair.first); + result.second = toUTF8(pair.second); + + return result; + } + + public static String toUTF16(byte[] utf8) { + try { + return new String(utf8, "UTF-8"); + } + catch (UnsupportedEncodingException e) { + assert false; + return new String(); + } + } + + public static ArrayList toUTF16(ArrayList list) { + ArrayList result = new ArrayList(list.size()); + + for (int i=0; i toUTF16(Pair pair) { + Pair result = new Pair(); + + result.first = toUTF16(pair.first); + result.second = toUTF16(pair.second); + + return result; + } + + public class _Identity { + public byte[] address; + public byte[] fpr; + public byte[] user_id; + public byte[] username; + CommType comm_type; + public byte[] lang; + public boolean me; + + public _Identity(Identity value) { + address = toUTF8(value.address); + fpr = toUTF8(value.fpr); + user_id = toUTF8(value.user_id); + username = toUTF8(value.username); + comm_type = value.comm_type; + lang = toUTF8(value.lang); + me = value.me; + } + + public Identity getIdentity() { + Identity ident = new Identity(me); + + ident.address = toUTF16(address); + ident.fpr = toUTF16(fpr); + ident.user_id = toUTF16(user_id); + ident.username = toUTF16(username); + ident.comm_type = comm_type; + ident.lang = toUTF16(lang); + ident.me = me; + + return ident; + } + } } diff --git a/src/org/pEp/jniadapter/Identity.java b/src/org/pEp/jniadapter/Identity.java index c544f86..92a5c9b 100644 --- a/src/org/pEp/jniadapter/Identity.java +++ b/src/org/pEp/jniadapter/Identity.java @@ -7,9 +7,14 @@ public class Identity { public String username; CommType comm_type; public String lang; - private boolean me; + public boolean me; - public Identity() { this.me = false; } - public Identity(boolean me) { this.me = me; } + public Identity() { + this.me = false; + } + + public Identity(boolean me) { + this.me = me; + } } diff --git a/src/org/pEp/jniadapter/Pair.java b/src/org/pEp/jniadapter/Pair.java index 9ea7d57..4cddbc0 100644 --- a/src/org/pEp/jniadapter/Pair.java +++ b/src/org/pEp/jniadapter/Pair.java @@ -1,7 +1,7 @@ package org.pEp.jniadapter; public class Pair { - private F first; - private S second; + public F first; + public S second; } diff --git a/src/pEp.yml2 b/src/pEp.yml2 index 9d2ca41..5a0ac78 100644 --- a/src/pEp.yml2 +++ b/src/pEp.yml2 @@ -4,7 +4,6 @@ decl interface @name; decl exception @name; decl method @name; decl struct @name; -decl entry @type @name; namespace pEp { exception pEp_status { @@ -51,7 +50,7 @@ namespace pEp { pEp_unknown_error > -1 }; - interface message_api { + interface Engine { method encrypt_message( in message src, in stringlist extra, @@ -73,26 +72,45 @@ namespace pEp { }; struct message { - entry direction dir; - entry string id; - entry string shortmsg; - entry string longmsg; - entry string longmsg_formatted; - entry bloblist attachments; - entry timestamp sent; - entry timestamp recv; - entry identity from; - entry identitylist to; - entry identity recv_by; - entry identitylist cc; - entry identitylist bcc; - entry identitylist reply_to; - entry stringlist in_reply_to; - entry stringlist references; - entry stringlist keywords; - entry string comments; - entry stringpairlist opt_fields; - entry encformat enc_format; + enum TextFormat { + plain > 0 + html > 1 + other > 255 + } + + enum Direction { + incoming > 0 + outgoing > 1 + } + + enum EncFormat { + none > 0 + pieces > 1 + SMIME > 2 + PGPMIME > 3 + PEP > 4 + } + + direction dir; + string id; + string shortmsg; + string longmsg; + string longmsg_formatted; + bloblist attachments; + timestamp sent; + timestamp recv; + identity from; + identitylist to; + identity recv_by; + identitylist cc; + identitylist bcc; + identitylist reply_to; + stringlist in_reply_to; + stringlist references; + stringlist keywords; + string comments; + stringpairlist opt_fields; + encformat enc_format; }; }; diff --git a/src/types_java.ysl2 b/src/types_java.ysl2 index 8d0b02f..c09300a 100644 --- a/src/types_java.ysl2 +++ b/src/types_java.ysl2 @@ -4,19 +4,33 @@ function "toJava" { param "type"; choose { - when "$type='stringlist'" > String[] + when "$type='stringlist'" > ArrayList when "$type='string'" > String when "$type='message'" > MimeMessage when "$type='identity'" > Identity - when "$type='identitylist'" > Identity[] + when "$type='identitylist'" > ArrayList when "$type='timestamp'" > Date when "$type='encformat'" > MimeMessage.EncFormat - when "$type='bloblist'" > Blob[] - when "$type='stringpairlist'" > Pair[] + when "$type='bloblist'" > ArrayList + when "$type='stringpairlist'" > ArrayList> + when "$type='message'" > MimeMessage otherwise call "CamelCase" with "text", "$type"; } } +function "toIntermediate" { + param "type"; + + choose { + when "$type='stringlist'" > ArrayList + when "$type='stringpairlist'" > ArrayList> + when "$type='string'" > byte[] + + otherwise call "toJava" with "type", "$type"; + } +} + define operator "†([$@]?[a-zA-Z0-9_]+)" as call "toJava" with "type", "%1"; +define operator "¡([$@]?[a-zA-Z0-9_]+)" as call "toIntermediate" with "type", "%1";