p≡p for Java
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

381 lines
12 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
3 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. include yslt.yml2
  2. tstylesheet {
  3. include ./textutils.ysl2
  4. include ./types_java.ysl2
  5. template "/namespace[@name='pEp']" {
  6. apply "struct|enum|exception", 0;
  7. document "../java/foundation/pEp/jniadapter/exceptions/pEpException.java", "text" {
  8. ||
  9. package foundation.pEp.jniadapter.exceptions;
  10. public class pEpException extends RuntimeException {
  11. public pEpException(String message) {
  12. super(message);
  13. }
  14. }
  15. ||
  16. }
  17. document("../../build/marker/gen_java_Message.marker", "text") > ""
  18. }
  19. template "struct" {
  20. const "cname" call "toJava" with "type", "@name";
  21. document("../java/foundation/pEp/jniadapter/{$cname}.java", "text")
  22. ||
  23. package foundation.pEp.jniadapter;
  24. import foundation.pEp.jniadapter.interfaces.*;
  25. import foundation.pEp.jniadapter.exceptions.*;
  26. import java.util.ArrayList;
  27. import java.util.Vector;
  28. import java.util.Date;
  29. import java.util.HashMap;
  30. import java.io.Serializable;
  31. public class «$cname» implements MessageInterface, AutoCloseable, Serializable {
  32. private static final long serialVersionUID = 2119420428331150924L;
  33. private long handle;
  34. private static int instanceCount = 0;
  35. native long init();
  36. native void release(long handle);
  37. public «$cname»() {
  38. handle = init();
  39. instanceCount++;
  40. }
  41. private native long _«$cname»(
  42. byte[] mime_text
  43. ) throws pEpException;
  44. public «$cname»(String mime_text) {
  45. byte[] _mime_text = Utils.toUTF8(mime_text);
  46. handle = _«$cname»(_mime_text);
  47. instanceCount++;
  48. }
  49. private native byte[] _encodeMIME() throws pEpException;
  50. public String encodeMIME() {
  51. return Utils.toUTF16(_encodeMIME());
  52. }
  53. private «$cname»(long h) {
  54. handle = h;
  55. instanceCount++;
  56. }
  57. public synchronized final void close() {
  58. if(handle != 0) {
  59. release(handle);
  60. handle = 0;
  61. instanceCount--;
  62. }
  63. }
  64. public static int getInstanceCount() {
  65. return instanceCount;
  66. }
  67. final protected long getHandle() {
  68. return handle;
  69. }
  70. `` apply "enum", mode=inner
  71. `` apply "*[name(.)!='enum']", mode=entry
  72. }
  73. ||
  74. document("../java/foundation/pEp/jniadapter/interfaces/{$cname}Interface.java", "text")
  75. ||
  76. package foundation.pEp.jniadapter.interfaces;
  77. import foundation.pEp.jniadapter.*;
  78. import foundation.pEp.jniadapter.Message.*;
  79. import java.util.Date;
  80. import java.util.Vector;
  81. import java.util.ArrayList;
  82. public interface «$cname»Interface {
  83. public String encodeMIME();
  84. `` apply "*[name(.)!='enum']", mode=interface
  85. }
  86. ||
  87. }
  88. template "enum" {
  89. const "jname" call "toJava" with "type", "@name";
  90. document("../java/foundation/pEp/jniadapter/{$jname}.java", "text")
  91. ||
  92. // CodeGen template enum
  93. package foundation.pEp.jniadapter;
  94. import java.util.HashMap;
  95. `` apply ".", 0, mode=inner
  96. ||
  97. }
  98. template "enum", mode=inner {
  99. const "jname" call "CamelCase" with "text", "@name";
  100. ||
  101. // CodeGen template enum, mode=inner
  102. public enum «$jname» {
  103. `` apply "enumitem"
  104. ;
  105. public final int value;
  106. private static HashMap<Integer, «$jname»> intMap;
  107. private «$jname»(int value) {
  108. this.value = value;
  109. }
  110. public static «$jname» getByInt(int value){
  111. if (intMap == null) {
  112. intMap = new HashMap<Integer, «$jname»>();
  113. for («$jname» s : «$jname».values()) {
  114. intMap.put(s.value, s);
  115. }
  116. }
  117. if (intMap.containsKey(value)) {
  118. return intMap.get(value);
  119. }
  120. return null;
  121. }
  122. ||
  123. choose {
  124. when "@has_int_str_val = 'true'" {
  125. ||
  126. public String getInternalStringValue() {
  127. return "Unimplemented";
  128. }
  129. ||
  130. }
  131. }
  132. ||
  133. }
  134. ||
  135. }
  136. function "exception" {
  137. param "name";
  138. document "../java/foundation/pEp/jniadapter/exceptions/{$name}.java", "text" {
  139. | package foundation.pEp.jniadapter.exceptions;
  140. |
  141. | public class «$name» extends pEpException {
  142. | public «$name»(String message) {
  143. | super(message);
  144. | }
  145. | }
  146. }
  147. }
  148. template "exception" for "*[text()!=0]" call "exception"
  149. with "name" call "CamelCase" with "text", "name(.)";
  150. template "*", mode=entry {
  151. const "ctype", "name(.)";
  152. const "type" call "toJava" with "type", "name(.)";
  153. const "itype" call "toIntermediate" with "type", "name(.)";
  154. const "name" call "toJava" with "type", "name(*[position()=1])";
  155. ||
  156. // CodeGen template * mode=entry
  157. ||
  158. choose {
  159. when "$ctype = 'identity'" {
  160. ||
  161. // Property type: Identity. [java: «$type», intermediate: «$itype», ctype: «$ctype»]
  162. public «$type» get«$name»() {
  163. «$itype» res = _get«$name»();
  164. if (res != null) {
  165. return new «$type»(_get«$name»());
  166. } else {
  167. return null;
  168. }
  169. }
  170. private native «$itype» _get«$name»();
  171. public void set«$name»(«$type» value) {
  172. if (value != null) {
  173. _set«$name»(new «$itype»(value));
  174. } else {
  175. _set«$name»(null);
  176. }
  177. }
  178. private native void _set«$name»(«$itype» value);
  179. ||
  180. }
  181. when "$ctype = 'identitylist' or $ctype = 'bloblist' or $ctype = 'stringlist' or $ctype = 'stringpairlist'" {
  182. const "ename", "substring-after(substring($type,1,string-length($type)-1), '<')";
  183. const "iename" choose {
  184. when "$ctype = 'stringlist'" > byte[]
  185. when "$ctype = 'stringpairlist'" > Pair<byte[],byte[]>
  186. otherwise > _«$ename»
  187. }
  188. const "convget" choose {
  189. when "$ctype = 'stringlist'" > Utils.toUTF16(i)
  190. when "$ctype = 'stringpairlist'" > new Pair<String, String>(Utils.toUTF16(i.first), Utils.toUTF16(i.second))
  191. otherwise > new «$ename»(i)
  192. }
  193. const "convset" choose {
  194. when "$ctype = 'stringlist'" > Utils.toUTF8(i)
  195. when "$ctype = 'stringpairlist'" > new Pair<byte[],byte[]>(Utils.toUTF8(i.first), Utils.toUTF8(i.second))
  196. otherwise > new _«$ename»(i)
  197. }
  198. ||
  199. // Property type: list type. [java: «$type», intermediate: «$itype», ctype: «$ctype»]
  200. public «$type» get«$name»() {
  201. «$itype» glist = _get«$name»();
  202. if (glist != null) {
  203. «$type» list = new «$type»();
  204. for («$iename» i : glist) {
  205. list.add(«$convget»);
  206. }
  207. return list;
  208. }
  209. return null;
  210. }
  211. private native «$itype» _get«$name»();
  212. public void set«$name»(«$type» value) {
  213. if (value != null) {
  214. «$itype» list = new «$itype»();
  215. for («$ename» i : value) {
  216. list.add(«$convset»);
  217. }
  218. _set«$name»(list);
  219. } else {
  220. _set«$name»(null);
  221. }
  222. }
  223. private native void _set«$name»(«$itype» value);
  224. ||
  225. }
  226. when "$itype != $type" {
  227. ||
  228. // Property type: differs from intermediate. [java: «$type», intermediate: «$itype», ctype: «$ctype»]
  229. public «$type» get«$name»() {
  230. «$itype» res = _get«$name»();
  231. if (res != null) {
  232. return Utils.toUTF16(res);
  233. } else {
  234. return null;
  235. }
  236. }
  237. private native «$itype» _get«$name»();
  238. public void set«$name»(«$type» value) {
  239. if (value != null) {
  240. _set«$name»(Utils.toUTF8(value));
  241. } else {
  242. _set«$name»(new byte[0]);
  243. }
  244. }
  245. private native void _set«$name»(«$itype» value);
  246. ||
  247. }
  248. when "../enum[@name=$ctype]" {
  249. ||
  250. // Property type: enum type. [java: «$type», intermediate: «$itype», ctype: «$ctype»]
  251. public «$type» get«$name»() {
  252. return «$type».getByInt(_get«$name»());
  253. }
  254. private native int _get«$name»();
  255. public void set«$name»(«$type» value) {
  256. if(value != null)
  257. _set«$name»(value.value);
  258. else
  259. _set«$name»(0);
  260. }
  261. private native void _set«$name»(int value);
  262. ||
  263. }
  264. otherwise {
  265. ||
  266. // Property type: no intermediate type [java: «$type», intermediate: «$itype», ctype: «$ctype»]
  267. public «$type» get«$name»() {
  268. return _get«$name»();
  269. }
  270. private native «$type» _get«$name»();
  271. public void set«$name»(«$type» value) {
  272. _set«$name»(value);
  273. }
  274. private native void _set«$name»(«$type» value);
  275. ||
  276. }
  277. }
  278. }
  279. template "*", mode=interface {
  280. const "type" call "toJava" with "type", "name(.)";
  281. const "name" call "toJava" with "type", "name(*[position()=1])";
  282. ||
  283. public «$type» get«$name»();
  284. public void set«$name»(«$type» value);
  285. ||
  286. }
  287. template "enumitem" {
  288. const "name_hyphenized" call "hyphenize" with "text", "@name";
  289. const "javaname" call "toJava" with "type", "$name_hyphenized";
  290. const "enum_index", ".";
  291. ||
  292. «$javaname» («$enum_index») {
  293. public String toString() {
  294. ||
  295. choose {
  296. when "@toString" {
  297. ||
  298. return "«@toString»";
  299. ||
  300. } otherwise {
  301. ||
  302. return "«$javaname»";
  303. ||
  304. }
  305. }
  306. ||
  307. }
  308. ||
  309. choose {
  310. when "@int_str_val" {
  311. ||
  312. public String getInternalStringValue() {
  313. return "«@int_str_val»";
  314. }
  315. ||
  316. }
  317. }
  318. | }`if "position()!=last()" > , `
  319. }
  320. }