{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TupleSections   #-}
{-# LANGUAGE GADTs           #-}
{-# LANGUAGE LambdaCase      #-}
{-# LANGUAGE PatternGuards   #-}
{- |
   Module      : Text.Pandoc.Readers.Odt.Generic.XMLConverter
   Copyright   : Copyright (C) 2015 Martin Linnemann
   License     : GNU GPL, version 2 or above

   Maintainer  : Martin Linnemann <theCodingMarlin@googlemail.com>
   Stability   : alpha
   Portability : portable

A generalized XML parser based on stateful arrows.
It might be sufficient to define this reader as a comonad, but there is
not a lot of use in trying.
-}

module Text.Pandoc.Readers.Odt.Generic.XMLConverter
( ElementName
, XMLConverterState
, XMLConverter
, FallibleXMLConverter
, runConverter'
, getExtraState
, setExtraState
, modifyExtraState
, producingExtraState
, findChild'
, filterChildrenName'
, isSet'
, isSetWithDefault
, elName
, searchAttr
, lookupAttr
, lookupAttr'
, lookupDefaultingAttr
, findAttr'
, findAttrText'
, findAttr
, findAttrText
, findAttrWithDefault
, findAttrTextWithDefault
, readAttr
, readAttr'
, readAttrWithDefault
, getAttr
, executeIn
, executeInSub
, withEveryL
, tryAll
, matchContent'
, matchContent
) where

import           Prelude hiding (Applicative(..))
import           Control.Applicative  hiding ( liftA, liftA2 )
import           Control.Monad               ( MonadPlus )
import           Control.Arrow

import           Data.Bool ( bool )
import           Data.Either ( rights )
import qualified Data.Map             as M
import           Data.Text (Text)
import           Data.Default
import           Data.Maybe
import           Data.List (foldl')

import qualified Text.Pandoc.XML.Light as XML

import           Text.Pandoc.Readers.Odt.Arrows.State
import           Text.Pandoc.Readers.Odt.Arrows.Utils
import           Text.Pandoc.Readers.Odt.Generic.Namespaces
import           Text.Pandoc.Readers.Odt.Generic.Utils
import           Text.Pandoc.Readers.Odt.Generic.Fallible

--------------------------------------------------------------------------------
--  Basis types for readability
--------------------------------------------------------------------------------

--
type ElementName           = Text
type AttributeName         = Text
type AttributeValue        = Text
type TextAttributeValue    = Text

--
type NameSpacePrefix       = Text

--
type NameSpacePrefixes nsID = M.Map nsID NameSpacePrefix

--------------------------------------------------------------------------------
-- Main converter state
--------------------------------------------------------------------------------

-- GADT so some of the NameSpaceID restrictions can be deduced
data XMLConverterState nsID extraState where
  XMLConverterState :: NameSpaceID nsID
    => { -- | A stack of parent elements. The top element is the current one.
         -- Arguably, a real Zipper would be better. But that is an
         -- optimization that can be made at a later time, e.g. when
         -- replacing Text.XML.Light.
         forall nsID extraState.
XMLConverterState nsID extraState -> [Element]
parentElements    :: [XML.Element]
         -- | A map from internal namespace IDs to the namespace prefixes
         -- used in XML elements
       , forall nsID extraState.
XMLConverterState nsID extraState -> NameSpacePrefixes nsID
namespacePrefixes :: NameSpacePrefixes nsID
         -- | A map from internal namespace IDs to namespace IRIs
         -- (Only necessary for matching namespace IDs and prefixes)
       , forall nsID extraState.
XMLConverterState nsID extraState -> NameSpacePrefixes nsID
namespaceIRIs     :: NameSpaceIRIs nsID
         -- | A place to put "something else". This feature is used heavily
         -- to keep the main code cleaner. More specifically, the main reader
         -- is divided into different stages. Each stage lifts something up
         -- here, which the next stage can then use. This could of course be
         -- generalized to a state-tree or used for the namespace IRIs. The
         -- border between states and values is an imaginary one, after all.
         -- But the separation as it is seems to be enough for now.
       , forall nsID extraState.
XMLConverterState nsID extraState -> extraState
moreState         :: extraState
       }
    -> XMLConverterState nsID extraState

--
createStartState :: (NameSpaceID nsID)
                    => XML.Element
                    -> extraState
                    -> XMLConverterState nsID extraState
createStartState :: forall nsID extraState.
NameSpaceID nsID =>
Element -> extraState -> XMLConverterState nsID extraState
createStartState Element
element extraState
extraState =
  XMLConverterState
       { parentElements :: [Element]
parentElements    = [Element
element]
       , namespacePrefixes :: NameSpacePrefixes nsID
namespacePrefixes = forall k a. Map k a
M.empty
       , namespaceIRIs :: NameSpacePrefixes nsID
namespaceIRIs     = forall nsID. NameSpaceID nsID => NameSpaceIRIs nsID
getInitialIRImap
       , moreState :: extraState
moreState         = extraState
extraState
       }

-- | Functor over extra state
instance Functor (XMLConverterState nsID) where
  fmap :: forall a b.
(a -> b) -> XMLConverterState nsID a -> XMLConverterState nsID b
fmap a -> b
f ( XMLConverterState [Element]
parents NameSpacePrefixes nsID
prefixes NameSpacePrefixes nsID
iRIs    a
extraState  )
       =   forall nsID extraState.
NameSpaceID nsID =>
[Element]
-> NameSpacePrefixes nsID
-> NameSpacePrefixes nsID
-> extraState
-> XMLConverterState nsID extraState
XMLConverterState [Element]
parents NameSpacePrefixes nsID
prefixes NameSpacePrefixes nsID
iRIs (a -> b
f a
extraState)

--
replaceExtraState   :: extraState
                    -> XMLConverterState nsID _x
                    -> XMLConverterState nsID extraState
replaceExtraState :: forall extraState nsID _x.
extraState
-> XMLConverterState nsID _x -> XMLConverterState nsID extraState
replaceExtraState extraState
x XMLConverterState nsID _x
s
                     = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a b. a -> b -> a
const extraState
x) XMLConverterState nsID _x
s

--
currentElement      :: XMLConverterState nsID extraState
                    -> XML.Element
currentElement :: forall nsID extraState.
XMLConverterState nsID extraState -> Element
currentElement XMLConverterState nsID extraState
state = forall a. [a] -> a
head (forall nsID extraState.
XMLConverterState nsID extraState -> [Element]
parentElements XMLConverterState nsID extraState
state)

-- | Replace the current position by another, modifying the extra state
-- in the process
swapStack'          :: XMLConverterState nsID extraState
                    -> [XML.Element]
                    -> ( XMLConverterState nsID extraState , [XML.Element] )
swapStack' :: forall nsID extraState.
XMLConverterState nsID extraState
-> [Element] -> (XMLConverterState nsID extraState, [Element])
swapStack' XMLConverterState nsID extraState
state [Element]
stack
                     = ( XMLConverterState nsID extraState
state { parentElements :: [Element]
parentElements = [Element]
stack }
                       , forall nsID extraState.
XMLConverterState nsID extraState -> [Element]
parentElements XMLConverterState nsID extraState
state
                       )

--
pushElement         :: XML.Element
                    -> XMLConverterState nsID extraState
                    -> XMLConverterState nsID extraState
pushElement :: forall nsID extraState.
Element
-> XMLConverterState nsID extraState
-> XMLConverterState nsID extraState
pushElement Element
e XMLConverterState nsID extraState
state  = XMLConverterState nsID extraState
state { parentElements :: [Element]
parentElements = Element
eforall a. a -> [a] -> [a]
:forall nsID extraState.
XMLConverterState nsID extraState -> [Element]
parentElements XMLConverterState nsID extraState
state }

-- | Pop the top element from the call stack, unless it is the last one.
popElement          :: XMLConverterState nsID extraState
                    -> Maybe (XMLConverterState nsID extraState)
popElement :: forall nsID extraState.
XMLConverterState nsID extraState
-> Maybe (XMLConverterState nsID extraState)
popElement XMLConverterState nsID extraState
state
  | Element
_:es :: [Element]
es@(Element
_:[Element]
_) <- forall nsID extraState.
XMLConverterState nsID extraState -> [Element]
parentElements XMLConverterState nsID extraState
state = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ XMLConverterState nsID extraState
state { parentElements :: [Element]
parentElements = [Element]
es }
  | Bool
otherwise                          = forall a. Maybe a
Nothing

--------------------------------------------------------------------------------
-- Main type
--------------------------------------------------------------------------------

-- It might be a good idea to pack the converters in a GADT
-- Downside: data instead of type
-- Upside: 'Failure' could be made a parameter as well.

--
type XMLConverter nsID extraState input output
      = ArrowState (XMLConverterState nsID extraState ) input output

type FallibleXMLConverter nsID extraState input output
     = XMLConverter nsID extraState input (Fallible output)

--
runConverter     :: XMLConverter nsID extraState input output
                 -> XMLConverterState nsID extraState
                 -> input
                 -> output
runConverter :: forall nsID extraState input output.
XMLConverter nsID extraState input output
-> XMLConverterState nsID extraState -> input -> output
runConverter XMLConverter nsID extraState input output
converter XMLConverterState nsID extraState
state input
input = forall a b. (a, b) -> b
snd forall a b. (a -> b) -> a -> b
$ forall state a b. ArrowState state a b -> (state, a) -> (state, b)
runArrowState XMLConverter nsID extraState input output
converter (XMLConverterState nsID extraState
state,input
input)

runConverter' :: (NameSpaceID nsID)
              => FallibleXMLConverter nsID extraState () success
              -> extraState
              -> XML.Element
              -> Fallible success
runConverter' :: forall nsID extraState success.
NameSpaceID nsID =>
FallibleXMLConverter nsID extraState () success
-> extraState -> Element -> Fallible success
runConverter' FallibleXMLConverter nsID extraState () success
converter extraState
extraState Element
element = forall nsID extraState input output.
XMLConverter nsID extraState input output
-> XMLConverterState nsID extraState -> input -> output
runConverter (forall nsID extraState x.
NameSpaceID nsID =>
FallibleXMLConverter nsID extraState x ()
readNSattributes forall (a :: * -> * -> *) x failure success success'.
ArrowChoice a =>
FallibleArrow a x failure success
-> FallibleArrow a success failure success'
-> FallibleArrow a x failure success'
>>? FallibleXMLConverter nsID extraState () success
converter) (forall nsID extraState.
NameSpaceID nsID =>
Element -> extraState -> XMLConverterState nsID extraState
createStartState Element
element extraState
extraState) ()

--
getCurrentElement :: XMLConverter nsID extraState x XML.Element
getCurrentElement :: forall nsID extraState x. XMLConverter nsID extraState x Element
getCurrentElement  = forall state b x. (state -> b) -> ArrowState state x b
extractFromState forall nsID extraState.
XMLConverterState nsID extraState -> Element
currentElement

--
getExtraState     :: XMLConverter nsID extraState x extraState
getExtraState :: forall nsID extraState x. XMLConverter nsID extraState x extraState
getExtraState      = forall state b x. (state -> b) -> ArrowState state x b
extractFromState forall nsID extraState.
XMLConverterState nsID extraState -> extraState
moreState

--
setExtraState     :: XMLConverter nsID extraState extraState extraState
setExtraState :: forall nsID extraState.
XMLConverter nsID extraState extraState extraState
setExtraState      = forall state a b.
(state -> a -> (state, b)) -> ArrowState state a b
withState forall a b. (a -> b) -> a -> b
$ \XMLConverterState nsID extraState
state extraState
extra
                                  -> (forall extraState nsID _x.
extraState
-> XMLConverterState nsID _x -> XMLConverterState nsID extraState
replaceExtraState extraState
extra XMLConverterState nsID extraState
state , extraState
extra)


-- | Lifts a function to the extra state.
modifyExtraState  :: (extraState -> extraState)
                  -> XMLConverter nsID extraState x x
modifyExtraState :: forall extraState nsID x.
(extraState -> extraState) -> XMLConverter nsID extraState x x
modifyExtraState   = forall state a. (state -> state) -> ArrowState state a a
modifyStateforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap


-- | First sets the extra state to the new value. Then modifies the original
-- extra state with a converter that uses the new state. Finally, the
-- intermediate state is dropped and the extra state is lifted into the
-- state as it was at the beginning of the function.
-- As a result, exactly the extra state and nothing else is changed.
-- The resulting converter even behaves like an identity converter on the
-- value level.
--
-- (The -ing form is meant to be mnemonic in a sequence of arrows as in
--  convertingExtraState () converter >>> doOtherStuff)
--
convertingExtraState :: extraState'
                     -> FallibleXMLConverter nsID extraState' extraState extraState
                     -> FallibleXMLConverter nsID extraState x x
convertingExtraState :: forall extraState' nsID extraState x.
extraState'
-> FallibleXMLConverter nsID extraState' extraState extraState
-> FallibleXMLConverter nsID extraState x x
convertingExtraState extraState'
v FallibleXMLConverter nsID extraState' extraState extraState
a = forall s x f s'.
ArrowState s x (Either f s')
-> ArrowState s' s (Either f s) -> ArrowState s x (Either f x)
withSubStateF FallibleArrow
  (ArrowState (XMLConverterState nsID extraState))
  x
  ()
  (XMLConverterState nsID extraState')
setVAsExtraState ArrowState
  (XMLConverterState nsID extraState')
  (XMLConverterState nsID extraState)
  (Either () (XMLConverterState nsID extraState))
modifyWithA
  where
    setVAsExtraState :: FallibleArrow
  (ArrowState (XMLConverterState nsID extraState))
  x
  ()
  (XMLConverterState nsID extraState')
setVAsExtraState     = forall (a :: * -> * -> *) x success failure.
ArrowChoice a =>
a x success -> FallibleArrow a x failure success
liftAsSuccess forall a b. (a -> b) -> a -> b
$ forall state b x. (state -> b) -> ArrowState state x b
extractFromState forall a. a -> a
id forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> (c -> d) -> a b d
>>^ forall extraState nsID _x.
extraState
-> XMLConverterState nsID _x -> XMLConverterState nsID extraState
replaceExtraState extraState'
v
    modifyWithA :: ArrowState
  (XMLConverterState nsID extraState')
  (XMLConverterState nsID extraState)
  (Either () (XMLConverterState nsID extraState))
modifyWithA          = forall (a :: * -> * -> *) b c. Arrow a => a b c -> a b (b, c)
keepingTheValue (forall nsID extraState.
XMLConverterState nsID extraState -> extraState
moreState forall (a :: * -> * -> *) b c d.
Arrow a =>
(b -> c) -> a c d -> a b d
^>> FallibleXMLConverter nsID extraState' extraState extraState
a)
                           forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> (c -> d) -> a b d
>>^ forall (v :: * -> *) f a.
ChoiceVector v =>
v (Either f a) -> Either f (v a)
spreadChoice forall (a :: * -> * -> *) x f b b' c.
ArrowChoice a =>
FallibleArrow a x f (b, b')
-> (b -> b' -> c) -> FallibleArrow a x f c
>>?% forall a b c. (a -> b -> c) -> b -> a -> c
flip forall extraState nsID _x.
extraState
-> XMLConverterState nsID _x -> XMLConverterState nsID extraState
replaceExtraState

-- | First sets the extra state to the new value. Then produces a new
-- extra state with a converter that uses the new state. Finally, the
-- intermediate state is dropped and the extra state is lifted into the
-- state as it was at the beginning of the function.
-- As a result, exactly the extra state and nothing else is changed.
-- The resulting converter even behaves like an identity converter on the
-- value level.
--
-- Equivalent to
--
-- > \v x a -> convertingExtraState v (returnV x >>> a)
--
-- (The -ing form is meant to be mnemonic in a sequence of arrows as in
--  producingExtraState () () producer >>> doOtherStuff)
--
producingExtraState  :: extraState'
                     -> a
                     -> FallibleXMLConverter nsID extraState' a extraState
                     -> FallibleXMLConverter nsID extraState x x
producingExtraState :: forall extraState' a nsID extraState x.
extraState'
-> a
-> FallibleXMLConverter nsID extraState' a extraState
-> FallibleXMLConverter nsID extraState x x
producingExtraState extraState'
v a
x FallibleXMLConverter nsID extraState' a extraState
a = forall extraState' nsID extraState x.
extraState'
-> FallibleXMLConverter nsID extraState' extraState extraState
-> FallibleXMLConverter nsID extraState x x
convertingExtraState extraState'
v (forall (a :: * -> * -> *) c x. Arrow a => c -> a x c
returnV a
x forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> FallibleXMLConverter nsID extraState' a extraState
a)


--------------------------------------------------------------------------------
-- Work in namespaces
--------------------------------------------------------------------------------

-- | Arrow version of 'getIRI'
lookupNSiri             :: (NameSpaceID nsID)
                        => nsID
                        -> XMLConverter nsID extraState x (Maybe NameSpaceIRI)
lookupNSiri :: forall nsID extraState x.
NameSpaceID nsID =>
nsID -> XMLConverter nsID extraState x (Maybe Text)
lookupNSiri nsID
nsID        = forall state b x. (state -> b) -> ArrowState state x b
extractFromState
                          forall a b. (a -> b) -> a -> b
$ \XMLConverterState nsID extraState
state -> forall nsID.
NameSpaceID nsID =>
nsID -> NameSpaceIRIs nsID -> Maybe Text
getIRI nsID
nsID forall a b. (a -> b) -> a -> b
$ forall nsID extraState.
XMLConverterState nsID extraState -> NameSpacePrefixes nsID
namespaceIRIs XMLConverterState nsID extraState
state

--
lookupNSprefix           :: (NameSpaceID nsID)
                         => nsID
                         -> XMLConverter nsID extraState x (Maybe NameSpacePrefix)
lookupNSprefix :: forall nsID extraState x.
NameSpaceID nsID =>
nsID -> XMLConverter nsID extraState x (Maybe Text)
lookupNSprefix nsID
nsID      = forall state b x. (state -> b) -> ArrowState state x b
extractFromState
                           forall a b. (a -> b) -> a -> b
$ \XMLConverterState nsID extraState
state -> forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup nsID
nsID forall a b. (a -> b) -> a -> b
$ forall nsID extraState.
XMLConverterState nsID extraState -> NameSpacePrefixes nsID
namespacePrefixes XMLConverterState nsID extraState
state

-- | Extracts namespace attributes from the current element and tries to
-- update the current mapping accordingly
readNSattributes         :: (NameSpaceID nsID)
                         => FallibleXMLConverter nsID extraState x ()
readNSattributes :: forall nsID extraState x.
NameSpaceID nsID =>
FallibleXMLConverter nsID extraState x ()
readNSattributes         = forall state b a. (state -> (state, b)) -> ArrowState state a b
fromState forall a b. (a -> b) -> a -> b
$ \XMLConverterState nsID extraState
state -> forall b a. b -> (a -> b) -> Maybe a -> b
maybe (XMLConverterState nsID extraState
state, forall failure _x. Monoid failure => Either failure _x
failEmpty     )
                                                       (     , forall a _x. a -> Either _x a
succeedWith ())
                                                       (forall nsID extraState.
NameSpaceID nsID =>
XMLConverterState nsID extraState
-> Maybe (XMLConverterState nsID extraState)
extractNSAttrs XMLConverterState nsID extraState
state )
  where
    extractNSAttrs       :: (NameSpaceID nsID)
                         => XMLConverterState nsID extraState
                         -> Maybe (XMLConverterState nsID extraState)
    extractNSAttrs :: forall nsID extraState.
NameSpaceID nsID =>
XMLConverterState nsID extraState
-> Maybe (XMLConverterState nsID extraState)
extractNSAttrs XMLConverterState nsID extraState
startState
                         = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (\Maybe (XMLConverterState nsID extraState)
state (Text, Text)
d -> Maybe (XMLConverterState nsID extraState)
state forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall {nsID} {extraState}.
NameSpaceID nsID =>
(Text, Text)
-> XMLConverterState nsID extraState
-> Maybe (XMLConverterState nsID extraState)
addNS (Text, Text)
d)
                                 (forall a. a -> Maybe a
Just XMLConverterState nsID extraState
startState)
                                 [(Text, Text)]
nsAttribs
      where nsAttribs :: [(Text, Text)]
nsAttribs    = forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe Attr -> Maybe (Text, Text)
readNSattr (Element -> [Attr]
XML.elAttribs Element
element)
            element :: Element
element      = forall nsID extraState.
XMLConverterState nsID extraState -> Element
currentElement XMLConverterState nsID extraState
startState
            readNSattr :: Attr -> Maybe (Text, Text)
readNSattr (XML.Attr (XML.QName Text
name Maybe Text
_ (Just Text
"xmlns")) Text
iri)
                         = forall a. a -> Maybe a
Just (Text
name, Text
iri)
            readNSattr Attr
_ = forall a. Maybe a
Nothing
    addNS :: (Text, Text)
-> XMLConverterState nsID extraState
-> Maybe (XMLConverterState nsID extraState)
addNS  (Text
prefix, Text
iri) XMLConverterState nsID extraState
state = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (NameSpaceIRIs nsID, nsID) -> XMLConverterState nsID extraState
updateState
                                 forall a b. (a -> b) -> a -> b
$ forall nsID.
NameSpaceID nsID =>
Text -> NameSpaceIRIs nsID -> Maybe (NameSpaceIRIs nsID, nsID)
getNamespaceID Text
iri
                                 forall a b. (a -> b) -> a -> b
$ forall nsID extraState.
XMLConverterState nsID extraState -> NameSpacePrefixes nsID
namespaceIRIs XMLConverterState nsID extraState
state
      where updateState :: (NameSpaceIRIs nsID, nsID) -> XMLConverterState nsID extraState
updateState (NameSpaceIRIs nsID
iris,nsID
nsID)
                         = XMLConverterState nsID extraState
state { namespaceIRIs :: NameSpaceIRIs nsID
namespaceIRIs     = NameSpaceIRIs nsID
iris
                                 , namespacePrefixes :: NameSpaceIRIs nsID
namespacePrefixes = forall k a. Ord k => k -> a -> Map k a -> Map k a
M.insert nsID
nsID Text
prefix
                                                       forall a b. (a -> b) -> a -> b
$ forall nsID extraState.
XMLConverterState nsID extraState -> NameSpacePrefixes nsID
namespacePrefixes XMLConverterState nsID extraState
state
                                 }

--------------------------------------------------------------------------------
-- Common namespace accessors
--------------------------------------------------------------------------------

-- | Given a namespace id and an element name, creates a 'XML.QName' for
-- internal use
qualifyName              :: (NameSpaceID nsID)
                         => nsID -> ElementName
                         -> XMLConverter nsID extraState x XML.QName
qualifyName :: forall nsID extraState x.
NameSpaceID nsID =>
nsID -> Text -> XMLConverter nsID extraState x QName
qualifyName nsID
nsID Text
name    =         forall nsID extraState x.
NameSpaceID nsID =>
nsID -> XMLConverter nsID extraState x (Maybe Text)
lookupNSiri nsID
nsID
                               forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> a b c' -> a b (c, c')
&&& forall nsID extraState x.
NameSpaceID nsID =>
nsID -> XMLConverter nsID extraState x (Maybe Text)
lookupNSprefix nsID
nsID
                           forall (a :: * -> * -> *) x b c d.
Arrow a =>
a x (b, c) -> (b -> c -> d) -> a x d
>>% Text -> Maybe Text -> Maybe Text -> QName
XML.QName Text
name

-- | Checks if a given element matches both a specified namespace id
-- and a predicate
elemNameMatches          :: (NameSpaceID nsID)
                         => nsID -> (ElementName -> Bool)
                         -> XMLConverter nsID extraState XML.Element Bool
elemNameMatches :: forall nsID extraState.
NameSpaceID nsID =>
nsID -> (Text -> Bool) -> XMLConverter nsID extraState Element Bool
elemNameMatches nsID
nsID Text -> Bool
f    = forall (a :: * -> * -> *) b c. Arrow a => a b c -> a b (b, c)
keepingTheValue (forall nsID extraState x.
NameSpaceID nsID =>
nsID -> XMLConverter nsID extraState x (Maybe Text)
lookupNSiri nsID
nsID) forall (a :: * -> * -> *) x b c d.
Arrow a =>
a x (b, c) -> (b -> c -> d) -> a x d
>>% Element -> Maybe Text -> Bool
hasMatchingName
  where hasMatchingName :: Element -> Maybe Text -> Bool
hasMatchingName Element
e Maybe Text
iri = let name :: QName
name = Element -> QName
XML.elName Element
e
                                in     Text -> Bool
f (QName -> Text
XML.qName QName
name)
                                    Bool -> Bool -> Bool
&& QName -> Maybe Text
XML.qURI QName
name forall a. Eq a => a -> a -> Bool
== Maybe Text
iri

-- | Checks if a given element matches both a specified namespace id
-- and a specified element name
elemNameIs               :: (NameSpaceID nsID)
                         => nsID -> ElementName
                         -> XMLConverter nsID extraState XML.Element Bool
elemNameIs :: forall nsID extraState.
NameSpaceID nsID =>
nsID -> Text -> XMLConverter nsID extraState Element Bool
elemNameIs nsID
nsID Text
name     = forall nsID extraState.
NameSpaceID nsID =>
nsID -> (Text -> Bool) -> XMLConverter nsID extraState Element Bool
elemNameMatches nsID
nsID (forall a. Eq a => a -> a -> Bool
== Text
name)

--------------------------------------------------------------------------------
-- General content
--------------------------------------------------------------------------------

elName :: XML.Element -> ElementName
elName :: Element -> Text
elName = QName -> Text
XML.qName forall b c a. (b -> c) -> (a -> b) -> a -> c
. Element -> QName
XML.elName

--
elContent               :: XMLConverter nsID extraState x [XML.Content]
elContent :: forall nsID extraState x. XMLConverter nsID extraState x [Content]
elContent               =     forall nsID extraState x. XMLConverter nsID extraState x Element
getCurrentElement
                           forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> (c -> d) -> a b d
>>^ Element -> [Content]
XML.elContent

--------------------------------------------------------------------------------
-- Children
--------------------------------------------------------------------------------

--
--
findChildren             :: (NameSpaceID nsID)
                         => nsID -> ElementName
                         -> XMLConverter nsID extraState x [XML.Element]
findChildren :: forall nsID extraState x.
NameSpaceID nsID =>
nsID -> Text -> XMLConverter nsID extraState x [Element]
findChildren nsID
nsID Text
name   =         forall nsID extraState x.
NameSpaceID nsID =>
nsID -> Text -> XMLConverter nsID extraState x QName
qualifyName nsID
nsID Text
name
                               forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> a b c' -> a b (c, c')
&&& forall nsID extraState x. XMLConverter nsID extraState x Element
getCurrentElement
                           forall (a :: * -> * -> *) x b c d.
Arrow a =>
a x (b, c) -> (b -> c -> d) -> a x d
>>% QName -> Element -> [Element]
XML.findChildren

--
findChild'              :: (NameSpaceID nsID)
                        => nsID
                        -> ElementName
                        -> XMLConverter nsID extraState x (Maybe XML.Element)
findChild' :: forall nsID extraState x.
NameSpaceID nsID =>
nsID -> Text -> XMLConverter nsID extraState x (Maybe Element)
findChild' nsID
nsID Text
name    =         forall nsID extraState x.
NameSpaceID nsID =>
nsID -> Text -> XMLConverter nsID extraState x QName
qualifyName nsID
nsID Text
name
                              forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> a b c' -> a b (c, c')
&&& forall nsID extraState x. XMLConverter nsID extraState x Element
getCurrentElement
                          forall (a :: * -> * -> *) x b c d.
Arrow a =>
a x (b, c) -> (b -> c -> d) -> a x d
>>% QName -> Element -> Maybe Element
XML.findChild

--
findChild              :: (NameSpaceID nsID)
                       => nsID -> ElementName
                       -> FallibleXMLConverter nsID extraState x XML.Element
findChild :: forall nsID extraState x.
NameSpaceID nsID =>
nsID -> Text -> FallibleXMLConverter nsID extraState x Element
findChild nsID
nsID Text
name    =     forall nsID extraState x.
NameSpaceID nsID =>
nsID -> Text -> XMLConverter nsID extraState x (Maybe Element)
findChild' nsID
nsID Text
name
                         forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall (a :: * -> * -> *) b.
ArrowChoice a =>
a (Maybe b) (Fallible b)
maybeToChoice

filterChildrenName'        :: (NameSpaceID nsID)
                           => nsID
                           -> (ElementName -> Bool)
                           -> XMLConverter nsID extraState x [XML.Element]
filterChildrenName' :: forall nsID extraState x.
NameSpaceID nsID =>
nsID -> (Text -> Bool) -> XMLConverter nsID extraState x [Element]
filterChildrenName' nsID
nsID Text -> Bool
f =     forall nsID extraState x. XMLConverter nsID extraState x Element
getCurrentElement
                             forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr Element -> [Element]
XML.elChildren
                             forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall (f :: * -> *) (m :: * -> *) s x y.
(Foldable f, MonadPlus m) =>
ArrowState s x y -> ArrowState s (f x) (m y)
iterateS (forall (a :: * -> * -> *) b c. Arrow a => a b c -> a b (b, c)
keepingTheValue (forall nsID extraState.
NameSpaceID nsID =>
nsID -> (Text -> Bool) -> XMLConverter nsID extraState Element Bool
elemNameMatches nsID
nsID Text -> Bool
f))
                             forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr (forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> a
fst forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (a -> Bool) -> [a] -> [a]
filter forall a b. (a, b) -> b
snd)

--------------------------------------------------------------------------------
-- Attributes
--------------------------------------------------------------------------------

--
isSet'                   :: (NameSpaceID nsID)
                         => nsID -> AttributeName
                         -> XMLConverter nsID extraState x (Maybe Bool)
isSet' :: forall nsID extraState x.
NameSpaceID nsID =>
nsID -> Text -> XMLConverter nsID extraState x (Maybe Bool)
isSet' nsID
nsID Text
attrName     =     forall nsID extraState x.
NameSpaceID nsID =>
nsID -> Text -> XMLConverter nsID extraState x (Maybe Text)
findAttr' nsID
nsID Text
attrName
                           forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> (c -> d) -> a b d
>>^ (forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Text -> Maybe Bool
stringToBool')

isSetWithDefault         :: (NameSpaceID nsID)
                         => nsID -> AttributeName
                         -> Bool
                         -> XMLConverter nsID extraState x Bool
isSetWithDefault :: forall nsID extraState x.
NameSpaceID nsID =>
nsID -> Text -> Bool -> XMLConverter nsID extraState x Bool
isSetWithDefault nsID
nsID Text
attrName Bool
def'
                         =     forall nsID extraState x.
NameSpaceID nsID =>
nsID -> Text -> XMLConverter nsID extraState x (Maybe Bool)
isSet' nsID
nsID Text
attrName
                           forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> (c -> d) -> a b d
>>^ forall a. a -> Maybe a -> a
fromMaybe Bool
def'

-- | Lookup value in a dictionary, fail if no attribute found or value
-- not in dictionary
searchAttrIn             :: (NameSpaceID nsID)
                         => nsID -> AttributeName
                         -> [(AttributeValue,a)]
                         -> FallibleXMLConverter nsID extraState x a
searchAttrIn :: forall nsID a extraState x.
NameSpaceID nsID =>
nsID
-> Text -> [(Text, a)] -> FallibleXMLConverter nsID extraState x a
searchAttrIn nsID
nsID Text
attrName [(Text, a)]
dict
                         =       forall nsID extraState x.
NameSpaceID nsID =>
nsID -> Text -> FallibleXMLConverter nsID extraState x Text
findAttr nsID
nsID Text
attrName
                           forall (a :: * -> * -> *) x failure success success'.
ArrowChoice a =>
FallibleArrow a x failure success
-> (success -> Either failure success')
-> FallibleArrow a x failure success'
>>?^? forall (a :: * -> * -> *) b.
ArrowChoice a =>
a (Maybe b) (Fallible b)
maybeToChoiceforall b c a. (b -> c) -> (a -> b) -> a -> c
.(forall a b. Eq a => a -> [(a, b)] -> Maybe b
`lookup` [(Text, a)]
dict )

-- | Lookup value in a dictionary. If attribute or value not found,
-- return default value
searchAttr               :: (NameSpaceID nsID)
                         => nsID -> AttributeName
                         -> a
                         -> [(AttributeValue,a)]
                         -> XMLConverter nsID extraState x a
searchAttr :: forall nsID a extraState x.
NameSpaceID nsID =>
nsID
-> Text -> a -> [(Text, a)] -> XMLConverter nsID extraState x a
searchAttr nsID
nsID Text
attrName a
defV [(Text, a)]
dict
                         =     forall nsID a extraState x.
NameSpaceID nsID =>
nsID
-> Text -> [(Text, a)] -> FallibleXMLConverter nsID extraState x a
searchAttrIn nsID
nsID Text
attrName [(Text, a)]
dict
                           forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall a b. a -> b -> a
const a
defV forall (a :: * -> * -> *) b d c.
ArrowChoice a =>
(b -> d) -> (c -> d) -> a (Either b c) d
^|||^ forall a. a -> a
id

-- | Read a 'Lookupable' attribute. Fail if no match.
lookupAttr               :: (NameSpaceID nsID, Lookupable a)
                         => nsID -> AttributeName
                         -> FallibleXMLConverter nsID extraState x a
lookupAttr :: forall nsID a extraState x.
(NameSpaceID nsID, Lookupable a) =>
nsID -> Text -> FallibleXMLConverter nsID extraState x a
lookupAttr nsID
nsID Text
attrName =     forall nsID a extraState x.
(NameSpaceID nsID, Lookupable a) =>
nsID -> Text -> XMLConverter nsID extraState x (Maybe a)
lookupAttr' nsID
nsID Text
attrName
                           forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> (c -> d) -> a b d
>>^ forall (a :: * -> * -> *) b.
ArrowChoice a =>
a (Maybe b) (Fallible b)
maybeToChoice


-- | Read a 'Lookupable' attribute. Return the result as a 'Maybe'.
lookupAttr'              :: (NameSpaceID nsID, Lookupable a)
                         => nsID -> AttributeName
                         -> XMLConverter nsID extraState x (Maybe a)
lookupAttr' :: forall nsID a extraState x.
(NameSpaceID nsID, Lookupable a) =>
nsID -> Text -> XMLConverter nsID extraState x (Maybe a)
lookupAttr' nsID
nsID Text
attrName
                         =     forall nsID extraState x.
NameSpaceID nsID =>
nsID -> Text -> XMLConverter nsID extraState x (Maybe Text)
findAttr' nsID
nsID Text
attrName
                           forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> (c -> d) -> a b d
>>^ (forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall a. Lookupable a => Text -> Maybe a
readLookupable)

-- | Read a 'Lookupable' attribute with explicit default
lookupAttrWithDefault    :: (NameSpaceID nsID, Lookupable a)
                         => nsID -> AttributeName
                         -> a
                         -> XMLConverter nsID extraState x a
lookupAttrWithDefault :: forall nsID a extraState x.
(NameSpaceID nsID, Lookupable a) =>
nsID -> Text -> a -> XMLConverter nsID extraState x a
lookupAttrWithDefault nsID
nsID Text
attrName a
deflt
                         =     forall nsID a extraState x.
(NameSpaceID nsID, Lookupable a) =>
nsID -> Text -> XMLConverter nsID extraState x (Maybe a)
lookupAttr' nsID
nsID Text
attrName
                           forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> (c -> d) -> a b d
>>^ forall a. a -> Maybe a -> a
fromMaybe a
deflt

-- | Read a 'Lookupable' attribute with implicit default
lookupDefaultingAttr     :: (NameSpaceID nsID, Lookupable a, Default a)
                         => nsID -> AttributeName
                         -> XMLConverter nsID extraState x a
lookupDefaultingAttr :: forall nsID a extraState x.
(NameSpaceID nsID, Lookupable a, Default a) =>
nsID -> Text -> XMLConverter nsID extraState x a
lookupDefaultingAttr nsID
nsID Text
attrName
                         = forall nsID a extraState x.
(NameSpaceID nsID, Lookupable a) =>
nsID -> Text -> a -> XMLConverter nsID extraState x a
lookupAttrWithDefault nsID
nsID Text
attrName forall a. Default a => a
def

-- | Return value as a (Maybe Text)
findAttr'               :: (NameSpaceID nsID)
                        => nsID -> AttributeName
                        -> XMLConverter nsID extraState x (Maybe AttributeValue)
findAttr' :: forall nsID extraState x.
NameSpaceID nsID =>
nsID -> Text -> XMLConverter nsID extraState x (Maybe Text)
findAttr' nsID
nsID Text
attrName =         forall nsID extraState x.
NameSpaceID nsID =>
nsID -> Text -> XMLConverter nsID extraState x QName
qualifyName nsID
nsID Text
attrName
                              forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> a b c' -> a b (c, c')
&&& forall nsID extraState x. XMLConverter nsID extraState x Element
getCurrentElement
                          forall (a :: * -> * -> *) x b c d.
Arrow a =>
a x (b, c) -> (b -> c -> d) -> a x d
>>% QName -> Element -> Maybe Text
XML.findAttr

-- | Return value as a (Maybe Text)
findAttrText'           :: (NameSpaceID nsID)
                        => nsID -> AttributeName
                        -> XMLConverter nsID extraState x (Maybe TextAttributeValue)
findAttrText' :: forall nsID extraState x.
NameSpaceID nsID =>
nsID -> Text -> XMLConverter nsID extraState x (Maybe Text)
findAttrText' nsID
nsID Text
attrName
                        =         forall nsID extraState x.
NameSpaceID nsID =>
nsID -> Text -> XMLConverter nsID extraState x QName
qualifyName nsID
nsID Text
attrName
                              forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> a b c' -> a b (c, c')
&&& forall nsID extraState x. XMLConverter nsID extraState x Element
getCurrentElement
                          forall (a :: * -> * -> *) x b c d.
Arrow a =>
a x (b, c) -> (b -> c -> d) -> a x d
>>% QName -> Element -> Maybe Text
XML.findAttr

-- | Return value as string or fail
findAttr               :: (NameSpaceID nsID)
                       => nsID -> AttributeName
                       -> FallibleXMLConverter nsID extraState x AttributeValue
findAttr :: forall nsID extraState x.
NameSpaceID nsID =>
nsID -> Text -> FallibleXMLConverter nsID extraState x Text
findAttr nsID
nsID Text
attrName =     forall nsID extraState x.
NameSpaceID nsID =>
nsID -> Text -> XMLConverter nsID extraState x (Maybe Text)
findAttr' nsID
nsID Text
attrName
                         forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall (a :: * -> * -> *) b.
ArrowChoice a =>
a (Maybe b) (Fallible b)
maybeToChoice

-- | Return value as text or fail
findAttrText           :: (NameSpaceID nsID)
                       => nsID -> AttributeName
                       -> FallibleXMLConverter nsID extraState x TextAttributeValue
findAttrText :: forall nsID extraState x.
NameSpaceID nsID =>
nsID -> Text -> FallibleXMLConverter nsID extraState x Text
findAttrText nsID
nsID Text
attrName
                       = forall nsID extraState x.
NameSpaceID nsID =>
nsID -> Text -> XMLConverter nsID extraState x (Maybe Text)
findAttr' nsID
nsID Text
attrName
                         forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall (a :: * -> * -> *) b.
ArrowChoice a =>
a (Maybe b) (Fallible b)
maybeToChoice

-- | Return value as string or return provided default value
findAttrWithDefault    :: (NameSpaceID nsID)
                       => nsID -> AttributeName
                       -> AttributeValue
                       -> XMLConverter nsID extraState x AttributeValue
findAttrWithDefault :: forall nsID extraState x.
NameSpaceID nsID =>
nsID -> Text -> Text -> XMLConverter nsID extraState x Text
findAttrWithDefault nsID
nsID Text
attrName Text
deflt
                       = forall nsID extraState x.
NameSpaceID nsID =>
nsID -> Text -> XMLConverter nsID extraState x (Maybe Text)
findAttr' nsID
nsID Text
attrName
                         forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> (c -> d) -> a b d
>>^ forall a. a -> Maybe a -> a
fromMaybe Text
deflt

-- | Return value as string or return provided default value
findAttrTextWithDefault :: (NameSpaceID nsID)
                        => nsID -> AttributeName
                        -> TextAttributeValue
                        -> XMLConverter nsID extraState x TextAttributeValue
findAttrTextWithDefault :: forall nsID extraState x.
NameSpaceID nsID =>
nsID -> Text -> Text -> XMLConverter nsID extraState x Text
findAttrTextWithDefault nsID
nsID Text
attrName Text
deflt
                       = forall nsID extraState x.
NameSpaceID nsID =>
nsID -> Text -> XMLConverter nsID extraState x (Maybe Text)
findAttr' nsID
nsID Text
attrName
                         forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> (c -> d) -> a b d
>>^ forall a. a -> Maybe a -> a
fromMaybe Text
deflt

-- | Read and return value or fail
readAttr               :: (NameSpaceID nsID, Read attrValue)
                       => nsID -> AttributeName
                       -> FallibleXMLConverter nsID extraState x attrValue
readAttr :: forall nsID attrValue extraState x.
(NameSpaceID nsID, Read attrValue) =>
nsID -> Text -> FallibleXMLConverter nsID extraState x attrValue
readAttr nsID
nsID Text
attrName =     forall nsID attrValue extraState x.
(NameSpaceID nsID, Read attrValue) =>
nsID -> Text -> XMLConverter nsID extraState x (Maybe attrValue)
readAttr' nsID
nsID Text
attrName
                         forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall (a :: * -> * -> *) b.
ArrowChoice a =>
a (Maybe b) (Fallible b)
maybeToChoice

-- | Read and return value or return Nothing
readAttr'              :: (NameSpaceID nsID, Read attrValue)
                       => nsID -> AttributeName
                       -> XMLConverter nsID extraState x (Maybe attrValue)
readAttr' :: forall nsID attrValue extraState x.
(NameSpaceID nsID, Read attrValue) =>
nsID -> Text -> XMLConverter nsID extraState x (Maybe attrValue)
readAttr' nsID
nsID Text
attrName =     forall nsID extraState x.
NameSpaceID nsID =>
nsID -> Text -> XMLConverter nsID extraState x (Maybe Text)
findAttr' nsID
nsID Text
attrName
                          forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> (c -> d) -> a b d
>>^ (forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall r. Read r => Text -> Maybe r
tryToRead)

-- | Read and return value or return provided default value
readAttrWithDefault    :: (NameSpaceID nsID, Read attrValue)
                       => nsID -> AttributeName
                       -> attrValue
                       -> XMLConverter nsID extraState x attrValue
readAttrWithDefault :: forall nsID attrValue extraState x.
(NameSpaceID nsID, Read attrValue) =>
nsID
-> Text -> attrValue -> XMLConverter nsID extraState x attrValue
readAttrWithDefault nsID
nsID Text
attrName attrValue
deflt
                       =     forall nsID extraState x.
NameSpaceID nsID =>
nsID -> Text -> XMLConverter nsID extraState x (Maybe Text)
findAttr' nsID
nsID Text
attrName
                         forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> (c -> d) -> a b d
>>^ (forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall r. Read r => Text -> Maybe r
tryToRead)
                         forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> (c -> d) -> a b d
>>^ forall a. a -> Maybe a -> a
fromMaybe attrValue
deflt

-- | Read and return value or return default value from 'Default' instance
getAttr                :: (NameSpaceID nsID, Read attrValue, Default attrValue)
                       => nsID -> AttributeName
                       -> XMLConverter nsID extraState x attrValue
getAttr :: forall nsID attrValue extraState x.
(NameSpaceID nsID, Read attrValue, Default attrValue) =>
nsID -> Text -> XMLConverter nsID extraState x attrValue
getAttr nsID
nsID Text
attrName  = forall nsID attrValue extraState x.
(NameSpaceID nsID, Read attrValue) =>
nsID
-> Text -> attrValue -> XMLConverter nsID extraState x attrValue
readAttrWithDefault nsID
nsID Text
attrName forall a. Default a => a
def

--------------------------------------------------------------------------------
-- Movements
--------------------------------------------------------------------------------

--
jumpThere              :: XMLConverter nsID extraState XML.Element XML.Element
jumpThere :: forall nsID extraState.
XMLConverter nsID extraState Element Element
jumpThere              = forall state a b.
(state -> a -> (state, b)) -> ArrowState state a b
withState (\XMLConverterState nsID extraState
state Element
element
                                     -> ( forall nsID extraState.
Element
-> XMLConverterState nsID extraState
-> XMLConverterState nsID extraState
pushElement Element
element XMLConverterState nsID extraState
state , Element
element )
                                   )

--
swapStack             :: XMLConverter nsID extraState [XML.Element] [XML.Element]
swapStack :: forall nsID extraState.
XMLConverter nsID extraState [Element] [Element]
swapStack             = forall state a b.
(state -> a -> (state, b)) -> ArrowState state a b
withState forall nsID extraState.
XMLConverterState nsID extraState
-> [Element] -> (XMLConverterState nsID extraState, [Element])
swapStack'

--
jumpBack               :: FallibleXMLConverter nsID extraState _x _x
jumpBack :: forall nsID extraState _x.
FallibleXMLConverter nsID extraState _x _x
jumpBack               = forall state f a.
(state -> Either f state) -> ArrowState state a (Either f a)
tryModifyState (forall nsID extraState.
XMLConverterState nsID extraState
-> Maybe (XMLConverterState nsID extraState)
popElement forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall (a :: * -> * -> *) b.
ArrowChoice a =>
a (Maybe b) (Fallible b)
maybeToChoice)

-- | Support function for "procedural" converters: jump to an element, execute
-- a converter, jump back.
-- This version is safer than 'executeThere', because it does not rely on the
-- internal stack. As a result, the converter can not move around in arbitrary
-- ways. The downside is of course that some of the environment is not
-- accessible to the converter.
switchingTheStack      :: XMLConverter nsID moreState a b
                       -> XMLConverter nsID moreState (a, XML.Element) b
switchingTheStack :: forall nsID moreState a b.
XMLConverter nsID moreState a b
-> XMLConverter nsID moreState (a, Element) b
switchingTheStack XMLConverter nsID moreState a b
a    =     forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (d, b) (d, c)
second ( (forall a. a -> [a] -> [a]
:[]) forall (a :: * -> * -> *) b c d.
Arrow a =>
(b -> c) -> a c d -> a b d
^>> forall nsID extraState.
XMLConverter nsID extraState [Element] [Element]
swapStack )
                         forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (b, d) (c, d)
first  XMLConverter nsID moreState a b
a
                         forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (d, b) (d, c)
second forall nsID extraState.
XMLConverter nsID extraState [Element] [Element]
swapStack
                         forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> (c -> d) -> a b d
>>^ forall a b. (a, b) -> a
fst

-- | Support function for "procedural" converters: jumps to an element, executes
-- a converter, jumps back.
-- Make sure that the converter is well-behaved; that is it should
-- return to the exact position it started from in /every possible path/ of
-- execution, even if it "fails". If it does not, you may encounter
-- strange bugs. If you are not sure about the behaviour or want to use
-- shortcuts, you can often use 'switchingTheStack' instead.
executeThere           :: FallibleXMLConverter nsID moreState a b
                       -> FallibleXMLConverter nsID moreState (a, XML.Element) b
executeThere :: forall nsID moreState a b.
FallibleXMLConverter nsID moreState a b
-> FallibleXMLConverter nsID moreState (a, Element) b
executeThere FallibleXMLConverter nsID moreState a b
a         =      forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (d, b) (d, c)
second forall nsID extraState.
XMLConverter nsID extraState Element Element
jumpThere
                          forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall a b. (a, b) -> a
fst
                          forall (a :: * -> * -> *) b c d.
Arrow a =>
(b -> c) -> a c d -> a b d
^>> FallibleXMLConverter nsID moreState a b
a
                          forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall nsID extraState _x.
FallibleXMLConverter nsID extraState _x _x
jumpBack -- >>? jumpBack  would not ensure the jump.
                          forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> (c -> d) -> a b d
>>^ forall failure x.
Either failure (Either failure x) -> Either failure x
collapseEither


-- | Do something in a specific element, then come back
executeIn   :: XMLConverter nsID extraState XML.Element s
            -> XMLConverter nsID extraState XML.Element s
executeIn :: forall nsID extraState s.
XMLConverter nsID extraState Element s
-> XMLConverter nsID extraState Element s
executeIn XMLConverter nsID extraState Element s
a = forall (a :: * -> * -> *) b. Arrow a => a b (b, b)
duplicate forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall nsID moreState a b.
XMLConverter nsID moreState a b
-> XMLConverter nsID moreState (a, Element) b
switchingTheStack XMLConverter nsID extraState Element s
a

-- | Do something in a sub-element, then come back
executeInSub              :: (NameSpaceID nsID)
                          => nsID -> ElementName
                          -> FallibleXMLConverter nsID extraState f s
                          -> FallibleXMLConverter nsID extraState f s
executeInSub :: forall nsID extraState f s.
NameSpaceID nsID =>
nsID
-> Text
-> FallibleXMLConverter nsID extraState f s
-> FallibleXMLConverter nsID extraState f s
executeInSub nsID
nsID Text
name FallibleXMLConverter nsID extraState f s
a  =     forall (a :: * -> * -> *) b c. Arrow a => a b c -> a b (b, c)
keepingTheValue
                                  (forall nsID extraState x.
NameSpaceID nsID =>
nsID -> Text -> FallibleXMLConverter nsID extraState x Element
findChild nsID
nsID Text
name)
                            forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall a b state. (a -> b) -> ArrowState state a b
ignoringState forall {a} {a} {b}. (a, Either a b) -> Either a (a, b)
liftFailure
                            forall (a :: * -> * -> *) x failure success success'.
ArrowChoice a =>
FallibleArrow a x failure success
-> FallibleArrow a success failure success'
-> FallibleArrow a x failure success'
>>? forall nsID moreState a b.
XMLConverter nsID moreState a b
-> XMLConverter nsID moreState (a, Element) b
switchingTheStack FallibleXMLConverter nsID extraState f s
a
  where liftFailure :: (a, Either a b) -> Either a (a, b)
liftFailure (a
_, Left  a
f) = forall a b. a -> Either a b
Left  a
f
        liftFailure (a
x, Right b
e) = forall a b. b -> Either a b
Right (a
x, b
e)

--------------------------------------------------------------------------------
-- Iterating over children
--------------------------------------------------------------------------------

-- Helper converter to prepare different types of iterations.
-- It lifts the children (of a certain type) of the current element
-- into the value level and pairs each one with the current input value.
prepareIteration       :: (NameSpaceID nsID)
                       => nsID -> ElementName
                       -> XMLConverter nsID extraState b [(b, XML.Element)]
prepareIteration :: forall nsID extraState b.
NameSpaceID nsID =>
nsID -> Text -> XMLConverter nsID extraState b [(b, Element)]
prepareIteration nsID
nsID Text
name =     forall (a :: * -> * -> *) b c. Arrow a => a b c -> a b (b, c)
keepingTheValue
                                   (forall nsID extraState x.
NameSpaceID nsID =>
nsID -> Text -> XMLConverter nsID extraState x [Element]
findChildren nsID
nsID Text
name)
                             forall (a :: * -> * -> *) x b c d.
Arrow a =>
a x (b, c) -> (b -> c -> d) -> a x d
>>% forall a b. a -> [b] -> [(a, b)]
distributeValue

--
withEveryL             :: (NameSpaceID nsID)
                       => nsID -> ElementName
                       -> FallibleXMLConverter nsID extraState a  b
                       -> FallibleXMLConverter nsID extraState a [b]
withEveryL :: forall nsID extraState a b.
NameSpaceID nsID =>
nsID
-> Text
-> FallibleXMLConverter nsID extraState a b
-> FallibleXMLConverter nsID extraState a [b]
withEveryL = forall nsID (m :: * -> *) extraState a b.
(NameSpaceID nsID, MonadPlus m) =>
nsID
-> Text
-> FallibleXMLConverter nsID extraState a b
-> FallibleXMLConverter nsID extraState a (m b)
withEvery

-- | Applies a converter to every child element of a specific type.
-- Collects results in a 'MonadPlus'.
-- Fails completely if any conversion fails.
withEvery              :: (NameSpaceID nsID, MonadPlus m)
                       => nsID -> ElementName
                       -> FallibleXMLConverter nsID extraState a    b
                       -> FallibleXMLConverter nsID extraState a (m b)
withEvery :: forall nsID (m :: * -> *) extraState a b.
(NameSpaceID nsID, MonadPlus m) =>
nsID
-> Text
-> FallibleXMLConverter nsID extraState a b
-> FallibleXMLConverter nsID extraState a (m b)
withEvery nsID
nsID Text
name FallibleXMLConverter nsID extraState a b
a      =     forall nsID extraState b.
NameSpaceID nsID =>
nsID -> Text -> XMLConverter nsID extraState b [(b, Element)]
prepareIteration nsID
nsID Text
name
                             forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall (f :: * -> *) (m :: * -> *) s x e y.
(Foldable f, MonadPlus m) =>
ArrowState s x (Either e y) -> ArrowState s (f x) (Either e (m y))
iterateS' (forall nsID moreState a b.
XMLConverter nsID moreState a b
-> XMLConverter nsID moreState (a, Element) b
switchingTheStack FallibleXMLConverter nsID extraState a b
a)

-- | Applies a converter to every child element of a specific type.
-- Collects all successful results in a list.
tryAll                 :: (NameSpaceID nsID)
                       => nsID -> ElementName
                       -> FallibleXMLConverter nsID extraState b  a
                       ->         XMLConverter nsID extraState b [a]
tryAll :: forall nsID extraState b a.
NameSpaceID nsID =>
nsID
-> Text
-> FallibleXMLConverter nsID extraState b a
-> XMLConverter nsID extraState b [a]
tryAll nsID
nsID Text
name FallibleXMLConverter nsID extraState b a
a         =     forall nsID extraState b.
NameSpaceID nsID =>
nsID -> Text -> XMLConverter nsID extraState b [(b, Element)]
prepareIteration nsID
nsID Text
name
                             forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall (f :: * -> *) (m :: * -> *) s x y.
(Foldable f, MonadPlus m) =>
ArrowState s x y -> ArrowState s (f x) (m y)
iterateS (forall nsID moreState a b.
XMLConverter nsID moreState a b
-> XMLConverter nsID moreState (a, Element) b
switchingTheStack FallibleXMLConverter nsID extraState b a
a)
                             forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> (c -> d) -> a b d
>>^ forall a b. [Either a b] -> [b]
rights

--------------------------------------------------------------------------------
-- Matching children
--------------------------------------------------------------------------------

type IdXMLConverter nsID moreState x
   = XMLConverter   nsID moreState x x

type MaybeCConverter nsID moreState x
   = Maybe (IdXMLConverter nsID moreState (x, XML.Content))

-- Chainable converter that helps deciding which converter to actually use.
type ContentMatchConverter nsID extraState x
   = IdXMLConverter  nsID
                     extraState
                     (MaybeCConverter nsID extraState x, XML.Content)

-- Helper function: The @c@ is actually a converter that is to be selected by
-- matching XML content to the first two parameters.
-- The fold used to match elements however is very simple, so to use it,
-- this function wraps the converter in another converter that unifies
-- the accumulator. Think of a lot of converters with the resulting type
-- chained together. The accumulator not only transports the element
-- unchanged to the next matcher, it also does the actual selecting by
-- combining the intermediate results with '(<|>)'.
makeMatcherC           :: (NameSpaceID nsID)
                       => nsID -> ElementName
                       -> FallibleXMLConverter  nsID extraState a a
                       -> ContentMatchConverter nsID extraState a
makeMatcherC :: forall nsID extraState a.
NameSpaceID nsID =>
nsID
-> Text
-> FallibleXMLConverter nsID extraState a a
-> ContentMatchConverter nsID extraState a
makeMatcherC nsID
nsID Text
name FallibleXMLConverter nsID extraState a a
c = (    forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (d, b) (d, c)
second (    forall nsID extraState.
FallibleXMLConverter nsID extraState Content Element
contentToElem
                                         forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall (a :: * -> * -> *) c x. Arrow a => c -> a x c
returnV forall a. Maybe a
Nothing
                                         forall (a :: * -> * -> *) b d c.
ArrowChoice a =>
a b d -> a c d -> a (Either b c) d
||| (    forall nsID extraState.
NameSpaceID nsID =>
nsID -> Text -> XMLConverter nsID extraState Element Bool
elemNameIs nsID
nsID Text
name
                                              forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> (c -> d) -> a b d
>>^ forall a. a -> a -> Bool -> a
bool forall a. Maybe a
Nothing (forall a. a -> Maybe a
Just ArrowState
  (XMLConverterState nsID extraState) (a, Content) (a, Content)
cWithJump)
                                             )
                                        )
                             forall (a :: * -> * -> *) x b c d.
Arrow a =>
a x (b, c) -> (b -> c -> d) -> a x d
>>% forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
(<|>)
                           ) forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> (b -> c') -> a b (c, c')
&&&^ forall a b. (a, b) -> b
snd
  where cWithJump :: ArrowState
  (XMLConverterState nsID extraState) (a, Content) (a, Content)
cWithJump =      ( forall a b. (a, b) -> a
fst
                           forall (a :: * -> * -> *) b c c'.
Arrow a =>
(b -> c) -> a b c' -> a b (c, c')
^&&& (      forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (d, b) (d, c)
second forall nsID extraState.
FallibleXMLConverter nsID extraState Content Element
contentToElem
                                  forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>>  forall (v :: * -> *) f a.
ChoiceVector v =>
v (Either f a) -> Either f (v a)
spreadChoice
                                  forall (a :: * -> * -> *) x failure success success'.
ArrowChoice a =>
(x -> Either failure success)
-> FallibleArrow a success failure success'
-> FallibleArrow a x failure success'
^>>? forall nsID moreState a b.
FallibleXMLConverter nsID moreState a b
-> FallibleXMLConverter nsID moreState (a, Element) b
executeThere FallibleXMLConverter nsID extraState a a
c
                                )
                            forall (a :: * -> * -> *) x b c d.
Arrow a =>
a x (b, c) -> (b -> c -> d) -> a x d
>>% forall a _f. a -> Either _f a -> a
recover)
                    forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> (b -> c') -> a b (c, c')
&&&^ forall a b. (a, b) -> b
snd
        contentToElem :: FallibleXMLConverter nsID extraState XML.Content XML.Element
        contentToElem :: forall nsID extraState.
FallibleXMLConverter nsID extraState Content Element
contentToElem = forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr forall a b. (a -> b) -> a -> b
$ \case
                                     XML.Elem Element
e' -> forall a _x. a -> Either _x a
succeedWith Element
e'
                                     Content
_           -> forall failure _x. Monoid failure => Either failure _x
failEmpty

-- Creates and chains a bunch of matchers
prepareMatchersC      :: (NameSpaceID nsID)
                       => [(nsID, ElementName, FallibleXMLConverter nsID extraState x x)]
                       -> ContentMatchConverter nsID extraState x
--prepareMatchersC      = foldSs . (map $ uncurry3  makeMatcherC)
prepareMatchersC :: forall nsID extraState x.
NameSpaceID nsID =>
[(nsID, Text, FallibleXMLConverter nsID extraState x x)]
-> ContentMatchConverter nsID extraState x
prepareMatchersC      = forall (cat :: * -> * -> *) (f :: * -> *) a.
(Category cat, Foldable f) =>
f (cat a a) -> cat a a
reverseComposition forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map (forall a b c z. (a -> b -> c -> z) -> (a, b, c) -> z
uncurry3  forall nsID extraState a.
NameSpaceID nsID =>
nsID
-> Text
-> FallibleXMLConverter nsID extraState a a
-> ContentMatchConverter nsID extraState a
makeMatcherC)

-- | Takes a list of element-data - converter groups and
-- * Finds all content of the current element
-- * Matches each group to each piece of content in order
--   (at most one group per piece of content)
-- * Filters non-matched content
-- * Chains all found converters in content-order
-- * Applies the chain to the input element
matchContent'           :: (NameSpaceID nsID)
                       => [(nsID, ElementName, FallibleXMLConverter nsID extraState a a)]
                       -> XMLConverter nsID extraState a a
matchContent' :: forall nsID extraState a.
NameSpaceID nsID =>
[(nsID, Text, FallibleXMLConverter nsID extraState a a)]
-> XMLConverter nsID extraState a a
matchContent' [(nsID, Text, FallibleXMLConverter nsID extraState a a)]
lookups   = forall nsID extraState a.
NameSpaceID nsID =>
[(nsID, Text, FallibleXMLConverter nsID extraState a a)]
-> XMLConverter nsID extraState (a, Content) a
-> XMLConverter nsID extraState a a
matchContent [(nsID, Text, FallibleXMLConverter nsID extraState a a)]
lookups (forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr forall a b. (a, b) -> a
fst)

-- | Takes a list of element-data - converter groups and
-- * Finds all content of the current element
-- * Matches each group to each piece of content in order
--   (at most one group per piece of content)
-- * Adds a default converter for all non-matched content
-- * Chains all found converters in content-order
-- * Applies the chain to the input element
matchContent          :: (NameSpaceID nsID)
                       => [(nsID, ElementName, FallibleXMLConverter nsID extraState a a)]
                       -> XMLConverter nsID extraState (a,XML.Content) a
                       -> XMLConverter nsID extraState a a
matchContent :: forall nsID extraState a.
NameSpaceID nsID =>
[(nsID, Text, FallibleXMLConverter nsID extraState a a)]
-> XMLConverter nsID extraState (a, Content) a
-> XMLConverter nsID extraState a a
matchContent [(nsID, Text, FallibleXMLConverter nsID extraState a a)]
lookups XMLConverter nsID extraState (a, Content) a
fallback
                        = let matcher :: ContentMatchConverter nsID extraState a
matcher = forall nsID extraState x.
NameSpaceID nsID =>
[(nsID, Text, FallibleXMLConverter nsID extraState x x)]
-> ContentMatchConverter nsID extraState x
prepareMatchersC [(nsID, Text, FallibleXMLConverter nsID extraState a a)]
lookups
                          in  forall (a :: * -> * -> *) b c. Arrow a => a b c -> a b (b, c)
keepingTheValue (
                                   forall nsID extraState x. XMLConverter nsID extraState x [Content]
elContent
                               forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall a b. (a -> b) -> [a] -> [b]
map (forall a. Maybe a
Nothing,)
                               forall (a :: * -> * -> *) b c d.
Arrow a =>
(b -> c) -> a c d -> a b d
^>> forall (f :: * -> *) (m :: * -> *) s x y.
(Foldable f, MonadPlus m) =>
ArrowState s x y -> ArrowState s (f x) (m y)
iterateSL ContentMatchConverter nsID extraState a
matcher
                               forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> (c -> d) -> a b d
>>^ forall a b. (a -> b) -> [a] -> [b]
map (Maybe (IdXMLConverter nsID extraState (a, Content)), Content)
-> ArrowState (XMLConverterState nsID extraState) a a
swallowOrFallback
                              -- >>> foldSs
                               forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall (cat :: * -> * -> *) (f :: * -> *) a.
(Category cat, Foldable f) =>
f (cat a a) -> cat a a
reverseComposition
                             )
                         forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> forall a b. (a, b) -> (b, a)
swap
                         forall (a :: * -> * -> *) b c d.
Arrow a =>
(b -> c) -> a c d -> a b d
^>> forall (a :: * -> * -> *) b c. ArrowApply a => a (a b c, b) c
app
  where
        -- let the converter swallow the content and drop the content
        -- in the return value
        swallowOrFallback :: (Maybe (IdXMLConverter nsID extraState (a, Content)), Content)
-> ArrowState (XMLConverterState nsID extraState) a a
swallowOrFallback (Just IdXMLConverter nsID extraState (a, Content)
converter,Content
content) = (,Content
content) forall (a :: * -> * -> *) b c d.
Arrow a =>
(b -> c) -> a c d -> a b d
^>> IdXMLConverter nsID extraState (a, Content)
converter forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> (c -> d) -> a b d
>>^ forall a b. (a, b) -> a
fst
        swallowOrFallback (Maybe (IdXMLConverter nsID extraState (a, Content))
Nothing       ,Content
content) = (,Content
content) forall (a :: * -> * -> *) b c d.
Arrow a =>
(b -> c) -> a c d -> a b d
^>> XMLConverter nsID extraState (a, Content) a
fallback

--------------------------------------------------------------------------------
-- Internals
--------------------------------------------------------------------------------

stringToBool' :: Text -> Maybe Bool
stringToBool' :: Text -> Maybe Bool
stringToBool' Text
val | Text
val forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Text]
trueValues  = forall a. a -> Maybe a
Just Bool
True
                  | Text
val forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Text]
falseValues = forall a. a -> Maybe a
Just Bool
False
                  | Bool
otherwise              = forall a. Maybe a
Nothing
  where trueValues :: [Text]
trueValues  = [Text
"true" ,Text
"on" ,Text
"1"]
        falseValues :: [Text]
falseValues = [Text
"false",Text
"off",Text
"0"]


distributeValue ::  a -> [b] -> [(a,b)]
distributeValue :: forall a b. a -> [b] -> [(a, b)]
distributeValue = forall a b. (a -> b) -> [a] -> [b]
mapforall b c a. (b -> c) -> (a -> b) -> a -> c
.(,)

--------------------------------------------------------------------------------

{-
NOTES
It might be a good idea to refactor the namespace stuff.
E.g.: if a namespace constructor took a string as a parameter, things like
> a ?>/< (NsText,"body")
would be nicer.
Together with a rename and some trickery, something like
> |< NsText "body" >< NsText "p" ?> a </> </>|
might even be possible.

Some day, XML.Light should be replaced by something better.
While doing that, it might be useful to replace String as the type of element
names with something else, too. (Of course with OverloadedStrings).
While doing that, maybe the types can be created in a way that something like
> NsText:"body"
could be used. Overloading (:) does not sounds like the best idea, but if the
element name type was a list, this might be possible.
Of course that would be a bit hackish, so the "right" way would probably be
something like
> InNS NsText "body"
but isn't that a bit boring? ;)
-}