module Text.XML.HXT.Parser.XmlParser
( module Text.XML.HXT.Parser.XmlParsec
, module Text.XML.HXT.Parser.XmlTokenParser
, parseXmlDoc
, substXmlEntities
)
where
import Text.XML.HXT.DOM.XmlTree
import Text.XML.HXT.DOM.XmlState
import Text.XML.HXT.Parser.XmlTokenParser
import Text.XML.HXT.Parser.XmlEntities
import Text.XML.HXT.Parser.XmlParsec
import Text.XML.HXT.Parser.XmlOutput
( traceTree
, traceSource
, traceMsg
)
parseXmlDoc :: XmlStateFilter a
parseXmlDoc
= parseDoc
`whenM` ( isRoot .> getChildren .> isXText )
where
parseDoc t
= ( traceMsg 2 ("parseXmlDoc: parse XML document " ++ show loc)
.>>
parser
.>>
liftMf checkRes
.>>
traceTree
.>>
traceSource
) $ t
where
loc = valueOf a_source t
checkRes
= setStatus c_err ("parsing XML source " ++ show loc)
`whenNot`
getChildren
parser
= processChildrenM (liftF (parseXmlText document' loc))
substXmlEntities :: XmlFilter
substXmlEntities
= choice
[ isXEntityRef :-> substEntity
, isXTag :-> processAttr (processChildren substXmlEntities)
, this :-> this
]
where
substEntity t'@(NTree (XEntityRef en) _)
= case (lookup en xmlEntities) of
Just i
-> [mkXCharRefTree i]
Nothing
-> this t'
substEntity _
= error "substXmlEntities: illegal argument"