-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathnode.cljc
More file actions
227 lines (200 loc) · 7.31 KB
/
node.cljc
File metadata and controls
227 lines (200 loc) · 7.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
; 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.node
"Data types for xml nodes: Element, CData and Comment"
{:author "Herwig Hochleitner"}
(:require [clojure.data.xml.name :refer [as-qname]])
#?(:clj (:import (clojure.lang IHashEq IObj ILookup IKeywordLookup Counted
Associative Seqable IPersistentMap
APersistentMap RT MapEquivalence MapEntry)
(java.io Serializable Writer)
(java.util Map Iterator))))
;; Parsed data format
;; Represents a node of an XML tree
;; We implement a custom deftype for elements
;; it is similar to (defrecord Element [tag attrs content])
;; but we override its hash and equality to be compatible with
;; clojure's hash-maps
;; see https://clojure.atlassian.net/browse/CLJ-2084
;; also, elements don't have an extmap and degrade to hash-maps also
;; when assoc'ing unknown keys
;; FIXME hash caching cannot be used: https://clojure.atlassian.net/browse/CLJ-2092
#?
(:clj
(deftype ElementIterator [el ^:volatile-mutable fields]
Iterator
(hasNext [_] (boolean (seq fields)))
(next [_]
(let [f (first fields)]
(set! fields (next fields))
(MapEntry. f (get el f))))))
(deftype Element [tag attrs content meta]
;; serializing/cloning, hashing, equality, iteration
#?@
(:clj
[Serializable
MapEquivalence
IHashEq
(hasheq [this] (APersistentMap/mapHasheq this))
Iterable
(iterator [this] (ElementIterator. this '(:tag :attrs :content)))]
:cljs
[ICloneable
(-clone [_] (Element. tag attrs content meta))
IHash
(-hash [this] (hash-unordered-coll this))
IEquiv
(-equiv [this other] (or (identical? this other)
^boolean (js/cljs.core.equiv_map this other)))
IIterable
(-iterator [this] (RecordIter. 0 this 3 [:tag :attrs :content] (nil-iter)))])
Object
(toString [_]
(let [qname (as-qname tag)]
(apply str (concat ["<" qname]
(mapcat (fn [[n a]]
[" " (as-qname n) "=" (pr-str a)])
attrs)
(if (seq content)
(concat [">"] content ["</" qname ">"])
["/>"])))))
#?@(:clj
[(hashCode [this] (APersistentMap/mapHash this))
(equals [this other] (APersistentMap/mapEquals this other))
IPersistentMap
(equiv [this other] (APersistentMap/mapEquals this other))])
;; Main collection interfaces, that are included in IPersistentMap,
;; but are separate protocols in cljs
#?(:cljs ILookup)
(#?(:clj valAt :cljs -lookup) [this k]
(#?(:clj .valAt :cljs -lookup)
this k nil))
(#?(:clj valAt :cljs -lookup) [this k nf]
(case k
:tag tag
:attrs attrs
:content content
nf))
#?(:cljs ICounted)
(#?(:clj count :cljs -count) [this] 3)
#?(:cljs ICollection)
(#?(:clj cons :cljs -conj) [this entry]
(conj (with-meta {:tag tag :attrs attrs :content content} meta)
entry))
#?(:cljs IAssociative)
(#?(:clj assoc :cljs -assoc) [this k v]
(case k
:tag (Element. v attrs content meta)
:attrs (Element. tag v content meta)
:content (Element. tag attrs v meta)
(with-meta {:tag tag :attrs attrs :content content k v} meta)))
#?(:cljs IMap)
(#?(:clj without :cljs -dissoc) [this k]
(with-meta
(case k
:tag {:attrs attrs :content content}
:attrs {:tag tag :content content}
:content {:tag tag :attrs attrs}
this)
meta))
#?@(:cljs
[ISeqable
(-seq [this]
(seq [[:tag tag] [:attrs attrs] [:content content]]))]
:clj
[(seq [this] (iterator-seq (.iterator this)))])
#?(:clj (empty [_] (Element. tag {} [] {})))
#?@(:cljs
[IEmptyableCollection
(-empty [_] (Element. tag {} [] {}))])
;; j.u.Map and included interfaces
#?@(:clj
[Map
(entrySet [this] (set this))
(values [this] (vals this))
(keySet [this] (set (keys this)))
(get [this k] (.valAt this k))
(containsKey [this k] (case k (:tag :attrs :content) true false))
(containsValue [this v] (boolean (some #{v} (vals this))))
(isEmpty [this] false)
(size [this] 3)])
;; Metadata interface
#?(:clj IObj :cljs IMeta)
(#?(:clj meta :cljs -meta) [this] meta)
#?(:cljs IWithMeta)
(#?(:clj withMeta :cljs -with-meta) [this next-meta]
(Element. tag attrs content next-meta))
;; cljs printing is protocol-based
#?@
(:cljs
[IPrintWithWriter
(-pr-writer [this writer opts]
(-write writer "#xml/element{:tag ")
(pr-writer tag writer opts)
(when-not (empty? attrs)
(-write writer ", :attrs ")
(pr-writer attrs writer opts))
(when-not (empty? content)
(-write writer ", :content ")
(pr-sequential-writer writer pr-writer "[" " " "]" opts content))
(-write writer "}"))]))
;; clj printing is a multimethod
#?
(:clj
(defmethod print-method Element [{:keys [tag attrs content]} ^Writer writer]
(.write writer "#xml/element{:tag ")
(print-method tag writer)
(when-not (empty? attrs)
(.write writer ", :attrs ")
(print-method attrs writer))
(when-not (empty? content)
(.write writer ", :content [")
(print-method (first content) writer)
(doseq [c (next content)]
(.write writer " ")
(print-method c writer))
(.write writer "]"))
(.write writer "}")))
(defrecord CData [content])
(defrecord Comment [content])
(defn element*
"Create an xml element from a content collection and optional metadata"
([tag attrs content meta]
(Element. tag (or attrs {}) (remove nil? content) meta))
([tag attrs content]
(Element. tag (or attrs {}) (remove nil? content) nil)))
#?(:clj
;; Compiler macro for inlining the two constructors
(alter-meta! #'element* assoc :inline
(fn
([tag attrs content meta]
`(Element. ~tag (or ~attrs {}) (remove nil? ~content) ~meta))
([tag attrs content]
`(Element. ~tag (or ~attrs {}) (remove nil? ~content) nil)))))
(defn element
"Create an xml Element from content varargs"
([tag] (element* tag nil nil))
([tag attrs] (element* tag attrs nil))
([tag attrs & content] (element* tag attrs content)))
(defn cdata
"Create a CData node"
[content]
(CData. content))
(defn xml-comment
"Create a Comment node"
[content]
(Comment. content))
(defn map->Element [{:keys [tag attrs content] :as el}]
(element* tag attrs content (meta el)))
(defn tagged-element [el]
(cond (map? el) (map->Element el)
;; TODO support hiccup syntax
:else (throw (ex-info "Unsupported element representation"
{:element el}))))
(defn element? [el]
(and (map? el) (some? (:tag el))))