emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Bart Post <bart.post@gmail.com>
To: emacs-orgmode@gnu.org
Subject: [PATCH] ob-java.el: Add `:var' variables import
Date: Sun, 22 Jun 2014 00:30:08 +0200	[thread overview]
Message-ID: <87fvixdhfz.fsf@gmail.com> (raw)


* 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

             reply	other threads:[~2014-06-21 22:30 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-06-21 22:30 Bart Post [this message]
2014-06-22  9:11 ` [PATCH] ob-java.el: Add `:var' variables import Thierry Banel
2014-06-25  8:44 ` Bastien
2014-06-25 10:00   ` Bart Post
2014-07-28 14:25     ` Bastien
2014-07-31  9:41       ` Bart Post

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87fvixdhfz.fsf@gmail.com \
    --to=bart.post@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).