module Text.XML.Expat.NodeClass where
import Control.Monad.Identity
import Data.List.Class
import Data.Monoid (Monoid)
import Text.XML.Expat.SAX (GenericXMLString)
type Attributes tag text = [(tag, text)]
type UAttributes text = Attributes text text
textContent :: (NodeClass n [], Monoid text) => n [] tag text -> text
textContent node = runIdentity $ textContentM node
class (Functor c, List c) => NodeClass n c where
isElement :: n c tag text -> Bool
isText :: n c tag text -> Bool
textContentM :: Monoid text => n c tag text -> ItemM c text
isNamed :: Eq tag => tag -> n c tag text -> Bool
getName :: Monoid tag => n c tag text -> tag
getAttributes :: n c tag text -> [(tag,text)]
getChildren :: n c tag text -> c (n c tag text)
getText :: Monoid text => n c tag text -> text
modifyName :: (tag -> tag)
-> n c tag text
-> n c tag text
modifyAttributes :: ([(tag, text)] -> [(tag, text)])
-> n c tag text
-> n c tag text
modifyChildren :: (c (n c tag text) -> c (n c tag text))
-> n c tag text
-> n c tag text
mapAllTags :: (tag -> tag')
-> n c tag text
-> n c tag' text
mapElement :: ((tag, [(tag, text)], c (n c tag text))
-> (tag', [(tag', text)], c (n c tag' text)))
-> n c tag text
-> n c tag' text
mapNodeContainer :: (c (n c tag text) -> ItemM c (c' (n c' tag text)))
-> n c tag text
-> ItemM c (n c' tag text)
mkText :: text -> n c tag text
class NodeClass n c => MkElementClass n c where
mkElement :: tag -> Attributes tag text -> c (n c tag text) -> n c tag text
getAttribute :: (NodeClass n c, GenericXMLString tag) => n c tag text -> tag -> Maybe text
getAttribute n t = lookup t $ getAttributes n
setAttribute :: (Eq tag, NodeClass n c, GenericXMLString tag) => tag -> text -> n c tag text -> n c 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 c, GenericXMLString tag) => tag -> n c tag text -> n c 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 c, GenericXMLString tag) => tag -> Maybe text -> n c tag text -> n c tag text
alterAttribute t (Just newValue) = setAttribute t newValue
alterAttribute t Nothing = deleteAttribute t