xml-enumerator 0.3.1 → 0.3.2
raw patch · 6 files changed
+587/−78 lines, 6 files
Files
- Text/XML/Enumerator/Cursor.hs +228/−38
- Text/XML/Enumerator/Document.hs +2/−0
- Text/XML/Enumerator/Parse.hs +23/−31
- Text/XML/Enumerator/Resolved.hs +209/−0
- runtests.hs +122/−7
- xml-enumerator.cabal +3/−2
Text/XML/Enumerator/Cursor.hs view
@@ -1,40 +1,129 @@ module Text.XML.Enumerator.Cursor- ( Cursor- , toCursor+ (+ Boolean(..)+ , Axis+ , Cursor+ , fromDocument+ , fromNode+ , cut , parent , precedingSibling , followingSibling- , children+ , child , node- {- , preceding , following- -} , ancestor--- , descendant+ , descendant+ , orSelf+ , (&/)+ , (&//)+ , (&.//)+ , ($|)+ , ($/)+ , ($//)+ , ($.//)+ , check+ , checkNode+ , checkElement+ , checkName+ , anyElement+ , element+ , content+ , attribute+ , (>=>) ) where -import Data.XML.Types+import Control.Monad+import Data.List (foldl')+import Text.XML.Enumerator.Resolved+import qualified Data.Text as T +-- TODO: Consider [Cursor] -> [Cursor]?+-- | The type of an Axis that returns a list of Cursors.+-- They are roughly modeled after <http://www.w3.org/TR/xpath/#axes>.+-- +-- Axes can be composed with '>=>', where e.g. @f >=> g@ means that on all results of+-- the @f@ axis, the @g@ axis will be applied, and all results joined together. +-- Because Axis is just a type synonym for @Cursor -> [Cursor]@, it is possible to use+-- other standard functions like '>>=' or 'concatMap' similarly.+-- +-- The operators '&/', '&//' and '&.//' can be used to combine axes so that the second+-- axis works on the children, descendants, respectively the context node as well as its+-- descendants of the results of the first axis.+-- +-- The operators '$|', '$/', '$//' and '$.//' can be used to apply an axis (right-hand side)+-- to a cursor so that it is applied on the cursor itself, its children, its descendants,+-- respectively itself and its descendants.+-- +-- Note that many of these operators also work on /generalised Axes/ that can return +-- lists of something other than Cursors, for example Content elements.+type Axis = Cursor -> [Cursor]++-- XPath axes as in http://www.w3.org/TR/xpath/#axes+ type DiffCursor = [Cursor] -> [Cursor] +-- TODO: Decide whether to use an existing package for this+-- | Something that can be used in a predicate check as a "boolean".+class Boolean a where+ bool :: a -> Bool++instance Boolean Bool where + bool = id+instance Boolean [a] where + bool = not . null+instance Boolean (Maybe a) where + bool (Just _) = True+ bool _ = False+instance Boolean (Either a b) where+ bool (Left _) = False+ bool (Right _) = True++-- | A cursor: contains an XML 'Node' and pointers to its children, ancestors and siblings. data Cursor = Cursor- { parent :: Maybe Cursor+ { parent' :: Maybe Cursor , precedingSibling' :: DiffCursor , followingSibling' :: DiffCursor- , children :: [Cursor]+ -- | The child axis. XPath:+ -- /the child axis contains the children of the context node/.+ , child :: [Cursor]+ -- | The current node. , node :: Node } -precedingSibling :: Cursor -> [Cursor]+instance Show Cursor where+ show Cursor { node = n } = "Cursor @ " ++ show n++-- | Cut a cursor off from its parent. The idea is to allow restricting the scope of queries on it.+cut :: Cursor -> Cursor+cut = fromNode . node++-- | The parent axis. As described in XPath:+-- /the parent axis contains the parent of the context node, if there is one/.+parent :: Axis+parent c = case parent' c of+ Nothing -> []+ Just p -> [p]++-- | The preceding-sibling axis. XPath:+-- /the preceding-sibling axis contains all the preceding siblings of the context node [...]/.+precedingSibling :: Axis precedingSibling = ($ []) . precedingSibling' -followingSibling :: Cursor -> [Cursor]+-- | The following-sibling axis. XPath:+-- /the following-sibling axis contains all the following siblings of the context node [...]/.+followingSibling :: Axis followingSibling = ($ []) . followingSibling' -toCursor :: Node -> Cursor-toCursor = toCursor' Nothing id id+-- | Convert a 'Document' to a 'Cursor'. It will point to the document root.+fromDocument :: Document -> Cursor+fromDocument = fromNode . NodeElement . documentRoot +-- | Convert a 'Node' to a 'Cursor' (without parents).+fromNode :: Node -> Cursor+fromNode = toCursor' Nothing id id+ toCursor' :: Maybe Cursor -> DiffCursor -> DiffCursor -> Node -> Cursor toCursor' par pre fol n = me@@ -50,33 +139,134 @@ (:) me' . fol' where me' = toCursor' (Just me) pre' fol' n'- fol' = go ((:) me' . pre') ns'+ fol' = go (pre' . (:) me') ns' -{- FIXME-preceding :: Cursor -> [Cursor]+-- | The preceding axis. XPath:+-- /the preceding axis contains all nodes in the same document as the context node that are before the context node in document order, excluding any ancestors and excluding attribute nodes and namespace nodes/.+preceding :: Axis preceding c =- precedingSibling' c- $ case parent c of- Nothing -> []- Just p -> p : preceding p+ go (precedingSibling' c []) (parent c >>= preceding)+ where+ go x y = foldl' (\b a -> go' a b) y x+ go' :: Cursor -> DiffCursor+ go' x rest = foldl' (\b a -> go' a b) (x : rest) (child x) -following :: Cursor -> [Cursor]-following--}+-- | The following axis. XPath:+-- /the following axis contains all nodes in the same document as the context node that are after the context node in document order, excluding any descendants and excluding attribute nodes and namespace nodes/.+following :: Axis+following c =+ go (followingSibling' c) (parent c >>= following)+ where+ go x z =+ foldr (\a b -> go' a b) z y+ where+ y = x []+ go' :: Cursor -> DiffCursor+ go' x rest = x : foldr (\a b -> go' a b) rest (child x) -ancestor :: Cursor -> [Cursor]-ancestor c =- case parent c of- Nothing -> []- Just p -> p : ancestor p+-- | The ancestor axis. XPath:+-- /the ancestor axis contains the ancestors of the context node; the ancestors of the context node consist of the parent of context node and the parent's parent and so on; thus, the ancestor axis will always include the root node, unless the context node is the root node/.+ancestor :: Axis+ancestor = parent >=> (\p -> p : ancestor p) -{--descendant :: Cursor -> [Cursor]-descendant =- go' . map go . children- where- go :: Cursor -> DiffCursor- go c = (c :) . go' . map go (children c)- go' :: [DiffCursor] -> DiffCursor- go' = undefined--}+-- | The descendant axis. XPath:+-- /the descendant axis contains the descendants of the context node; a descendant is a child or a child of a child and so on; thus the descendant axis never contains attribute or namespace nodes/.+descendant :: Axis+descendant = child >=> (\c -> c : descendant c)++-- | Modify an axis by adding the context node itself as the first element of the result list.+orSelf :: Axis -> Axis+orSelf ax c = c : ax c++infixr 1 &/ +infixr 1 &// +infixr 1 &.// +infixr 1 $|+infixr 1 $/+infixr 1 $//+infixr 1 $.//++-- | Combine two axes so that the second works on the children of the results+-- of the first.+(&/) :: Axis -> (Cursor -> [a]) -> (Cursor -> [a])+f &/ g = f >=> child >=> g++-- | Combine two axes so that the second works on the descendants of the results+-- of the first.+(&//) :: Axis -> (Cursor -> [a]) -> (Cursor -> [a])+f &// g = f >=> descendant >=> g++-- | Combine two axes so that the second works on both the result nodes, and their+-- descendants.+(&.//) :: Axis -> (Cursor -> [a]) -> (Cursor -> [a])+f &.// g = f >=> orSelf descendant >=> g++-- | Apply an axis to a 'Cursor'.+($|) :: Cursor -> (Cursor -> [a]) -> [a]+v $| f = f v++-- | Apply an axis to the children of a 'Cursor'.+($/) :: Cursor -> (Cursor -> [a]) -> [a]+v $/ f = child v >>= f++-- | Apply an axis to the descendants of a 'Cursor'.+($//) :: Cursor -> (Cursor -> [a]) -> [a]+v $// f = descendant v >>= f++-- | Apply an axis to a 'Cursor' as well as its descendants.+($.//) :: Cursor -> (Cursor -> [a]) -> [a]+v $.// f = orSelf descendant v >>= f++-- | Filter cursors that don't pass a check.+check :: Boolean b => (Cursor -> b) -> Axis+check f c = case bool $ f c of+ False -> []+ True -> [c]++-- | Filter nodes that don't pass a check.+checkNode :: Boolean b => (Node -> b) -> Axis+checkNode f c = check (f . node) c++-- | Filter elements that don't pass a check, and remove all non-elements.+checkElement :: Boolean b => (Element -> b) -> Axis+checkElement f c = case node c of+ NodeElement e -> case bool $ f e of+ True -> [c]+ False -> []+ _ -> []++-- | Filter elements that don't pass a name check, and remove all non-elements.+checkName :: Boolean b => (Name -> b) -> Axis+checkName f c = checkElement (f . elementName) c++-- | Remove all non-elements. Compare roughly to XPath:+-- /A node test * is true for any node of the principal node type. For example, child::* will select all element children of the context node [...]/.+anyElement :: Axis+anyElement = checkElement (const True)++-- | Select only those elements with a matching tag name. XPath:+-- /A node test that is a QName is true if and only if the type of the node (see [5 Data Model]) is the principal node type and has an expanded-name equal to the expanded-name specified by the QName./+element :: Name -> Axis+element n = checkName (== n)++-- | Select only text nodes, and directly give the 'Content' values. XPath:+-- /The node test text() is true for any text node./+-- +-- Note that this is not strictly an 'Axis', but will work with most combinators.+content :: Cursor -> [T.Text]+content c = case node c of+ (NodeContent v) -> [v]+ _ -> []++-- | Select attributes on the current element (or nothing if it is not an element). XPath:+-- /the attribute axis contains the attributes of the context node; the axis will be empty unless the context node is an element/+-- +-- Note that this is not strictly an 'Axis', but will work with most combinators.+-- +-- The return list of the generalised axis contains as elements lists of 'Content' +-- elements, each full list representing an attribute value.+attribute :: Name -> Cursor -> [T.Text]+attribute n Cursor{node=NodeElement e} = do (n', v) <- elementAttributes e+ guard $ n == n'+ return v+attribute _ _ = []
Text/XML/Enumerator/Document.hs view
@@ -22,6 +22,8 @@ , prettyText -- * Exceptions , InvalidEventStream (InvalidEventStream)+ -- * Internal+ , lazyConsume ) where import Prelude hiding (writeFile, readFile)
Text/XML/Enumerator/Parse.hs view
@@ -85,6 +85,11 @@ import Control.Applicative ((<|>), (<$>)) import Data.Text (Text) import qualified Data.Text as T+import Data.Text.Read (Reader, decimal, hexadecimal)+import Data.Text.Encoding (decodeUtf32BEWith)+import Data.Text.Encoding.Error (ignore)+import Data.Word (Word32)+import Blaze.ByteString.Builder (fromWord32be, toByteString) import Text.XML.Enumerator.Token import Prelude hiding (takeWhile) import qualified Data.ByteString as S@@ -645,36 +650,23 @@ decodeEntities "amp" = ContentText "&" decodeEntities "quot" = ContentText "\"" decodeEntities "apos" = ContentText "'"-decodeEntities t =- case T.uncons t of- Just ('#', t') ->- case T.uncons t' of- Just ('x', t'') -> decodeHex (ContentEntity t) t''- _ -> decodeDec (ContentEntity t) t'- _ -> ContentEntity t--decodeHex :: Content -> Text -> Content-decodeHex backup val- | T.null val = backup-decodeHex backup val =- go (T.unpack val) 0- where- go [] i = ContentText $ T.singleton $ toEnum i- go (c:cs) i = maybe backup (go cs . ((i * 16) +)) $ getHex c- getHex c- | '0' <= c && c <= '9' = Just $ fromEnum c - fromEnum '0'- | 'A' <= c && c <= 'F' = Just $ fromEnum c - fromEnum 'A' + 10- | 'a' <= c && c <= 'f' = Just $ fromEnum c - fromEnum 'a' + 10- | otherwise = Nothing+decodeEntities t = let backup = ContentEntity t in+ case T.uncons t of+ Just ('#', t') ->+ case T.uncons t' of+ Just ('x', t'')+ | T.length t'' > 6 -> backup+ | otherwise -> decodeChar hexadecimal backup t''+ _+ | T.length t' > 7 -> backup+ | otherwise -> decodeChar decimal backup t'+ _ -> backup -decodeDec :: Content -> Text -> Content-decodeDec backup val- | T.null val = backup-decodeDec backup val =- go (T.unpack val) 0+decodeChar :: Reader Word32 -> Content -> Text -> Content+decodeChar readNum backup = either (const backup) toContent . readNum where- go [] i = ContentText $ T.singleton $ toEnum i- go (c:cs) i = maybe backup (go cs . ((i * 10) +)) $ getHex c- getHex c- | '0' <= c && c <= '9' = Just $ fromEnum c - fromEnum '0'- | otherwise = Nothing+ toContent (num, extra) | T.null extra =+ case decodeUtf32BEWith ignore . toByteString $ fromWord32be num of+ char | T.length char == 1 -> ContentText char+ | otherwise -> backup+ toContent _ = backup
+ Text/XML/Enumerator/Resolved.hs view
@@ -0,0 +1,209 @@+{-# LANGUAGE DeriveDataTypeable #-}+module Text.XML.Enumerator.Resolved+ ( -- * Data types+ Document (..)+ , Prologue (..)+ , Instruction (..)+ , Miscellaneous (..)+ , Node (..)+ , Element (..)+ , Name (..)+ , Doctype (..)+ , ExternalID (..)+ -- * Parsing+ , DecodeEntities+ , decodeEntities+ , readFile+ , readFile_+ , parseLBS+ , parseLBS_+ , parseEnum+ , parseEnum_+ , fromEvents+ , UnresolvedEntityException (..)+ -- * Rendering+ , writeFile+ , writePrettyFile+ , renderLBS+ , prettyLBS+ , renderBytes+ , prettyBytes+ -- * Conversion+ , toXMLDocument+ , fromXMLDocument+ , toXMLNode+ , fromXMLNode+ , toXMLElement+ , fromXMLElement+ ) where++import qualified Data.XML.Types as X+import Data.XML.Types+ ( Prologue (..)+ , Miscellaneous (..)+ , Instruction (..)+ , Name (..)+ , Doctype (..)+ , ExternalID (..)+ )+import Data.Typeable (Typeable)+import Data.Text (Text)+import Text.XML.Enumerator.Parse (DecodeEntities, decodeEntities)+import qualified Text.XML.Enumerator.Parse as P+import qualified Text.XML.Enumerator.Document as D+import qualified Text.XML.Enumerator.Render as R+import qualified Data.Text as T+import Data.Either (partitionEithers)+import Prelude hiding (readFile, writeFile)+import Control.Exception (SomeException, Exception)+import Data.Enumerator.Binary (enumFile, iterHandle)+import Control.Monad.IO.Class (MonadIO)+import Data.Enumerator+ ( Enumerator, Iteratee, throwError, ($$), run, run_, joinI, enumList+ , joinE+ )+import Data.ByteString (ByteString)+import qualified Data.ByteString.Lazy as L+import Data.Functor.Identity (runIdentity)+import qualified System.IO as SIO+import System.IO.Unsafe (unsafePerformIO)+import Text.XML.Enumerator.Document (lazyConsume)+import qualified Data.Set as Set+import Data.Set (Set)++data Document = Document+ { documentPrologue :: Prologue+ , documentRoot :: Element+ , documentEpilogue :: [Miscellaneous]+ }+ deriving (Show, Eq, Typeable)++data Node+ = NodeElement Element+ | NodeInstruction Instruction+ | NodeContent Text+ | NodeComment Text+ deriving (Show, Eq, Typeable)++data Element = Element+ { elementName :: Name+ , elementAttributes :: [(Name, Text)]+ , elementNodes :: [Node]+ }+ deriving (Show, Eq, Typeable)++{-+readFile :: FilePath -> DecodeEntities -> IO (Either SomeException Document)+readFile_ :: FIlePath -> DecodeEntities -> IO Document+-}++toXMLDocument :: Document -> X.Document+toXMLDocument (Document a b c) = X.Document a (toXMLElement b) c++toXMLElement :: Element -> X.Element+toXMLElement (Element name as nodes) =+ X.Element name as' nodes'+ where+ as' = map (\(x, y) -> (x, [X.ContentText y])) as+ nodes' = map toXMLNode nodes++toXMLNode :: Node -> X.Node+toXMLNode (NodeElement e) = X.NodeElement $ toXMLElement e+toXMLNode (NodeContent t) = X.NodeContent $ X.ContentText t+toXMLNode (NodeComment c) = X.NodeComment c+toXMLNode (NodeInstruction i) = X.NodeInstruction i++fromXMLDocument :: X.Document -> Either (Set Text) Document+fromXMLDocument (X.Document a b c) =+ case fromXMLElement b of+ Left es -> Left es+ Right b' -> Right $ Document a b' c++fromXMLElement :: X.Element -> Either (Set Text) Element+fromXMLElement (X.Element name as nodes) =+ case (lnodes, las) of+ ([], []) -> Right $ Element name ras rnodes+ (x, []) -> Left $ Set.unions x+ ([], y) -> Left $ Set.unions y+ (x, y) -> Left $ Set.unions x `Set.union` Set.unions y+ where+ enodes = map fromXMLNode nodes+ (lnodes, rnodes) = partitionEithers enodes+ eas = map go as+ (las, ras) = partitionEithers eas+ go (x, y) =+ case go' [] id y of+ Left es -> Left es+ Right y' -> Right (x, y')+ go' [] front [] = Right $ T.concat $ front []+ go' errs _ [] = Left $ Set.fromList errs+ go' errs front (X.ContentText t:ys) = go' errs (front . (:) t) ys+ go' errs front (X.ContentEntity t:ys) = go' (t : errs) front ys++fromXMLNode :: X.Node -> Either (Set Text) Node+fromXMLNode (X.NodeElement e) =+ either Left (Right . NodeElement) $ fromXMLElement e+fromXMLNode (X.NodeContent (X.ContentText t)) = Right $ NodeContent t+fromXMLNode (X.NodeContent (X.ContentEntity t)) = Left $ Set.singleton t+fromXMLNode (X.NodeComment c) = Right $ NodeComment c+fromXMLNode (X.NodeInstruction i) = Right $ NodeInstruction i++readFile :: FilePath -> DecodeEntities -> IO (Either SomeException Document)+readFile fn = parseEnum $ enumFile fn++readFile_ :: FilePath -> DecodeEntities -> IO Document+readFile_ fn = parseEnum_ $ enumFile fn++lbsEnum :: Monad m => L.ByteString -> Enumerator ByteString m a+lbsEnum = enumList 8 . L.toChunks++parseLBS :: L.ByteString -> DecodeEntities -> Either SomeException Document+parseLBS lbs = runIdentity . parseEnum (lbsEnum lbs)++parseLBS_ :: L.ByteString -> DecodeEntities -> Document+parseLBS_ lbs = runIdentity . parseEnum_ (lbsEnum lbs)++parseEnum :: Monad m+ => Enumerator ByteString m Document+ -> DecodeEntities+ -> m (Either SomeException Document)+parseEnum enum de = run $ enum $$ joinI $ P.parseBytes de $$ fromEvents++parseEnum_ :: Monad m+ => Enumerator ByteString m Document+ -> DecodeEntities+ -> m Document+parseEnum_ enum de = run_ $ enum $$ joinI $ P.parseBytes de $$ fromEvents++fromEvents :: Monad m => Iteratee X.Event m Document+fromEvents = do+ d <- D.fromEvents+ either (throwError . UnresolvedEntityException) return $ fromXMLDocument d++data UnresolvedEntityException = UnresolvedEntityException (Set Text)+ deriving (Show, Typeable)+instance Exception UnresolvedEntityException++renderBytes :: MonadIO m => Document -> Enumerator ByteString m a+renderBytes doc = enumList 8 (D.toEvents $ toXMLDocument doc) `joinE` R.renderBytes++prettyBytes :: MonadIO m => Document -> Enumerator ByteString m a+prettyBytes doc = enumList 8 (D.toEvents $ toXMLDocument doc) `joinE` R.prettyBytes++writeFile :: FilePath -> Document -> IO ()+writeFile fn doc = SIO.withBinaryFile fn SIO.WriteMode $ \h ->+ run_ $ renderBytes doc $$ iterHandle h++-- | Pretty prints via 'prettyBytes'.+writePrettyFile :: FilePath -> Document -> IO ()+writePrettyFile fn doc = SIO.withBinaryFile fn SIO.WriteMode $ \h ->+ run_ $ prettyBytes doc $$ iterHandle h++renderLBS :: Document -> L.ByteString+renderLBS doc =+ L.fromChunks $ unsafePerformIO $ lazyConsume $ renderBytes doc++-- | Pretty prints via 'prettyBytes'.+prettyLBS :: Document -> L.ByteString+prettyLBS doc =+ L.fromChunks $ unsafePerformIO $ lazyConsume $ prettyBytes doc
runtests.hs view
@@ -17,10 +17,13 @@ import qualified Data.Map as Map import qualified Text.XML.Enumerator.Document as D import qualified Text.XML.Enumerator.Parse as P+import qualified Text.XML.Enumerator.Resolved as Res import Text.XML.Enumerator.Parse (decodeEntities) import qualified Text.XML.Enumerator.Parse as P import qualified Text.XML.Enumerator.Render as R+import qualified Text.XML.Enumerator.Cursor as Cu+import Text.XML.Enumerator.Cursor ((&/), (&//), (&.//), ($|), ($/), ($//), ($.//)) import qualified Data.Map as Map import qualified Data.ByteString.Lazy.Char8 as L import Control.Monad.IO.Class (liftIO)@@ -32,14 +35,43 @@ import Control.Monad.IO.Class(MonadIO) import Control.Monad import Control.Applicative((<$>), (<*>))+import qualified Data.Text as T+import qualified Data.Set as Set+import Control.Exception (toException) -main :: IO ()-main = hspec $ describe "XML parsing and rendering"- [ it "is idempotent to parse and render a document" documentParseRender- , it "has valid parser combinators" combinators- , it "has working choose function" testChoose- , it "has working many function" testMany- , it "has working orE" testOrE+--main :: IO [Spec]+main = hspec $ descriptions $+ [ describe "XML parsing and rendering"+ [ it "is idempotent to parse and render a document" documentParseRender+ , it "has valid parser combinators" combinators+ , it "has working choose function" testChoose+ , it "has working many function" testMany+ , it "has working orE" testOrE+ ]+ , describe "XML Cursors"+ [ it "has correct parent" cursorParent+ , it "has correct ancestor" cursorAncestor+ , it "has correct orSelf" cursorOrSelf+ , it "has correct preceding" cursorPreceding+ , it "has correct following" cursorFollowing+ , it "has correct precedingSibling" cursorPrecedingSib+ , it "has correct followingSibling" cursorFollowingSib+ , it "has correct descendant" cursorDescendant+ , it "has correct check" cursorCheck+ , it "has correct check with lists" cursorPredicate+ , it "has correct checkNode" cursorCheckNode+ , it "has correct checkElement" cursorCheckElement+ , it "has correct checkName" cursorCheckName+ , it "has correct anyElement" cursorAnyElement+ , it "has correct element" cursorElement+ , it "has correct content" cursorContent+ , it "has correct attribute" cursorAttribute+ , it "has correct &* and $* operators" cursorDeep+ ]+ , describe "resolved"+ [ it "identifies unresolved entities" resolvedIdentifies+ , it "works for resolvable entities" resolvedAllGood+ ] ] documentParseRender =@@ -137,3 +169,86 @@ , "<success/>" , "</hello>" ]+++name :: [Cu.Cursor] -> [Text]+name [] = []+name (c:cs) = ($ name cs) $ case Cu.node c of+ Res.NodeElement e -> ((Res.nameLocalName $ Res.elementName e) :)+ _ -> id++cursor =+ Cu.fromDocument $ Res.parseLBS_ input decodeEntities+ where+ input = L.concat+ [ "<foo attr=\"x\">"+ , "<bar1/>"+ , "<bar2>"+ , "<baz1/>"+ , "<baz2 attr=\"y\"/>"+ , "<baz3>a</baz3>"+ , "</bar2>"+ , "<bar3>"+ , "<bin1/>"+ , "b"+ , "<bin2/>"+ , "<bin3/>"+ , "</bar3>"+ , "</foo>"+ ]++bar2 = Cu.child cursor !! 1+baz2 = Cu.child bar2 !! 1++bar3 = Cu.child cursor !! 2+bin2 = Cu.child bar3 !! 1++cursorParent = name (Cu.parent bar2) @?= ["foo"]+cursorAncestor = name (Cu.ancestor baz2) @?= ["bar2", "foo"]+cursorOrSelf = name (Cu.orSelf Cu.ancestor baz2) @?= ["baz2", "bar2", "foo"]+cursorPreceding = do+ name (Cu.preceding baz2) @?= ["baz1", "bar1"]+ name (Cu.preceding bin2) @?= ["bin1", "baz3", "baz2", "baz1", "bar2", "bar1"]+cursorFollowing = do+ name (Cu.following baz2) @?= ["baz3", "bar3", "bin1", "bin2", "bin3"]+ name (Cu.following bar2) @?= ["bar3", "bin1", "bin2", "bin3"]+cursorPrecedingSib = name (Cu.precedingSibling baz2) @?= ["baz1"]+cursorFollowingSib = name (Cu.followingSibling baz2) @?= ["baz3"]+cursorDescendant = (name $ Cu.descendant cursor) @?= T.words "bar1 bar2 baz1 baz2 baz3 bar3 bin1 bin2 bin3"+cursorCheck = null (cursor $.// Cu.check (const False)) @?= True+cursorPredicate = (name $ cursor $.// Cu.check Cu.descendant) @?= T.words "foo bar2 baz3 bar3"+cursorCheckNode = (name $ cursor $// Cu.checkNode f) @?= T.words "bar1 bar2 bar3"+ where f (Res.NodeElement e) = "bar" `T.isPrefixOf` Res.nameLocalName (Res.elementName e)+ f _ = False+cursorCheckElement = (name $ cursor $// Cu.checkElement f) @?= T.words "bar1 bar2 bar3"+ where f e = "bar" `T.isPrefixOf` Res.nameLocalName (Res.elementName e)+cursorCheckName = (name $ cursor $// Cu.checkName f) @?= T.words "bar1 bar2 bar3"+ where f n = "bar" `T.isPrefixOf` nameLocalName n+cursorAnyElement = (name $ cursor $// Cu.anyElement) @?= T.words "bar1 bar2 baz1 baz2 baz3 bar3 bin1 bin2 bin3"+cursorElement = (name $ cursor $// Cu.element "baz2") @?= ["baz2"]+cursorContent = do+ Cu.content cursor @?= []+ (cursor $.// Cu.content) @?= ["a", "b"]+cursorAttribute = Cu.attribute "attr" cursor @?= ["x"]+cursorDeep = do+ (Cu.element "foo" &/ Cu.element "bar2" &// Cu.attribute "attr") cursor @?= ["y"]+ (return &.// Cu.attribute "attr") cursor @?= ["x", "y"]+ (cursor $.// Cu.attribute "attr") @?= ["x", "y"]+ (cursor $/ Cu.element "bar2" &// Cu.attribute "attr") @?= ["y"]+ (cursor $/ Cu.element "bar2" &/ Cu.element "baz2" >=> Cu.attribute "attr") @?= ["y"]+ null (cursor $| Cu.element "foo") @?= False++showEq :: (Show a, Show b) => Either a b -> Either a b -> Assertion+showEq x y = show x @=? show y++resolvedIdentifies =+ Left (toException $ Res.UnresolvedEntityException $ Set.fromList ["foo", "bar", "baz"]) `showEq`+ Res.parseLBS+ "<root attr='&bar;'>&foo; --- &baz; &foo;</root>"+ Res.decodeEntities++resolvedAllGood =+ D.parseLBS_ xml P.decodeEntities @=?+ Res.toXMLDocument (Res.parseLBS_ xml P.decodeEntities)+ where+ xml = "<foo><bar/><baz/></foo>"
xml-enumerator.cabal view
@@ -1,8 +1,8 @@ name: xml-enumerator-version: 0.3.1+version: 0.3.2 license: BSD3 license-file: LICENSE-author: Michael Snoyman <michaels@suite-sol.com>+author: Michael Snoyman <michaels@suite-sol.com>, Aristid Breitkreuz <aristidb@googlemail.com> maintainer: Michael Snoyman <michaels@suite-sol.com> synopsis: Pure-Haskell utilities for dealing with XML with the enumerator package. description:@@ -32,6 +32,7 @@ Text.XML.Enumerator.Render Text.XML.Enumerator.Document Text.XML.Enumerator.Cursor+ Text.XML.Enumerator.Resolved other-modules: Text.XML.Enumerator.Token ghc-options: -Wall