aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias Andreas Benkard <code@mail.matthias.benkard.de>2009-02-23 14:45:50 +0100
committerMatthias Andreas Benkard <code@mail.matthias.benkard.de>2009-02-23 14:45:50 +0100
commit7c791f06ff0887c57f68dea9351b8f22142ae0ad (patch)
tree3a7a8c46c5048a3dfb7252df8da4a7cd62420c8c
parent1d7292fda2170d3c8ab5c56d1c266a5856a14012 (diff)
parent64b7320d281382f325aa5e8b7df64564771c18ba (diff)
Merge branch 'master' of ssh://matthias@uxul.org:63332/home/matthias/public_html/cljssss-g
Conflicts: cljssss-g.clj
-rw-r--r--Geniface.java136
-rw-r--r--cljssss-g.clj108
-rw-r--r--opml.st17
-rw-r--r--opmlclass.clj18
4 files changed, 263 insertions, 16 deletions
diff --git a/Geniface.java b/Geniface.java
new file mode 100644
index 0000000..c307050
--- /dev/null
+++ b/Geniface.java
@@ -0,0 +1,136 @@
+/* just an experiment - should be implemented in clojure later */
+
+import java.io.*;
+import javax.tools.*;
+
+import java.nio.charset.Charset;
+import java.util.Locale;
+import java.util.UUID;
+import java.util.Properties;
+
+public class Geniface {
+
+ class ByteClassLoader extends ClassLoader{
+ String name; byte[] b;
+ ByteClassLoader (String name, byte[] b) {
+ this.b=b;
+ this.name=name;
+ }
+ Class getTheClass() {
+ return defineClass(name, b, 0, b.length);
+ }
+ }
+
+ private String temp_prefix;
+ private String my_uuid;
+
+ private String GeneratedText;
+
+ public Geniface (String temp_prefix) {
+ this.temp_prefix = temp_prefix;
+ this.my_uuid = "myuid" + UUID.randomUUID().toString().replace('-', '_');
+ this.GeneratedText =
+ "public interface " + my_uuid + " {";
+ }
+
+ public Geniface () {
+ this(System.getProperty("java.io.tmpdir"));
+ }
+
+ public void addMethod (String name, int ObjectArgCount, boolean ret) {
+ /* Add a Method with ObjectArgCount arguments which either
+ * returns void if !ret, or returns an Object if ret */
+ GeneratedText +=
+ (ret ? "\n\tObject " : "\n\tvoid ") + name + "(";
+ for (int i = 0; i < ObjectArgCount; i++) {
+ GeneratedText += (i == 0 ? "" : ", ") + "Object arg" + i;
+ }
+ GeneratedText += ");";
+ }
+
+ public String getSourceString () {
+ return GeneratedText + "}";
+ }
+
+ public static byte[] getBytesFromFile(File file) throws IOException {
+ InputStream is = new FileInputStream(file);
+
+ // Get the size of the file
+ long length = file.length();
+
+ if (length > Integer.MAX_VALUE) {
+ // File is too large
+ }
+
+ // Create the byte array to hold the data
+ byte[] bytes = new byte[(int)length];
+
+ // Read in the bytes
+ int offset = 0;
+ int numRead = 0;
+ while (offset < bytes.length
+ && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
+ offset += numRead;
+ }
+
+ // Ensure all the bytes have been read in
+ if (offset < bytes.length) {
+ throw new IOException("Could not completely read file "+file.getName());
+ }
+
+ // Close the input stream and return bytes
+ is.close();
+ return bytes;
+ }
+
+ public Class compileToClass () {
+ try {
+ String javaname = temp_prefix + "/" + my_uuid + ".java";
+ String classname = temp_prefix + "/" + my_uuid + ".class";
+
+ FileOutputStream out = new FileOutputStream(javaname);
+ out.write(getSourceString().getBytes());
+ out.close();
+
+ File [] files1 = new File[]
+ { new File(javaname) };
+
+ JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
+ DiagnosticCollector<JavaFileObject> diagnostics = new
+ DiagnosticCollector<JavaFileObject>();
+ StandardJavaFileManager fileManager =
+ compiler.getStandardFileManager(diagnostics, Locale.GERMANY,
+ Charset.forName("UTF-8"));
+
+ Iterable<? extends JavaFileObject> compilationUnits1 =
+ fileManager.getJavaFileObjectsFromFiles(java.util.Arrays.asList(files1));
+ compiler.getTask(null, fileManager, diagnostics, null, null,
+compilationUnits1).call();
+
+ for (Diagnostic diagnostic : diagnostics.getDiagnostics())
+ {
+ System.out.println(diagnostic);
+ }
+
+ fileManager.close();
+
+ return new ByteClassLoader(my_uuid, getBytesFromFile (new File(classname))).getTheClass();
+ }
+ catch (IOException exn1) {
+ System.out.println("IOExn");
+ }
+
+ return null;
+
+ }
+
+ public static void main (String[] args) {
+ Geniface iface1 = new Geniface();
+ iface1.addMethod("Hallo", 10, false);
+ iface1.addMethod("Fuck", 2, true);
+ iface1.addMethod("valuesofbetawillgiverisetodom", 1, true);
+ iface1.addMethod("nilpferd", 0, false);
+ System.out.println(iface1.getSourceString());
+ System.out.println(iface1.compileToClass());
+ }
+} \ No newline at end of file
diff --git a/cljssss-g.clj b/cljssss-g.clj
index a9e8ffe..315cbc4 100644
--- a/cljssss-g.clj
+++ b/cljssss-g.clj
@@ -17,6 +17,9 @@
:subname "cljssss-g.sqlite3"
:create true})
+;; FIXME: WAAAAAAAAAH!
+(def session {:id false})
+
(defmacro with-db [& body]
`(sql/with-connection db-connection-data
~@body))
@@ -26,6 +29,73 @@
(sql/transaction
~@body)))
+;;; FIXME: clojure has no way of creating interfaces on-the-fly. So I
+;;; cannot use stringtemplate here.
+(defn opml-string [id]
+ (with-dbt
+ (sql/with-query-results
+ results
+ ["SELECT feed.uri, feed.link, user_feed_link.title FROM feed, user_feed_link WHERE user_feed_link.feed=feed.id AND user_feed_link.user=?" id]
+ (str "<?xml version=\"1.0\" encoding=\"utf-8\"?><opml version=\"1.0\"><head>"
+ "<dateCreated>blah</dateCreated>"
+ "<title>G&ouml;del-Gentzen Clojure Syndication Services Super System Feed Export</title></head><body>"
+ (loop [clstr "" r results]
+ (if (first r)
+ (recur
+ (str clstr
+ "<outline text=\""
+ (:title (first r))
+ "\" xmlUrl=\""
+ (:uri (first r))
+ "\" htmlUrl=\""
+ (:link (first r))
+ "\" />")
+ (rest r))
+ clstr))
+ "</body></opml>"))))
+
+(defn lynxy-feedlist [id]
+ (with-dbt
+ (sql/with-query-results
+ results
+ ["SELECT feed.id, feed.uri, feed.link, user_feed_link.title FROM feed, user_feed_link WHERE user_feed_link.feed=feed.id AND user_feed_link.user=?" id]
+ (html `[html [head [title "G&ouml;del-Gentzen Clojure Syndication Services Super System"]]
+ [body [ul
+ ~@(loop [back '[] r results]
+ (if (first r)
+ (recur (concat back `[[li
+ [a {:href ~(str "lynxy-showfeed?feed=" (:id (first r)))}
+ ~(:title (first r)) " - "
+ ~(:link (first r))
+ ]]])
+ (rest r))
+ back))]]]))))
+
+(defn lynxy-showfeed [id feednum]
+ (with-dbt
+ (sql/with-query-results
+ results
+ ["SELECT entry.link, entry.title FROM entry, feed_entry_link, user_feed_link WHERE entry.id = feed_entry_link.entry AND feed_entry_link.feed = user_feed_link.feed AND user_feed_link.user = ? AND user_feed_link.feed = ?" id feednum]
+ (html `[html [head [title "G&ouml;del-Gentzen Clojure Syndication Services Super System"]]
+ [body [ul
+ ~@(loop [back '[] r results]
+ (if (first r)
+ (recur (concat back `[[li
+ [a {:href ~(:link (first r))}
+ ~(:title (first r)) " - "
+ ~(:link (first r))
+ ]]])
+ (rest r))
+ back))]
+ [a {:href "lynxy-feedlist.html"} "&laquo; To Feedlist"]
+ ]]))))
+
+(defmacro with-session [& body]
+ `(if (not (session :id))
+ (redirect-to "/login")
+ (do ~@body)))
+
+
(defservlet cljssss-g
(GET "/login"
(if (= (params :valuesofbetawillgiverisetodom) "true")
@@ -36,23 +106,29 @@
(.getInstanceOf templates "login")
(.setAttributes {"logintext" "Login"})))))
(POST "/login"
- (dosync
- (with-db
- (sql/with-query-results [{id :id password :password}]
- ["SELECT id, password FROM user WHERE name = ?"
- (@params :name)]
- (if (= password (@params :password))
- (do
- (alter session assoc :id id)
- (redirect-to "/"))
- (redirect-to "/login?valuesofbetawillgiverisetodom=true"))))))
+ (dosync
+ (with-db
+ (sql/with-query-results [{id :id password :password}]
+ ["SELECT id, password FROM user WHERE name = ?"
+ (params :name)]
+ (if (= password (params :password))
+ (do
+ (alter session assoc :id id)
+ (redirect-to "/"))
+ (redirect-to "/login?valuesofbetawillgiverisetodom=true"))))))
+ (GET "/feedlist.opml"
+ (with-session (opml-string (session :id))))
+ (GET "/lynxy-feedlist.html"
+ (with-session (lynxy-feedlist (session :id))))
+ (GET "/lynxy-showfeed"
+ (with-session
+ (lynxy-showfeed (session :id) (. Integer parseInt (params :feed)))))
(GET "/"
- (if (@session :id)
- (.toString
- (doto (.getInstanceOf templates "index")
- (.setAttributes {"title" "Subscriptions",
- "mainParagraph" "Hi there!"})))
- (redirect-to "/login")))
+ (with-session
+ (.toString
+ (doto (.getInstanceOf templates "index")
+ (.setAttributes {"title" "Subscriptions",
+ "mainParagraph" "Hi there!"})))))
(ANY "*"
(page-not-found)))
diff --git a/opml.st b/opml.st
new file mode 100644
index 0000000..3fd6360
--- /dev/null
+++ b/opml.st
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="utf-8"?>
+<opml version="1.0">
+ <head>
+ <dateCreated>$date$</dateCreated>
+ <title>G&ouml;del-Gentzen Clojure Syndication Services Super System Feed Export</title>
+ </head>
+ <body>
+ $feeds:{f |
+ <outline
+ text="$f.text$"
+ xmlUrl="$f.xmlurl$"
+ htmlUrl="$f.htmlurl$"
+ /> }$
+ </body>
+</opml>
+
+
diff --git a/opmlclass.clj b/opmlclass.clj
new file mode 100644
index 0000000..4486580
--- /dev/null
+++ b/opmlclass.clj
@@ -0,0 +1,18 @@
+(ns cljssss-g
+ (require [clojure.xml :as xml]
+ [clojure.contrib.sql :as sql]
+ compojure)
+ (import (org.antlr.stringtemplate StringTemplateGroup)
+ (com.sun.syndication.io SyndFeedInput XmlReader)
+ (com.sun.syndication.feed.synd SyndFeed SyndEntry)
+ (java.net URL))
+ (use compojure))
+
+
+(gen-interface
+ :name opmliface
+ :prefix "opml-"
+ :init init
+ :methods [[getXmlurl [] String]
+ [getHtmlurl [] String]
+ [getText [] String]]) \ No newline at end of file