-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | XML parser/formatter based on expat -- -- This package provides a general purpose Haskell XML library using -- Expat to do its parsing (http://expat.sourceforge.net/ - a fast -- stream-oriented XML parser written in C). It is extensible to any -- string type, with String, ByteString and -- Text provided out of the box. -- -- Basic usage: Parsing a tree (Tree), formatting a tree -- (Format). Other features: Helpers for processing XML trees -- (Proc), trees annotated with XML source location -- (Annotated), XML cursors (Cursor), SAX-style parse -- (SAX), and access to the low-level interface in case speed is -- paramount (Internal.IO). -- -- The design goals are speed, speed, speed, interface simplicity and -- modularity. -- -- For introduction and examples, see the Text.XML.Expat.Tree -- module. For benchmarks, http://haskell.org/haskellwiki/Hexpat/ -- -- If you want to do interactive I/O, an obvious option is to use lazy -- parsing with one of the lazy I/O functions such as hGetContents. -- However, this can be problematic in some applications because it -- doesn't handle I/O errors properly and can give no guarantee of timely -- resource cleanup. In these cases, chunked I/O is a better approach: -- Take a look at the hexpat-iteratee package. -- -- IO is filed under Internal because it's low-level and -- most users won't want it. The other Internal modules are -- re-exported by Annotated and Tree, so you won't need to -- import them directly. -- -- Credits to Iavor Diatchki and the xml (XML.Light) package for -- Proc and Cursor. -- -- INSTALLATION: Unix install requires an OS package called something -- like libexpat-dev. On MacOSX, expat comes with Apple's -- optional X11 package, or you can install it from source. To install on -- Windows, first install the Windows binary that's available from -- http://expat.sourceforge.net/, then type (assuming you're using -- v2.0.1): -- --
-- cabal install hexpat --extra-lib-dirs=C:\Program Files\Expat 2.0.1\Bin --extra-include-dirs=C:\Program Files\Expat 2.0.1\Source\Lib ---- -- Ensure libexpat.dll can be found in your system PATH (or copy -- it into your executable's directory). -- -- 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 @package hexpat @version 0.16 -- | Low-level interface to Expat. Unless speed is paramount, this should -- normally be avoided in favour of the interfaces provided by -- Text.XML.Expat.SAX and Text.XML.Expat.Tree. Basic usage is: -- --
-- -- | A "hello world" example of hexpat that lazily parses a document, printing -- -- it to standard out. -- -- import Text.XML.Expat.Tree -- import Text.XML.Expat.Format -- import System.Environment -- import System.Exit -- import System.IO -- import qualified Data.ByteString.Lazy as L -- -- main = do -- args <- getArgs -- case args of -- [filename] -> process filename -- otherwise -> do -- hPutStrLn stderr "Usage: helloworld <file.xml>" -- exitWith $ ExitFailure 1 -- -- process :: String -> IO () -- process filename = do -- 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) -- -- Process document before handling error, so we get lazy processing. -- L.hPutStr stdout $ format xml -- putStrLn "" -- case mErr of -- Nothing -> return () -- Just err -> do -- hPutStrLn stderr $ "XML parse failed: "++show err -- exitWith $ ExitFailure 2 ---- -- Error handling in strict parses is very straight forward - 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: -- -- Way no. 1 - Using a Maybe value -- --
-- import Text.XML.Expat.Tree -- import qualified Data.ByteString.Lazy as L -- import Data.ByteString.Internal (c2w) -- -- -- This is the recommended way to handle errors in lazy parses -- main = do -- let (tree, mError) = parse defaultParserOptions -- (L.pack $ map c2w $ "<top><banana></apple></top>") -- print (tree :: UNode String) -- -- -- Note: We check the error _after_ we have finished our processing -- -- on the tree. -- case mError of -- Just err -> putStrLn $ "It failed : "++show err -- Nothing -> putStrLn "Success!" ---- -- Way no. 2 - Using exceptions -- -- parseThrowing can throw an exception from pure code, which is -- generally a bad way to handle errors, because Haskell's lazy -- evaluation means it's hard to predict where it will be thrown from. -- However, it may be acceptable in situations where it's not expected -- during normal operation, depending on the design of your program. -- --
-- ... -- import Control.Exception.Extensible as E -- -- -- This is not the recommended way to handle errors. -- main = do -- do -- let tree = parseThrowing defaultParserOptions -- (L.pack $ map c2w $ "<top><banana></apple></top>") -- print (tree :: UNode String) -- -- Because of lazy evaluation, you should not process the tree outside -- -- the 'do' block, or exceptions could be thrown that won't get caught. -- `E.catch` (\exc -> -- case E.fromException exc of -- Just (XMLParseException err) -> putStrLn $ "It failed : "++show err -- Nothing -> E.throwIO exc) --module Text.XML.Expat.Tree -- | A pure tree representation that uses a list as its container type. -- -- In the hexpat package, a list of nodes has the type [Node -- tag text], but note that you can also use the more general type -- function ListOf to give a list of any node type, using that -- node's associated list type, e.g. ListOf (UNode Text). type Node tag text = NodeG [] tag text -- | The tree representation of the XML document. -- -- c is the container type for the element's children, which is -- [] in the hexpat package, and a monadic list type for -- hexpat-iteratee. -- -- tag is the tag type, which can either be one of several -- string types, or a special type from the -- Text.XML.Expat.Namespaced or -- Text.XML.Expat.Qualified modules. -- -- text is the string type for text content. data NodeG c tag text Element :: !tag -> ![(tag, text)] -> c (NodeG c tag text) -> NodeG c tag text eName :: NodeG c tag text -> !tag eAttributes :: NodeG c tag text -> ![(tag, text)] eChildren :: NodeG c tag text -> c (NodeG c tag text) Text :: !text -> NodeG c tag text -- | Type alias for a single node with unqualified tag names where tag and -- text are the same string type. type UNode text = Node text text -- | Type alias for a single node where qualified names are used for tags type QNode text = Node (QName text) text -- | Type alias for a single node where namespaced names are used for tags type NNode text = Node (NName text) text data ParserOptions tag text ParserOptions :: Maybe Encoding -> Maybe (tag -> Maybe text) -> ParserOptions tag text -- | The encoding parameter, if provided, overrides the document's encoding -- declaration. parserEncoding :: ParserOptions tag text -> Maybe Encoding -- | If provided, entity references (i.e. and friends) -- will be decoded into text using the supplied lookup function entityDecoder :: ParserOptions tag text -> Maybe (tag -> Maybe text) defaultParserOptions :: ParserOptions tag text -- | Encoding types available for the document encoding. data Encoding ASCII :: Encoding UTF8 :: Encoding UTF16 :: Encoding ISO88591 :: Encoding -- | Lazily parse XML to tree. Note that forcing the XMLParseError return -- value 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 -> ByteString -> (Node tag text, Maybe XMLParseError) -- | Strictly parse XML to tree. Returns error message or valid parsed -- tree. parse' :: (GenericXMLString tag, GenericXMLString text) => ParserOptions tag text -> ByteString -> Either XMLParseError (Node tag text) -- | Parse error, consisting of message text and error location data XMLParseError XMLParseError :: String -> XMLParseLocation -> XMLParseError -- | Specifies a location of an event within the input text data XMLParseLocation XMLParseLocation :: Int64 -> Int64 -> Int64 -> Int64 -> XMLParseLocation -- | Line number of the event xmlLineNumber :: XMLParseLocation -> Int64 -- | Column number of the event xmlColumnNumber :: XMLParseLocation -> Int64 -- | Byte index of event from start of document xmlByteIndex :: XMLParseLocation -> Int64 -- | The number of bytes in the event xmlByteCount :: XMLParseLocation -> Int64 -- | Lazily parse XML to tree. In the event of an error, throw -- XMLParseException. -- -- parseThrowing can throw an exception from pure code, which is -- generally a bad way to handle errors, because Haskell's lazy -- evaluation means it's hard to predict where it will be thrown from. -- However, it may be acceptable in situations where it's not expected -- during normal operation, depending on the design of your program. parseThrowing :: (GenericXMLString tag, GenericXMLString text) => ParserOptions tag text -> ByteString -> Node tag text -- | An exception indicating an XML parse error, used by the -- ..Throwing variants. data XMLParseException XMLParseException :: XMLParseError -> XMLParseException data SAXEvent tag text StartElement :: tag -> [(tag, text)] -> SAXEvent tag text EndElement :: tag -> SAXEvent tag text CharacterData :: text -> SAXEvent tag text FailDocument :: XMLParseError -> SAXEvent tag text -- | A lower level function that lazily converts a SAX stream into a tree -- structure. saxToTree :: (GenericXMLString tag) => [SAXEvent tag text] -> (Node tag text, Maybe XMLParseError) -- | 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 -- GenericXMLString. class (Monoid s, Eq s) => GenericXMLString s gxNullString :: (GenericXMLString s) => s -> Bool gxToString :: (GenericXMLString s) => s -> String gxFromString :: (GenericXMLString s) => String -> s gxFromChar :: (GenericXMLString s) => Char -> s gxHead :: (GenericXMLString s) => s -> Char gxTail :: (GenericXMLString s) => s -> s gxBreakOn :: (GenericXMLString s) => Char -> s -> (s, s) gxFromCStringLen :: (GenericXMLString s) => CStringLen -> IO s gxToByteString :: (GenericXMLString s) => s -> ByteString eAttrs :: Node tag text -> [(tag, text)] -- | DEPRECATED: Use [Node tag text] instead. -- -- Type alias for nodes. type Nodes tag text = [Node tag text] -- | DEPRECATED: Use [UNode text] instead. -- -- Type alias for nodes with unqualified tag names where tag and text are -- the same string type. DEPRECATED. type UNodes text = Nodes text text -- | DEPRECATED: Use [QNode text] instead. -- -- Type alias for nodes where qualified names are used for tags type QNodes text = [Node (QName text) text] -- | DEPRECATED: Use [NNode text] instead. -- -- Type alias for nodes where namespaced names are used for tags. type NNodes text = [Node (NName text) text] -- | DEPREACTED: Use parse instead. -- -- Lazily parse XML to tree. Note that forcing the XMLParseError return -- value will force the entire parse. Therefore, to ensure lazy -- operation, don't check the error status until you have processed the -- tree. parseTree :: (GenericXMLString tag, GenericXMLString text) => Maybe Encoding -> ByteString -> (Node tag text, Maybe XMLParseError) -- | DEPRECATED: use parse instead. -- -- Strictly parse XML to tree. Returns error message or valid parsed -- tree. parseTree' :: (GenericXMLString tag, GenericXMLString text) => Maybe Encoding -> ByteString -> Either XMLParseError (Node tag text) -- | DEPRECATED: Use parse instead. -- -- Lazily parse XML to SAX events. In the event of an error, FailDocument -- is the last element of the output list. Deprecated in favour of new -- parse parseSAX :: (GenericXMLString tag, GenericXMLString text) => Maybe Encoding -> ByteString -> [SAXEvent tag text] -- | DEPRECATED: Use parseLocations instead. -- -- A variant of parseSAX that gives a document location with each SAX -- event. parseSAXLocations :: (GenericXMLString tag, GenericXMLString text) => Maybe Encoding -> ByteString -> [(SAXEvent tag text, XMLParseLocation)] -- | DEPRECATED: Use parseThrowing instead. -- -- Lazily parse XML to tree. In the event of an error, throw -- XMLParseException. parseTreeThrowing :: (GenericXMLString tag, GenericXMLString text) => Maybe Encoding -> ByteString -> Node tag text -- | DEPRECATED: Use parseThrowing instead. -- -- Lazily parse XML to SAX events. In the event of an error, throw -- XMLParseException. parseSAXThrowing :: (GenericXMLString tag, GenericXMLString text) => Maybe Encoding -> ByteString -> [SAXEvent tag text] -- | DEPRECATED: Used parseLocationsThrowing instead. -- -- A variant of parseSAX that gives a document location with each SAX -- event. In the event of an error, throw XMLParseException. parseSAXLocationsThrowing :: (GenericXMLString tag, GenericXMLString text) => Maybe Encoding -> ByteString -> [(SAXEvent tag text, XMLParseLocation)] instance (Functor c, List c) => MkElementClass NodeG c instance (Functor c, List c) => NodeClass NodeG c instance (NFData tag, NFData text) => NFData (NodeG [] tag text) instance (Eq tag, Eq text) => Eq (NodeG [] tag text) instance (Show tag, Show text) => Show (NodeG [] tag text) -- | This module ported from Text.XML.Light.Cursor -- -- XML cursors for working XML content withing the context of an XML -- document. This implementation is based on the general tree zipper -- written by Krasimir Angelov and Iavor S. Diatchki. -- -- With the exception of modifyContentM, then M-suffixed functions -- are for use with monadic node types, as used when dealing with chunked -- I/O with the hexpat-iteratee package. In the more common pure -- case, you wouldn't need these *M functions. module Text.XML.Expat.Cursor -- | A cursor specific to Text.XML.Expat.Tree.Node trees. type Cursor tag text = CursorG NodeG [] tag text -- | Generalized cursor: The position of a piece of content in an XML -- document. n is the Node type and c is the list type, -- which would usually be [], except when you're using chunked I/O. data CursorG n c tag text Cur :: n c tag text -> c (n c tag text) -> c (n c tag text) -> PathG n c tag text -> CursorG n c tag text -- | The currently selected content. current :: CursorG n c tag text -> n c tag text -- | Siblings on the left, closest first. lefts :: CursorG n c tag text -> c (n c tag text) -- | Siblings on the right, closest first. rights :: CursorG n c tag text -> c (n c tag text) -- | The contexts of the parent elements of this location. parents :: CursorG n c tag text -> PathG n c tag text -- | A path specific to Text.XML.Expat.Tree.Node trees. type Path tag text = PathG NodeG [] tag text -- | Generalized path within an XML document. type PathG n c tag text = [(c (n c tag text), Tag tag text, c (n c tag text))] data Tag tag text Tag :: tag -> Attributes tag text -> Tag tag text tagName :: Tag tag text -> tag tagAttribs :: Tag tag text -> Attributes tag text getTag :: Node tag text -> Tag tag text fromTag :: (MkElementClass n c) => Tag tag text -> c (n c tag text) -> n c tag text -- | A cursor for the given content. fromTree :: (List c) => n c tag text -> CursorG n c tag text -- | The location of the first tree in a forest - pure version. fromForest :: (NodeClass n []) => [n [] tag text] -> Maybe (CursorG n [] tag text) -- | Computes the forest containing this location. toForest :: (MkElementClass n c) => CursorG n c tag text -> c (n c tag text) -- | Computes the tree containing this location. toTree :: (MkElementClass n c) => CursorG n c tag text -> n c tag text -- | The parent of the given location. parent :: (MkElementClass n c) => CursorG n c tag text -> Maybe (CursorG n c tag text) -- | The top-most parent of the given location. root :: (MkElementClass n c) => CursorG n c tag text -> CursorG n c tag text -- | The child with the given index (starting from 0). - pure version. getChild :: (NodeClass n [], Monoid tag) => Int -> CursorG n [] tag text -> Maybe (CursorG n [] tag text) -- | The child with the given index (starting from 0) - used for monadic -- node types. getChildM :: (NodeClass n c, Monoid tag) => Int -> CursorG n c tag text -> ItemM c (Maybe (CursorG n c tag text)) -- | The first child of the given location - pure version. firstChild :: (NodeClass n [], Monoid tag) => CursorG n [] tag text -> Maybe (CursorG n [] tag text) -- | The first child of the given location - used for monadic node types. firstChildM :: (NodeClass n c, Monoid tag) => CursorG n c tag text -> ItemM c (Maybe (CursorG n c tag text)) -- | The last child of the given location - pure version. lastChild :: (NodeClass n [], Monoid tag) => CursorG n [] tag text -> Maybe (CursorG n [] tag text) -- | The last child of the given location - used for monadic node types. lastChildM :: (NodeClass n c, Monoid tag) => CursorG n c tag text -> ItemM c (Maybe (CursorG n c tag text)) -- | The left sibling of the given location - pure version. left :: CursorG n [] tag text -> Maybe (CursorG n [] tag text) -- | The left sibling of the given location - used for monadic node types. leftM :: (List c) => CursorG n c tag text -> ItemM c (Maybe (CursorG n c tag text)) -- | The right sibling of the given location - pure version. right :: CursorG n [] tag text -> Maybe (CursorG n [] tag text) -- | The right sibling of the given location - used for monadic node types. rightM :: (List c) => CursorG n c tag text -> ItemM c (Maybe (CursorG n c tag text)) -- | The next position in a left-to-right depth-first traversal of a -- document: either the first child, right sibling, or the right sibling -- of a parent that has one. Pure version. nextDF :: (MkElementClass n [], Monoid tag) => CursorG n [] tag text -> Maybe (CursorG n [] tag text) -- | The next position in a left-to-right depth-first traversal of a -- document: either the first child, right sibling, or the right sibling -- of a parent that has one. Used for monadic node types. nextDFM :: (MkElementClass n c, Monoid tag) => CursorG n c tag text -> ItemM c (Maybe (CursorG n c tag text)) -- | The first child that satisfies a predicate - pure version. findChild :: (NodeClass n [], Monoid tag) => (CursorG n [] tag text -> Bool) -> CursorG n [] tag text -> Maybe (CursorG n [] tag text) -- | Find the next left sibling that satisfies a predicate. findLeft :: (NodeClass n []) => (CursorG n [] tag text -> Bool) -> CursorG n [] tag text -> Maybe (CursorG n [] tag text) -- | Find the next right sibling that satisfies a predicate - pure version. findRight :: (CursorG n [] tag text -> Bool) -> CursorG n [] tag text -> Maybe (CursorG n [] tag text) -- | Perform a depth first search for a descendant that satisfies the given -- predicate. Pure version. findRec :: (MkElementClass n [], Monoid tag) => (CursorG n [] tag text -> Bool) -> CursorG n [] tag text -> Maybe (CursorG n [] tag text) -- | Perform a depth first search for a descendant that satisfies the given -- predicate. Used for monadic node types. findRecM :: (MkElementClass n c, Monoid tag) => (CursorG n c tag text -> ItemM c Bool) -> CursorG n c tag text -> ItemM c (Maybe (CursorG n c tag text)) -- | Are we at the top of the document? isRoot :: CursorG n c tag text -> Bool -- | Are we at the left end of the the document? (Pure version.) isFirst :: CursorG n [] tag text -> Bool -- | Are we at the left end of the the document? (Used for monadic node -- types.) isFirstM :: (List c) => CursorG n c tag text -> ItemM c Bool -- | Are we at the right end of the document? (Pure version.) isLast :: CursorG n [] tag text -> Bool -- | Are we at the right end of the document? (Used for monadic node -- types.) isLastM :: (List c) => CursorG n c tag text -> ItemM c Bool -- | Are we at the bottom of the document? isLeaf :: (NodeClass n c, Monoid tag) => CursorG n c tag text -> Bool -- | Do we have a parent? isChild :: CursorG n c tag text -> Bool -- | Do we have children? hasChildren :: (NodeClass n c, Monoid tag) => CursorG n c tag text -> Bool -- | Get the node index inside the sequence of children - pure version. getNodeIndex :: CursorG n [] tag text -> Int -- | Change the current content. setContent :: n c tag text -> CursorG n c tag text -> CursorG n c tag text -- | Modify the current content. modifyContent :: (n c tag text -> n c tag text) -> CursorG n c tag text -> CursorG n c tag text -- | Modify the current content - pure version. modifyContentList :: (NodeClass n []) => (n [] tag text -> [n [] tag text]) -> CursorG n [] tag text -> Maybe (CursorG n [] tag text) -- | Modify the current content - used for monadic node types. modifyContentListM :: (NodeClass n c) => (n c tag text -> c (n c tag text)) -> CursorG n c tag text -> ItemM c (Maybe (CursorG n c tag text)) -- | Modify the current content, allowing for an effect. modifyContentM :: (Monad m) => (n [] tag text -> m (n [] tag text)) -> CursorG n [] tag text -> m (CursorG n [] tag text) -- | Insert content to the left of the current position. insertLeft :: (List c) => n c tag text -> CursorG n c tag text -> CursorG n c tag text -- | Insert content to the right of the current position. insertRight :: (List c) => n c tag text -> CursorG n c tag text -> CursorG n c tag text -- | Insert content to the left of the current position. insertManyLeft :: (List c) => c (n c tag text) -> CursorG n c tag text -> CursorG n c tag text -- | Insert content to the right of the current position. insertManyRight :: (List c) => c (n c tag text) -> CursorG n c tag text -> CursorG n c tag text -- | Insert content as the first child of the current position. insertFirstChild :: (NodeClass n c) => n c tag text -> CursorG n c tag text -> Maybe (CursorG n c tag text) -- | Insert content as the first child of the current position. insertLastChild :: (NodeClass n c) => n c tag text -> CursorG n c tag text -> Maybe (CursorG n c tag text) -- | Insert content as the first child of the current position. insertManyFirstChild :: (NodeClass n c) => c (n c tag text) -> CursorG n c tag text -> Maybe (CursorG n c tag text) -- | Insert content as the first child of the current position. insertManyLastChild :: (NodeClass n c) => c (n c tag text) -> CursorG n c tag text -> Maybe (CursorG n c tag text) -- | Insert content to the left of the current position. The new content -- becomes the current position. insertGoLeft :: (List c) => n c tag text -> CursorG n c tag text -> CursorG n c tag text -- | Insert content to the right of the current position. The new content -- becomes the current position. insertGoRight :: (List c) => n c tag text -> CursorG n c tag text -> CursorG n c tag text -- | Remove the content on the left of the current position, if any - pure -- version. removeLeft :: CursorG n [] tag text -> Maybe (n [] tag text, CursorG n [] tag text) -- | Remove the content on the left of the current position, if any - used -- for monadic node types. removeLeftM :: (List c) => CursorG n c tag text -> ItemM c (Maybe (n c tag text, CursorG n c tag text)) -- | Remove the content on the right of the current position, if any - pure -- version. removeRight :: CursorG n [] tag text -> Maybe (n [] tag text, CursorG n [] tag text) -- | Remove the content on the left of the current position, if any - used -- for monadic node types. removeRightM :: (List c) => CursorG n c tag text -> ItemM c (Maybe (n c tag text, CursorG n c tag text)) -- | Remove the current element. The new position is the one on the left. -- Pure version. removeGoLeft :: CursorG n [] tag text -> Maybe (CursorG n [] tag text) -- | Remove the current element. The new position is the one on the left. -- Pure version. removeGoLeftM :: (List c) => CursorG n c tag text -> ItemM c (Maybe (CursorG n c tag text)) -- | Remove the current element. The new position is the one on the right. -- Pure version. removeGoRight :: CursorG n [] tag text -> Maybe (CursorG n [] tag text) -- | Remove the current element. The new position is the one on the right. -- Used for monadic node types. removeGoRightM :: (List c) => CursorG n c tag text -> ItemM c (Maybe (CursorG n c tag text)) -- | Remove the current element. The new position is the parent of the old -- position. removeGoUp :: (MkElementClass n c) => CursorG n c tag text -> Maybe (CursorG n c tag text) instance (Show tag, Show text) => Show (Tag tag text) instance (Show (n c tag text), Show (c (n c tag text)), Show tag, Show text) => Show (CursorG n c tag text) -- | This module provides functions to format a tree structure or SAX -- stream as UTF-8 encoded XML. module Text.XML.Expat.Format -- | Format document with <?xml.. header - lazy variant that returns -- lazy ByteString. format :: (NodeClass n [], GenericXMLString tag, GenericXMLString text) => n [] tag text -> ByteString -- | Format document with <?xml.. header - strict variant that returns -- strict ByteString. format' :: (NodeClass n [], GenericXMLString tag, GenericXMLString text) => n [] tag text -> ByteString -- | Format document with <?xml.. header - generalized variant that -- returns a generic list of strict ByteStrings. formatG :: (NodeClass n c, GenericXMLString tag, GenericXMLString text) => n c tag text -> c ByteString -- | Format XML node with no header - lazy variant that returns lazy -- ByteString. formatNode :: (NodeClass n [], GenericXMLString tag, GenericXMLString text) => n [] tag text -> ByteString -- | Format XML node with no header - strict variant that returns strict -- ByteString. formatNode' :: (NodeClass n [], GenericXMLString tag, GenericXMLString text) => n [] tag text -> ByteString -- | Format XML node with no header - generalized variant that returns a -- generic list of strict ByteStrings. formatNodeG :: (NodeClass n c, GenericXMLString tag, GenericXMLString text) => n c tag text -> c ByteString -- | DEPRECATED: Renamed to format. formatTree :: (NodeClass n [], GenericXMLString tag, GenericXMLString text) => n [] tag text -> ByteString -- | DEPRECATED: Renamed to format'. formatTree' :: (NodeClass n [], GenericXMLString tag, GenericXMLString text) => n [] tag text -> ByteString -- | The standard XML header with UTF-8 encoding. xmlHeader :: ByteString -- | Flatten a tree structure into SAX events, monadic version. treeToSAX :: (GenericXMLString tag, GenericXMLString text, Monoid text, NodeClass n c) => n c tag text -> c (SAXEvent tag text) -- | Format SAX events with no header - lazy variant that returns lazy -- ByteString. formatSAX :: (GenericXMLString tag, GenericXMLString text) => [SAXEvent tag text] -> ByteString -- | Format SAX events with no header - strict variant that returns strict -- ByteString. formatSAX' :: (GenericXMLString tag, GenericXMLString text) => [SAXEvent tag text] -> ByteString -- | Format SAX events with no header - generalized variant that uses -- generic list. formatSAXG :: (List c, GenericXMLString tag, GenericXMLString text) => c (SAXEvent tag text) -> c ByteString -- | Make the output prettier by adding indentation. indent :: (NodeClass n c, GenericXMLString tag, GenericXMLString text) => Int -> n c tag text -> n c tag text -- | Make the output prettier by adding indentation, specifying initial -- indent. indent_ :: (NodeClass n c, GenericXMLString tag, GenericXMLString text) => Int -> Int -> n c tag text -> n c tag text -- | This module ported from Text.XML.Light.Proc module Text.XML.Expat.Proc -- | Select only the elements from a list of XML content. onlyElems :: (NodeClass n c) => c (n c tag text) -> c (n c tag text) -- | Select only the text from a list of XML content. onlyText :: (NodeClass n c, Monoid text) => c (n c tag text) -> c text -- | Find all immediate children with the given name. findChildren :: (NodeClass n c, Eq tag, Monoid tag) => tag -> n c tag text -> c (n c tag text) -- | Filter all immediate children wrt a given predicate. filterChildren :: (NodeClass n c) => (n c tag text -> Bool) -> n c tag text -> c (n c tag text) -- | Filter all immediate children wrt a given predicate over their names. filterChildrenName :: (NodeClass n c, Monoid tag) => (tag -> Bool) -> n c tag text -> c (n c tag text) -- | Find an immediate child with the given name. findChild :: (NodeClass n [], GenericXMLString tag) => tag -> n [] tag text -> Maybe (n [] tag text) -- | Find an immediate child with the given name. filterChild :: (NodeClass n []) => (n [] tag text -> Bool) -> n [] tag text -> Maybe (n [] tag text) -- | Find an immediate child with name matching a predicate. filterChildName :: (NodeClass n [], Monoid tag) => (tag -> Bool) -> n [] tag text -> Maybe (n [] tag text) -- | Find the left-most occurrence of an element matching given name. findElement :: (NodeClass n [], Eq tag, Monoid tag) => tag -> n [] tag text -> Maybe (n [] tag text) -- | Filter the left-most occurrence of an element wrt. given predicate. filterElement :: (NodeClass n []) => (n [] tag text -> Bool) -> n [] tag text -> Maybe (n [] tag text) -- | Filter the left-most occurrence of an element wrt. given predicate. filterElementName :: (NodeClass n [], Monoid tag) => (tag -> Bool) -> n [] tag text -> Maybe (n [] tag text) -- | Find all non-nested occurances of an element. (i.e., once we have -- found an element, we do not search for more occurances among the -- element's children). findElements :: (NodeClass n c, Eq tag, Monoid tag) => tag -> n c tag text -> c (n c tag text) -- | Find all non-nested occurrences of an element wrt. given predicate. -- (i.e., once we have found an element, we do not search for more -- occurances among the element's children). filterElements :: (NodeClass n c) => (n c tag text -> Bool) -> n c tag text -> c (n c tag text) -- | Find all non-nested occurences of an element wrt a predicate over -- element names. (i.e., once we have found an element, we do not search -- for more occurances among the element's children). filterElementsName :: (NodeClass n c, Monoid tag) => (tag -> Bool) -> n c tag text -> c (n c tag text) -- | A variant of Node in which Element nodes have an annotation of -- any type, and some concrete functions that annotate with the XML parse -- location. -- -- The names conflict with those in Tree so you must use qualified -- import if you want to use both modules. module Text.XML.Expat.Annotated -- | A pure tree representation that uses a list as its container type, -- annotated variant. -- -- In the hexpat package, a list of nodes has the type [Node -- tag text], but note that you can also use the more general type -- function ListOf to give a list of any node type, using that -- node's associated list type, e.g. ListOf (UNode Text). type Node a tag text = NodeG a [] tag text -- | Annotated variant of the tree representation of the XML document, -- meaning that it has an extra piece of information of your choice -- attached to each Element. -- -- c is the container type for the element's children, which is -- [] in the hexpat package, and a monadic list type for -- hexpat-iteratee. -- -- tag is the tag type, which can either be one of several -- string types, or a special type from the -- Text.XML.Expat.Namespaced or -- Text.XML.Expat.Qualified modules. -- -- text is the string type for text content. -- -- a is the type of the annotation. One of the things this can -- be used for is to store the XML parse location, which is useful for -- error handling. -- -- Note that some functions in the Text.XML.Expat.Cursor module -- need to create new nodes through the MkElementClass type class. -- Normally this can only be done if a is a Maybe type (so it -- can provide the Nothing value for the annotation on newly created -- nodes). Or, you can write your own MkElementClass instance. -- Apart from that, there is no requirement for a to be a Maybe -- type. data NodeG a c tag text Element :: !tag -> ![(tag, text)] -> c (NodeG a c tag text) -> a -> NodeG a c tag text eName :: NodeG a c tag text -> !tag eAttributes :: NodeG a c tag text -> ![(tag, text)] eChildren :: NodeG a c tag text -> c (NodeG a c tag text) eAnn :: NodeG a c tag text -> a Text :: !text -> NodeG a c tag text -- | Type alias for a single annotated node with unqualified tag names -- where tag and text are the same string type type UNode a text = Node a text text -- | Type alias for a single annotated node, annotated with parse location type LNode tag text = Node XMLParseLocation tag text -- | Type alias for a single node with unqualified tag names where tag and -- text are the same string type, annotated with parse location type ULNode text = LNode text text -- | Modify this node's annotation (non-recursively) if it's an element, -- otherwise no-op. modifyAnnotation :: (a -> a) -> Node a tag text -> Node a tag text -- | Modify this node's annotation and all its children recursively if it's -- an element, otherwise no-op. mapAnnotation :: (a -> b) -> Node a tag text -> Node b tag text -- | Type alias for a single annotated node where qualified names are used -- for tags type QNode a text = Node a (QName text) text -- | Type alias for a single node where qualified names are used for tags, -- annotated with parse location type QLNode text = LNode (QName text) text -- | Type alias for a single annotated node where namespaced names are used -- for tags type NNode a text = Node a (NName text) text -- | Type alias for a single node where namespaced names are used for tags, -- annotated with parse location type NLNode text = LNode (NName text) text data ParserOptions tag text ParserOptions :: Maybe Encoding -> Maybe (tag -> Maybe text) -> ParserOptions tag text -- | The encoding parameter, if provided, overrides the document's encoding -- declaration. parserEncoding :: ParserOptions tag text -> Maybe Encoding -- | If provided, entity references (i.e. and friends) -- will be decoded into text using the supplied lookup function entityDecoder :: ParserOptions tag text -> Maybe (tag -> Maybe text) defaultParserOptions :: ParserOptions tag text -- | Encoding types available for the document encoding. data Encoding ASCII :: Encoding UTF8 :: Encoding UTF16 :: Encoding ISO88591 :: Encoding -- | Lazily parse XML to tree. Note that forcing the XMLParseError return -- value 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 -> ByteString -> (LNode tag text, Maybe XMLParseError) -- | Strictly parse XML to tree. Returns error message or valid parsed -- tree. parse' :: (GenericXMLString tag, GenericXMLString text) => ParserOptions tag text -> ByteString -> Either XMLParseError (LNode tag text) -- | Parse error, consisting of message text and error location data XMLParseError XMLParseError :: String -> XMLParseLocation -> XMLParseError -- | Specifies a location of an event within the input text data XMLParseLocation XMLParseLocation :: Int64 -> Int64 -> Int64 -> Int64 -> XMLParseLocation -- | Line number of the event xmlLineNumber :: XMLParseLocation -> Int64 -- | Column number of the event xmlColumnNumber :: XMLParseLocation -> Int64 -- | Byte index of event from start of document xmlByteIndex :: XMLParseLocation -> Int64 -- | The number of bytes in the event xmlByteCount :: XMLParseLocation -> Int64 -- | Lazily parse XML to tree. In the event of an error, throw -- XMLParseException. -- -- parseThrowing can throw an exception from pure code, which is -- generally a bad way to handle errors, because Haskell's lazy -- evaluation means it's hard to predict where it will be thrown from. -- However, it may be acceptable in situations where it's not expected -- during normal operation, depending on the design of your program. parseThrowing :: (GenericXMLString tag, GenericXMLString text) => ParserOptions tag text -> ByteString -> LNode tag text -- | An exception indicating an XML parse error, used by the -- ..Throwing variants. data XMLParseException XMLParseException :: XMLParseError -> XMLParseException data SAXEvent tag text StartElement :: tag -> [(tag, text)] -> SAXEvent tag text EndElement :: tag -> SAXEvent tag text CharacterData :: text -> SAXEvent tag text FailDocument :: XMLParseError -> SAXEvent tag text -- | A lower level function that lazily converts a SAX stream into a tree -- structure. Variant that takes annotations for start tags. saxToTree :: (GenericXMLString tag) => [(SAXEvent tag text, a)] -> (Node a tag text, Maybe XMLParseError) -- | 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 -- GenericXMLString. class (Monoid s, Eq s) => GenericXMLString s gxNullString :: (GenericXMLString s) => s -> Bool gxToString :: (GenericXMLString s) => s -> String gxFromString :: (GenericXMLString s) => String -> s gxFromChar :: (GenericXMLString s) => Char -> s gxHead :: (GenericXMLString s) => s -> Char gxTail :: (GenericXMLString s) => s -> s gxBreakOn :: (GenericXMLString s) => Char -> s -> (s, s) gxFromCStringLen :: (GenericXMLString s) => CStringLen -> IO s gxToByteString :: (GenericXMLString s) => s -> ByteString eAttrs :: Node a tag text -> [(tag, text)] -- | DEPRECATED: Use parse instead. -- -- Lazily parse XML to SAX events. In the event of an error, FailDocument -- is the last element of the output list. Deprecated in favour of new -- parse parseSAX :: (GenericXMLString tag, GenericXMLString text) => Maybe Encoding -> ByteString -> [SAXEvent tag text] -- | DEPRECATED: Use parseThrowing instead. -- -- Lazily parse XML to SAX events. In the event of an error, throw -- XMLParseException. parseSAXThrowing :: (GenericXMLString tag, GenericXMLString text) => Maybe Encoding -> ByteString -> [SAXEvent tag text] -- | DEPRECATED: Use parseLocations instead. -- -- A variant of parseSAX that gives a document location with each SAX -- event. parseSAXLocations :: (GenericXMLString tag, GenericXMLString text) => Maybe Encoding -> ByteString -> [(SAXEvent tag text, XMLParseLocation)] -- | DEPRECATED: Used parseLocationsThrowing instead. -- -- A variant of parseSAX that gives a document location with each SAX -- event. In the event of an error, throw XMLParseException. parseSAXLocationsThrowing :: (GenericXMLString tag, GenericXMLString text) => Maybe Encoding -> ByteString -> [(SAXEvent tag text, XMLParseLocation)] -- | DEPRECATED: Use parse instead. -- -- Lazily parse XML to tree. Note that forcing the XMLParseError return -- value will force the entire parse. Therefore, to ensure lazy -- operation, don't check the error status until you have processed the -- tree. parseTree :: (GenericXMLString tag, GenericXMLString text) => Maybe Encoding -> ByteString -> (LNode tag text, Maybe XMLParseError) -- | DEPRECATED: use parse instead. -- -- Strictly parse XML to tree. Returns error message or valid parsed -- tree. parseTree' :: (GenericXMLString tag, GenericXMLString text) => Maybe Encoding -> ByteString -> Either XMLParseError (LNode tag text) -- | DEPRECATED: use parseThrowing instead -- -- Lazily parse XML to tree. In the event of an error, throw -- XMLParseException. parseTreeThrowing :: (GenericXMLString tag, GenericXMLString text) => Maybe Encoding -> ByteString -> LNode tag text -- | Convert an annotated tree (Annotated module) into a -- non-annotated tree (Tree module). DEPRECATED in favour of -- fromElement. unannotate :: (Functor c) => NodeG a c tag text -> NodeG c tag text instance (Functor c, List c) => MkElementClass (NodeG (Maybe a)) c instance (Functor c, List c) => NodeClass (NodeG a) c instance (NFData tag, NFData text, NFData a) => NFData (NodeG a [] tag text) instance (Eq tag, Eq text, Eq a) => Eq (NodeG a [] tag text) instance (Show tag, Show text, Show a) => Show (NodeG a [] tag text)