
clojure.data.xml.prxml.clj Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rest-resources-viz Show documentation
Show all versions of rest-resources-viz Show documentation
Transformations and visualizations for Cortex Rest resources
The newest version!
; Copyright (c) Rich Hickey. 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 clojure.data.xml.prxml
(:require
[clojure.data.xml.protocols :refer [AsElements as-elements]]
[clojure.data.xml.node :refer [cdata xml-comment element* element]]))
(defn sexp-element [tag attrs child]
(cond
(= :-cdata tag) (cdata (first child))
(= :-comment tag) (xml-comment (first child))
:else (element* tag attrs (mapcat as-elements child))))
(extend-protocol AsElements
clojure.lang.IPersistentVector
(as-elements [v]
(let [[tag & [attrs & after-attrs :as content]] v
[attrs content] (if (map? attrs)
[(into {} (for [[k v] attrs]
[k (str v)]))
after-attrs]
[{} content])]
[(sexp-element tag attrs content)]))
clojure.lang.ISeq
(as-elements [s]
(mapcat as-elements s))
clojure.lang.Keyword
(as-elements [k]
[(element k)])
java.lang.String
(as-elements [s]
[s])
nil
(as-elements [_] nil)
java.lang.Object
(as-elements [o]
[(str o)]))
(defn sexps-as-fragment
"Convert a compact prxml/hiccup-style data structure into the more formal
tag/attrs/content format. A seq of elements will be returned, which may
not be suitable for immediate use as there is no root element. See also
sexp-as-element.
The format is [:tag-name attr-map? content*]. Each vector opens a new tag;
seqs do not open new tags, and are just used for inserting groups of elements
into the parent tag. A bare keyword not in a vector creates an empty element.
To provide XML conversion for your own data types, extend the AsElements
protocol to them."
([] nil)
([sexp] (as-elements sexp))
([sexp & sexps] (mapcat as-elements (cons sexp sexps))))
(defn sexp-as-element
"Convert a single sexp into an Element"
[sexp]
(let [[root & more] (sexps-as-fragment sexp)]
(when more
(throw
(IllegalArgumentException.
"Cannot have multiple root elements; try creating a fragment instead")))
root))
© 2015 - 2025 Weber Informatics LLC | Privacy Policy