 
 (defn sax-seq
   "Given a source of XML, returns a sequence of events, in the form of
-  vectors of [type data ...]. See xml-handler for details."
+   vectors of [type data ...]. See sax-handler for details."
   [in & [opts]]
-  (let [{:keys [namespaces]
-         :or   {namespaces true}} opts
-         
+  (let [{:keys [namespaces]} opts
         ^InputSource in (as-input-source in)
         parser          (XMLReaderFactory/createXMLReader)
-        [put event-seq] (pipe/pipe)
-        handler         (xml-handler put)]
+        [put event-seq] (pipe/pipe {:capacity 4096})
+        handler         (sax-handler put opts)]
     
     (.setContentHandler parser handler)
     
-    ;; Set the "lexical-handler" property in order to get
-    ;; start-prefix-mapping & end-prefix-mapping events.
-    (.setProperty parser "http://xml.org/sax/properties/lexical-handler" handler)
-    
-    ;; Setting this to true means that undeclared namespace-prefixes
-    ;; will be an error.
-    (.setFeature parser "http://xml.org/sax/features/namespaces" namespaces)
+    (when namespaces
+      ;; Set the "lexical-handler" property in order to get
+      ;; start-prefix-mapping & end-prefix-mapping events.
+      (.setProperty parser "http://xml.org/sax/properties/lexical-handler" handler)
+      
+      ;; Setting this to true means that undeclared namespace-prefixes
+      ;; will be an error.
+      (.setFeature parser "http://xml.org/sax/features/namespaces" namespaces))
 
     ;; Run the SAX parser in a thread.
     (future

   1 - A seq of nodes extracted from the event seq, and
   2 - A seq of the unconsumed events.
  "
  [events & [xmlns-prefix-map]]

  ;; Notes: I tried for a while to make this consume the event-seq
  ;;        lazily, but that made my brain explode.
  
  (loop [nodes []
         [[type data :as evt] & events] events
         ns-stack (list xmlns-prefix-map)]
    
    (case type
      
      :start-prefix-mapping
      (let [[_ prefix uri] evt
            ns-map (assoc-in (peek ns-stack) [:xmlns uri] prefix)]
        (recur nodes events (conj ns-stack ns-map)))

      :end-prefix-mapping
      (recur nodes events (pop ns-stack))
      
      :start-element
      (let [[_ tag attrs]     evt
            [children events] (events->nodes events (peek ns-stack))
            elt               (Element. tag attrs children nil)]
        (recur (conj nodes (with-meta elt {:xmlns (peek ns-stack)}))
               events
               ns-stack))

      :text
      (recur (conj nodes data)
             events
             ns-stack)

      :comment
      (recur (conj nodes (Comment. data))
             events
             ns-stack)

      :start-cdata
      (let [[text-nodes [end-cdata & events]]
            (split-with (fn [[type]] (= type :text)) events)]
        (assert (= (end-cdata 0) :end-cdata) "Expected :end-cdata event.")
        (recur (conj nodes (CData. (apply str (map second text-nodes))))
               events
               ns-stack))

      :processing-instruction
      (let [[_ target text] evt]
        (recur (conj nodes (PI. target text))
               events
               ns-stack))

      (:end-element nil)
      [nodes events]
      
      )))

(defn make-tree
  "Takes a seq of SAX events and returns a vector of the top-level nodes."
  [events]
  (let [[nodes events] (events->nodes events)]
    (when (seq events)
      ;; This shouldn't be possible if the source of the events is an
      ;; actual SAX parser, but could happen for an event-seq from

    ;; nodes. This actually returns all the top-level nodes, which
    ;; should consist of exactly one Element, as well as any number of
    ;; Comments and PIs preceeding and/or following the Element.
    (make-tree e opts)))



