--------------------------------------------------------------------
-- |
-- Module    : Text.RSS1.Import
-- Copyright : (c) Galois, Inc. 2008,
--             (c) Sigbjorn Finne 2009-
-- License   : BSD3
--
-- Maintainer: Sigbjorn Finne <sof@forkIO.com>
-- Stability : provisional
-- Portability: portable
--
--------------------------------------------------------------------
module Text.RSS1.Import
  ( elementToFeed
  ) where

import Prelude.Compat

import Data.XML.Compat
import Data.XML.Types as XML
import Text.DublinCore.Types
import Text.RSS1.Syntax
import Text.RSS1.Utils

import Control.Monad.Compat (guard, mplus)
import Data.Maybe (mapMaybe)
import Data.Text.Util

---
elementToFeed :: XML.Element -> Maybe Feed
elementToFeed :: Element -> Maybe Feed
elementToFeed Element
e = do
  Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Element -> Name
elementName Element
e Name -> Name -> Bool
forall a. Eq a => a -> a -> Bool
== Text -> Name
rdfName Text
"RDF")
  Text
ver <- (Maybe Text, Maybe Text) -> Text -> Element -> Maybe Text
pAttr (Maybe Text
forall a. Maybe a
Nothing, Maybe Text
forall a. Maybe a
Nothing) Text
"xmlns" Element
e Maybe Text -> Maybe Text -> Maybe Text
forall (m :: * -> *) a. MonadPlus m => m a -> m a -> m a
`mplus` Text -> Maybe Text
forall a. a -> Maybe a
Just Text
rss10NS
  Channel
ch <- Text -> Element -> Maybe Element
pNode Text
"channel" Element
e Maybe Element -> (Element -> Maybe Channel) -> Maybe Channel
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Element -> Maybe Channel
elementToChannel
  let mbImg :: Maybe Image
mbImg = Text -> Element -> Maybe Element
pNode Text
"image" Element
e Maybe Element -> (Element -> Maybe Image) -> Maybe Image
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Element -> Maybe Image
elementToImage
  let is :: [Text]
is = [Text] -> (Element -> [Text]) -> Maybe Element -> [Text]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] Element -> [Text]
elementToItems (Maybe Element -> [Text]) -> Maybe Element -> [Text]
forall a b. (a -> b) -> a -> b
$ Text -> Element -> Maybe Element
pNode Text
"items" Element
e
  let mbTI :: Maybe TextInputInfo
mbTI = Text -> Element -> Maybe Element
pNode Text
"textinput" Element
e Maybe Element
-> (Element -> Maybe TextInputInfo) -> Maybe TextInputInfo
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Element -> Maybe TextInputInfo
elementToTextInput
  let ch1 :: Channel
ch1 = Channel
ch {channelItemURIs :: [Text]
channelItemURIs = [Text]
is}
  let its :: [Item]
its = (Maybe Text, Maybe Text)
-> Text -> (Element -> Maybe Item) -> Element -> [Item]
forall a.
(Maybe Text, Maybe Text)
-> Text -> (Element -> Maybe a) -> Element -> [a]
pMany (Text -> Maybe Text
forall a. a -> Maybe a
Just Text
rss10NS, Maybe Text
forall a. Maybe a
Nothing) Text
"item" Element -> Maybe Item
elementToItem Element
e
  let es_rest :: [Element]
es_rest = Element -> [Element]
removeKnownElts Element
e
  let as_rest :: [Attr]
as_rest = Element -> [Attr]
removeKnownAttrs Element
e
  Feed -> Maybe Feed
forall (m :: * -> *) a. Monad m => a -> m a
return
    Feed :: Text
-> Channel
-> Maybe Image
-> [Item]
-> Maybe TextInputInfo
-> [TaxonomyTopic]
-> [Element]
-> [Attr]
-> Feed
Feed
      { feedVersion :: Text
feedVersion = Text
ver
      , feedChannel :: Channel
feedChannel = Channel
ch1
      , feedImage :: Maybe Image
feedImage = Maybe Image
mbImg
      , feedItems :: [Item]
feedItems = [Item]
its
      , feedTextInput :: Maybe TextInputInfo
feedTextInput = Maybe TextInputInfo
mbTI
      , feedTopics :: [TaxonomyTopic]
feedTopics =
          (Element -> Maybe TaxonomyTopic) -> [Element] -> [TaxonomyTopic]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe Element -> Maybe TaxonomyTopic
elementToTaxonomyTopic ([Element] -> [TaxonomyTopic]) -> [Element] -> [TaxonomyTopic]
forall a b. (a -> b) -> a -> b
$ Name -> Element -> [Element]
pQNodes ((Text, Text) -> Text -> Name
qualName' (Text
taxNS, Text
taxPrefix) Text
"topic") Element
e
      , feedOther :: [Element]
feedOther = [Element]
es_rest
      , feedAttrs :: [Attr]
feedAttrs = [Attr]
as_rest
      }

elementToItems :: XML.Element -> [URIString]
elementToItems :: Element -> [Text]
elementToItems = Element -> [Text]
seqLeaves

elementToTextInput :: XML.Element -> Maybe TextInputInfo
elementToTextInput :: Element -> Maybe TextInputInfo
elementToTextInput Element
e = do
  let es :: [Element]
es = Element -> [Element]
children Element
e
  Text
uri <- (Text, Text) -> Text -> Element -> Maybe Text
pAttr' (Text
rdfNS, Text
rdfPrefix) Text
"about" Element
e
  Text
ti <- (Text, Maybe Text) -> Text -> Element -> Maybe Text
pQLeaf (Text
rss10NS, Maybe Text
forall a. Maybe a
Nothing) Text
"title" Element
e
  Text
desc <- (Text, Maybe Text) -> Text -> Element -> Maybe Text
pQLeaf (Text
rss10NS, Maybe Text
forall a. Maybe a
Nothing) Text
"description" Element
e
  Text
na <- (Text, Maybe Text) -> Text -> Element -> Maybe Text
pQLeaf (Text
rss10NS, Maybe Text
forall a. Maybe a
Nothing) Text
"name" Element
e
  Text
li <- (Text, Maybe Text) -> Text -> Element -> Maybe Text
pQLeaf (Text
rss10NS, Maybe Text
forall a. Maybe a
Nothing) Text
"link" Element
e
  let dcs :: [DCItem]
dcs = (Element -> Maybe DCItem) -> [Element] -> [DCItem]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe Element -> Maybe DCItem
elementToDC [Element]
es
  TextInputInfo -> Maybe TextInputInfo
forall (m :: * -> *) a. Monad m => a -> m a
return
    TextInputInfo :: Text
-> Text
-> Text
-> Text
-> Text
-> [DCItem]
-> [Element]
-> [Attr]
-> TextInputInfo
TextInputInfo
      { textInputURI :: Text
textInputURI = Text
uri
      , textInputTitle :: Text
textInputTitle = Text
ti
      , textInputDesc :: Text
textInputDesc = Text
desc
      , textInputName :: Text
textInputName = Text
na
      , textInputLink :: Text
textInputLink = Text
li
      , textInputDC :: [DCItem]
textInputDC = [DCItem]
dcs
      , textInputOther :: [Element]
textInputOther = [Element]
es
      , textInputAttrs :: [Attr]
textInputAttrs = Element -> [Attr]
elementAttributes Element
e
      }

elementToItem :: XML.Element -> Maybe Item
elementToItem :: Element -> Maybe Item
elementToItem Element
e = do
  Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Element -> Name
elementName Element
e Name -> Name -> Bool
forall a. Eq a => a -> a -> Bool
== (Maybe Text, Maybe Text) -> Text -> Name
qualName (Text -> Maybe Text
forall a. a -> Maybe a
Just Text
rss10NS, Maybe Text
forall a. Maybe a
Nothing) Text
"item")
  let es :: [Element]
es = Element -> [Element]
children Element
e
  Text
uri <- (Text, Text) -> Text -> Element -> Maybe Text
pAttr' (Text
rdfNS, Text
rdfPrefix) Text
"about" Element
e
  Text
ti <- (Text, Maybe Text) -> Text -> Element -> Maybe Text
pQLeaf (Text
rss10NS, Maybe Text
forall a. Maybe a
Nothing) Text
"title" Element
e
  Text
li <- (Text, Maybe Text) -> Text -> Element -> Maybe Text
pQLeaf (Text
rss10NS, Maybe Text
forall a. Maybe a
Nothing) Text
"link" Element
e
  let desc :: Maybe Text
desc = (Text, Maybe Text) -> Text -> Element -> Maybe Text
pQLeaf (Text
rss10NS, Maybe Text
forall a. Maybe a
Nothing) Text
"description" Element
e
  let dcs :: [DCItem]
dcs = (Element -> Maybe DCItem) -> [Element] -> [DCItem]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe Element -> Maybe DCItem
elementToDC [Element]
es
  let tos :: [Text]
tos = [Text] -> (Element -> [Text]) -> Maybe Element -> [Text]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] Element -> [Text]
bagLeaves (Maybe Element -> [Text]) -> Maybe Element -> [Text]
forall a b. (a -> b) -> a -> b
$ Name -> Element -> Maybe Element
pQNode ((Text, Text) -> Text -> Name
qualName' (Text
taxNS, Text
taxPrefix) Text
"topics") Element
e
  let cs :: [ContentInfo]
cs = (Element -> Maybe ContentInfo) -> [Element] -> [ContentInfo]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe Element -> Maybe ContentInfo
elementToContent [Element]
es
  let es_other :: [Element]
es_other = Element -> [Element]
removeKnownElts Element
e
  let as_other :: [Attr]
as_other = Element -> [Attr]
removeKnownAttrs Element
e
  Item -> Maybe Item
forall (m :: * -> *) a. Monad m => a -> m a
return
    Item :: Text
-> Text
-> Text
-> Maybe Text
-> [DCItem]
-> [Text]
-> [ContentInfo]
-> [Element]
-> [Attr]
-> Item
Item
      { itemURI :: Text
itemURI = Text
uri
      , itemTitle :: Text
itemTitle = Text
ti
      , itemLink :: Text
itemLink = Text
li
      , itemDesc :: Maybe Text
itemDesc = Maybe Text
desc
      , itemDC :: [DCItem]
itemDC = [DCItem]
dcs
      , itemTopics :: [Text]
itemTopics = [Text]
tos
      , itemContent :: [ContentInfo]
itemContent = [ContentInfo]
cs
      , itemOther :: [Element]
itemOther = [Element]
es_other
      , itemAttrs :: [Attr]
itemAttrs = [Attr]
as_other
      }

elementToImage :: XML.Element -> Maybe Image
elementToImage :: Element -> Maybe Image
elementToImage Element
e = do
  let es :: [Element]
es = Element -> [Element]
children Element
e
  let as :: [Attr]
as = Element -> [Attr]
elementAttributes Element
e
  Text
uri <- (Text, Text) -> Text -> Element -> Maybe Text
pAttr' (Text
rdfNS, Text
rdfPrefix) Text
"about" Element
e
  Text
ti <- Text -> Element -> Maybe Text
pLeaf Text
"title" Element
e
  Text
ur <- Text -> Element -> Maybe Text
pLeaf Text
"url" Element
e
  Text
li <- Text -> Element -> Maybe Text
pLeaf Text
"link" Element
e
  let dcs :: [DCItem]
dcs = (Element -> Maybe DCItem) -> [Element] -> [DCItem]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe Element -> Maybe DCItem
elementToDC [Element]
es
  Image -> Maybe Image
forall (m :: * -> *) a. Monad m => a -> m a
return
    Image :: Text
-> Text -> Text -> Text -> [DCItem] -> [Element] -> [Attr] -> Image
Image
      { imageURI :: Text
imageURI = Text
uri
      , imageTitle :: Text
imageTitle = Text
ti
      , imageURL :: Text
imageURL = Text
ur
      , imageLink :: Text
imageLink = Text
li
      , imageDC :: [DCItem]
imageDC = [DCItem]
dcs
      , imageOther :: [Element]
imageOther = [Element]
es
      , imageAttrs :: [Attr]
imageAttrs = [Attr]
as
      }

elementToChannel :: XML.Element -> Maybe Channel
elementToChannel :: Element -> Maybe Channel
elementToChannel Element
e = do
  let es :: [Element]
es = Element -> [Element]
children Element
e
  Text
uri <- (Text, Text) -> Text -> Element -> Maybe Text
pAttr' (Text
rdfNS, Text
rdfPrefix) Text
"about" Element
e
  Text
ti <- Text -> Element -> Maybe Text
pLeaf Text
"title" Element
e
  Text
li <- Text -> Element -> Maybe Text
pLeaf Text
"link" Element
e
  Text
de <- Text -> Element -> Maybe Text
pLeaf Text
"description" Element
e
  let mbImg :: Maybe Text
mbImg = Text -> Element -> Maybe Text
pLeaf Text
"image" Element
e
  let is :: [Text]
is = [Text] -> (Element -> [Text]) -> Maybe Element -> [Text]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] Element -> [Text]
seqLeaves (Maybe Element -> [Text]) -> Maybe Element -> [Text]
forall a b. (a -> b) -> a -> b
$ Text -> Element -> Maybe Element
pNode Text
"items" Element
e
  let tinp :: Maybe Text
tinp = Text -> Element -> Maybe Text
pLeaf Text
"textinput" Element
e
  let dcs :: [DCItem]
dcs = (Element -> Maybe DCItem) -> [Element] -> [DCItem]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe Element -> Maybe DCItem
elementToDC [Element]
es
  let tos :: [Text]
tos = [Text] -> (Element -> [Text]) -> Maybe Element -> [Text]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] Element -> [Text]
bagLeaves (Maybe Element -> [Text]) -> Maybe Element -> [Text]
forall a b. (a -> b) -> a -> b
$ Name -> Element -> Maybe Element
pQNode ((Text, Text) -> Text -> Name
qualName' (Text
taxNS, Text
taxPrefix) Text
"topics") Element
e
  let cs :: [ContentInfo]
cs = (Element -> Maybe ContentInfo) -> [Element] -> [ContentInfo]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe Element -> Maybe ContentInfo
elementToContent [Element]
es
  let es_other :: [Element]
es_other = Element -> [Element]
removeKnownElts Element
e
  let as_other :: [Attr]
as_other = Element -> [Attr]
removeKnownAttrs Element
e
  let def_chan :: Channel
def_chan =
        Channel :: Text
-> Text
-> Text
-> Text
-> Maybe Text
-> [Text]
-> Maybe Text
-> [DCItem]
-> Maybe UpdatePeriod
-> Maybe Integer
-> Maybe Text
-> [ContentInfo]
-> [Text]
-> [Element]
-> [Attr]
-> Channel
Channel
          { channelURI :: Text
channelURI = Text
uri
          , channelTitle :: Text
channelTitle = Text
ti
          , channelLink :: Text
channelLink = Text
li
          , channelDesc :: Text
channelDesc = Text
de
          , channelImageURI :: Maybe Text
channelImageURI = Maybe Text
mbImg
          , channelItemURIs :: [Text]
channelItemURIs = [Text]
is
          , channelTextInputURI :: Maybe Text
channelTextInputURI = Maybe Text
tinp
          , channelDC :: [DCItem]
channelDC = [DCItem]
dcs
          , channelUpdatePeriod :: Maybe UpdatePeriod
channelUpdatePeriod = Maybe UpdatePeriod
forall a. Maybe a
Nothing
          , channelUpdateFreq :: Maybe Integer
channelUpdateFreq = Maybe Integer
forall a. Maybe a
Nothing
          , channelUpdateBase :: Maybe Text
channelUpdateBase = Maybe Text
forall a. Maybe a
Nothing
          , channelContent :: [ContentInfo]
channelContent = [ContentInfo]
cs
          , channelTopics :: [Text]
channelTopics = [Text]
tos
          , channelOther :: [Element]
channelOther = [Element]
es_other
          , channelAttrs :: [Attr]
channelAttrs = [Attr]
as_other
          }
  Channel -> Maybe Channel
forall (m :: * -> *) a. Monad m => a -> m a
return (Element -> Channel -> Channel
addSyndication Element
e Channel
def_chan)

addSyndication :: XML.Element -> Channel -> Channel
addSyndication :: Element -> Channel -> Channel
addSyndication Element
e Channel
ch =
  Channel
ch
    { channelUpdatePeriod :: Maybe UpdatePeriod
channelUpdatePeriod = Text -> UpdatePeriod
forall a. (Eq a, IsString a) => a -> UpdatePeriod
toUpdatePeriod (Text -> UpdatePeriod) -> Maybe Text -> Maybe UpdatePeriod
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Text, Text) -> Text -> Element -> Maybe Text
pQLeaf' (Text
synNS, Text
synPrefix) Text
"updatePeriod" Element
e
    , channelUpdateFreq :: Maybe Integer
channelUpdateFreq = Text -> Maybe Integer
readInt (Text -> Maybe Integer) -> Maybe Text -> Maybe Integer
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< (Text, Text) -> Text -> Element -> Maybe Text
pQLeaf' (Text
synNS, Text
synPrefix) Text
"updateFrequency" Element
e
    , channelUpdateBase :: Maybe Text
channelUpdateBase = (Text, Text) -> Text -> Element -> Maybe Text
pQLeaf' (Text
synNS, Text
synPrefix) Text
"updateBase" Element
e
    }
  where
    toUpdatePeriod :: a -> UpdatePeriod
toUpdatePeriod a
x =
      case a
x of
        a
"hourly" -> UpdatePeriod
Update_Hourly
        a
"daily" -> UpdatePeriod
Update_Daily
        a
"weekly" -> UpdatePeriod
Update_Weekly
        a
"monthly" -> UpdatePeriod
Update_Monthly
        a
"yearly" -> UpdatePeriod
Update_Yearly
        a
_ -> UpdatePeriod
Update_Hourly -- ToDo: whine

elementToDC :: XML.Element -> Maybe DCItem
elementToDC :: Element -> Maybe DCItem
elementToDC Element
e = do
  Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Name -> Maybe Text
nameNamespace (Element -> Name
elementName Element
e) Maybe Text -> Maybe Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text -> Maybe Text
forall a. a -> Maybe a
Just Text
dcNS)
  let dcItem :: DCInfo -> DCItem
dcItem DCInfo
x = DCItem :: DCInfo -> Text -> DCItem
DCItem {dcElt :: DCInfo
dcElt = DCInfo
x, dcText :: Text
dcText = Element -> Text
strContent Element
e}
  DCItem -> Maybe DCItem
forall (m :: * -> *) a. Monad m => a -> m a
return (DCItem -> Maybe DCItem) -> DCItem -> Maybe DCItem
forall a b. (a -> b) -> a -> b
$
    DCInfo -> DCItem
dcItem (DCInfo -> DCItem) -> DCInfo -> DCItem
forall a b. (a -> b) -> a -> b
$
    case Name -> Text
nameLocalName (Name -> Text) -> Name -> Text
forall a b. (a -> b) -> a -> b
$ Element -> Name
elementName Element
e of
      Text
"title" -> DCInfo
DC_Title
      Text
"creator" -> DCInfo
DC_Creator
      Text
"subject" -> DCInfo
DC_Subject
      Text
"description" -> DCInfo
DC_Description
      Text
"publisher" -> DCInfo
DC_Publisher
      Text
"contributor" -> DCInfo
DC_Contributor
      Text
"date" -> DCInfo
DC_Date
      Text
"type" -> DCInfo
DC_Type
      Text
"format" -> DCInfo
DC_Format
      Text
"identifier" -> DCInfo
DC_Identifier
      Text
"source" -> DCInfo
DC_Source
      Text
"language" -> DCInfo
DC_Language
      Text
"relation" -> DCInfo
DC_Relation
      Text
"coverage" -> DCInfo
DC_Coverage
      Text
"rights" -> DCInfo
DC_Rights
      Text
oth -> Text -> DCInfo
DC_Other Text
oth

elementToTaxonomyTopic :: XML.Element -> Maybe TaxonomyTopic
elementToTaxonomyTopic :: Element -> Maybe TaxonomyTopic
elementToTaxonomyTopic Element
e = do
  Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Element -> Name
elementName Element
e Name -> Name -> Bool
forall a. Eq a => a -> a -> Bool
== (Text, Text) -> Text -> Name
qualName' (Text
taxNS, Text
taxPrefix) Text
"topic")
  let es :: [Element]
es = Element -> [Element]
children Element
e
  Text
uri <- (Text, Text) -> Text -> Element -> Maybe Text
pAttr' (Text
rdfNS, Text
rdfPrefix) Text
"about" Element
e
  Text
li <- (Text, Text) -> Text -> Element -> Maybe Text
pQLeaf' (Text
taxNS, Text
taxPrefix) Text
"link" Element
e
  TaxonomyTopic -> Maybe TaxonomyTopic
forall (m :: * -> *) a. Monad m => a -> m a
return
    TaxonomyTopic :: Text
-> Text
-> Maybe Text
-> Maybe Text
-> [Text]
-> [DCItem]
-> [Element]
-> TaxonomyTopic
TaxonomyTopic
      { taxonomyURI :: Text
taxonomyURI = Text
uri
      , taxonomyLink :: Text
taxonomyLink = Text
li
      , taxonomyTitle :: Maybe Text
taxonomyTitle = Text -> Element -> Maybe Text
pLeaf Text
"title" Element
e
      , taxonomyDesc :: Maybe Text
taxonomyDesc = Text -> Element -> Maybe Text
pLeaf Text
"description" Element
e
      , taxonomyTopics :: [Text]
taxonomyTopics = [Text] -> (Element -> [Text]) -> Maybe Element -> [Text]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] Element -> [Text]
bagLeaves (Maybe Element -> [Text]) -> Maybe Element -> [Text]
forall a b. (a -> b) -> a -> b
$ Name -> Element -> Maybe Element
pQNode ((Text, Text) -> Text -> Name
qualName' (Text
taxNS, Text
taxPrefix) Text
"topics") Element
e
      , taxonomyDC :: [DCItem]
taxonomyDC = (Element -> Maybe DCItem) -> [Element] -> [DCItem]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe Element -> Maybe DCItem
elementToDC [Element]
es
      , taxonomyOther :: [Element]
taxonomyOther = [Element]
es
      }

elementToContent :: XML.Element -> Maybe ContentInfo
elementToContent :: Element -> Maybe ContentInfo
elementToContent Element
e = do
  Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Element -> Name
elementName Element
e Name -> Name -> Bool
forall a. Eq a => a -> a -> Bool
== (Text, Text) -> Text -> Name
qualName' (Text
conNS, Text
conPrefix) Text
"items")
  ContentInfo -> Maybe ContentInfo
forall (m :: * -> *) a. Monad m => a -> m a
return
    ContentInfo :: Maybe Text -> Maybe Text -> Maybe Text -> Maybe Text -> ContentInfo
ContentInfo
      { contentURI :: Maybe Text
contentURI = (Text, Text) -> Text -> Element -> Maybe Text
pAttr' (Text
rdfNS, Text
rdfPrefix) Text
"about" Element
e
      , contentFormat :: Maybe Text
contentFormat = (Text, Text) -> Text -> Element -> Maybe Text
pQLeaf' (Text
conNS, Text
conPrefix) Text
"format" Element
e
      , contentEncoding :: Maybe Text
contentEncoding = (Text, Text) -> Text -> Element -> Maybe Text
pQLeaf' (Text
conNS, Text
conPrefix) Text
"encoding" Element
e
      , contentValue :: Maybe Text
contentValue = (Text, Text) -> Text -> Element -> Maybe Text
pQLeaf' (Text
rdfNS, Text
rdfPrefix) Text
"value" Element
e
      }

bagLeaves :: XML.Element -> [URIString]
bagLeaves :: Element -> [Text]
bagLeaves Element
be =
  (Element -> Maybe Text) -> [Element] -> [Text]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe
    (\Element
e -> do
       Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Element -> Name
elementName Element
e Name -> Name -> Bool
forall a. Eq a => a -> a -> Bool
== (Text, Text) -> Text -> Name
qualName' (Text
rdfNS, Text
rdfPrefix) Text
"li")
       (Text, Text) -> Text -> Element -> Maybe Text
pAttr' (Text
rdfNS, Text
rdfPrefix) Text
"resource" Element
e Maybe Text -> Maybe Text -> Maybe Text
forall (m :: * -> *) a. MonadPlus m => m a -> m a -> m a
`mplus`
         (Element -> Text) -> Maybe Element -> Maybe Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Element -> Text
strContent (Name -> Element -> Maybe Element
pQNode ((Text, Text) -> Text -> Name
qualName' (Text
rdfNS, Text
rdfPrefix) Text
"li") Element
e))
    ([Element] -> (Element -> [Element]) -> Maybe Element -> [Element]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] Element -> [Element]
children (Maybe Element -> [Element]) -> Maybe Element -> [Element]
forall a b. (a -> b) -> a -> b
$ Name -> Element -> Maybe Element
pQNode ((Text, Text) -> Text -> Name
qualName' (Text
rdfNS, Text
rdfPrefix) Text
"Bag") Element
be)

{-
bagElements :: XML.Element -> [XML.Element]
bagElements be =
  mapMaybe
    (\ e -> do
      guard (elementName e == rdfName "li")
      return e)
    (fromMaybe [] $ fmap children $ pQNode (rdfName "Bag") be)
-}
seqLeaves :: XML.Element -> [URIString]
seqLeaves :: Element -> [Text]
seqLeaves Element
se =
  (Element -> Maybe Text) -> [Element] -> [Text]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe
    (\Element
e -> do
       Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Element -> Name
elementName Element
e Name -> Name -> Bool
forall a. Eq a => a -> a -> Bool
== Text -> Name
rdfName Text
"li")
       Text -> Maybe Text
forall (m :: * -> *) a. Monad m => a -> m a
return (Element -> Text
strContent Element
e))
    ([Element] -> (Element -> [Element]) -> Maybe Element -> [Element]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] Element -> [Element]
children (Maybe Element -> [Element]) -> Maybe Element -> [Element]
forall a b. (a -> b) -> a -> b
$ Name -> Element -> Maybe Element
pQNode (Text -> Name
rdfName Text
"Seq") Element
se)