module Text.XML.Expat.NodeClass where
import Data.Monoid (Monoid)
import Text.XML.Expat.SAX (GenericXMLString)
class NodeClass n where
isElement :: n tag text -> Bool
isText :: n tag text -> Bool
textContent :: Monoid text => n tag text -> text
isNamed :: Eq tag => tag -> n tag text -> Bool
getName :: GenericXMLString tag => n tag text -> tag
getAttributes :: n tag text -> [(tag,text)]
getChildren :: n tag text -> [n tag text]
modifyName :: (tag -> tag)
-> n tag text
-> n tag text
modifyAttributes :: ([(tag, text)] -> [(tag, text)])
-> n tag text
-> n tag text
modifyChildren :: ([n tag text] -> [n tag text])
-> n tag text
-> n tag text
mapAllTags :: (tag -> tag')
-> n tag text
-> n tag' text
mapElement :: ((tag, [(tag, text)], [n tag text]) -> (tag', [(tag', text)], [n tag' text]))
-> n tag text
-> n tag' text
getAttribute :: (NodeClass n, GenericXMLString tag) => n tag text -> tag -> Maybe text
getAttribute n t = lookup t $ getAttributes n
setAttribute :: (Eq tag, NodeClass n, GenericXMLString tag) => tag -> text -> n tag text -> n tag text
setAttribute t newValue = modifyAttributes set
where
set [] = [(t, newValue)]
set ((name, _):atts) | name == t = (name, newValue):atts
set (att:atts) = att:set atts
deleteAttribute :: (Eq tag, NodeClass n, GenericXMLString tag) => tag -> n tag text -> n tag text
deleteAttribute t = modifyAttributes del
where
del [] = []
del ((name, _):atts) | name == t = atts
del (att:atts) = att:del atts
alterAttribute :: (Eq tag, NodeClass n, GenericXMLString tag) => tag -> Maybe text -> n tag text -> n tag text
alterAttribute t (Just newValue) = setAttribute t newValue
alterAttribute t Nothing = deleteAttribute t