From mboxrd@z Thu Jan 1 00:00:00 1970 From: Bart Post Subject: [PATCH] ob-java.el: Add `:var' variables import Date: Sun, 22 Jun 2014 00:30:08 +0200 Message-ID: <87fvixdhfz.fsf@gmail.com> Mime-Version: 1.0 Content-Type: text/plain Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:48512) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WyTnt-0003DM-Bk for emacs-orgmode@gnu.org; Sat, 21 Jun 2014 18:30:42 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WyTnj-0006HD-OC for emacs-orgmode@gnu.org; Sat, 21 Jun 2014 18:30:33 -0400 Received: from mail-we0-x231.google.com ([2a00:1450:400c:c03::231]:55314) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WyTnj-0006H2-Ho for emacs-orgmode@gnu.org; Sat, 21 Jun 2014 18:30:23 -0400 Received: by mail-we0-f177.google.com with SMTP id u56so5158233wes.22 for ; Sat, 21 Jun 2014 15:30:22 -0700 (PDT) Received: from arabesque (ip54569416.speed.planet.nl. [84.86.148.22]) by mx.google.com with ESMTPSA id w5sm17819653wif.3.2014.06.21.15.30.20 for (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Sat, 21 Jun 2014 15:30:21 -0700 (PDT) List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: emacs-orgmode@gnu.org * lisp/ob-java.el: (org-babel-execute:java): Inject a private static class inside the Java class designated by `:classname'. (org-babel-variable-assignments:java): Assign the header variables to static fields in a private static class `Data'. (org-babel-java-var-to-java): Translate string to String, list to java.util.List, float to double and others to int. --- lisp/ob-java.el | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/lisp/ob-java.el b/lisp/ob-java.el index 8c64171..a2a1f40 100644 --- a/lisp/ob-java.el +++ b/lisp/ob-java.el @@ -58,7 +58,13 @@ parameters may be used, like javac -verbose" (src-file (concat classname ".java")) (cmpflag (or (cdr (assoc :cmpflag params)) "")) (cmdline (or (cdr (assoc :cmdline params)) "")) - (full-body (org-babel-expand-body:generic body params)) + (split-body + (split-string body (concat classname "[\n\t ]*{"))) + (full-body + (org-babel-expand-body:generic + (concat (car split-body) (concat classname " {\n") (org-babel-variable-assignments:java params) + (mapconcat 'identity (cdr split-body) (concat classname " {"))) + params)) (compile (progn (with-temp-file src-file (insert full-body)) (org-babel-eval @@ -80,6 +86,37 @@ parameters may be used, like javac -verbose" (org-babel-pick-name (cdr (assoc :rowname-names params)) (cdr (assoc :rownames params))))))) +(defun org-babel-variable-assignments:java (params) + "Return an internal Java class assigning the block's variables." + (concat " private static class Data {\n" + (mapconcat 'identity + (mapcar + (lambda (pair) + (format " public static %s %s = %s;" + (cond + ((stringp (cdr pair)) "String") + ((listp (cdr pair)) "java.util.List") + ((floatp (cdr pair)) "double") + (t "int")) + (car pair) + (org-babel-java-var-to-java (cdr pair)))) + (mapcar #'cdr (org-babel-get-header params :var))) + "\n") + "\n }") + ) + +(defun org-babel-java-var-to-java (var) + "Convert an elisp value to a java string. +Convert an elisp value, VAR, into a string of Java source code +specifying a variable of the same value." + (if (listp var) + (concat "java.util.Arrays.asList(" (mapconcat #'org-babel-java-var-to-java var ", ") ")") + (if (equal var 'hline) + "null" + (format + (if (and (stringp var) (string-match "[\n\r]" var)) "\"\"%S\"\"" "%S") + var)))) + (provide 'ob-java) -- 1.9.1