diff src/clojure/contrib/lazy_xml/with_pull.clj @ 10:ef7dbbd6452c

added clojure source goodness
author Robert McIntyre <rlm@mit.edu>
date Sat, 21 Aug 2010 06:25:44 -0400
parents
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/clojure/contrib/lazy_xml/with_pull.clj	Sat Aug 21 06:25:44 2010 -0400
     1.3 @@ -0,0 +1,58 @@
     1.4 +;   Copyright (c) Chris Houser, Dec 2008. All rights reserved.
     1.5 +;   The use and distribution terms for this software are covered by the
     1.6 +;   Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
     1.7 +;   which can be found in the file epl-v10.html at the root of this distribution.
     1.8 +;   By using this software in any fashion, you are agreeing to be bound by
     1.9 +;   the terms of this license.
    1.10 +;   You must not remove this notice, or any other, from this software.
    1.11 +
    1.12 +; optional module to allow lazy-xml to use pull parser instead of sax
    1.13 +
    1.14 +(in-ns 'clojure.contrib.lazy-xml)
    1.15 +(import '(org.xmlpull.v1 XmlPullParser XmlPullParserFactory))
    1.16 +
    1.17 +(defn- attrs [xpp]
    1.18 +  (for [i (range (.getAttributeCount xpp))]
    1.19 +    [(keyword (.getAttributeName xpp i))
    1.20 +     (.getAttributeValue xpp i)]))
    1.21 +
    1.22 +(defn- ns-decs [xpp]
    1.23 +  (let [d (.getDepth xpp)]
    1.24 +    (for [i (range (.getNamespaceCount xpp (dec d)) (.getNamespaceCount xpp d))]
    1.25 +      (let [prefix (.getNamespacePrefix xpp i)]
    1.26 +        [(keyword (str "xmlns" (when prefix (str ":" prefix))))
    1.27 +         (.getNamespaceUri xpp i)]))))
    1.28 +
    1.29 +(defn- attr-hash [xpp]
    1.30 +  (into {} (concat (ns-decs xpp) (attrs xpp))))
    1.31 +
    1.32 +(defn- pull-step [xpp]
    1.33 +  (let [step (fn [xpp]
    1.34 +               (condp = (.next xpp)
    1.35 +                 XmlPullParser/START_TAG
    1.36 +                   (cons (struct node :start-element
    1.37 +                                 (keyword (.getName xpp))
    1.38 +                                 (attr-hash xpp))
    1.39 +                         (pull-step xpp))
    1.40 +                   XmlPullParser/END_TAG
    1.41 +                   (cons (struct node :end-element
    1.42 +                                 (keyword (.getName xpp)))
    1.43 +                         (pull-step xpp))
    1.44 +                   XmlPullParser/TEXT
    1.45 +                   (let [text (.trim (.getText xpp))]
    1.46 +                     (if (empty? text)
    1.47 +                       (recur xpp)
    1.48 +                       (cons (struct node :characters nil nil text)
    1.49 +                             (pull-step xpp))))))]
    1.50 +    (lazy-seq (step xpp))))
    1.51 +
    1.52 +(def ^{:private true} factory
    1.53 +  (doto (XmlPullParserFactory/newInstance)
    1.54 +    (.setNamespaceAware true)))
    1.55 +
    1.56 +(defn- parse-seq-pull [s]
    1.57 +  (let [xpp (.newPullParser factory)]
    1.58 +    (.setInput xpp s)
    1.59 +    (pull-step xpp)))
    1.60 +
    1.61 +(def has-pull true)