Permalink
Browse files

Merge branch 'perf' of github.com:ztellman/aleph into perf

  • Loading branch information...
2 parents e53a612 + 5c3fb6f commit da9ba5d63a4cbe71bd483322014b2f8f9aff7e03 @ztellman committed Oct 6, 2012
Showing with 66 additions and 24 deletions.
  1. +3 −3 project.clj
  2. +18 −21 src/aleph/formats.clj
  3. +2 −0 src/aleph/netty/core.clj
  4. +43 −0 test/aleph/test/formats.clj
View
@@ -7,19 +7,19 @@
:distribution :repo}
:dependencies [[org.clojure/clojure "1.4.0"]
[org.clojure/tools.logging "0.2.3"]
+ [org.clojure/data.xml "0.0.6"]
[io.netty/netty "3.5.7.Final"]
[lamina "0.5.0-beta6"]
[gloss "0.2.2-beta2"]
[cheshire "4.0.1"]
- [prxml "1.3.1"]
[criterium "0.3.0"]]
:multi-deps {:all [[org.clojure/tools.logging "0.2.3"]
+ [org.clojure/data.xml "0.0.6"]
[io.netty/netty "3.5.7.Final"]
[lamina "0.5.0-beta5"]
[gloss "0.2.2-beta2"]
[cheshire "4.0.1"]
- [criterium "0.3.0"]
- [prxml "1.3.1"]]
+ [criterium "0.3.0"]]
"master" [[org.clojure/clojure "1.5.0-master-SNAPSHOT"]]
"1.2" [[org.clojure/clojure "1.2.0"]]
"1.3" [[org.clojure/clojure "1.3.0"]]}
View
@@ -9,11 +9,11 @@
(ns ^{:author "Zachary Tellman"}
aleph.formats
(:use
- [lamina core])
+ [lamina core]
+ [clojure.data.xml :only [sexp-as-element emit]])
(:require
[cheshire.core :as json]
- [clojure.xml :as xml]
- [clojure.contrib.prxml :as prxml])
+ [clojure.xml :as xml])
(:require
[gloss.io :as gloss-io]
[gloss.core :as gloss])
@@ -288,38 +288,35 @@
charset)]))
(-> data bytes->input-stream xml/parse)))))
+(defn- emit-element-with-declaration
+ [x charset]
+ (print "<?xml version=\"1.0\" encoding=\"")
+ (print charset)
+ (print "\"?>")
+ (xml/emit-element x))
+
(defn encode-xml->string
- "Takes a Clojure data structure representing a parse tree or prxml structure, and returns an XML string.
+ "Takes a Clojure data structure representing a parse tree or prxml/hiccup-style data structure,
+ and returns an XML string.
By default, 'charset' is UTF-8."
([x]
(encode-xml->string x "utf-8"))
([x charset]
(when x
- (with-out-str
- (prxml/prxml [:decl! {:version "1.0" :encoding charset}])
- (cond
- (vector? x) (prxml/prxml x)
- (map? x) (xml/emit-element x))))))
+ (cond
+ (vector? x) (with-out-str (emit (sexp-as-element x) *out* :encoding charset))
+ (map? x) (with-out-str (emit-element-with-declaration x charset))))))
(defn encode-xml->bytes
- "Takes a Clojure data structure representing a parse tree or prxml structure, and an XML representation as bytes.
+ "Takes a Clojure data structure representing a parse tree or prxml/hiccup-style data structure,
+ and returns an XML representation as bytes.
By default, 'charset' is UTF-8."
([x]
(encode-xml->bytes x "utf-8"))
([x charset]
- (when x
- (channel-buffers->channel-buffer
- [(string->channel-buffer
- (with-out-str (prxml/prxml [:decl! {:version "1.0" :encoding charset}]))
- "ascii")
- (string->channel-buffer
- (with-out-str
- (cond
- (vector? x) (prxml/prxml x)
- (map? x) (xml/emit-element x)))
- charset)]))))
+ (string->channel-buffer (encode-xml->string x charset))))
;;;
View
@@ -18,6 +18,7 @@
DateFormat
SimpleDateFormat]
[java.util
+ TimeZone
Date]
[org.jboss.netty.buffer
ChannelBuffer]
@@ -82,6 +83,7 @@
(or
(.get date-format)
(let [format (SimpleDateFormat. "EEE, dd MMM yyyy HH:mm:ss z")]
+ (.setTimeZone format (TimeZone/getTimeZone "GMT"))
(.set date-format format)
format))]
(.format format (Date.))))
@@ -0,0 +1,43 @@
+;; Copyright (c) Zachary Tellman. All rights reserved.
+;; The use and distribution terms for this software are covered by the
+;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
+;; which can be found in the file epl-v10.html at the root of this distribution.
+;; By using this software in any fashion, you are agreeing to be bound by
+;; the terms of this license.
+;; You must not remove this notice, or any other, from this software.
+
+(ns aleph.test.formats
+ (:use
+ [aleph formats]
+ [clojure test]))
+
+(def prxml-greeting [:p {:class "greet"} [:i "Ladies & gentlemen"]])
+
+(def element-greeting
+ {:tag :p
+ :attrs {:class "greet"}
+ :content '(
+ {:tag :i
+ :attrs {}
+ :content ["Ladies & gentlemen"]})})
+
+(deftest test-encode-xml
+ (is (=
+ "<?xml version=\"1.0\" encoding=\"utf-8\"?><p class=\"greet\"><i>Ladies &amp; gentlemen</i></p>"
+ (encode-xml->string prxml-greeting)
+ (channel-buffer->string (encode-xml->bytes prxml-greeting))))
+ (is (=
+ "<?xml version=\"1.0\" encoding=\"KOI8-R\"?><p class=\"greet\"><i>Ladies &amp; gentlemen</i></p>"
+ (encode-xml->string prxml-greeting "KOI8-R")
+ (channel-buffer->string (encode-xml->bytes prxml-greeting "KOI8-R"))))
+
+ ; maps are emitted with clojure.xml, which uses single quotes, emits newlines, and doesn't entity-escape strings.
+ (is (=
+ "<?xml version=\"1.0\" encoding=\"utf-8\"?><p class='greet'>\n<i>\nLadies & gentlemen\n</i>\n</p>\n"
+ (encode-xml->string element-greeting)
+ (channel-buffer->string (encode-xml->bytes element-greeting))))
+
+ (is (=
+ "<?xml version=\"1.0\" encoding=\"KOI8-R\"?><p class='greet'>\n<i>\nLadies & gentlemen\n</i>\n</p>\n"
+ (encode-xml->string element-greeting "KOI8-R")
+ (channel-buffer->string (encode-xml->bytes element-greeting "KOI8-R")))))

0 comments on commit da9ba5d

Please sign in to comment.