emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [PATCH] ob-java.el: Add `:var' variables import
@ 2014-06-21 22:30 Bart Post
  2014-06-22  9:11 ` Thierry Banel
  2014-06-25  8:44 ` Bastien
  0 siblings, 2 replies; 6+ messages in thread
From: Bart Post @ 2014-06-21 22:30 UTC (permalink / raw)
  To: emacs-orgmode


* 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

^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2014-07-31  9:41 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-06-21 22:30 [PATCH] ob-java.el: Add `:var' variables import Bart Post
2014-06-22  9:11 ` 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

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).