module Data.GI.GIR.XMLUtils
( nodeToElement
, subelements
, localName
, lookupAttr
, GIRXMLNamespace(..)
, lookupAttrWithNamespace
, childElemsWithLocalName
, childElemsWithNSName
, firstChildWithLocalName
, getElementContent
, xmlLocalName
, xmlNSName
) where
import Text.XML (Element(elementNodes, elementName, elementAttributes),
Node(NodeContent, NodeElement), nameLocalName, Name(..))
import Data.Maybe (mapMaybe, listToMaybe)
import qualified Data.Map as M
import Data.Text (Text)
nodeToElement :: Node -> Maybe Element
nodeToElement :: Node -> Maybe Element
nodeToElement (NodeElement Element
e) = Element -> Maybe Element
forall a. a -> Maybe a
Just Element
e
nodeToElement Node
_ = Maybe Element
forall a. Maybe a
Nothing
subelements :: Element -> [Element]
subelements :: Element -> [Element]
subelements = (Node -> Maybe Element) -> [Node] -> [Element]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe Node -> Maybe Element
nodeToElement ([Node] -> [Element])
-> (Element -> [Node]) -> Element -> [Element]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Element -> [Node]
elementNodes
localName :: Element -> Text
localName :: Element -> Text
localName = Name -> Text
nameLocalName (Name -> Text) -> (Element -> Name) -> Element -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Element -> Name
elementName
childElemsWithLocalName :: Text -> Element -> [Element]
childElemsWithLocalName :: Text -> Element -> [Element]
childElemsWithLocalName Text
n =
(Element -> Bool) -> [Element] -> [Element]
forall a. (a -> Bool) -> [a] -> [a]
filter Element -> Bool
localNameMatch ([Element] -> [Element])
-> (Element -> [Element]) -> Element -> [Element]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Element -> [Element]
subelements
where localNameMatch :: Element -> Bool
localNameMatch = (Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
n) (Text -> Bool) -> (Element -> Text) -> Element -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Element -> Text
localName
childElemsWithNSName :: GIRXMLNamespace -> Text -> Element -> [Element]
childElemsWithNSName :: GIRXMLNamespace -> Text -> Element -> [Element]
childElemsWithNSName GIRXMLNamespace
ns Text
n = (Element -> Bool) -> [Element] -> [Element]
forall a. (a -> Bool) -> [a] -> [a]
filter Element -> Bool
nameMatch ([Element] -> [Element])
-> (Element -> [Element]) -> Element -> [Element]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Element -> [Element]
subelements
where nameMatch :: Element -> Bool
nameMatch = (Name -> Name -> Bool
forall a. Eq a => a -> a -> Bool
== Name
name) (Name -> Bool) -> (Element -> Name) -> Element -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Element -> Name
elementName
name :: Name
name = Name :: Text -> Maybe Text -> Maybe Text -> Name
Name {
nameLocalName :: Text
nameLocalName = Text
n
, nameNamespace :: Maybe Text
nameNamespace = Text -> Maybe Text
forall a. a -> Maybe a
Just (GIRXMLNamespace -> Text
girNamespace GIRXMLNamespace
ns)
, namePrefix :: Maybe Text
namePrefix = Maybe Text
forall a. Maybe a
Nothing
}
firstChildWithLocalName :: Text -> Element -> Maybe Element
firstChildWithLocalName :: Text -> Element -> Maybe Element
firstChildWithLocalName Text
n = [Element] -> Maybe Element
forall a. [a] -> Maybe a
listToMaybe ([Element] -> Maybe Element)
-> (Element -> [Element]) -> Element -> Maybe Element
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Element -> [Element]
childElemsWithLocalName Text
n
getElementContent :: Element -> Maybe Text
getElementContent :: Element -> Maybe Text
getElementContent = [Text] -> Maybe Text
forall a. [a] -> Maybe a
listToMaybe ([Text] -> Maybe Text)
-> (Element -> [Text]) -> Element -> Maybe Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Node -> Maybe Text) -> [Node] -> [Text]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe Node -> Maybe Text
getContent ([Node] -> [Text]) -> (Element -> [Node]) -> Element -> [Text]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Element -> [Node]
elementNodes
where getContent :: Node -> Maybe Text
getContent :: Node -> Maybe Text
getContent (NodeContent Text
t) = Text -> Maybe Text
forall a. a -> Maybe a
Just Text
t
getContent Node
_ = Maybe Text
forall a. Maybe a
Nothing
lookupAttr :: Name -> Element -> Maybe Text
lookupAttr :: Name -> Element -> Maybe Text
lookupAttr Name
attr Element
element = Name -> Map Name Text -> Maybe Text
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup Name
attr (Element -> Map Name Text
elementAttributes Element
element)
data GIRXMLNamespace = GLibGIRNS | CGIRNS | CoreGIRNS
deriving Int -> GIRXMLNamespace -> ShowS
[GIRXMLNamespace] -> ShowS
GIRXMLNamespace -> String
(Int -> GIRXMLNamespace -> ShowS)
-> (GIRXMLNamespace -> String)
-> ([GIRXMLNamespace] -> ShowS)
-> Show GIRXMLNamespace
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [GIRXMLNamespace] -> ShowS
$cshowList :: [GIRXMLNamespace] -> ShowS
show :: GIRXMLNamespace -> String
$cshow :: GIRXMLNamespace -> String
showsPrec :: Int -> GIRXMLNamespace -> ShowS
$cshowsPrec :: Int -> GIRXMLNamespace -> ShowS
Show
girNamespace :: GIRXMLNamespace -> Text
girNamespace :: GIRXMLNamespace -> Text
girNamespace GIRXMLNamespace
GLibGIRNS = Text
"http://www.gtk.org/introspection/glib/1.0"
girNamespace GIRXMLNamespace
CGIRNS = Text
"http://www.gtk.org/introspection/c/1.0"
girNamespace GIRXMLNamespace
CoreGIRNS = Text
"http://www.gtk.org/introspection/core/1.0"
lookupAttrWithNamespace :: GIRXMLNamespace -> Name -> Element -> Maybe Text
lookupAttrWithNamespace :: GIRXMLNamespace -> Name -> Element -> Maybe Text
lookupAttrWithNamespace GIRXMLNamespace
ns Name
attr Element
element =
let attr' :: Name
attr' = Name
attr {nameNamespace :: Maybe Text
nameNamespace = Text -> Maybe Text
forall a. a -> Maybe a
Just (GIRXMLNamespace -> Text
girNamespace GIRXMLNamespace
ns)}
in Name -> Map Name Text -> Maybe Text
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup Name
attr' (Element -> Map Name Text
elementAttributes Element
element)
xmlLocalName :: Text -> Name
xmlLocalName :: Text -> Name
xmlLocalName Text
n = Name :: Text -> Maybe Text -> Maybe Text -> Name
Name { nameLocalName :: Text
nameLocalName = Text
n
, nameNamespace :: Maybe Text
nameNamespace = Maybe Text
forall a. Maybe a
Nothing
, namePrefix :: Maybe Text
namePrefix = Maybe Text
forall a. Maybe a
Nothing }
xmlNSName :: GIRXMLNamespace -> Text -> Name
xmlNSName :: GIRXMLNamespace -> Text -> Name
xmlNSName GIRXMLNamespace
ns Text
n = Name :: Text -> Maybe Text -> Maybe Text -> Name
Name { nameLocalName :: Text
nameLocalName = Text
n
, nameNamespace :: Maybe Text
nameNamespace = Text -> Maybe Text
forall a. a -> Maybe a
Just (GIRXMLNamespace -> Text
girNamespace GIRXMLNamespace
ns)
, namePrefix :: Maybe Text
namePrefix = Maybe Text
forall a. Maybe a
Nothing }