module Text.XML.Proc where
import Common
import Text.XML.Types
import Data.List (find)
import qualified Data.Text as T
strContent :: Element -> Text
strContent e = T.concat $ map cdData $ onlyText $ elContent e
onlyElems :: [Content] -> [Element]
onlyElems xs = [ x | Elem x <- xs ]
elChildren :: Element -> [Element]
elChildren e = [ x | Elem x <- elContent e ]
onlyText :: [Content] -> [CData]
onlyText xs = [ x | Text x <- xs ]
findChildren :: QName -> Element -> [Element]
findChildren q e = filterChildren ((q ==) . elName) e
filterChildren :: (Element -> Bool) -> Element -> [Element]
filterChildren p e = filter p (onlyElems (elContent e))
filterChildrenName :: (QName -> Bool) -> Element -> [Element]
filterChildrenName p e = filter (p.elName) (onlyElems (elContent e))
findChild :: QName -> Element -> Maybe Element
findChild q e = listToMaybe (findChildren q e)
filterChild :: (Element -> Bool) -> Element -> Maybe Element
filterChild p e = listToMaybe (filterChildren p e)
filterChildName :: (QName -> Bool) -> Element -> Maybe Element
filterChildName p e = listToMaybe (filterChildrenName p e)
findElement :: QName -> Element -> Maybe Element
findElement q e = listToMaybe (findElements q e)
filterElement :: (Element -> Bool) -> Element -> Maybe Element
filterElement p e = listToMaybe (filterElements p e)
filterElementName :: (QName -> Bool) -> Element -> Maybe Element
filterElementName p e = listToMaybe (filterElementsName p e)
findElements :: QName -> Element -> [Element]
findElements qn e = filterElementsName (qn==) e
filterElements :: (Element -> Bool) -> Element -> [Element]
filterElements p e
| p e = [e]
| otherwise = concatMap (filterElements p) $ onlyElems $ elContent e
filterElementsName :: (QName -> Bool) -> Element -> [Element]
filterElementsName p e = filterElements (p.elName) e
findAttr :: QName -> Element -> Maybe Text
findAttr x e = lookupAttr x (elAttribs e)
lookupAttr :: QName -> [Attr] -> Maybe Text
lookupAttr x = lookupAttrBy (x ==)
lookupAttrBy :: (QName -> Bool) -> [Attr] -> Maybe Text
lookupAttrBy p as = attrVal `fmap` find (p . attrKey) as
findAttrBy :: (QName -> Bool) -> Element -> Maybe Text
findAttrBy p e = lookupAttrBy p (elAttribs e)