module Network.SOAP.Parsing.Cursor
(
readT, readC
, Dict, readDict, dictBy
) where
import Network.SOAP (ResponseParser(CursorParser))
import Text.XML
import Text.XML.Cursor
import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.HashMap.Strict as HM
import Data.Maybe (mapMaybe)
readT :: Text -> Cursor -> Text
readT n c = T.concat $ c $/ laxElement n &/ content
readC :: (Read a) => Text -> Cursor -> a
readC n c = read . T.unpack $ readT n c
type Dict = HM.HashMap Text Text
readDict :: Axis -> Cursor -> Dict
readDict a c = extract . head $ c $/ a
where
extract cur = HM.fromList . mapMaybe dict . map node $ cur $| child
dict (NodeElement (Element (Name n _ _) _ [NodeContent cont])) = Just (n, cont)
dict (NodeElement (Element (Name n _ _) _ [])) = Just (n, T.empty)
dict _ = Nothing
dictBy :: T.Text -> ResponseParser Dict
dictBy n = CursorParser . readDict $ anyElement &/ laxElement n