Skip to content

Instantly share code, notes, and snippets.

@kognate
Created June 9, 2009 06:21
Show Gist options
  • Save kognate/126315 to your computer and use it in GitHub Desktop.
Save kognate/126315 to your computer and use it in GitHub Desktop.
clojure code
; now with multimethods.
(ns properties)
(defn from_string_file
[fname]
{:propfile :text :filename fname})
(defn from_xml_file
[fname]
{:propfile :xml :filename fname})
(defn to_string_file
([fname,dat]
{:propfile :text :filename fname :propdata dat :comment "From Clojure"})
([fname,dat,comment]
{:propfile :text :filename fname :propdata dat :comment comment}))
(defn to_xml_file
([fname,dat]
{:propfile :xml :filename fname :propdata dat :comment "From Clojure"})
([fname,dat,comment]
{:propfile :xml :filename fname :propdata dat :comment comment}))
(defn- property_loader
[filename,loadfunc]
(with-open [ifst (new java.io.FileInputStream filename)]
(let [prop (new java.util.Properties)]
(do
(loadfunc prop ifst)
(let [keysseq (enumeration-seq (. prop propertyNames))]
(reduce #(assoc %1 %2 (. prop getProperty %2)) {} keysseq))))))
(defmulti load_properties :propfile)
(defmethod load_properties :xml [d]
(property_loader (:filename d) #(. %1 loadFromXML %2)))
(defmethod load_properties :text [d]
(property_loader (:filename d) #(. %1 load %2)))
(defn- store_propdata
"This is a function called by the store_propfile function. It
encasulates the recursive walk through the hashmap and sets the value
using the setProperty method of the Properties class. This function is
not publically available." [prop,hashm]
(loop [myprop prop
x hashm]
(if (keys x)
(do
(. myprop setProperty (.toString (nth (first x) 0)) (.toString (nth (first x) 1)))
(recur myprop (rest x))
)
myprop
)
)
)
(defmulti store_propfile :propfile)
(defmethod store_propfile :xml [d]
(with-open [ofst (new java.io.FileOutputStream (:filename d))]
(. (store_propdata (new java.util.Properties) (:propdata d)) storeToXML ofst (:comment d))))
(defmethod store_propfile :text [d]
(with-open [ofst (new java.io.FileOutputStream (:filename d))]
(. (store_propdata (new java.util.Properties) (:propdata d)) store ofst (:comment d))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment