packages feed

hexpat 0.16 → 0.17

raw patch · 7 files changed

+141/−85 lines, 7 filesPVP ok

version bump matches the API change (PVP)

API changes (from Hackage documentation)

- Text.XML.Expat.Annotated: ParserOptions :: Maybe Encoding -> Maybe (tag -> Maybe text) -> ParserOptions tag text
- Text.XML.Expat.Annotated: data ParserOptions tag text
- Text.XML.Expat.Annotated: parserEncoding :: ParserOptions tag text -> Maybe Encoding
- Text.XML.Expat.SAX: ParserOptions :: Maybe Encoding -> Maybe (tag -> Maybe text) -> ParserOptions tag text
- Text.XML.Expat.SAX: data ParserOptions tag text
- Text.XML.Expat.SAX: parserEncoding :: ParserOptions tag text -> Maybe Encoding
- Text.XML.Expat.Tree: ParserOptions :: Maybe Encoding -> Maybe (tag -> Maybe text) -> ParserOptions tag text
- Text.XML.Expat.Tree: data ParserOptions tag text
- Text.XML.Expat.Tree: parserEncoding :: ParserOptions tag text -> Maybe Encoding
+ Text.XML.Expat.Annotated: ParseOptions :: Maybe Encoding -> Maybe (tag -> Maybe text) -> ParseOptions tag text
+ Text.XML.Expat.Annotated: data ParseOptions tag text
+ Text.XML.Expat.Annotated: defaultEncoding :: ParseOptions tag text -> Maybe Encoding
+ Text.XML.Expat.Annotated: defaultParseOptions :: ParseOptions tag text
+ Text.XML.Expat.Annotated: type ParserOptions tag text = ParseOptions tag text
+ Text.XML.Expat.Internal.NodeClass: fromNodeContainer :: (NodeClass n c, List c') => n c tag text -> ItemM c (n c' tag text)
+ Text.XML.Expat.Internal.NodeClass: fromNodeListContainer :: (NodeClass n c, List c') => c (n c tag text) -> ItemM c (c' (n c' tag text))
+ Text.XML.Expat.Internal.NodeClass: mapNodeListContainer :: (NodeClass n c, List c') => (forall a. c a -> ItemM c (c' a)) -> c (n c tag text) -> ItemM c (c' (n c' tag text))
+ Text.XML.Expat.Internal.NodeClass: modifyElement :: (NodeClass n c) => ((tag, [(tag, text)], c (n c tag text)) -> (tag', [(tag', text)], c (n c tag' text))) -> n c tag text -> n c tag' text
+ Text.XML.Expat.SAX: ParseOptions :: Maybe Encoding -> Maybe (tag -> Maybe text) -> ParseOptions tag text
+ Text.XML.Expat.SAX: data ParseOptions tag text
+ Text.XML.Expat.SAX: defaultEncoding :: ParseOptions tag text -> Maybe Encoding
+ Text.XML.Expat.SAX: defaultParseOptions :: ParseOptions tag text
+ Text.XML.Expat.SAX: type ParserOptions tag text = ParseOptions tag text
+ Text.XML.Expat.Tree: ParseOptions :: Maybe Encoding -> Maybe (tag -> Maybe text) -> ParseOptions tag text
+ Text.XML.Expat.Tree: data ParseOptions tag text
+ Text.XML.Expat.Tree: defaultEncoding :: ParseOptions tag text -> Maybe Encoding
+ Text.XML.Expat.Tree: defaultParseOptions :: ParseOptions tag text
+ Text.XML.Expat.Tree: type ParserOptions tag text = ParseOptions tag text
- Text.XML.Expat.Annotated: defaultParserOptions :: ParserOptions tag text
+ Text.XML.Expat.Annotated: defaultParserOptions :: ParseOptions tag text
- Text.XML.Expat.Annotated: entityDecoder :: ParserOptions tag text -> Maybe (tag -> Maybe text)
+ Text.XML.Expat.Annotated: entityDecoder :: ParseOptions tag text -> Maybe (tag -> Maybe text)
- Text.XML.Expat.Annotated: parse :: (GenericXMLString tag, GenericXMLString text) => ParserOptions tag text -> ByteString -> (LNode tag text, Maybe XMLParseError)
+ Text.XML.Expat.Annotated: parse :: (GenericXMLString tag, GenericXMLString text) => ParseOptions tag text -> ByteString -> (LNode tag text, Maybe XMLParseError)
- Text.XML.Expat.Annotated: parse' :: (GenericXMLString tag, GenericXMLString text) => ParserOptions tag text -> ByteString -> Either XMLParseError (LNode tag text)
+ Text.XML.Expat.Annotated: parse' :: (GenericXMLString tag, GenericXMLString text) => ParseOptions tag text -> ByteString -> Either XMLParseError (LNode tag text)
- Text.XML.Expat.Annotated: parseThrowing :: (GenericXMLString tag, GenericXMLString text) => ParserOptions tag text -> ByteString -> LNode tag text
+ Text.XML.Expat.Annotated: parseThrowing :: (GenericXMLString tag, GenericXMLString text) => ParseOptions tag text -> ByteString -> LNode tag text
- Text.XML.Expat.Internal.NodeClass: mapNodeContainer :: (NodeClass n c) => (c (n c tag text) -> ItemM c (c' (n c' tag text))) -> n c tag text -> ItemM c (n c' tag text)
+ Text.XML.Expat.Internal.NodeClass: mapNodeContainer :: (NodeClass n c, List c') => (forall a. c a -> ItemM c (c' a)) -> n c tag text -> ItemM c (n c' tag text)
- Text.XML.Expat.SAX: defaultParserOptions :: ParserOptions tag text
+ Text.XML.Expat.SAX: defaultParserOptions :: ParseOptions tag text
- Text.XML.Expat.SAX: entityDecoder :: ParserOptions tag text -> Maybe (tag -> Maybe text)
+ Text.XML.Expat.SAX: entityDecoder :: ParseOptions tag text -> Maybe (tag -> Maybe text)
- Text.XML.Expat.SAX: parse :: (GenericXMLString tag, GenericXMLString text) => ParserOptions tag text -> ByteString -> [SAXEvent tag text]
+ Text.XML.Expat.SAX: parse :: (GenericXMLString tag, GenericXMLString text) => ParseOptions tag text -> ByteString -> [SAXEvent tag text]
- Text.XML.Expat.SAX: parseLocations :: (GenericXMLString tag, GenericXMLString text) => ParserOptions tag text -> ByteString -> [(SAXEvent tag text, XMLParseLocation)]
+ Text.XML.Expat.SAX: parseLocations :: (GenericXMLString tag, GenericXMLString text) => ParseOptions tag text -> ByteString -> [(SAXEvent tag text, XMLParseLocation)]
- Text.XML.Expat.SAX: parseLocationsThrowing :: (GenericXMLString tag, GenericXMLString text) => ParserOptions tag text -> ByteString -> [(SAXEvent tag text, XMLParseLocation)]
+ Text.XML.Expat.SAX: parseLocationsThrowing :: (GenericXMLString tag, GenericXMLString text) => ParseOptions tag text -> ByteString -> [(SAXEvent tag text, XMLParseLocation)]
- Text.XML.Expat.SAX: parseThrowing :: (GenericXMLString tag, GenericXMLString text) => ParserOptions tag text -> ByteString -> [SAXEvent tag text]
+ Text.XML.Expat.SAX: parseThrowing :: (GenericXMLString tag, GenericXMLString text) => ParseOptions tag text -> ByteString -> [SAXEvent tag text]
- Text.XML.Expat.Tree: defaultParserOptions :: ParserOptions tag text
+ Text.XML.Expat.Tree: defaultParserOptions :: ParseOptions tag text
- Text.XML.Expat.Tree: entityDecoder :: ParserOptions tag text -> Maybe (tag -> Maybe text)
+ Text.XML.Expat.Tree: entityDecoder :: ParseOptions tag text -> Maybe (tag -> Maybe text)
- Text.XML.Expat.Tree: parse :: (GenericXMLString tag, GenericXMLString text) => ParserOptions tag text -> ByteString -> (Node tag text, Maybe XMLParseError)
+ Text.XML.Expat.Tree: parse :: (GenericXMLString tag, GenericXMLString text) => ParseOptions tag text -> ByteString -> (Node tag text, Maybe XMLParseError)
- Text.XML.Expat.Tree: parse' :: (GenericXMLString tag, GenericXMLString text) => ParserOptions tag text -> ByteString -> Either XMLParseError (Node tag text)
+ Text.XML.Expat.Tree: parse' :: (GenericXMLString tag, GenericXMLString text) => ParseOptions tag text -> ByteString -> Either XMLParseError (Node tag text)
- Text.XML.Expat.Tree: parseThrowing :: (GenericXMLString tag, GenericXMLString text) => ParserOptions tag text -> ByteString -> Node tag text
+ Text.XML.Expat.Tree: parseThrowing :: (GenericXMLString tag, GenericXMLString text) => ParseOptions tag text -> ByteString -> Node tag text

Files

Text/XML/Expat/Annotated.hs view
@@ -31,8 +31,8 @@   module Text.XML.Expat.Internal.Namespaced,    -- * Parse to tree-  ParserOptions(..),-  defaultParserOptions,+  ParseOptions(..),+  defaultParseOptions,   Encoding(..),   parse,   parse',@@ -59,15 +59,17 @@   parseTree,   parseTree',   parseTreeThrowing,-  unannotate+  unannotate,+  ParserOptions,+  defaultParserOptions   ) where  import Control.Arrow import qualified Text.XML.Expat.Tree as Tree import Text.XML.Expat.SAX ( Encoding(..)                           , GenericXMLString(..)-                          , ParserOptions(..)-                          , defaultParserOptions+                          , ParseOptions(..)+                          , defaultParseOptions                           , SAXEvent(..)                           , XMLParseError(..)                           , XMLParseException(..)@@ -75,7 +77,9 @@                           , parseSAX                           , parseSAXThrowing                           , parseSAXLocations-                          , parseSAXLocationsThrowing )+                          , parseSAXLocationsThrowing+                          , ParserOptions+                          , defaultParserOptions ) import qualified Text.XML.Expat.SAX as SAX import Text.XML.Expat.Internal.Namespaced import Text.XML.Expat.Internal.NodeClass@@ -188,13 +192,13 @@     mapAllTags _ (Text t) = Text t     mapAllTags f (Element n a c ann) = Element (f n) (map (first f) a) (fmap (mapAllTags f) c) ann -    mapElement _ (Text t) = Text t-    mapElement f (Element n a c ann) =+    modifyElement _ (Text t) = Text t+    modifyElement f (Element n a c ann) =         let (n', a', c') = f (n, a, c)         in  Element n' a' c' ann      mapNodeContainer f (Element n a ch an) = do-        ch' <- f ch+        ch' <- mapNodeListContainer f ch         return $ Element n a ch' an     mapNodeContainer _ (Text t) = return $ Text t @@ -270,7 +274,7 @@ -- will force the entire parse.  Therefore, to ensure lazy operation, don't -- check the error status until you have processed the tree. parse :: (GenericXMLString tag, GenericXMLString text) =>-         ParserOptions tag text   -- ^ Optional encoding override+         ParseOptions tag text   -- ^ Optional encoding override       -> L.ByteString             -- ^ Input text (a lazy ByteString)       -> (LNode tag text, Maybe XMLParseError) parse opts bs = saxToTree $ SAX.parseLocations opts bs@@ -285,7 +289,7 @@           -> L.ByteString        -- ^ Input text (a lazy ByteString)           -> (LNode tag text, Maybe XMLParseError) {-# DEPRECATED parseTree "use Text.XML.Annotated.parse instead" #-}-parseTree mEnc = parse (ParserOptions mEnc Nothing)+parseTree mEnc = parse (ParseOptions mEnc Nothing)  -- | Lazily parse XML to tree. In the event of an error, throw 'XMLParseException'. --@@ -295,7 +299,7 @@ -- situations where it's not expected during normal operation, depending on the -- design of your program. parseThrowing :: (GenericXMLString tag, GenericXMLString text) =>-                 ParserOptions tag text   -- ^ Optional encoding override+                 ParseOptions tag text   -- ^ Optional encoding override               -> L.ByteString             -- ^ Input text (a lazy ByteString)               -> LNode tag text parseThrowing opts bs = fst $ saxToTree $ SAX.parseLocationsThrowing opts bs@@ -308,11 +312,11 @@           -> L.ByteString        -- ^ Input text (a lazy ByteString)           -> LNode tag text {-# DEPRECATED parseTreeThrowing "use Text.XML.Annotated.parseThrowing instead" #-}-parseTreeThrowing mEnc = parseThrowing (ParserOptions mEnc Nothing)+parseTreeThrowing mEnc = parseThrowing (ParseOptions mEnc Nothing)  -- | Strictly parse XML to tree. Returns error message or valid parsed tree. parse' :: (GenericXMLString tag, GenericXMLString text) =>-          ParserOptions tag text  -- ^ Optional encoding override+          ParseOptions tag text  -- ^ Optional encoding override        -> B.ByteString            -- ^ Input text (a strict ByteString)        -> Either XMLParseError (LNode tag text) parse' opts bs = case parse opts (L.fromChunks [bs]) of@@ -327,5 +331,5 @@            -> B.ByteString        -- ^ Input text (a strict ByteString)            -> Either XMLParseError (LNode tag text) {-# DEPRECATED parseTree' "use Text.XML.Expat.parse' instead" #-}-parseTree' mEnc = parse' (ParserOptions mEnc Nothing)+parseTree' mEnc = parse' (ParseOptions mEnc Nothing) 
Text/XML/Expat/Internal/Namespaced.hs view
@@ -81,7 +81,7 @@  nodeWithNamespaces :: (NodeClass n c, GenericXMLString text, Ord text, Show text)                    => NsPrefixMap text -> n c (QName text) text -> n c (NName text) text-nodeWithNamespaces bindings = mapElement namespaceify+nodeWithNamespaces bindings = modifyElement namespaceify   where     namespaceify (qname, qattrs, qchildren) = (nname, nattrs, nchildren)       where@@ -123,7 +123,7 @@                    -> PrefixNsMap text                    -> n c (NName text) text                    -> n c (QName text) text-nodeWithQualifiers cntr bindings = mapElement namespaceify+nodeWithQualifiers cntr bindings = modifyElement namespaceify   where     namespaceify (nname, nattrs, nchildren) = (qname, qattrs, qchildren)        where
Text/XML/Expat/Internal/NodeClass.hs view
@@ -1,10 +1,11 @@-{-# LANGUAGE MultiParamTypeClasses, FlexibleContexts, TypeFamilies #-}+{-# LANGUAGE MultiParamTypeClasses, FlexibleContexts, TypeFamilies,+        ScopedTypeVariables, Rank2Types #-} -- | Type classes to allow for XML handling functions to be generalized to -- work with different node types, including the ones defined in /Tree/ and -- /Annotated/. module Text.XML.Expat.Internal.NodeClass where -import Control.Monad (mzero)+import Control.Monad (mzero, liftM) import Data.Functor.Identity import Data.List.Class import Data.Monoid (Monoid)@@ -69,24 +70,61 @@                    -> n c tag text                    -> n c tag text +    -- | Map an element non-recursively, allowing the tag type to be changed.+    modifyElement :: ((tag, [(tag, text)], c (n c tag text))+                  -> (tag', [(tag', text)], c (n c tag' text)))+                  -> n c tag text+                  -> n c tag' text+     -- | Map all tags (both tag names and attribute names) recursively.     mapAllTags :: (tag -> tag')                -> n c tag text                -> n c tag' text -    -- | Map an element non-recursively, allowing the tag type to be changed.-    mapElement :: ((tag, [(tag, text)], c (n c tag text))-                       -> (tag', [(tag', text)], c (n c tag' text)))-                  -> n c tag text-                  -> n c tag' text--    -- | Change a node from one container type to another.-    mapNodeContainer :: (c (n c tag text) -> ItemM c (c' (n c' tag text)))+    -- | Change a node recursively from one container type to another, with a+    -- specified function to convert the container type.+    mapNodeContainer :: List c' => +                        (forall a . c a -> ItemM c (c' a))                      -> n c tag text                      -> ItemM c (n c' tag text)      -- | Generic text node constructor.     mkText :: text -> n c tag text++-- | DEPRECATED. Map an element non-recursively, allowing the tag type to be changed.+mapElement :: NodeClass n c =>+              ((tag, [(tag, text)], c (n c tag text))+           -> (tag', [(tag', text)], c (n c tag' text)))+           -> n c tag text+           -> n c tag' text+{-# DEPRECATED mapElement "renamed to modifyElement" #-}+mapElement = modifyElement++-- | Change a list of nodes recursively from one container type to another, with+-- a specified function to convert the container type.+mapNodeListContainer :: (NodeClass n c, List c') =>+                        (forall a . c a -> ItemM c (c' a))+                     -> c (n c tag text)+                     -> ItemM c (c' (n c' tag text))+mapNodeListContainer f = f . mapL (mapNodeContainer f)++-- | Change a node recursively from one container type to another.  This+-- extracts the entire tree contents to standard lists and re-constructs them+-- with the new container type.  For monadic list types used in+-- @hexpat-iteratee@ this operation forces evaluation. +fromNodeContainer :: (NodeClass n c, List c') => +                     n c tag text+                  -> ItemM c (n c' tag text)+fromNodeContainer = mapNodeContainer  (\l -> fromList `liftM` toList l)++-- | Change a list of nodes recursively from one container type to another.  This+-- extracts the entire tree contents to standard lists and re-constructs them+-- with the new container type.  For monadic list types used in+-- @hexpat-iteratee@ this operation forces evaluation.+fromNodeListContainer :: (NodeClass n c, List c') =>+                         c (n c tag text)+                      -> ItemM c (c' (n c' tag text))+fromNodeListContainer = mapNodeListContainer  (\l -> fromList `liftM` toList l)  -- | A class of node types where an Element can be constructed given a tag, -- attributes and children.
Text/XML/Expat/SAX.hs view
@@ -13,7 +13,7 @@   XMLParseLocation(..),    -- * SAX-style parse-  ParserOptions(..),+  ParseOptions(..),   SAXEvent(..),    textFromCString,@@ -21,22 +21,24 @@   parseLocations,   parseLocationsThrowing,   parseThrowing,-  defaultParserOptions,+  defaultParseOptions,    -- * Variants that throw exceptions   XMLParseException(..), +  -- * Helpers+  setEntityDecoder,++  -- * Abstraction of string types+  GenericXMLString(..),+   -- * Deprecated parse functions   parseSAX,   parseSAXLocations,   parseSAXLocationsThrowing,   parseSAXThrowing,--  -- * Helpers-  setEntityDecoder,--  -- * Abstraction of string types-  GenericXMLString(..)+  ParserOptions,+  defaultParserOptions   ) where  import Text.XML.Expat.Internal.IO hiding (parse)@@ -59,8 +61,8 @@ import Foreign.Ptr  -data ParserOptions tag text = ParserOptions-    { parserEncoding :: Maybe Encoding+data ParseOptions tag text = ParseOptions+    { defaultEncoding :: Maybe Encoding           -- ^ The encoding parameter, if provided, overrides the document's           -- encoding declaration.     , entityDecoder  :: Maybe (tag -> Maybe text)@@ -68,10 +70,18 @@           -- be decoded into text using the supplied lookup function     } -defaultParserOptions :: ParserOptions tag text-defaultParserOptions = ParserOptions Nothing Nothing+{-# DEPRECATED ParserOptions "renamed to ParseOptions" #-}+type ParserOptions tag text = ParseOptions tag text +defaultParseOptions :: ParseOptions tag text+defaultParseOptions = ParseOptions Nothing Nothing +-- | DEPRECATED. Renamed to defaultParseOptions.+defaultParserOptions :: ParseOptions tag text+{-# DEPRECATED defaultParserOptions "renamed to defaultParseOptions" #-}+defaultParserOptions = defaultParseOptions++ -- | An abstraction for any string type you want to use as xml text (that is, -- attribute values or element text content). If you want to use a -- new string type with /hexpat/, you must make it an instance of@@ -181,11 +191,11 @@ -- | Lazily parse XML to SAX events. In the event of an error, FailDocument is -- the last element of the output list. parse :: (GenericXMLString tag, GenericXMLString text) =>-         ParserOptions tag text -- ^ Parser options+         ParseOptions tag text -- ^ Parser options       -> L.ByteString           -- ^ Input text (a lazy ByteString)       -> [SAXEvent tag text] parse opts input = unsafePerformIO $ do-    let enc = parserEncoding opts+    let enc = defaultEncoding opts     let mEntityDecoder = entityDecoder opts      parser <- newParser enc@@ -244,7 +254,7 @@          -> L.ByteString        -- ^ Input text (a lazy ByteString)          -> [SAXEvent tag text] {-# DEPRECATED parseSAX "use Text.XML.Expat.SAX.parse instead" #-}-parseSAX enc = parse (ParserOptions enc Nothing)+parseSAX enc = parse (ParseOptions enc Nothing)   -- | An exception indicating an XML parse error, used by the /..Throwing/ variants.@@ -256,11 +266,11 @@  -- | A variant of parseSAX that gives a document location with each SAX event. parseLocations :: (GenericXMLString tag, GenericXMLString text) =>-                  ParserOptions tag text  -- ^ Parser options+                  ParseOptions tag text  -- ^ Parser options                -> L.ByteString            -- ^ Input text (a lazy ByteString)                -> [(SAXEvent tag text, XMLParseLocation)] parseLocations opts input = unsafePerformIO $ do-    let enc = parserEncoding opts+    let enc = defaultEncoding opts     let mEntityDecoder = entityDecoder opts      -- Done with cut & paste coding for maximum speed.@@ -326,7 +336,7 @@          -> L.ByteString        -- ^ Input text (a lazy ByteString)          -> [(SAXEvent tag text, XMLParseLocation)] {-# DEPRECATED parseSAXLocations "use Text.XML.Expat.SAX.parseLocations instead" #-}-parseSAXLocations enc = parseLocations (ParserOptions enc Nothing)+parseSAXLocations enc = parseLocations (ParseOptions enc Nothing)   -- | Lazily parse XML to SAX events. In the event of an error, throw@@ -338,7 +348,7 @@ -- situations where it's not expected during normal operation, depending on the -- design of your program. parseThrowing :: (GenericXMLString tag, GenericXMLString text) =>-                 ParserOptions tag text  -- ^ Parser options+                 ParseOptions tag text  -- ^ Parser options               -> L.ByteString            -- ^ input text (a lazy ByteString)               -> [SAXEvent tag text] parseThrowing opts bs = map freakOut $ parse opts bs@@ -356,7 +366,7 @@                  -> L.ByteString        -- ^ Input text (a lazy ByteString)                  -> [SAXEvent tag text] {-# DEPRECATED parseSAXThrowing "use Text.XML.Expat.SAX.parseThrowing instead" #-}-parseSAXThrowing mEnc = parseThrowing (ParserOptions mEnc Nothing)+parseSAXThrowing mEnc = parseThrowing (ParseOptions mEnc Nothing)   -- | A variant of parseSAX that gives a document location with each SAX event.@@ -368,7 +378,7 @@ -- situations where it's not expected during normal operation, depending on the -- design of your program. parseLocationsThrowing :: (GenericXMLString tag, GenericXMLString text) =>-                          ParserOptions tag text  -- ^ Optional encoding override+                          ParseOptions tag text  -- ^ Optional encoding override                        -> L.ByteString            -- ^ Input text (a lazy ByteString)                        -> [(SAXEvent tag text, XMLParseLocation)] parseLocationsThrowing opts bs = map freakOut $ parseLocations opts bs@@ -387,4 +397,4 @@                           -> [(SAXEvent tag text, XMLParseLocation)] {-# DEPRECATED parseSAXLocationsThrowing "use Text.XML.Expat.SAX.parseLocationsThrowing instead" #-} parseSAXLocationsThrowing mEnc =-    parseLocationsThrowing (ParserOptions mEnc Nothing)+    parseLocationsThrowing (ParseOptions mEnc Nothing)
Text/XML/Expat/Tree.hs view
@@ -36,7 +36,7 @@ -- >     inputText <- L.readFile filename -- >     -- Note: Because we're not using the tree, Haskell can't infer the type of -- >     -- strings we're using so we need to tell it explicitly with a type signature.--- >     let (xml, mErr) = parse defaultParserOptions inputText :: (UNode String, Maybe XMLParseError)+-- >     let (xml, mErr) = parse defaultParseOptions inputText :: (UNode String, Maybe XMLParseError) -- >     -- Process document before handling error, so we get lazy processing. -- >     L.hPutStr stdout $ format xml -- >     putStrLn ""@@ -46,7 +46,7 @@ -- >             hPutStrLn stderr $ "XML parse failed: "++show err -- >             exitWith $ ExitFailure 2 ----- Error handling in strict parses is very straight forward - just check the+-- Error handling in strict parses is very straightforward - just check the -- 'Either' return value.  Lazy parses are not so simple.  Here are two working -- examples that illustrate the ways to handle errors.  Here they are: --@@ -58,7 +58,7 @@ -- > -- > -- This is the recommended way to handle errors in lazy parses -- > main = do--- >     let (tree, mError) = parse defaultParserOptions+-- >     let (tree, mError) = parse defaultParseOptions -- >                    (L.pack $ map c2w $ "<top><banana></apple></top>") -- >     print (tree :: UNode String) -- >@@ -82,7 +82,7 @@ -- > -- This is not the recommended way to handle errors. -- > main = do -- >     do--- >         let tree = parseThrowing defaultParserOptions+-- >         let tree = parseThrowing defaultParseOptions -- >                        (L.pack $ map c2w $ "<top><banana></apple></top>") -- >         print (tree :: UNode String) -- >         -- Because of lazy evaluation, you should not process the tree outside@@ -110,8 +110,8 @@   module Text.XML.Expat.Internal.Namespaced,    -- * Parse to tree-  ParserOptions(..),-  defaultParserOptions,+  ParseOptions(..),+  defaultParseOptions,   Encoding(..),   parse,   parse',@@ -141,22 +141,26 @@   parseSAXLocations,   parseTreeThrowing,   parseSAXThrowing,-  parseSAXLocationsThrowing+  parseSAXLocationsThrowing,+  ParserOptions,+  defaultParserOptions   ) where  import Text.XML.Expat.Internal.IO hiding (parse,parse') import qualified Text.XML.Expat.Internal.IO as IO-import Text.XML.Expat.SAX ( ParserOptions(..)+import Text.XML.Expat.SAX ( ParseOptions(..)                           , XMLParseException(..)                           , SAXEvent(..)-                          , defaultParserOptions+                          , defaultParseOptions                           , textFromCString                           , parseSAX                           , parseSAXLocations                           , parseSAXLocationsThrowing                           , parseSAXThrowing                           , GenericXMLString(..)-                          , setEntityDecoder )+                          , setEntityDecoder+                          , ParserOptions+                          , defaultParserOptions ) import qualified Text.XML.Expat.SAX as SAX import Text.XML.Expat.Internal.Namespaced import Text.XML.Expat.Internal.NodeClass@@ -293,13 +297,13 @@     mapAllTags _ (Text t) = Text t     mapAllTags f (Element n a c) = Element (f n) (map (first f) a) (fmap (mapAllTags f) c) -    mapElement _ (Text t) = Text t-    mapElement f (Element n a c) =+    modifyElement _ (Text t) = Text t+    modifyElement f (Element n a c) =         let (n', a', c') = f (n, a, c)         in  Element n' a' c'      mapNodeContainer f (Element n a ch) = do-        ch' <- f ch+        ch' <- mapNodeListContainer f ch         return $ Element n a ch'     mapNodeContainer _ (Text t) = return $ Text t @@ -310,12 +314,12 @@  -- | Strictly parse XML to tree. Returns error message or valid parsed tree. parse' :: (GenericXMLString tag, GenericXMLString text) =>-          ParserOptions tag text  -- ^ Parser options+          ParseOptions tag text  -- ^ Parser options        -> ByteString              -- ^ Input text (a strict ByteString)        -> Either XMLParseError (Node tag text) parse' opts doc = unsafePerformIO $ runParse where   runParse = do-    let enc = parserEncoding opts+    let enc = defaultEncoding opts     let mEntityDecoder = entityDecoder opts      parser <- newParser enc@@ -370,7 +374,7 @@            -> ByteString          -- ^ Input text (a strict ByteString)            -> Either XMLParseError (Node tag text) {-# DEPRECATED parseTree' "use Text.XML.Expat.parse' instead" #-}-parseTree' enc = parse' (ParserOptions enc Nothing)+parseTree' enc = parse' (ParseOptions enc Nothing)   -- | A lower level function that lazily converts a SAX stream into a tree structure.@@ -400,7 +404,7 @@ -- will force the entire parse.  Therefore, to ensure lazy operation, don't -- check the error status until you have processed the tree. parse :: (GenericXMLString tag, GenericXMLString text) =>-         ParserOptions tag text   -- ^ Parser options+         ParseOptions tag text   -- ^ Parser options       -> L.ByteString             -- ^ Input text (a lazy ByteString)       -> (Node tag text, Maybe XMLParseError) parse opts bs = saxToTree $ SAX.parse opts bs@@ -416,7 +420,7 @@           -> L.ByteString        -- ^ Input text (a lazy ByteString)           -> (Node tag text, Maybe XMLParseError) {-# DEPRECATED parseTree "use Text.XML.Expat.Tree.parse instead" #-}-parseTree mEnc = parse (ParserOptions mEnc Nothing)+parseTree mEnc = parse (ParseOptions mEnc Nothing)   -- | Lazily parse XML to tree. In the event of an error, throw 'XMLParseException'.@@ -427,7 +431,7 @@ -- situations where it's not expected during normal operation, depending on the -- design of your program. parseThrowing :: (GenericXMLString tag, GenericXMLString text) =>-         ParserOptions tag text -- ^ Parser options+         ParseOptions tag text -- ^ Parser options       -> L.ByteString           -- ^ Input text (a lazy ByteString)       -> Node tag text parseThrowing opts bs = fst $ saxToTree $ SAX.parseThrowing opts bs@@ -441,4 +445,4 @@           -> L.ByteString        -- ^ Input text (a lazy ByteString)           -> Node tag text {-# DEPRECATED parseTreeThrowing "use Text.XML.Expat.Tree.parseThrowing instead" #-}-parseTreeThrowing mEnc = parseThrowing (ParserOptions mEnc Nothing)+parseTreeThrowing mEnc = parseThrowing (ParseOptions mEnc Nothing)
hexpat.cabal view
@@ -1,6 +1,6 @@ Cabal-Version: >= 1.6 Name: hexpat-Version: 0.16+Version: 0.17 Synopsis: XML parser/formatter based on expat Description:   This package provides a general purpose Haskell XML library using Expat to@@ -41,7 +41,7 @@   .   ChangeLog: 0.15 changes intended to fix a (rare) \"error: a C finalizer called back into Haskell.\"     that seemed only to happen only on ghc6.12.X; 0.15.1 Fix broken Annotated parse;-    0.16 switch from mtl to transformers+    0.16 switch from mtl to transformers; 0.17 fix mapNodeContainer & rename some things. Category: XML License: BSD3 License-File: LICENSE
test/suite/Text/XML/Expat/UnitTests.hs view
@@ -38,7 +38,7 @@ fromByteString = map w2c . B.unpack  testDoc :: (Show tag, Show text) =>-           (ParserOptions tag text+           (ParseOptions tag text                 -> bs                 -> Either XMLParseError (Node tag text))         -> (Node tag text -> L.ByteString)@@ -58,7 +58,7 @@             hPutStrLn stderr $ "parse failed: "++show error             assertFailure descr   where-    popts = defaultParserOptions { parserEncoding = Just UTF8 }+    popts = defaultParseOptions { defaultEncoding = Just UTF8 }   eitherify f mEnc bs = do@@ -68,7 +68,7 @@  test_error1 :: IO () test_error1 = do-    let eDoc = Tree.parse' defaultParserOptions (toByteString "<hello></goodbye>") :: Either XMLParseError (UNode String)+    let eDoc = Tree.parse' defaultParseOptions (toByteString "<hello></goodbye>") :: Either XMLParseError (UNode String)     assertEqual "error1" (Left $ XMLParseError "mismatched tag" (XMLParseLocation 1 9 9 0)) eDoc  test_error2 :: IO ()@@ -76,7 +76,7 @@     assertEqual "error2" (             Element {eName = "hello", eAttributes = [], eChildren = []},             Just (XMLParseError "mismatched tag" (XMLParseLocation 1 9 9 0))-        ) (Tree.parse defaultParserOptions+        ) (Tree.parse defaultParseOptions               (toByteStringL "<hello></goodbye>") :: (UNode String, Maybe XMLParseError))  test_error3 :: IO ()@@ -87,12 +87,12 @@                 Element {eName = "hello", eAttributes = [], eChildren = []}             ]},             Just (XMLParseError "mismatched tag" (XMLParseLocation 1 35 35 0))-        ) $ Tree.parse defaultParserOptions+        ) $ Tree.parse defaultParseOptions               (toByteStringL "<open><test1>Hello</test1><hello></goodbye>")  test_error4 :: IO () test_error4 = do-    let eDoc = Tree.parse' defaultParserOptions (toByteString "!") :: Either XMLParseError (UNode String)+    let eDoc = Tree.parse' defaultParseOptions (toByteString "!") :: Either XMLParseError (UNode String)     assertEqual "error1" (Left $ XMLParseError "not well-formed (invalid token)"         (XMLParseLocation 1 0 0 0)) eDoc @@ -126,7 +126,7 @@   where     xml = "<root>&entity;</root>" -    popts = defaultParserOptions { entityDecoder = Just entityLookup }+    popts = defaultParseOptions { entityDecoder = Just entityLookup }      (tree,merr) = Tree.parse popts $ toByteStringL xml @@ -184,7 +184,7 @@                      "\n  <wisdom>\n    <ignorance>strength</ignorance>\n  </wisdom>\n</test>")             ]     forM_ tests $ \(name, inp, outSB) -> do-        let eree = Tree.parse' defaultParserOptions inp :: Either XMLParseError (UNode String)+        let eree = Tree.parse' defaultParseOptions inp :: Either XMLParseError (UNode String)         case eree of             Left err -> assertFailure $ show err             Right tree -> do@@ -229,32 +229,32 @@ tests = hUnitTestToTests $     TestList [         t' ("String",-            Tree.parse' :: ParserOptions String String+            Tree.parse' :: ParseOptions String String                         -> B.ByteString                         -> Either XMLParseError (Node String String),             formatTree),         t' ("ByteString",-            Tree.parse' :: ParserOptions B.ByteString B.ByteString+            Tree.parse' :: ParseOptions B.ByteString B.ByteString                         -> B.ByteString                         -> Either XMLParseError (Node B.ByteString B.ByteString),             formatTree),         t' ("Text",-            Tree.parse' :: ParserOptions T.Text T.Text+            Tree.parse' :: ParseOptions T.Text T.Text                         -> B.ByteString                         -> Either XMLParseError (Node T.Text T.Text),             formatTree),         t ("String/Lazy",-            eitherify $ Tree.parse :: ParserOptions String String+            eitherify $ Tree.parse :: ParseOptions String String                                    -> L.ByteString                                    -> Either XMLParseError (Node String String),             formatTree),         t ("ByteString/Lazy",-            eitherify $ Tree.parse :: ParserOptions B.ByteString B.ByteString+            eitherify $ Tree.parse :: ParseOptions B.ByteString B.ByteString                                    -> L.ByteString                                    -> Either XMLParseError (Node B.ByteString B.ByteString),             formatTree),         t ("Text/Lazy",-            eitherify $ Tree.parse :: ParserOptions T.Text T.Text+            eitherify $ Tree.parse :: ParseOptions T.Text T.Text                                    -> L.ByteString                                    -> Either XMLParseError (Node T.Text T.Text),             formatTree),