forked from k2n/saml20-clj
-
Notifications
You must be signed in to change notification settings - Fork 11
/
xml.clj
32 lines (29 loc) · 1.29 KB
/
xml.clj
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
(ns saml20-clj.xml
(:require [saml20-clj.encode-decode :as encode-decode])
(:import [javax.xml.parsers DocumentBuilder DocumentBuilderFactory]
javax.xml.XMLConstants
org.w3c.dom.Document))
(defn document-builder
^DocumentBuilder []
(.newDocumentBuilder
(doto (DocumentBuilderFactory/newInstance)
(.setNamespaceAware true)
(.setFeature "http://xml.org/sax/features/external-parameter-entities" false)
(.setFeature "http://apache.org/xml/features/nonvalidating/load-external-dtd" false)
(.setFeature "http://apache.org/xml/features/disallow-doctype-decl" true)
(.setFeature XMLConstants/FEATURE_SECURE_PROCESSING true)
(.setXIncludeAware false)
(.setExpandEntityReferences false))))
(defn clone-document [^org.w3c.dom.Document document]
(when document
(let [clone (.. (DocumentBuilderFactory/newInstance) newDocumentBuilder newDocument)
original-root (.getDocumentElement document)
root-copy (.importNode clone original-root true)]
(.appendChild clone root-copy)
clone)))
(defn str->xmldoc
"Parse a string into an XML `Document`."
^Document [^String s]
(let [document (document-builder)]
(with-open [is (java.io.ByteArrayInputStream. (encode-decode/str->bytes s))]
(.parse document is))))