 
     ;; Text
     (characters [^chars ch ^long start ^long length]
-      (f [:characters (String. ch start length)]))
+      (f [:text (String. ch start length)]))
 
     ;; Keep track of which text came from a CDATA section
     (startCDATA []

(extend-protocol AsInputSource
  String
  (input-source [s]
    (let [^Reader r (if (.startsWith s "<")
                      (StringReader. s)
                      (io/reader s))]
      (InputSource. r)))

         (apply concat))))


(defn ignore
  "Filters out events of the given types."
  [events & event-types]
  (let [ignored-types (set event-types)
        ignorable? (fn [[type]] (ignored-types type))]
    (remove ignorable? events)))


(defn to-nodes
  "Returns a 2-element vector containing:
   1) A seq of sibling nodes extracted from the event stream, and
   2) A seq of the unconsumed events.
  "
  [events]
  (println events)
  (loop [[[type :as evt] & events] events
         xml []]
    (case type
      :start-element
      (let [[_ tag attrs] evt
            [children events] (to-nodes events)]
        (recur events
               (conj xml (Element. tag attrs children))))

      :text
      (recur events (conj xml (evt 1)))

      (:end-element nil)
      (do
        (println "Returning: " xml)
        [xml events])
      
      )))



(defn parse [in]
  (-> in
      event-seq
      merge-text
      ;;(ignore :start-cdata :end-cdata)
      ;;(ignore :start-prefix-mapping :end-prefix-mapping)
      ;;(ignore :comment)
      to-nodes
      ffirst
      ))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
