{-# 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           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 qualified Data.Text            as T
import           Data.Default
import           Data.Maybe

import qualified Text.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           = String
type AttributeName         = String
type AttributeValue        = String
type TextAttributeValue    = T.Text

--
type NameSpacePrefix       = String

--
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.
         XMLConverterState nsID extraState -> [Element]
parentElements    :: [XML.Element]
         -- | A map from internal namespace IDs to the namespace prefixes
         -- used in XML elements
       , 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)
       , XMLConverterState nsID extraState -> NameSpaceIRIs 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.
       , XMLConverterState nsID extraState -> extraState
moreState         :: extraState
       }
    -> XMLConverterState nsID extraState

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

-- | Functor over extra state
instance Functor (XMLConverterState nsID) where
  fmap :: (a -> b) -> XMLConverterState nsID a -> XMLConverterState nsID b
fmap a -> b
f ( XMLConverterState [Element]
parents NameSpacePrefixes nsID
prefixes NameSpacePrefixes nsID
iRIs    a
extraState  )
       =   [Element]
-> NameSpacePrefixes nsID
-> NameSpacePrefixes nsID
-> b
-> XMLConverterState nsID b
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 :: extraState
-> XMLConverterState nsID _x -> XMLConverterState nsID extraState
replaceExtraState extraState
x XMLConverterState nsID _x
s
                     = (_x -> extraState)
-> XMLConverterState nsID _x -> XMLConverterState nsID extraState
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (extraState -> _x -> extraState
forall a b. a -> b -> a
const extraState
x) XMLConverterState nsID _x
s

--
currentElement      :: XMLConverterState nsID extraState
                    -> XML.Element
currentElement :: XMLConverterState nsID extraState -> Element
currentElement XMLConverterState nsID extraState
state = [Element] -> Element
forall a. [a] -> a
head (XMLConverterState nsID extraState -> [Element]
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' :: XMLConverterState nsID extraState
-> [Element] -> (XMLConverterState nsID extraState, [Element])
swapStack' XMLConverterState nsID extraState
state [Element]
stack
                     = ( XMLConverterState nsID extraState
state { parentElements :: [Element]
parentElements = [Element]
stack }
                       , XMLConverterState nsID extraState -> [Element]
forall nsID extraState.
XMLConverterState nsID extraState -> [Element]
parentElements XMLConverterState nsID extraState
state
                       )

--
pushElement         :: XML.Element
                    -> XMLConverterState nsID extraState
                    -> XMLConverterState nsID extraState
pushElement :: Element
-> XMLConverterState nsID extraState
-> XMLConverterState nsID extraState
pushElement Element
e XMLConverterState nsID extraState
state  = XMLConverterState nsID extraState
state { parentElements :: [Element]
parentElements = Element
eElement -> [Element] -> [Element]
forall a. a -> [a] -> [a]
:XMLConverterState nsID extraState -> [Element]
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 :: XMLConverterState nsID extraState
-> Maybe (XMLConverterState nsID extraState)
popElement XMLConverterState nsID extraState
state
  | Element
_:es :: [Element]
es@(Element
_:[Element]
_) <- XMLConverterState nsID extraState -> [Element]
forall nsID extraState.
XMLConverterState nsID extraState -> [Element]
parentElements XMLConverterState nsID extraState
state = XMLConverterState nsID extraState
-> Maybe (XMLConverterState nsID extraState)
forall a. a -> Maybe a
Just (XMLConverterState nsID extraState
 -> Maybe (XMLConverterState nsID extraState))
-> XMLConverterState nsID extraState
-> Maybe (XMLConverterState nsID extraState)
forall a b. (a -> b) -> a -> b
$ XMLConverterState nsID extraState
state { parentElements :: [Element]
parentElements = [Element]
es }
  | Bool
otherwise                          = Maybe (XMLConverterState nsID extraState)
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 :: XMLConverter nsID extraState input output
-> XMLConverterState nsID extraState -> input -> output
runConverter XMLConverter nsID extraState input output
converter XMLConverterState nsID extraState
state input
input = (XMLConverterState nsID extraState, output) -> output
forall a b. (a, b) -> b
snd ((XMLConverterState nsID extraState, output) -> output)
-> (XMLConverterState nsID extraState, output) -> output
forall a b. (a -> b) -> a -> b
$ XMLConverter nsID extraState input output
-> (XMLConverterState nsID extraState, input)
-> (XMLConverterState nsID extraState, output)
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' :: FallibleXMLConverter nsID extraState () success
-> extraState -> Element -> Fallible success
runConverter' FallibleXMLConverter nsID extraState () success
converter extraState
extraState Element
element = FallibleXMLConverter nsID extraState () success
-> XMLConverterState nsID extraState -> () -> Fallible success
forall nsID extraState input output.
XMLConverter nsID extraState input output
-> XMLConverterState nsID extraState -> input -> output
runConverter (FallibleXMLConverter nsID extraState () ()
forall nsID extraState x.
NameSpaceID nsID =>
FallibleXMLConverter nsID extraState x ()
readNSattributes FallibleXMLConverter nsID extraState () ()
-> FallibleXMLConverter nsID extraState () success
-> FallibleXMLConverter nsID extraState () success
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) (Element -> extraState -> XMLConverterState nsID extraState
forall nsID extraState.
NameSpaceID nsID =>
Element -> extraState -> XMLConverterState nsID extraState
createStartState Element
element extraState
extraState) ()

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

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

--
setExtraState     :: XMLConverter nsID extraState extraState extraState
setExtraState :: XMLConverter nsID extraState extraState extraState
setExtraState      = (XMLConverterState nsID extraState
 -> extraState -> (XMLConverterState nsID extraState, extraState))
-> XMLConverter nsID extraState extraState extraState
forall state a b.
(state -> a -> (state, b)) -> ArrowState state a b
withState ((XMLConverterState nsID extraState
  -> extraState -> (XMLConverterState nsID extraState, extraState))
 -> XMLConverter nsID extraState extraState extraState)
-> (XMLConverterState nsID extraState
    -> extraState -> (XMLConverterState nsID extraState, extraState))
-> XMLConverter nsID extraState extraState extraState
forall a b. (a -> b) -> a -> b
$ \XMLConverterState nsID extraState
state extraState
extra
                                  -> (extraState
-> XMLConverterState nsID extraState
-> XMLConverterState nsID extraState
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 :: (extraState -> extraState) -> XMLConverter nsID extraState x x
modifyExtraState   = (XMLConverterState nsID extraState
 -> XMLConverterState nsID extraState)
-> XMLConverter nsID extraState x x
forall state a. (state -> state) -> ArrowState state a a
modifyState((XMLConverterState nsID extraState
  -> XMLConverterState nsID extraState)
 -> XMLConverter nsID extraState x x)
-> ((extraState -> extraState)
    -> XMLConverterState nsID extraState
    -> XMLConverterState nsID extraState)
-> (extraState -> extraState)
-> XMLConverter nsID extraState x x
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(extraState -> extraState)
-> XMLConverterState nsID extraState
-> XMLConverterState nsID extraState
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 :: extraState'
-> FallibleXMLConverter nsID extraState' extraState extraState
-> FallibleXMLConverter nsID extraState x x
convertingExtraState extraState'
v FallibleXMLConverter nsID extraState' extraState extraState
a = ArrowState
  (XMLConverterState nsID extraState)
  x
  (Either () (XMLConverterState nsID extraState'))
-> ArrowState
     (XMLConverterState nsID extraState')
     (XMLConverterState nsID extraState)
     (Either () (XMLConverterState nsID extraState))
-> FallibleXMLConverter nsID extraState x x
forall s x f s'.
ArrowState s x (Either f s')
-> ArrowState s' s (Either f s) -> ArrowState s x (Either f x)
withSubStateF ArrowState
  (XMLConverterState nsID extraState)
  x
  (Either () (XMLConverterState nsID extraState'))
setVAsExtraState ArrowState
  (XMLConverterState nsID extraState')
  (XMLConverterState nsID extraState)
  (Either () (XMLConverterState nsID extraState))
modifyWithA
  where
    setVAsExtraState :: ArrowState
  (XMLConverterState nsID extraState)
  x
  (Either () (XMLConverterState nsID extraState'))
setVAsExtraState     = ArrowState
  (XMLConverterState nsID extraState)
  x
  (XMLConverterState nsID extraState')
-> ArrowState
     (XMLConverterState nsID extraState)
     x
     (Either () (XMLConverterState nsID extraState'))
forall (a :: * -> * -> *) x success failure.
ArrowChoice a =>
a x success -> FallibleArrow a x failure success
liftAsSuccess (ArrowState
   (XMLConverterState nsID extraState)
   x
   (XMLConverterState nsID extraState')
 -> ArrowState
      (XMLConverterState nsID extraState)
      x
      (Either () (XMLConverterState nsID extraState')))
-> ArrowState
     (XMLConverterState nsID extraState)
     x
     (XMLConverterState nsID extraState')
-> ArrowState
     (XMLConverterState nsID extraState)
     x
     (Either () (XMLConverterState nsID extraState'))
forall a b. (a -> b) -> a -> b
$ (XMLConverterState nsID extraState
 -> XMLConverterState nsID extraState)
-> ArrowState
     (XMLConverterState nsID extraState)
     x
     (XMLConverterState nsID extraState)
forall state b x. (state -> b) -> ArrowState state x b
extractFromState XMLConverterState nsID extraState
-> XMLConverterState nsID extraState
forall a. a -> a
id ArrowState
  (XMLConverterState nsID extraState)
  x
  (XMLConverterState nsID extraState)
-> (XMLConverterState nsID extraState
    -> XMLConverterState nsID extraState')
-> ArrowState
     (XMLConverterState nsID extraState)
     x
     (XMLConverterState nsID extraState')
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> (c -> d) -> a b d
>>^ extraState'
-> XMLConverterState nsID extraState
-> XMLConverterState nsID extraState'
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          = ArrowState
  (XMLConverterState nsID extraState')
  (XMLConverterState nsID extraState)
  (Fallible extraState)
-> ArrowState
     (XMLConverterState nsID extraState')
     (XMLConverterState nsID extraState)
     (XMLConverterState nsID extraState, Fallible extraState)
forall (a :: * -> * -> *) b c. Arrow a => a b c -> a b (b, c)
keepingTheValue (XMLConverterState nsID extraState -> extraState
forall nsID extraState.
XMLConverterState nsID extraState -> extraState
moreState (XMLConverterState nsID extraState -> extraState)
-> FallibleXMLConverter nsID extraState' extraState extraState
-> ArrowState
     (XMLConverterState nsID extraState')
     (XMLConverterState nsID extraState)
     (Fallible extraState)
forall (a :: * -> * -> *) b c d.
Arrow a =>
(b -> c) -> a c d -> a b d
^>> FallibleXMLConverter nsID extraState' extraState extraState
a)
                           ArrowState
  (XMLConverterState nsID extraState')
  (XMLConverterState nsID extraState)
  (XMLConverterState nsID extraState, Fallible extraState)
-> ((XMLConverterState nsID extraState, Fallible extraState)
    -> Either () (XMLConverterState nsID extraState))
-> ArrowState
     (XMLConverterState nsID extraState')
     (XMLConverterState nsID extraState)
     (Either () (XMLConverterState nsID extraState))
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> (c -> d) -> a b d
>>^ (XMLConverterState nsID extraState, Fallible extraState)
-> Either () (XMLConverterState nsID extraState, extraState)
forall (v :: * -> *) f a.
ChoiceVector v =>
v (Either f a) -> Either f (v a)
spreadChoice ((XMLConverterState nsID extraState, Fallible extraState)
 -> Either () (XMLConverterState nsID extraState, extraState))
-> (XMLConverterState nsID extraState
    -> extraState -> XMLConverterState nsID extraState)
-> (XMLConverterState nsID extraState, Fallible extraState)
-> Either () (XMLConverterState nsID extraState)
forall (a :: * -> * -> *) x f b b' c.
ArrowChoice a =>
FallibleArrow a x f (b, b')
-> (b -> b' -> c) -> FallibleArrow a x f c
>>?% (extraState
 -> XMLConverterState nsID extraState
 -> XMLConverterState nsID extraState)
-> XMLConverterState nsID extraState
-> extraState
-> XMLConverterState nsID extraState
forall a b c. (a -> b -> c) -> b -> a -> c
flip extraState
-> XMLConverterState nsID extraState
-> XMLConverterState nsID extraState
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 :: extraState'
-> a
-> FallibleXMLConverter nsID extraState' a extraState
-> FallibleXMLConverter nsID extraState x x
producingExtraState extraState'
v a
x FallibleXMLConverter nsID extraState' a extraState
a = extraState'
-> FallibleXMLConverter nsID extraState' extraState extraState
-> FallibleXMLConverter nsID extraState x x
forall extraState' nsID extraState x.
extraState'
-> FallibleXMLConverter nsID extraState' extraState extraState
-> FallibleXMLConverter nsID extraState x x
convertingExtraState extraState'
v (a -> ArrowState (XMLConverterState nsID extraState') extraState a
forall (a :: * -> * -> *) c x. Arrow a => c -> a x c
returnV a
x ArrowState (XMLConverterState nsID extraState') extraState a
-> FallibleXMLConverter nsID extraState' a extraState
-> FallibleXMLConverter nsID extraState' extraState extraState
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 :: nsID -> XMLConverter nsID extraState x (Maybe NameSpaceIRI)
lookupNSiri nsID
nsID        = (XMLConverterState nsID extraState -> Maybe NameSpaceIRI)
-> XMLConverter nsID extraState x (Maybe NameSpaceIRI)
forall state b x. (state -> b) -> ArrowState state x b
extractFromState
                          ((XMLConverterState nsID extraState -> Maybe NameSpaceIRI)
 -> XMLConverter nsID extraState x (Maybe NameSpaceIRI))
-> (XMLConverterState nsID extraState -> Maybe NameSpaceIRI)
-> XMLConverter nsID extraState x (Maybe NameSpaceIRI)
forall a b. (a -> b) -> a -> b
$ \XMLConverterState nsID extraState
state -> nsID -> NameSpaceIRIs nsID -> Maybe NameSpaceIRI
forall nsID.
NameSpaceID nsID =>
nsID -> NameSpaceIRIs nsID -> Maybe NameSpaceIRI
getIRI nsID
nsID (NameSpaceIRIs nsID -> Maybe NameSpaceIRI)
-> NameSpaceIRIs nsID -> Maybe NameSpaceIRI
forall a b. (a -> b) -> a -> b
$ XMLConverterState nsID extraState -> NameSpaceIRIs nsID
forall nsID extraState.
XMLConverterState nsID extraState -> NameSpacePrefixes nsID
namespaceIRIs XMLConverterState nsID extraState
state

--
lookupNSprefix           :: (NameSpaceID nsID)
                         => nsID
                         -> XMLConverter nsID extraState x (Maybe NameSpacePrefix)
lookupNSprefix :: nsID -> XMLConverter nsID extraState x (Maybe NameSpaceIRI)
lookupNSprefix nsID
nsID      = (XMLConverterState nsID extraState -> Maybe NameSpaceIRI)
-> XMLConverter nsID extraState x (Maybe NameSpaceIRI)
forall state b x. (state -> b) -> ArrowState state x b
extractFromState
                           ((XMLConverterState nsID extraState -> Maybe NameSpaceIRI)
 -> XMLConverter nsID extraState x (Maybe NameSpaceIRI))
-> (XMLConverterState nsID extraState -> Maybe NameSpaceIRI)
-> XMLConverter nsID extraState x (Maybe NameSpaceIRI)
forall a b. (a -> b) -> a -> b
$ \XMLConverterState nsID extraState
state -> nsID -> Map nsID NameSpaceIRI -> Maybe NameSpaceIRI
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup nsID
nsID (Map nsID NameSpaceIRI -> Maybe NameSpaceIRI)
-> Map nsID NameSpaceIRI -> Maybe NameSpaceIRI
forall a b. (a -> b) -> a -> b
$ XMLConverterState nsID extraState -> Map nsID NameSpaceIRI
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 :: FallibleXMLConverter nsID extraState x ()
readNSattributes         = (XMLConverterState nsID extraState
 -> (XMLConverterState nsID extraState, Either () ()))
-> FallibleXMLConverter nsID extraState x ()
forall state b a. (state -> (state, b)) -> ArrowState state a b
fromState ((XMLConverterState nsID extraState
  -> (XMLConverterState nsID extraState, Either () ()))
 -> FallibleXMLConverter nsID extraState x ())
-> (XMLConverterState nsID extraState
    -> (XMLConverterState nsID extraState, Either () ()))
-> FallibleXMLConverter nsID extraState x ()
forall a b. (a -> b) -> a -> b
$ \XMLConverterState nsID extraState
state -> (XMLConverterState nsID extraState, Either () ())
-> (XMLConverterState nsID extraState
    -> (XMLConverterState nsID extraState, Either () ()))
-> Maybe (XMLConverterState nsID extraState)
-> (XMLConverterState nsID extraState, Either () ())
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (XMLConverterState nsID extraState
state, Either () ()
forall failure _x. Monoid failure => Either failure _x
failEmpty     )
                                                       (     , () -> Either () ()
forall a _x. a -> Either _x a
succeedWith ())
                                                       (XMLConverterState nsID extraState
-> Maybe (XMLConverterState nsID extraState)
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 :: XMLConverterState nsID extraState
-> Maybe (XMLConverterState nsID extraState)
extractNSAttrs XMLConverterState nsID extraState
startState
                         = (Maybe (XMLConverterState nsID extraState)
 -> (NameSpaceIRI, NameSpaceIRI)
 -> Maybe (XMLConverterState nsID extraState))
-> Maybe (XMLConverterState nsID extraState)
-> [(NameSpaceIRI, NameSpaceIRI)]
-> Maybe (XMLConverterState nsID extraState)
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl (\Maybe (XMLConverterState nsID extraState)
state (NameSpaceIRI, NameSpaceIRI)
d -> Maybe (XMLConverterState nsID extraState)
state Maybe (XMLConverterState nsID extraState)
-> (XMLConverterState nsID extraState
    -> Maybe (XMLConverterState nsID extraState))
-> Maybe (XMLConverterState nsID extraState)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (NameSpaceIRI, NameSpaceIRI)
-> XMLConverterState nsID extraState
-> Maybe (XMLConverterState nsID extraState)
forall nsID extraState.
NameSpaceID nsID =>
(NameSpaceIRI, NameSpaceIRI)
-> XMLConverterState nsID extraState
-> Maybe (XMLConverterState nsID extraState)
addNS (NameSpaceIRI, NameSpaceIRI)
d)
                                 (XMLConverterState nsID extraState
-> Maybe (XMLConverterState nsID extraState)
forall a. a -> Maybe a
Just XMLConverterState nsID extraState
startState)
                                 [(NameSpaceIRI, NameSpaceIRI)]
nsAttribs
      where nsAttribs :: [(NameSpaceIRI, NameSpaceIRI)]
nsAttribs    = (Attr -> Maybe (NameSpaceIRI, NameSpaceIRI))
-> [Attr] -> [(NameSpaceIRI, NameSpaceIRI)]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe Attr -> Maybe (NameSpaceIRI, NameSpaceIRI)
readNSattr (Element -> [Attr]
XML.elAttribs Element
element)
            element :: Element
element      = XMLConverterState nsID extraState -> Element
forall nsID extraState.
XMLConverterState nsID extraState -> Element
currentElement XMLConverterState nsID extraState
startState
            readNSattr :: Attr -> Maybe (NameSpaceIRI, NameSpaceIRI)
readNSattr (XML.Attr (XML.QName NameSpaceIRI
name Maybe NameSpaceIRI
_ (Just NameSpaceIRI
"xmlns")) NameSpaceIRI
iri)
                         = (NameSpaceIRI, NameSpaceIRI) -> Maybe (NameSpaceIRI, NameSpaceIRI)
forall a. a -> Maybe a
Just (NameSpaceIRI
name, NameSpaceIRI
iri)
            readNSattr Attr
_ = Maybe (NameSpaceIRI, NameSpaceIRI)
forall a. Maybe a
Nothing
    addNS :: (NameSpaceIRI, NameSpaceIRI)
-> XMLConverterState nsID extraState
-> Maybe (XMLConverterState nsID extraState)
addNS  (NameSpaceIRI
prefix, NameSpaceIRI
iri) XMLConverterState nsID extraState
state = ((NameSpaceIRIs nsID, nsID) -> XMLConverterState nsID extraState)
-> Maybe (NameSpaceIRIs nsID, nsID)
-> Maybe (XMLConverterState nsID extraState)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (NameSpaceIRIs nsID, nsID) -> XMLConverterState nsID extraState
updateState
                                 (Maybe (NameSpaceIRIs nsID, nsID)
 -> Maybe (XMLConverterState nsID extraState))
-> Maybe (NameSpaceIRIs nsID, nsID)
-> Maybe (XMLConverterState nsID extraState)
forall a b. (a -> b) -> a -> b
$ NameSpaceIRI
-> NameSpaceIRIs nsID -> Maybe (NameSpaceIRIs nsID, nsID)
forall nsID.
NameSpaceID nsID =>
NameSpaceIRI
-> NameSpaceIRIs nsID -> Maybe (NameSpaceIRIs nsID, nsID)
getNamespaceID NameSpaceIRI
iri
                                 (NameSpaceIRIs nsID -> Maybe (NameSpaceIRIs nsID, nsID))
-> NameSpaceIRIs nsID -> Maybe (NameSpaceIRIs nsID, nsID)
forall a b. (a -> b) -> a -> b
$ XMLConverterState nsID extraState -> NameSpaceIRIs nsID
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 = nsID -> NameSpaceIRI -> NameSpaceIRIs nsID -> NameSpaceIRIs nsID
forall k a. Ord k => k -> a -> Map k a -> Map k a
M.insert nsID
nsID NameSpaceIRI
prefix
                                                       (NameSpaceIRIs nsID -> NameSpaceIRIs nsID)
-> NameSpaceIRIs nsID -> NameSpaceIRIs nsID
forall a b. (a -> b) -> a -> b
$ XMLConverterState nsID extraState -> NameSpaceIRIs nsID
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 :: nsID -> NameSpaceIRI -> XMLConverter nsID extraState x QName
qualifyName nsID
nsID NameSpaceIRI
name    =         nsID -> XMLConverter nsID extraState x (Maybe NameSpaceIRI)
forall nsID extraState x.
NameSpaceID nsID =>
nsID -> XMLConverter nsID extraState x (Maybe NameSpaceIRI)
lookupNSiri nsID
nsID
                               XMLConverter nsID extraState x (Maybe NameSpaceIRI)
-> XMLConverter nsID extraState x (Maybe NameSpaceIRI)
-> ArrowState
     (XMLConverterState nsID extraState)
     x
     (Maybe NameSpaceIRI, Maybe NameSpaceIRI)
forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> a b c' -> a b (c, c')
&&& nsID -> XMLConverter nsID extraState x (Maybe NameSpaceIRI)
forall nsID extraState x.
NameSpaceID nsID =>
nsID -> XMLConverter nsID extraState x (Maybe NameSpaceIRI)
lookupNSprefix nsID
nsID
                           ArrowState
  (XMLConverterState nsID extraState)
  x
  (Maybe NameSpaceIRI, Maybe NameSpaceIRI)
-> (Maybe NameSpaceIRI -> Maybe NameSpaceIRI -> QName)
-> XMLConverter nsID extraState x QName
forall (a :: * -> * -> *) x b c d.
Arrow a =>
a x (b, c) -> (b -> c -> d) -> a x d
>>% NameSpaceIRI -> Maybe NameSpaceIRI -> Maybe NameSpaceIRI -> QName
XML.QName NameSpaceIRI
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 :: nsID
-> (NameSpaceIRI -> Bool)
-> XMLConverter nsID extraState Element Bool
elemNameMatches nsID
nsID NameSpaceIRI -> Bool
f    = ArrowState
  (XMLConverterState nsID extraState) Element (Maybe NameSpaceIRI)
-> ArrowState
     (XMLConverterState nsID extraState)
     Element
     (Element, Maybe NameSpaceIRI)
forall (a :: * -> * -> *) b c. Arrow a => a b c -> a b (b, c)
keepingTheValue (nsID
-> ArrowState
     (XMLConverterState nsID extraState) Element (Maybe NameSpaceIRI)
forall nsID extraState x.
NameSpaceID nsID =>
nsID -> XMLConverter nsID extraState x (Maybe NameSpaceIRI)
lookupNSiri nsID
nsID) ArrowState
  (XMLConverterState nsID extraState)
  Element
  (Element, Maybe NameSpaceIRI)
-> (Element -> Maybe NameSpaceIRI -> Bool)
-> XMLConverter nsID extraState Element Bool
forall (a :: * -> * -> *) x b c d.
Arrow a =>
a x (b, c) -> (b -> c -> d) -> a x d
>>% Element -> Maybe NameSpaceIRI -> Bool
hasMatchingName
  where hasMatchingName :: Element -> Maybe NameSpaceIRI -> Bool
hasMatchingName Element
e Maybe NameSpaceIRI
iri = let name :: QName
name = Element -> QName
XML.elName Element
e
                                in     NameSpaceIRI -> Bool
f (QName -> NameSpaceIRI
XML.qName QName
name)
                                    Bool -> Bool -> Bool
&& QName -> Maybe NameSpaceIRI
XML.qURI QName
name Maybe NameSpaceIRI -> Maybe NameSpaceIRI -> Bool
forall a. Eq a => a -> a -> Bool
== Maybe NameSpaceIRI
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 :: nsID -> NameSpaceIRI -> XMLConverter nsID extraState Element Bool
elemNameIs nsID
nsID NameSpaceIRI
name     = nsID
-> (NameSpaceIRI -> Bool)
-> XMLConverter nsID extraState Element Bool
forall nsID extraState.
NameSpaceID nsID =>
nsID
-> (NameSpaceIRI -> Bool)
-> XMLConverter nsID extraState Element Bool
elemNameMatches nsID
nsID (NameSpaceIRI -> NameSpaceIRI -> Bool
forall a. Eq a => a -> a -> Bool
== NameSpaceIRI
name)

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

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

--
elContent               :: XMLConverter nsID extraState x [XML.Content]
elContent :: XMLConverter nsID extraState x [Content]
elContent               =     XMLConverter nsID extraState x Element
forall nsID extraState x. XMLConverter nsID extraState x Element
getCurrentElement
                           XMLConverter nsID extraState x Element
-> (Element -> [Content])
-> XMLConverter nsID extraState x [Content]
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 :: nsID -> NameSpaceIRI -> XMLConverter nsID extraState x [Element]
findChildren nsID
nsID NameSpaceIRI
name   =         nsID -> NameSpaceIRI -> XMLConverter nsID extraState x QName
forall nsID extraState x.
NameSpaceID nsID =>
nsID -> NameSpaceIRI -> XMLConverter nsID extraState x QName
qualifyName nsID
nsID NameSpaceIRI
name
                               XMLConverter nsID extraState x QName
-> ArrowState (XMLConverterState nsID extraState) x Element
-> ArrowState
     (XMLConverterState nsID extraState) x (QName, Element)
forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> a b c' -> a b (c, c')
&&& ArrowState (XMLConverterState nsID extraState) x Element
forall nsID extraState x. XMLConverter nsID extraState x Element
getCurrentElement
                           ArrowState (XMLConverterState nsID extraState) x (QName, Element)
-> (QName -> Element -> [Element])
-> XMLConverter nsID extraState x [Element]
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' :: nsID
-> NameSpaceIRI -> XMLConverter nsID extraState x (Maybe Element)
findChild' nsID
nsID NameSpaceIRI
name    =         nsID -> NameSpaceIRI -> XMLConverter nsID extraState x QName
forall nsID extraState x.
NameSpaceID nsID =>
nsID -> NameSpaceIRI -> XMLConverter nsID extraState x QName
qualifyName nsID
nsID NameSpaceIRI
name
                              XMLConverter nsID extraState x QName
-> ArrowState (XMLConverterState nsID extraState) x Element
-> ArrowState
     (XMLConverterState nsID extraState) x (QName, Element)
forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> a b c' -> a b (c, c')
&&& ArrowState (XMLConverterState nsID extraState) x Element
forall nsID extraState x. XMLConverter nsID extraState x Element
getCurrentElement
                          ArrowState (XMLConverterState nsID extraState) x (QName, Element)
-> (QName -> Element -> Maybe Element)
-> XMLConverter nsID extraState x (Maybe Element)
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 :: nsID
-> NameSpaceIRI -> FallibleXMLConverter nsID extraState x Element
findChild nsID
nsID NameSpaceIRI
name    =     nsID
-> NameSpaceIRI -> XMLConverter nsID extraState x (Maybe Element)
forall nsID extraState x.
NameSpaceID nsID =>
nsID
-> NameSpaceIRI -> XMLConverter nsID extraState x (Maybe Element)
findChild' nsID
nsID NameSpaceIRI
name
                         XMLConverter nsID extraState x (Maybe Element)
-> ArrowState
     (XMLConverterState nsID extraState)
     (Maybe Element)
     (Fallible Element)
-> FallibleXMLConverter nsID extraState x Element
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> ArrowState
  (XMLConverterState nsID extraState)
  (Maybe Element)
  (Fallible Element)
forall (a :: * -> * -> *) b.
ArrowChoice a =>
a (Maybe b) (Fallible b)
maybeToChoice

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

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

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

isSetWithDefault         :: (NameSpaceID nsID)
                         => nsID -> AttributeName
                         -> Bool
                         -> XMLConverter nsID extraState x Bool
isSetWithDefault :: nsID -> NameSpaceIRI -> Bool -> XMLConverter nsID extraState x Bool
isSetWithDefault nsID
nsID NameSpaceIRI
attrName Bool
def'
                         =     nsID -> NameSpaceIRI -> XMLConverter nsID extraState x (Maybe Bool)
forall nsID extraState x.
NameSpaceID nsID =>
nsID -> NameSpaceIRI -> XMLConverter nsID extraState x (Maybe Bool)
isSet' nsID
nsID NameSpaceIRI
attrName
                           XMLConverter nsID extraState x (Maybe Bool)
-> (Maybe Bool -> Bool) -> XMLConverter nsID extraState x Bool
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> (c -> d) -> a b d
>>^ Bool -> Maybe Bool -> Bool
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 :: nsID
-> NameSpaceIRI
-> [(NameSpaceIRI, a)]
-> FallibleXMLConverter nsID extraState x a
searchAttrIn nsID
nsID NameSpaceIRI
attrName [(NameSpaceIRI, a)]
dict
                         =       nsID
-> NameSpaceIRI
-> FallibleXMLConverter nsID extraState x NameSpaceIRI
forall nsID extraState x.
NameSpaceID nsID =>
nsID
-> NameSpaceIRI
-> FallibleXMLConverter nsID extraState x NameSpaceIRI
findAttr nsID
nsID NameSpaceIRI
attrName
                           FallibleXMLConverter nsID extraState x NameSpaceIRI
-> (NameSpaceIRI -> Either () a)
-> FallibleXMLConverter nsID extraState x a
forall (a :: * -> * -> *) x failure success success'.
ArrowChoice a =>
FallibleArrow a x failure success
-> (success -> Either failure success')
-> FallibleArrow a x failure success'
>>?^? Maybe a -> Either () a
forall (a :: * -> * -> *) b.
ArrowChoice a =>
a (Maybe b) (Fallible b)
maybeToChoice(Maybe a -> Either () a)
-> (NameSpaceIRI -> Maybe a) -> NameSpaceIRI -> Either () a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(NameSpaceIRI -> [(NameSpaceIRI, a)] -> Maybe a
forall a b. Eq a => a -> [(a, b)] -> Maybe b
`lookup` [(NameSpaceIRI, 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 :: nsID
-> NameSpaceIRI
-> a
-> [(NameSpaceIRI, a)]
-> XMLConverter nsID extraState x a
searchAttr nsID
nsID NameSpaceIRI
attrName a
defV [(NameSpaceIRI, a)]
dict
                         =     nsID
-> NameSpaceIRI
-> [(NameSpaceIRI, a)]
-> FallibleXMLConverter nsID extraState x a
forall nsID a extraState x.
NameSpaceID nsID =>
nsID
-> NameSpaceIRI
-> [(NameSpaceIRI, a)]
-> FallibleXMLConverter nsID extraState x a
searchAttrIn nsID
nsID NameSpaceIRI
attrName [(NameSpaceIRI, a)]
dict
                           FallibleXMLConverter nsID extraState x a
-> ArrowState (XMLConverterState nsID extraState) (Fallible a) a
-> XMLConverter nsID extraState x a
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> a -> () -> a
forall a b. a -> b -> a
const a
defV (() -> a)
-> (a -> a)
-> ArrowState (XMLConverterState nsID extraState) (Fallible a) a
forall (a :: * -> * -> *) b d c.
ArrowChoice a =>
(b -> d) -> (c -> d) -> a (Either b c) d
^|||^ a -> a
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 :: nsID -> NameSpaceIRI -> FallibleXMLConverter nsID extraState x a
lookupAttr nsID
nsID NameSpaceIRI
attrName =     nsID -> NameSpaceIRI -> XMLConverter nsID extraState x (Maybe a)
forall nsID a extraState x.
(NameSpaceID nsID, Lookupable a) =>
nsID -> NameSpaceIRI -> XMLConverter nsID extraState x (Maybe a)
lookupAttr' nsID
nsID NameSpaceIRI
attrName
                           XMLConverter nsID extraState x (Maybe a)
-> (Maybe a -> Fallible a)
-> FallibleXMLConverter nsID extraState x a
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> (c -> d) -> a b d
>>^ Maybe a -> Fallible a
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' :: nsID -> NameSpaceIRI -> XMLConverter nsID extraState x (Maybe a)
lookupAttr' nsID
nsID NameSpaceIRI
attrName
                         =     nsID
-> NameSpaceIRI
-> XMLConverter nsID extraState x (Maybe NameSpaceIRI)
forall nsID extraState x.
NameSpaceID nsID =>
nsID
-> NameSpaceIRI
-> XMLConverter nsID extraState x (Maybe NameSpaceIRI)
findAttr' nsID
nsID NameSpaceIRI
attrName
                           XMLConverter nsID extraState x (Maybe NameSpaceIRI)
-> (Maybe NameSpaceIRI -> Maybe a)
-> XMLConverter nsID extraState x (Maybe a)
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> (c -> d) -> a b d
>>^ (Maybe NameSpaceIRI -> (NameSpaceIRI -> Maybe a) -> Maybe a
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= NameSpaceIRI -> Maybe a
forall a. Lookupable a => NameSpaceIRI -> Maybe a
readLookupable)

-- | Read a 'Lookupable' attribute with explicit default
lookupAttrWithDefault    :: (NameSpaceID nsID, Lookupable a)
                         => nsID -> AttributeName
                         -> a
                         -> XMLConverter nsID extraState x a
lookupAttrWithDefault :: nsID -> NameSpaceIRI -> a -> XMLConverter nsID extraState x a
lookupAttrWithDefault nsID
nsID NameSpaceIRI
attrName a
deflt
                         =     nsID -> NameSpaceIRI -> XMLConverter nsID extraState x (Maybe a)
forall nsID a extraState x.
(NameSpaceID nsID, Lookupable a) =>
nsID -> NameSpaceIRI -> XMLConverter nsID extraState x (Maybe a)
lookupAttr' nsID
nsID NameSpaceIRI
attrName
                           XMLConverter nsID extraState x (Maybe a)
-> (Maybe a -> a) -> XMLConverter nsID extraState x a
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> (c -> d) -> a b d
>>^ a -> Maybe a -> a
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 :: nsID -> NameSpaceIRI -> XMLConverter nsID extraState x a
lookupDefaultingAttr nsID
nsID NameSpaceIRI
attrName
                         = nsID -> NameSpaceIRI -> a -> XMLConverter nsID extraState x a
forall nsID a extraState x.
(NameSpaceID nsID, Lookupable a) =>
nsID -> NameSpaceIRI -> a -> XMLConverter nsID extraState x a
lookupAttrWithDefault nsID
nsID NameSpaceIRI
attrName a
forall a. Default a => a
def

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

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

-- | Return value as string or fail
findAttr               :: (NameSpaceID nsID)
                       => nsID -> AttributeName
                       -> FallibleXMLConverter nsID extraState x AttributeValue
findAttr :: nsID
-> NameSpaceIRI
-> FallibleXMLConverter nsID extraState x NameSpaceIRI
findAttr nsID
nsID NameSpaceIRI
attrName =     nsID
-> NameSpaceIRI
-> XMLConverter nsID extraState x (Maybe NameSpaceIRI)
forall nsID extraState x.
NameSpaceID nsID =>
nsID
-> NameSpaceIRI
-> XMLConverter nsID extraState x (Maybe NameSpaceIRI)
findAttr' nsID
nsID NameSpaceIRI
attrName
                         XMLConverter nsID extraState x (Maybe NameSpaceIRI)
-> ArrowState
     (XMLConverterState nsID extraState)
     (Maybe NameSpaceIRI)
     (Fallible NameSpaceIRI)
-> FallibleXMLConverter nsID extraState x NameSpaceIRI
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> ArrowState
  (XMLConverterState nsID extraState)
  (Maybe NameSpaceIRI)
  (Fallible NameSpaceIRI)
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 :: nsID
-> NameSpaceIRI
-> FallibleXMLConverter nsID extraState x TextAttributeValue
findAttrText nsID
nsID NameSpaceIRI
attrName
                       = nsID
-> NameSpaceIRI
-> XMLConverter nsID extraState x (Maybe NameSpaceIRI)
forall nsID extraState x.
NameSpaceID nsID =>
nsID
-> NameSpaceIRI
-> XMLConverter nsID extraState x (Maybe NameSpaceIRI)
findAttr' nsID
nsID NameSpaceIRI
attrName
                         XMLConverter nsID extraState x (Maybe NameSpaceIRI)
-> (Maybe NameSpaceIRI -> Fallible TextAttributeValue)
-> FallibleXMLConverter nsID extraState x TextAttributeValue
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> (c -> d) -> a b d
>>^ (NameSpaceIRI -> TextAttributeValue)
-> Maybe NameSpaceIRI -> Maybe TextAttributeValue
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap NameSpaceIRI -> TextAttributeValue
T.pack
                         (Maybe NameSpaceIRI -> Maybe TextAttributeValue)
-> (Maybe TextAttributeValue -> Fallible TextAttributeValue)
-> Maybe NameSpaceIRI
-> Fallible TextAttributeValue
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> Maybe TextAttributeValue -> Fallible TextAttributeValue
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 :: nsID
-> NameSpaceIRI
-> NameSpaceIRI
-> XMLConverter nsID extraState x NameSpaceIRI
findAttrWithDefault nsID
nsID NameSpaceIRI
attrName NameSpaceIRI
deflt
                       = nsID
-> NameSpaceIRI
-> XMLConverter nsID extraState x (Maybe NameSpaceIRI)
forall nsID extraState x.
NameSpaceID nsID =>
nsID
-> NameSpaceIRI
-> XMLConverter nsID extraState x (Maybe NameSpaceIRI)
findAttr' nsID
nsID NameSpaceIRI
attrName
                         XMLConverter nsID extraState x (Maybe NameSpaceIRI)
-> (Maybe NameSpaceIRI -> NameSpaceIRI)
-> XMLConverter nsID extraState x NameSpaceIRI
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> (c -> d) -> a b d
>>^ NameSpaceIRI -> Maybe NameSpaceIRI -> NameSpaceIRI
forall a. a -> Maybe a -> a
fromMaybe NameSpaceIRI
deflt

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

-- | Read and return value or fail
readAttr               :: (NameSpaceID nsID, Read attrValue)
                       => nsID -> AttributeName
                       -> FallibleXMLConverter nsID extraState x attrValue
readAttr :: nsID
-> NameSpaceIRI -> FallibleXMLConverter nsID extraState x attrValue
readAttr nsID
nsID NameSpaceIRI
attrName =     nsID
-> NameSpaceIRI -> XMLConverter nsID extraState x (Maybe attrValue)
forall nsID attrValue extraState x.
(NameSpaceID nsID, Read attrValue) =>
nsID
-> NameSpaceIRI -> XMLConverter nsID extraState x (Maybe attrValue)
readAttr' nsID
nsID NameSpaceIRI
attrName
                         XMLConverter nsID extraState x (Maybe attrValue)
-> ArrowState
     (XMLConverterState nsID extraState)
     (Maybe attrValue)
     (Fallible attrValue)
-> FallibleXMLConverter nsID extraState x attrValue
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> ArrowState
  (XMLConverterState nsID extraState)
  (Maybe attrValue)
  (Fallible attrValue)
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' :: nsID
-> NameSpaceIRI -> XMLConverter nsID extraState x (Maybe attrValue)
readAttr' nsID
nsID NameSpaceIRI
attrName =     nsID
-> NameSpaceIRI
-> XMLConverter nsID extraState x (Maybe NameSpaceIRI)
forall nsID extraState x.
NameSpaceID nsID =>
nsID
-> NameSpaceIRI
-> XMLConverter nsID extraState x (Maybe NameSpaceIRI)
findAttr' nsID
nsID NameSpaceIRI
attrName
                          XMLConverter nsID extraState x (Maybe NameSpaceIRI)
-> (Maybe NameSpaceIRI -> Maybe attrValue)
-> XMLConverter nsID extraState x (Maybe attrValue)
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> (c -> d) -> a b d
>>^ (Maybe NameSpaceIRI
-> (NameSpaceIRI -> Maybe attrValue) -> Maybe attrValue
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= NameSpaceIRI -> Maybe attrValue
forall r. Read r => NameSpaceIRI -> 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 :: nsID
-> NameSpaceIRI
-> attrValue
-> XMLConverter nsID extraState x attrValue
readAttrWithDefault nsID
nsID NameSpaceIRI
attrName attrValue
deflt
                       =     nsID
-> NameSpaceIRI
-> XMLConverter nsID extraState x (Maybe NameSpaceIRI)
forall nsID extraState x.
NameSpaceID nsID =>
nsID
-> NameSpaceIRI
-> XMLConverter nsID extraState x (Maybe NameSpaceIRI)
findAttr' nsID
nsID NameSpaceIRI
attrName
                         XMLConverter nsID extraState x (Maybe NameSpaceIRI)
-> (Maybe NameSpaceIRI -> attrValue)
-> XMLConverter nsID extraState x attrValue
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> (c -> d) -> a b d
>>^ (Maybe NameSpaceIRI
-> (NameSpaceIRI -> Maybe attrValue) -> Maybe attrValue
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= NameSpaceIRI -> Maybe attrValue
forall r. Read r => NameSpaceIRI -> Maybe r
tryToRead)
                         (Maybe NameSpaceIRI -> Maybe attrValue)
-> (Maybe attrValue -> attrValue)
-> Maybe NameSpaceIRI
-> attrValue
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> (c -> d) -> a b d
>>^ attrValue -> Maybe attrValue -> attrValue
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 :: nsID -> NameSpaceIRI -> XMLConverter nsID extraState x attrValue
getAttr nsID
nsID NameSpaceIRI
attrName  = nsID
-> NameSpaceIRI
-> attrValue
-> XMLConverter nsID extraState x attrValue
forall nsID attrValue extraState x.
(NameSpaceID nsID, Read attrValue) =>
nsID
-> NameSpaceIRI
-> attrValue
-> XMLConverter nsID extraState x attrValue
readAttrWithDefault nsID
nsID NameSpaceIRI
attrName attrValue
forall a. Default a => a
def

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

--
jumpThere              :: XMLConverter nsID extraState XML.Element XML.Element
jumpThere :: XMLConverter nsID extraState Element Element
jumpThere              = (XMLConverterState nsID extraState
 -> Element -> (XMLConverterState nsID extraState, Element))
-> XMLConverter nsID extraState Element Element
forall state a b.
(state -> a -> (state, b)) -> ArrowState state a b
withState (\XMLConverterState nsID extraState
state Element
element
                                     -> ( Element
-> XMLConverterState nsID extraState
-> XMLConverterState nsID extraState
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 :: XMLConverter nsID extraState [Element] [Element]
swapStack             = (XMLConverterState nsID extraState
 -> [Element] -> (XMLConverterState nsID extraState, [Element]))
-> XMLConverter nsID extraState [Element] [Element]
forall state a b.
(state -> a -> (state, b)) -> ArrowState state a b
withState XMLConverterState nsID extraState
-> [Element] -> (XMLConverterState nsID extraState, [Element])
forall nsID extraState.
XMLConverterState nsID extraState
-> [Element] -> (XMLConverterState nsID extraState, [Element])
swapStack'

--
jumpBack               :: FallibleXMLConverter nsID extraState _x _x
jumpBack :: FallibleXMLConverter nsID extraState _x _x
jumpBack               = (XMLConverterState nsID extraState
 -> Either () (XMLConverterState nsID extraState))
-> FallibleXMLConverter nsID extraState _x _x
forall state f a.
(state -> Either f state) -> ArrowState state a (Either f a)
tryModifyState (XMLConverterState nsID extraState
-> Maybe (XMLConverterState nsID extraState)
forall nsID extraState.
XMLConverterState nsID extraState
-> Maybe (XMLConverterState nsID extraState)
popElement (XMLConverterState nsID extraState
 -> Maybe (XMLConverterState nsID extraState))
-> (Maybe (XMLConverterState nsID extraState)
    -> Either () (XMLConverterState nsID extraState))
-> XMLConverterState nsID extraState
-> Either () (XMLConverterState nsID extraState)
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> Maybe (XMLConverterState nsID extraState)
-> Either () (XMLConverterState nsID extraState)
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 :: XMLConverter nsID moreState a b
-> XMLConverter nsID moreState (a, Element) b
switchingTheStack XMLConverter nsID moreState a b
a    =     ArrowState (XMLConverterState nsID moreState) Element [Element]
-> ArrowState
     (XMLConverterState nsID moreState) (a, Element) (a, [Element])
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (d, b) (d, c)
second ( (Element -> [Element] -> [Element]
forall a. a -> [a] -> [a]
:[]) (Element -> [Element])
-> ArrowState
     (XMLConverterState nsID moreState) [Element] [Element]
-> ArrowState (XMLConverterState nsID moreState) Element [Element]
forall (a :: * -> * -> *) b c d.
Arrow a =>
(b -> c) -> a c d -> a b d
^>> ArrowState (XMLConverterState nsID moreState) [Element] [Element]
forall nsID extraState.
XMLConverter nsID extraState [Element] [Element]
swapStack )
                         ArrowState
  (XMLConverterState nsID moreState) (a, Element) (a, [Element])
-> ArrowState (XMLConverterState nsID moreState) (a, [Element]) b
-> XMLConverter nsID moreState (a, Element) b
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> XMLConverter nsID moreState a b
-> ArrowState
     (XMLConverterState nsID moreState) (a, [Element]) (b, [Element])
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (b, d) (c, d)
first  XMLConverter nsID moreState a b
a
                         ArrowState
  (XMLConverterState nsID moreState) (a, [Element]) (b, [Element])
-> ArrowState (XMLConverterState nsID moreState) (b, [Element]) b
-> ArrowState (XMLConverterState nsID moreState) (a, [Element]) b
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> ArrowState (XMLConverterState nsID moreState) [Element] [Element]
-> ArrowState
     (XMLConverterState nsID moreState) (b, [Element]) (b, [Element])
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (d, b) (d, c)
second ArrowState (XMLConverterState nsID moreState) [Element] [Element]
forall nsID extraState.
XMLConverter nsID extraState [Element] [Element]
swapStack
                         ArrowState
  (XMLConverterState nsID moreState) (b, [Element]) (b, [Element])
-> ((b, [Element]) -> b)
-> ArrowState (XMLConverterState nsID moreState) (b, [Element]) b
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> (c -> d) -> a b d
>>^ (b, [Element]) -> b
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 :: FallibleXMLConverter nsID moreState a b
-> FallibleXMLConverter nsID moreState (a, Element) b
executeThere FallibleXMLConverter nsID moreState a b
a         =      ArrowState (XMLConverterState nsID moreState) Element Element
-> ArrowState
     (XMLConverterState nsID moreState) (a, Element) (a, Element)
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (d, b) (d, c)
second ArrowState (XMLConverterState nsID moreState) Element Element
forall nsID extraState.
XMLConverter nsID extraState Element Element
jumpThere
                          ArrowState
  (XMLConverterState nsID moreState) (a, Element) (a, Element)
-> FallibleXMLConverter nsID moreState (a, Element) b
-> FallibleXMLConverter nsID moreState (a, Element) b
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> (a, Element) -> a
forall a b. (a, b) -> a
fst
                          ((a, Element) -> a)
-> FallibleXMLConverter nsID moreState a b
-> FallibleXMLConverter nsID moreState (a, Element) b
forall (a :: * -> * -> *) b c d.
Arrow a =>
(b -> c) -> a c d -> a b d
^>> FallibleXMLConverter nsID moreState a b
a
                          FallibleXMLConverter nsID moreState a b
-> ArrowState
     (XMLConverterState nsID moreState) (Fallible b) (Fallible b)
-> FallibleXMLConverter nsID moreState a b
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> FallibleXMLConverter nsID moreState (Fallible b) (Fallible b)
forall nsID extraState _x.
FallibleXMLConverter nsID extraState _x _x
jumpBack -- >>? jumpBack  would not ensure the jump.
                          FallibleXMLConverter nsID moreState (Fallible b) (Fallible b)
-> (Fallible (Fallible b) -> Fallible b)
-> ArrowState
     (XMLConverterState nsID moreState) (Fallible b) (Fallible b)
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> (c -> d) -> a b d
>>^ Fallible (Fallible b) -> Fallible b
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 :: XMLConverter nsID extraState Element s
-> XMLConverter nsID extraState Element s
executeIn XMLConverter nsID extraState Element s
a = ArrowState
  (XMLConverterState nsID extraState) Element (Element, Element)
forall (a :: * -> * -> *) b. Arrow a => a b (b, b)
duplicate ArrowState
  (XMLConverterState nsID extraState) Element (Element, Element)
-> ArrowState
     (XMLConverterState nsID extraState) (Element, Element) s
-> XMLConverter nsID extraState Element s
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> XMLConverter nsID extraState Element s
-> ArrowState
     (XMLConverterState nsID extraState) (Element, Element) s
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 :: nsID
-> NameSpaceIRI
-> FallibleXMLConverter nsID extraState f s
-> FallibleXMLConverter nsID extraState f s
executeInSub nsID
nsID NameSpaceIRI
name FallibleXMLConverter nsID extraState f s
a  =     ArrowState (XMLConverterState nsID extraState) f (Fallible Element)
-> ArrowState
     (XMLConverterState nsID extraState) f (f, Fallible Element)
forall (a :: * -> * -> *) b c. Arrow a => a b c -> a b (b, c)
keepingTheValue
                                  (nsID
-> NameSpaceIRI
-> ArrowState
     (XMLConverterState nsID extraState) f (Fallible Element)
forall nsID extraState x.
NameSpaceID nsID =>
nsID
-> NameSpaceIRI -> FallibleXMLConverter nsID extraState x Element
findChild nsID
nsID NameSpaceIRI
name)
                            ArrowState
  (XMLConverterState nsID extraState) f (f, Fallible Element)
-> ArrowState
     (XMLConverterState nsID extraState)
     (f, Fallible Element)
     (Either () s)
-> FallibleXMLConverter nsID extraState f s
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> ((f, Fallible Element) -> Either () (f, Element))
-> ArrowState
     (XMLConverterState nsID extraState)
     (f, Fallible Element)
     (Either () (f, Element))
forall a b state. (a -> b) -> ArrowState state a b
ignoringState (f, Fallible Element) -> Either () (f, Element)
forall a a b. (a, Either a b) -> Either a (a, b)
liftFailure
                            ArrowState
  (XMLConverterState nsID extraState)
  (f, Fallible Element)
  (Either () (f, Element))
-> FallibleArrow
     (ArrowState (XMLConverterState nsID extraState)) (f, Element) () s
-> ArrowState
     (XMLConverterState nsID extraState)
     (f, Fallible Element)
     (Either () s)
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 f s
-> FallibleArrow
     (ArrowState (XMLConverterState nsID extraState)) (f, Element) () s
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) = a -> Either a (a, b)
forall a b. a -> Either a b
Left  a
f
        liftFailure (a
x, Right b
e) = (a, b) -> Either a (a, b)
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 :: nsID
-> NameSpaceIRI -> XMLConverter nsID extraState b [(b, Element)]
prepareIteration nsID
nsID NameSpaceIRI
name =     ArrowState (XMLConverterState nsID extraState) b [Element]
-> ArrowState (XMLConverterState nsID extraState) b (b, [Element])
forall (a :: * -> * -> *) b c. Arrow a => a b c -> a b (b, c)
keepingTheValue
                                   (nsID
-> NameSpaceIRI
-> ArrowState (XMLConverterState nsID extraState) b [Element]
forall nsID extraState x.
NameSpaceID nsID =>
nsID -> NameSpaceIRI -> XMLConverter nsID extraState x [Element]
findChildren nsID
nsID NameSpaceIRI
name)
                             ArrowState (XMLConverterState nsID extraState) b (b, [Element])
-> (b -> [Element] -> [(b, Element)])
-> XMLConverter nsID extraState b [(b, Element)]
forall (a :: * -> * -> *) x b c d.
Arrow a =>
a x (b, c) -> (b -> c -> d) -> a x d
>>% b -> [Element] -> [(b, Element)]
forall a b. a -> [b] -> [(a, b)]
distributeValue

--
withEveryL             :: (NameSpaceID nsID)
                       => nsID -> ElementName
                       -> FallibleXMLConverter nsID extraState a  b
                       -> FallibleXMLConverter nsID extraState a [b]
withEveryL :: nsID
-> NameSpaceIRI
-> FallibleXMLConverter nsID extraState a b
-> FallibleXMLConverter nsID extraState a [b]
withEveryL = nsID
-> NameSpaceIRI
-> FallibleXMLConverter nsID extraState a b
-> FallibleXMLConverter nsID extraState a [b]
forall nsID (m :: * -> *) extraState a b.
(NameSpaceID nsID, MonadPlus m) =>
nsID
-> NameSpaceIRI
-> 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 :: nsID
-> NameSpaceIRI
-> FallibleXMLConverter nsID extraState a b
-> FallibleXMLConverter nsID extraState a (m b)
withEvery nsID
nsID NameSpaceIRI
name FallibleXMLConverter nsID extraState a b
a      =     nsID
-> NameSpaceIRI -> XMLConverter nsID extraState a [(a, Element)]
forall nsID extraState b.
NameSpaceID nsID =>
nsID
-> NameSpaceIRI -> XMLConverter nsID extraState b [(b, Element)]
prepareIteration nsID
nsID NameSpaceIRI
name
                             XMLConverter nsID extraState a [(a, Element)]
-> ArrowState
     (XMLConverterState nsID extraState)
     [(a, Element)]
     (Either () (m b))
-> FallibleXMLConverter nsID extraState a (m b)
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> ArrowState
  (XMLConverterState nsID extraState) (a, Element) (Either () b)
-> ArrowState
     (XMLConverterState nsID extraState)
     [(a, Element)]
     (Either () (m b))
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' (FallibleXMLConverter nsID extraState a b
-> ArrowState
     (XMLConverterState nsID extraState) (a, Element) (Either () b)
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 :: nsID
-> NameSpaceIRI
-> FallibleXMLConverter nsID extraState b a
-> XMLConverter nsID extraState b [a]
tryAll nsID
nsID NameSpaceIRI
name FallibleXMLConverter nsID extraState b a
a         =     nsID
-> NameSpaceIRI -> XMLConverter nsID extraState b [(b, Element)]
forall nsID extraState b.
NameSpaceID nsID =>
nsID
-> NameSpaceIRI -> XMLConverter nsID extraState b [(b, Element)]
prepareIteration nsID
nsID NameSpaceIRI
name
                             XMLConverter nsID extraState b [(b, Element)]
-> ArrowState
     (XMLConverterState nsID extraState) [(b, Element)] [a]
-> XMLConverter nsID extraState b [a]
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> ArrowState
  (XMLConverterState nsID extraState) (b, Element) (Fallible a)
-> ArrowState
     (XMLConverterState nsID extraState) [(b, Element)] [Fallible a]
forall (f :: * -> *) (m :: * -> *) s x y.
(Foldable f, MonadPlus m) =>
ArrowState s x y -> ArrowState s (f x) (m y)
iterateS (FallibleXMLConverter nsID extraState b a
-> ArrowState
     (XMLConverterState nsID extraState) (b, Element) (Fallible a)
forall nsID moreState a b.
XMLConverter nsID moreState a b
-> XMLConverter nsID moreState (a, Element) b
switchingTheStack FallibleXMLConverter nsID extraState b a
a)
                             ArrowState
  (XMLConverterState nsID extraState) [(b, Element)] [Fallible a]
-> ([Fallible a] -> [a])
-> ArrowState
     (XMLConverterState nsID extraState) [(b, Element)] [a]
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> (c -> d) -> a b d
>>^ [Fallible a] -> [a]
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 :: nsID
-> NameSpaceIRI
-> FallibleXMLConverter nsID extraState a a
-> ContentMatchConverter nsID extraState a
makeMatcherC nsID
nsID NameSpaceIRI
name FallibleXMLConverter nsID extraState a a
c = (    ArrowState
  (XMLConverterState nsID extraState)
  Content
  (Maybe
     (ArrowState
        (XMLConverterState nsID extraState) (a, Content) (a, Content)))
-> ArrowState
     (XMLConverterState nsID extraState)
     (Maybe
        (ArrowState
           (XMLConverterState nsID extraState) (a, Content) (a, Content)),
      Content)
     (Maybe
        (ArrowState
           (XMLConverterState nsID extraState) (a, Content) (a, Content)),
      Maybe
        (ArrowState
           (XMLConverterState nsID extraState) (a, Content) (a, Content)))
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (d, b) (d, c)
second (    FallibleXMLConverter nsID extraState Content Element
forall nsID extraState.
FallibleXMLConverter nsID extraState Content Element
contentToElem
                                         FallibleXMLConverter nsID extraState Content Element
-> ArrowState
     (XMLConverterState nsID extraState)
     (Fallible Element)
     (Maybe
        (ArrowState
           (XMLConverterState nsID extraState) (a, Content) (a, Content)))
-> ArrowState
     (XMLConverterState nsID extraState)
     Content
     (Maybe
        (ArrowState
           (XMLConverterState nsID extraState) (a, Content) (a, Content)))
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> Maybe
  (ArrowState
     (XMLConverterState nsID extraState) (a, Content) (a, Content))
-> ArrowState
     (XMLConverterState nsID extraState)
     ()
     (Maybe
        (ArrowState
           (XMLConverterState nsID extraState) (a, Content) (a, Content)))
forall (a :: * -> * -> *) c x. Arrow a => c -> a x c
returnV Maybe
  (ArrowState
     (XMLConverterState nsID extraState) (a, Content) (a, Content))
forall a. Maybe a
Nothing
                                         ArrowState
  (XMLConverterState nsID extraState)
  ()
  (Maybe
     (ArrowState
        (XMLConverterState nsID extraState) (a, Content) (a, Content)))
-> ArrowState
     (XMLConverterState nsID extraState)
     Element
     (Maybe
        (ArrowState
           (XMLConverterState nsID extraState) (a, Content) (a, Content)))
-> ArrowState
     (XMLConverterState nsID extraState)
     (Fallible Element)
     (Maybe
        (ArrowState
           (XMLConverterState nsID extraState) (a, Content) (a, Content)))
forall (a :: * -> * -> *) b d c.
ArrowChoice a =>
a b d -> a c d -> a (Either b c) d
||| (    nsID -> NameSpaceIRI -> XMLConverter nsID extraState Element Bool
forall nsID extraState.
NameSpaceID nsID =>
nsID -> NameSpaceIRI -> XMLConverter nsID extraState Element Bool
elemNameIs nsID
nsID NameSpaceIRI
name
                                              XMLConverter nsID extraState Element Bool
-> (Bool
    -> Maybe
         (ArrowState
            (XMLConverterState nsID extraState) (a, Content) (a, Content)))
-> ArrowState
     (XMLConverterState nsID extraState)
     Element
     (Maybe
        (ArrowState
           (XMLConverterState nsID extraState) (a, Content) (a, Content)))
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> (c -> d) -> a b d
>>^ Maybe
  (ArrowState
     (XMLConverterState nsID extraState) (a, Content) (a, Content))
-> Maybe
     (ArrowState
        (XMLConverterState nsID extraState) (a, Content) (a, Content))
-> Bool
-> Maybe
     (ArrowState
        (XMLConverterState nsID extraState) (a, Content) (a, Content))
forall a. a -> a -> Bool -> a
bool Maybe
  (ArrowState
     (XMLConverterState nsID extraState) (a, Content) (a, Content))
forall a. Maybe a
Nothing (ArrowState
  (XMLConverterState nsID extraState) (a, Content) (a, Content)
-> Maybe
     (ArrowState
        (XMLConverterState nsID extraState) (a, Content) (a, Content))
forall a. a -> Maybe a
Just ArrowState
  (XMLConverterState nsID extraState) (a, Content) (a, Content)
cWithJump)
                                             )
                                        )
                             ArrowState
  (XMLConverterState nsID extraState)
  (Maybe
     (ArrowState
        (XMLConverterState nsID extraState) (a, Content) (a, Content)),
   Content)
  (Maybe
     (ArrowState
        (XMLConverterState nsID extraState) (a, Content) (a, Content)),
   Maybe
     (ArrowState
        (XMLConverterState nsID extraState) (a, Content) (a, Content)))
-> (Maybe
      (ArrowState
         (XMLConverterState nsID extraState) (a, Content) (a, Content))
    -> Maybe
         (ArrowState
            (XMLConverterState nsID extraState) (a, Content) (a, Content))
    -> Maybe
         (ArrowState
            (XMLConverterState nsID extraState) (a, Content) (a, Content)))
-> ArrowState
     (XMLConverterState nsID extraState)
     (Maybe
        (ArrowState
           (XMLConverterState nsID extraState) (a, Content) (a, Content)),
      Content)
     (Maybe
        (ArrowState
           (XMLConverterState nsID extraState) (a, Content) (a, Content)))
forall (a :: * -> * -> *) x b c d.
Arrow a =>
a x (b, c) -> (b -> c -> d) -> a x d
>>% Maybe
  (ArrowState
     (XMLConverterState nsID extraState) (a, Content) (a, Content))
-> Maybe
     (ArrowState
        (XMLConverterState nsID extraState) (a, Content) (a, Content))
-> Maybe
     (ArrowState
        (XMLConverterState nsID extraState) (a, Content) (a, Content))
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
(<|>)
                           ) ArrowState
  (XMLConverterState nsID extraState)
  (Maybe
     (ArrowState
        (XMLConverterState nsID extraState) (a, Content) (a, Content)),
   Content)
  (Maybe
     (ArrowState
        (XMLConverterState nsID extraState) (a, Content) (a, Content)))
-> ((Maybe
       (ArrowState
          (XMLConverterState nsID extraState) (a, Content) (a, Content)),
     Content)
    -> Content)
-> ContentMatchConverter nsID extraState a
forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> (b -> c') -> a b (c, c')
&&&^ (Maybe
   (ArrowState
      (XMLConverterState nsID extraState) (a, Content) (a, Content)),
 Content)
-> Content
forall a b. (a, b) -> b
snd
  where cWithJump :: ArrowState
  (XMLConverterState nsID extraState) (a, Content) (a, Content)
cWithJump =      ( (a, Content) -> a
forall a b. (a, b) -> a
fst
                           ((a, Content) -> a)
-> ArrowState
     (XMLConverterState nsID extraState) (a, Content) (Either () a)
-> ArrowState
     (XMLConverterState nsID extraState) (a, Content) (a, Either () a)
forall (a :: * -> * -> *) b c c'.
Arrow a =>
(b -> c) -> a b c' -> a b (c, c')
^&&& (      FallibleXMLConverter nsID extraState Content Element
-> ArrowState
     (XMLConverterState nsID extraState)
     (a, Content)
     (a, Fallible Element)
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (d, b) (d, c)
second FallibleXMLConverter nsID extraState Content Element
forall nsID extraState.
FallibleXMLConverter nsID extraState Content Element
contentToElem
                                  ArrowState
  (XMLConverterState nsID extraState)
  (a, Content)
  (a, Fallible Element)
-> ArrowState
     (XMLConverterState nsID extraState)
     (a, Fallible Element)
     (Either () a)
-> ArrowState
     (XMLConverterState nsID extraState) (a, Content) (Either () a)
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>>  (a, Fallible Element) -> Either () (a, Element)
forall (v :: * -> *) f a.
ChoiceVector v =>
v (Either f a) -> Either f (v a)
spreadChoice
                                  ((a, Fallible Element) -> Either () (a, Element))
-> FallibleArrow
     (ArrowState (XMLConverterState nsID extraState)) (a, Element) () a
-> ArrowState
     (XMLConverterState nsID extraState)
     (a, Fallible Element)
     (Either () a)
forall (a :: * -> * -> *) x failure success success'.
ArrowChoice a =>
(x -> Either failure success)
-> FallibleArrow a success failure success'
-> FallibleArrow a x failure success'
^>>? FallibleXMLConverter nsID extraState a a
-> FallibleArrow
     (ArrowState (XMLConverterState nsID extraState)) (a, Element) () a
forall nsID moreState a b.
FallibleXMLConverter nsID moreState a b
-> FallibleXMLConverter nsID moreState (a, Element) b
executeThere FallibleXMLConverter nsID extraState a a
c
                                )
                            ArrowState
  (XMLConverterState nsID extraState) (a, Content) (a, Either () a)
-> (a -> Either () a -> a)
-> ArrowState (XMLConverterState nsID extraState) (a, Content) a
forall (a :: * -> * -> *) x b c d.
Arrow a =>
a x (b, c) -> (b -> c -> d) -> a x d
>>% a -> Either () a -> a
forall a _f. a -> Either _f a -> a
recover)
                    ArrowState (XMLConverterState nsID extraState) (a, Content) a
-> ((a, Content) -> Content)
-> ArrowState
     (XMLConverterState nsID extraState) (a, Content) (a, Content)
forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> (b -> c') -> a b (c, c')
&&&^ (a, Content) -> Content
forall a b. (a, b) -> b
snd
        contentToElem :: FallibleXMLConverter nsID extraState XML.Content XML.Element
        contentToElem :: FallibleXMLConverter nsID extraState Content Element
contentToElem = (Content -> Fallible Element)
-> FallibleXMLConverter nsID extraState Content Element
forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr ((Content -> Fallible Element)
 -> FallibleXMLConverter nsID extraState Content Element)
-> (Content -> Fallible Element)
-> FallibleXMLConverter nsID extraState Content Element
forall a b. (a -> b) -> a -> b
$ \case
                                     XML.Elem Element
e' -> Element -> Fallible Element
forall a _x. a -> Either _x a
succeedWith Element
e'
                                     Content
_           -> Fallible Element
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 :: [(nsID, NameSpaceIRI, FallibleXMLConverter nsID extraState x x)]
-> ContentMatchConverter nsID extraState x
prepareMatchersC      = [ContentMatchConverter nsID extraState x]
-> ContentMatchConverter nsID extraState x
forall (cat :: * -> * -> *) (f :: * -> *) a.
(Category cat, Foldable f) =>
f (cat a a) -> cat a a
reverseComposition ([ContentMatchConverter nsID extraState x]
 -> ContentMatchConverter nsID extraState x)
-> ([(nsID, NameSpaceIRI,
      FallibleXMLConverter nsID extraState x x)]
    -> [ContentMatchConverter nsID extraState x])
-> [(nsID, NameSpaceIRI, FallibleXMLConverter nsID extraState x x)]
-> ContentMatchConverter nsID extraState x
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((nsID, NameSpaceIRI, FallibleXMLConverter nsID extraState x x)
 -> ContentMatchConverter nsID extraState x)
-> [(nsID, NameSpaceIRI, FallibleXMLConverter nsID extraState x x)]
-> [ContentMatchConverter nsID extraState x]
forall a b. (a -> b) -> [a] -> [b]
map ((nsID
 -> NameSpaceIRI
 -> FallibleXMLConverter nsID extraState x x
 -> ContentMatchConverter nsID extraState x)
-> (nsID, NameSpaceIRI, FallibleXMLConverter nsID extraState x x)
-> ContentMatchConverter nsID extraState x
forall a b c z. (a -> b -> c -> z) -> (a, b, c) -> z
uncurry3  nsID
-> NameSpaceIRI
-> FallibleXMLConverter nsID extraState x x
-> ContentMatchConverter nsID extraState x
forall nsID extraState a.
NameSpaceID nsID =>
nsID
-> NameSpaceIRI
-> 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' :: [(nsID, NameSpaceIRI, FallibleXMLConverter nsID extraState a a)]
-> XMLConverter nsID extraState a a
matchContent' [(nsID, NameSpaceIRI, FallibleXMLConverter nsID extraState a a)]
lookups   = [(nsID, NameSpaceIRI, FallibleXMLConverter nsID extraState a a)]
-> XMLConverter nsID extraState (a, Content) a
-> XMLConverter nsID extraState a a
forall nsID extraState a.
NameSpaceID nsID =>
[(nsID, NameSpaceIRI, FallibleXMLConverter nsID extraState a a)]
-> XMLConverter nsID extraState (a, Content) a
-> XMLConverter nsID extraState a a
matchContent [(nsID, NameSpaceIRI, FallibleXMLConverter nsID extraState a a)]
lookups (((a, Content) -> a) -> XMLConverter nsID extraState (a, Content) a
forall (a :: * -> * -> *) b c. Arrow a => (b -> c) -> a b c
arr (a, Content) -> a
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 :: [(nsID, NameSpaceIRI, FallibleXMLConverter nsID extraState a a)]
-> XMLConverter nsID extraState (a, Content) a
-> XMLConverter nsID extraState a a
matchContent [(nsID, NameSpaceIRI, FallibleXMLConverter nsID extraState a a)]
lookups XMLConverter nsID extraState (a, Content) a
fallback
                        = let matcher :: ContentMatchConverter nsID extraState a
matcher = [(nsID, NameSpaceIRI, FallibleXMLConverter nsID extraState a a)]
-> ContentMatchConverter nsID extraState a
forall nsID extraState x.
NameSpaceID nsID =>
[(nsID, NameSpaceIRI, FallibleXMLConverter nsID extraState x x)]
-> ContentMatchConverter nsID extraState x
prepareMatchersC [(nsID, NameSpaceIRI, FallibleXMLConverter nsID extraState a a)]
lookups
                          in  ArrowState
  (XMLConverterState nsID extraState)
  a
  (XMLConverter nsID extraState a a)
-> ArrowState
     (XMLConverterState nsID extraState)
     a
     (a, XMLConverter nsID extraState a a)
forall (a :: * -> * -> *) b c. Arrow a => a b c -> a b (b, c)
keepingTheValue (
                                   XMLConverter nsID extraState a [Content]
forall nsID extraState x. XMLConverter nsID extraState x [Content]
elContent
                               XMLConverter nsID extraState a [Content]
-> ArrowState
     (XMLConverterState nsID extraState)
     [Content]
     (XMLConverter nsID extraState a a)
-> ArrowState
     (XMLConverterState nsID extraState)
     a
     (XMLConverter nsID extraState a a)
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> (Content
 -> (Maybe (IdXMLConverter nsID extraState (a, Content)), Content))
-> [Content]
-> [(Maybe (IdXMLConverter nsID extraState (a, Content)), Content)]
forall a b. (a -> b) -> [a] -> [b]
map (Maybe (IdXMLConverter nsID extraState (a, Content))
forall a. Maybe a
Nothing,)
                               ([Content]
 -> [(Maybe (IdXMLConverter nsID extraState (a, Content)),
      Content)])
-> ArrowState
     (XMLConverterState nsID extraState)
     [(Maybe (IdXMLConverter nsID extraState (a, Content)), Content)]
     (XMLConverter nsID extraState a a)
-> ArrowState
     (XMLConverterState nsID extraState)
     [Content]
     (XMLConverter nsID extraState a a)
forall (a :: * -> * -> *) b c d.
Arrow a =>
(b -> c) -> a c d -> a b d
^>> ContentMatchConverter nsID extraState a
-> ArrowState
     (XMLConverterState nsID extraState)
     [(Maybe (IdXMLConverter nsID extraState (a, Content)), Content)]
     [(Maybe (IdXMLConverter nsID extraState (a, Content)), Content)]
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
                               ArrowState
  (XMLConverterState nsID extraState)
  [(Maybe (IdXMLConverter nsID extraState (a, Content)), Content)]
  [(Maybe (IdXMLConverter nsID extraState (a, Content)), Content)]
-> ([(Maybe (IdXMLConverter nsID extraState (a, Content)),
      Content)]
    -> XMLConverter nsID extraState a a)
-> ArrowState
     (XMLConverterState nsID extraState)
     [(Maybe (IdXMLConverter nsID extraState (a, Content)), Content)]
     (XMLConverter nsID extraState a a)
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> (c -> d) -> a b d
>>^ ((Maybe (IdXMLConverter nsID extraState (a, Content)), Content)
 -> XMLConverter nsID extraState a a)
-> [(Maybe (IdXMLConverter nsID extraState (a, Content)), Content)]
-> [XMLConverter nsID extraState a a]
forall a b. (a -> b) -> [a] -> [b]
map (Maybe (IdXMLConverter nsID extraState (a, Content)), Content)
-> XMLConverter nsID extraState a a
swallowOrFallback
                              -- >>> foldSs
                               ([(Maybe (IdXMLConverter nsID extraState (a, Content)), Content)]
 -> [XMLConverter nsID extraState a a])
-> ([XMLConverter nsID extraState a a]
    -> XMLConverter nsID extraState a a)
-> [(Maybe (IdXMLConverter nsID extraState (a, Content)), Content)]
-> XMLConverter nsID extraState a a
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> [XMLConverter nsID extraState a a]
-> XMLConverter nsID extraState a a
forall (cat :: * -> * -> *) (f :: * -> *) a.
(Category cat, Foldable f) =>
f (cat a a) -> cat a a
reverseComposition
                             )
                         ArrowState
  (XMLConverterState nsID extraState)
  a
  (a, XMLConverter nsID extraState a a)
-> ArrowState
     (XMLConverterState nsID extraState)
     (a, XMLConverter nsID extraState a a)
     a
-> XMLConverter nsID extraState a a
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> (a, XMLConverter nsID extraState a a)
-> (XMLConverter nsID extraState a a, a)
forall a b. (a, b) -> (b, a)
swap
                         ((a, XMLConverter nsID extraState a a)
 -> (XMLConverter nsID extraState a a, a))
-> ArrowState
     (XMLConverterState nsID extraState)
     (XMLConverter nsID extraState a a, a)
     a
-> ArrowState
     (XMLConverterState nsID extraState)
     (a, XMLConverter nsID extraState a a)
     a
forall (a :: * -> * -> *) b c d.
Arrow a =>
(b -> c) -> a c d -> a b d
^>> ArrowState
  (XMLConverterState nsID extraState)
  (XMLConverter nsID extraState a a, a)
  a
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)
-> XMLConverter nsID extraState a a
swallowOrFallback (Just IdXMLConverter nsID extraState (a, Content)
converter,Content
content) = (,Content
content) (a -> (a, Content))
-> XMLConverter nsID extraState (a, Content) a
-> XMLConverter nsID extraState a a
forall (a :: * -> * -> *) b c d.
Arrow a =>
(b -> c) -> a c d -> a b d
^>> IdXMLConverter nsID extraState (a, Content)
converter IdXMLConverter nsID extraState (a, Content)
-> ((a, Content) -> a)
-> XMLConverter nsID extraState (a, Content) a
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> (c -> d) -> a b d
>>^ (a, Content) -> a
forall a b. (a, b) -> a
fst
        swallowOrFallback (Maybe (IdXMLConverter nsID extraState (a, Content))
Nothing       ,Content
content) = (,Content
content) (a -> (a, Content))
-> XMLConverter nsID extraState (a, Content) a
-> XMLConverter nsID extraState a a
forall (a :: * -> * -> *) b c d.
Arrow a =>
(b -> c) -> a c d -> a b d
^>> XMLConverter nsID extraState (a, Content) a
fallback

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

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


distributeValue ::  a -> [b] -> [(a,b)]
distributeValue :: a -> [b] -> [(a, b)]
distributeValue = (b -> (a, b)) -> [b] -> [(a, b)]
forall a b. (a -> b) -> [a] -> [b]
map((b -> (a, b)) -> [b] -> [(a, b)])
-> (a -> b -> (a, b)) -> a -> [b] -> [(a, b)]
forall 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? ;)
-}