module Text.XML.Light.Proc where
import Text.XML.Light.Types
import Data.Maybe(listToMaybe)
import Data.List(find)
strContent :: Element -> String
strContent :: Element -> String
strContent Element
e = (CData -> String) -> [CData] -> String
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap CData -> String
cdData ([CData] -> String) -> [CData] -> String
forall a b. (a -> b) -> a -> b
$ [Content] -> [CData]
onlyText ([Content] -> [CData]) -> [Content] -> [CData]
forall a b. (a -> b) -> a -> b
$ Element -> [Content]
elContent Element
e
onlyElems :: [Content] -> [Element]
onlyElems :: [Content] -> [Element]
onlyElems [Content]
xs = [ Element
x | Elem Element
x <- [Content]
xs ]
elChildren :: Element -> [Element]
elChildren :: Element -> [Element]
elChildren Element
e = [ Element
x | Elem Element
x <- Element -> [Content]
elContent Element
e ]
onlyText :: [Content] -> [CData]
onlyText :: [Content] -> [CData]
onlyText [Content]
xs = [ CData
x | Text CData
x <- [Content]
xs ]
findChildren :: QName -> Element -> [Element]
findChildren :: QName -> Element -> [Element]
findChildren QName
q Element
e = (Element -> Bool) -> Element -> [Element]
filterChildren ((QName
q QName -> QName -> Bool
forall a. Eq a => a -> a -> Bool
==) (QName -> Bool) -> (Element -> QName) -> Element -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Element -> QName
elName) Element
e
filterChildren :: (Element -> Bool) -> Element -> [Element]
filterChildren :: (Element -> Bool) -> Element -> [Element]
filterChildren Element -> Bool
p Element
e = (Element -> Bool) -> [Element] -> [Element]
forall a. (a -> Bool) -> [a] -> [a]
filter Element -> Bool
p ([Content] -> [Element]
onlyElems (Element -> [Content]
elContent Element
e))
filterChildrenName :: (QName -> Bool) -> Element -> [Element]
filterChildrenName :: (QName -> Bool) -> Element -> [Element]
filterChildrenName QName -> Bool
p Element
e = (Element -> Bool) -> [Element] -> [Element]
forall a. (a -> Bool) -> [a] -> [a]
filter (QName -> Bool
p(QName -> Bool) -> (Element -> QName) -> Element -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
.Element -> QName
elName) ([Content] -> [Element]
onlyElems (Element -> [Content]
elContent Element
e))
findChild :: QName -> Element -> Maybe Element
findChild :: QName -> Element -> Maybe Element
findChild QName
q Element
e = [Element] -> Maybe Element
forall a. [a] -> Maybe a
listToMaybe (QName -> Element -> [Element]
findChildren QName
q Element
e)
filterChild :: (Element -> Bool) -> Element -> Maybe Element
filterChild :: (Element -> Bool) -> Element -> Maybe Element
filterChild Element -> Bool
p Element
e = [Element] -> Maybe Element
forall a. [a] -> Maybe a
listToMaybe ((Element -> Bool) -> Element -> [Element]
filterChildren Element -> Bool
p Element
e)
filterChildName :: (QName -> Bool) -> Element -> Maybe Element
filterChildName :: (QName -> Bool) -> Element -> Maybe Element
filterChildName QName -> Bool
p Element
e = [Element] -> Maybe Element
forall a. [a] -> Maybe a
listToMaybe ((QName -> Bool) -> Element -> [Element]
filterChildrenName QName -> Bool
p Element
e)
findElement :: QName -> Element -> Maybe Element
findElement :: QName -> Element -> Maybe Element
findElement QName
q Element
e = [Element] -> Maybe Element
forall a. [a] -> Maybe a
listToMaybe (QName -> Element -> [Element]
findElements QName
q Element
e)
filterElement :: (Element -> Bool) -> Element -> Maybe Element
filterElement :: (Element -> Bool) -> Element -> Maybe Element
filterElement Element -> Bool
p Element
e = [Element] -> Maybe Element
forall a. [a] -> Maybe a
listToMaybe ((Element -> Bool) -> Element -> [Element]
filterElements Element -> Bool
p Element
e)
filterElementName :: (QName -> Bool) -> Element -> Maybe Element
filterElementName :: (QName -> Bool) -> Element -> Maybe Element
filterElementName QName -> Bool
p Element
e = [Element] -> Maybe Element
forall a. [a] -> Maybe a
listToMaybe ((QName -> Bool) -> Element -> [Element]
filterElementsName QName -> Bool
p Element
e)
findElements :: QName -> Element -> [Element]
findElements :: QName -> Element -> [Element]
findElements QName
qn Element
e = (QName -> Bool) -> Element -> [Element]
filterElementsName (QName
qnQName -> QName -> Bool
forall a. Eq a => a -> a -> Bool
==) Element
e
filterElements :: (Element -> Bool) -> Element -> [Element]
filterElements :: (Element -> Bool) -> Element -> [Element]
filterElements Element -> Bool
p Element
e
| Element -> Bool
p Element
e = [Element
e]
| Bool
otherwise = (Element -> [Element]) -> [Element] -> [Element]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap ((Element -> Bool) -> Element -> [Element]
filterElements Element -> Bool
p) ([Element] -> [Element]) -> [Element] -> [Element]
forall a b. (a -> b) -> a -> b
$ [Content] -> [Element]
onlyElems ([Content] -> [Element]) -> [Content] -> [Element]
forall a b. (a -> b) -> a -> b
$ Element -> [Content]
elContent Element
e
filterElementsName :: (QName -> Bool) -> Element -> [Element]
filterElementsName :: (QName -> Bool) -> Element -> [Element]
filterElementsName QName -> Bool
p Element
e = (Element -> Bool) -> Element -> [Element]
filterElements (QName -> Bool
p(QName -> Bool) -> (Element -> QName) -> Element -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
.Element -> QName
elName) Element
e
findAttr :: QName -> Element -> Maybe String
findAttr :: QName -> Element -> Maybe String
findAttr QName
x Element
e = QName -> [Attr] -> Maybe String
lookupAttr QName
x (Element -> [Attr]
elAttribs Element
e)
lookupAttr :: QName -> [Attr] -> Maybe String
lookupAttr :: QName -> [Attr] -> Maybe String
lookupAttr QName
x = (QName -> Bool) -> [Attr] -> Maybe String
lookupAttrBy (QName
x QName -> QName -> Bool
forall a. Eq a => a -> a -> Bool
==)
lookupAttrBy :: (QName -> Bool) -> [Attr] -> Maybe String
lookupAttrBy :: (QName -> Bool) -> [Attr] -> Maybe String
lookupAttrBy QName -> Bool
p [Attr]
as = Attr -> String
attrVal (Attr -> String) -> Maybe Attr -> Maybe String
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` (Attr -> Bool) -> [Attr] -> Maybe Attr
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find (QName -> Bool
p (QName -> Bool) -> (Attr -> QName) -> Attr -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Attr -> QName
attrKey) [Attr]
as
findAttrBy :: (QName -> Bool) -> Element -> Maybe String
findAttrBy :: (QName -> Bool) -> Element -> Maybe String
findAttrBy QName -> Bool
p Element
e = (QName -> Bool) -> [Attr] -> Maybe String
lookupAttrBy QName -> Bool
p (Element -> [Attr]
elAttribs Element
e)