diff options
Diffstat (limited to 'Presence/NestingXML.hs')
-rw-r--r-- | Presence/NestingXML.hs | 36 |
1 files changed, 19 insertions, 17 deletions
diff --git a/Presence/NestingXML.hs b/Presence/NestingXML.hs index b90d0caa..5956b34d 100644 --- a/Presence/NestingXML.hs +++ b/Presence/NestingXML.hs | |||
@@ -2,28 +2,29 @@ module NestingXML where | |||
2 | 2 | ||
3 | import Data.Conduit | 3 | import Data.Conduit |
4 | import Data.XML.Types | 4 | import Data.XML.Types |
5 | import Control.Monad.Reader | 5 | import Control.Monad.State |
6 | -- import qualified Text.XML.Stream.Parse as Parse (content) | 6 | -- import qualified Text.XML.Stream.Parse as Parse (content) |
7 | 7 | ||
8 | type NestingXML o m a = ReaderT Int (ConduitM Event o m) a | 8 | type NestingXML o m a = StateT Int (ConduitM Event o m) a |
9 | 9 | ||
10 | runNestingXML :: NestingXML o m a -> Int -> ConduitM Event o m a | 10 | runNestingXML :: NestingXML o m a -> Int -> ConduitM Event o m (a,Int) |
11 | runNestingXML = runReaderT | 11 | runNestingXML = runStateT |
12 | 12 | ||
13 | doNestingXML :: NestingXML o m a -> ConduitM Event o m a | 13 | doNestingXML :: Monad m => NestingXML o m a -> ConduitM Event o m a |
14 | doNestingXML = flip runNestingXML 0 | 14 | doNestingXML = fmap fst . flip runNestingXML 0 |
15 | 15 | ||
16 | nesting :: Monad m => NestingXML o m Int | 16 | nesting :: Monad m => NestingXML o m Int |
17 | nesting = ask | 17 | nesting = get |
18 | 18 | ||
19 | awaitXML :: Monad m => NestingXML o m (Maybe Event) | 19 | awaitXML :: Monad m => NestingXML o m (Maybe Event) |
20 | awaitXML = do | 20 | awaitXML = do |
21 | lvl <- get | ||
21 | xml <- lift await | 22 | xml <- lift await |
22 | let f = case xml of | 23 | put $case xml of |
23 | Just (EventBeginElement _ _) -> (+1) | 24 | Just (EventBeginElement _ _) -> lvl+1 |
24 | Just (EventEndElement _) -> (subtract 1) | 25 | Just (EventEndElement _) -> lvl-1 |
25 | _ -> id | 26 | _ -> lvl |
26 | local f (return xml) | 27 | return xml |
27 | 28 | ||
28 | withXML | 29 | withXML |
29 | :: Monad m => (Event -> NestingXML o m ()) -> NestingXML o m () | 30 | :: Monad m => (Event -> NestingXML o m ()) -> NestingXML o m () |
@@ -31,30 +32,31 @@ withXML f = do | |||
31 | xml <- awaitXML | 32 | xml <- awaitXML |
32 | maybe (return ()) f xml | 33 | maybe (return ()) f xml |
33 | 34 | ||
35 | |||
34 | maybeXML | 36 | maybeXML |
35 | :: Monad m => NestingXML o m a -> (Event -> NestingXML o m a) -> NestingXML o m a | 37 | :: Monad m => NestingXML o m a -> (Event -> NestingXML o m a) -> NestingXML o m a |
36 | maybeXML whenNothing withJust = do | 38 | maybeXML whenNothing withJust = do |
37 | xml <- awaitXML | 39 | xml <- awaitXML |
38 | maybe whenNothing withJust xml | 40 | maybe whenNothing withJust xml |
39 | 41 | ||
40 | awaitCloser :: MonadIO m => Int -> NestingXML o m () | 42 | |
43 | awaitCloser :: Monad m => Int -> NestingXML o m () | ||
41 | awaitCloser lvl = do | 44 | awaitCloser lvl = do |
42 | fix $ \loop -> do | 45 | fix $ \loop -> do |
43 | lvl' <- nesting | 46 | lvl' <- nesting |
44 | when (lvl' >= lvl) $ do | 47 | when (lvl' >= lvl) $ do |
45 | withXML $ \xml -> do | 48 | withXML $ \xml -> do |
46 | liftIO $ putStrLn $ "awaitCloser: "++show (lvl',lvl,xml) | ||
47 | loop | 49 | loop |
48 | 50 | ||
49 | nextElement :: MonadIO m => NestingXML o m (Maybe Event) | 51 | nextElement :: Monad m => NestingXML o m (Maybe Event) |
50 | nextElement = do | 52 | nextElement = do |
51 | lvl <- nesting | 53 | lvl <- nesting |
52 | fix $ \loop -> do | 54 | fix $ \loop -> do |
53 | xml <- awaitXML | 55 | xml <- awaitXML |
54 | liftIO $ putStrLn $ "nextElement: "++show xml | ||
55 | case xml of | 56 | case xml of |
56 | Nothing -> return Nothing | 57 | Nothing -> return Nothing |
57 | Just (EventBeginElement _ _) -> return xml | 58 | Just (EventBeginElement _ _) -> do |
59 | return xml | ||
58 | Just _ -> do | 60 | Just _ -> do |
59 | lvl' <- nesting | 61 | lvl' <- nesting |
60 | if (lvl'>=lvl) then loop | 62 | if (lvl'>=lvl) then loop |