xml-extractors 0.2.0.0 → 0.2.1.0
raw patch · 6 files changed
+74/−44 lines, 6 files
Files
- src/Text/XML/Light/Extractors.hs +16/−6
- src/Text/XML/Light/Extractors/Extra.hs +0/−1
- src/Text/XML/Light/Extractors/Internal.hs +6/−10
- src/Text/XML/Light/Extractors/Internal/Result.hs +7/−6
- src/Text/XML/Light/Extractors/ShowErr.hs +31/−9
- xml-extractors.cabal +14/−12
src/Text/XML/Light/Extractors.hs view
@@ -21,17 +21,19 @@ -- -- And a data type for a book: ----- > data Book = Book { bookdId :: Int+-- > data Book = Book { bookId :: Int -- > , isbn :: Maybe String -- > , author, title :: String -- > } -- -- You can parse the xml file into a generic tree structure using--- 'Text.XML.Light.Input.parseXMLDoc', then extract information from--- the tree using this library.+-- 'Text.XML.Light.Input.parseXMLDoc' from the `xml` package. --+-- Using this library one can define extractors to extract data from+-- the generic tree.+-- -- @--- library = 'element' "library" $ 'children' $ 'many' book+-- library = 'element' "library" $ 'children' $ 'only' $ 'many' book -- -- book = 'element' "book" $ do -- i <- 'attribAs' "id" 'Text.XML.Light.Extractors.Extra.integer'@@ -45,8 +47,16 @@ -- extractLibrary = 'extractDocContents' library -- @ ----- /Note:/ The 'Control.Applicative' module contains some useful--- combinators like 'optional', 'many' and '<|>'.+-- = Notes+--+-- * The "Control.Applicative" module contains some useful+-- combinators like 'optional', 'many' and '<|>'.+--+-- * The "Text.XML.Light.Extractors.ShowErr" contains some+-- predefined functions to convert error values to strings.+--+-- * The "Text.XML.Light.Extractors.Extra" module provides some+-- functions to read numeric data. -- module Text.XML.Light.Extractors (
src/Text/XML/Light/Extractors/Extra.hs view
@@ -1,6 +1,5 @@ module Text.XML.Light.Extractors.Extra where - import Safe (readMay) import Text.XML.Light.Extractors
src/Text/XML/Light/Extractors/Internal.hs view
@@ -23,14 +23,10 @@ ) where -import Control.Applicative- import Control.Monad.Identity import Control.Monad.Trans.Reader import Control.Monad.Trans.State -import Data.Monoid- import Text.XML.Light.Types as XML import qualified Text.XML.Light.Proc as XML @@ -39,8 +35,10 @@ -------------------------------------------------------------------------------- +elemName :: Element -> String elemName = qName . elName +qname :: String -> QName qname name = QName name Nothing Nothing --------------------------------------------------------------------------------@@ -53,8 +51,6 @@ addIdx i p = show i : p addElem :: XML.Element -> Path -> Path-- addElem e p = elemName e : p addAttrib :: String -> Path -> Path@@ -99,7 +95,7 @@ type ElementExtractor a = ReaderT (Path, XML.Element) (ResultT ExtractionErr Identity) a runElementExtractor :: ElementExtractor a -> XML.Element -> Path -> Result ExtractionErr a-runElementExtractor p elem path = runIdentity $ runResultT $ runReaderT p (path,elem)+runElementExtractor p elem path = runIdentity $ runResultT $ runReaderT p (path, elem) makeElementExtractor :: Result ExtractionErr a -> ElementExtractor a makeElementExtractor (Fatal e) = throwFatal e@@ -142,7 +138,7 @@ -- | Lift a string function to an element extractor. liftToElement :: (String -> Either Err a) -> String -> ElementExtractor a liftToElement f s = do- (path,x) <- ask+ (path,_) <- ask case f s of Left e -> throwError (ExtractionErr e path) Right a -> return a@@ -184,9 +180,9 @@ element :: String -> ElementExtractor a -> ContentsExtractor a element name p = first expect go where- go c@(Elem x) path+ go (Elem x) path | elemName x == name = escalate $ runElementExtractor p x (addElem x path)- go c path = Fail (ExtractionErr (ErrExpect expect c) path)+ go c path = Fail (ExtractionErr (ErrExpect expect c) path) expect = "element " ++ show name
src/Text/XML/Light/Extractors/Internal/Result.hs view
@@ -69,6 +69,7 @@ -- | Maps 'Fail' and 'Fatal' to 'Left'.+toEither :: Result a b -> Either a b toEither (Fatal e) = Left e toEither (Fail e) = Left e toEither (Ok a) = Right a@@ -145,13 +146,13 @@ -------------------------------------------------------------------------------- -testX :: ResultT String IO Int-testX = lift (print "x") >> return 1+-- testX :: ResultT String IO Int+-- testX = lift (print "x") >> return 1 -testY :: ResultT String IO Int-testY = lift (print "error") >> throwError "error"+-- testY :: ResultT String IO Int+-- testY = lift (print "error") >> throwError "error" -testZ :: ResultT String IO Int-testZ = lift (print "fatal") >> throwFatal "fatal"+-- testZ :: ResultT String IO Int+-- testZ = lift (print "fatal") >> throwFatal "fatal" --------------------------------------------------------------------------------
src/Text/XML/Light/Extractors/ShowErr.hs view
@@ -1,4 +1,10 @@-module Text.XML.Light.Extractors.ShowErr where+-- | This module provide default functions to translate errors into strings.+module Text.XML.Light.Extractors.ShowErr+ ( showExtractionErr+ , showErr+ , showPath+ )+where import Data.List @@ -8,22 +14,38 @@ showExtractionErr :: ExtractionErr -> String-showExtractionErr (ExtractionErr e path) = showErr e ++ "\nin " ++ showPath path+showExtractionErr (ExtractionErr e path) = showErr e ++ "in path: " ++ showPath path showPath :: Path -> String showPath = intercalate "/" . reverse +showErr :: Err -> String showErr (ErrExpect expect found) =- unwords ["Expected", expect, "found", take 60 $ XML.showContent found]-+ unlines+ [ unwords ["Expected", expect, "found"]+ , take 60 $ XML.showContent found+ , unwords ["at line:", showLine found]+ ] showErr (ErrAttr expect parent) =- unwords ["Missing attribute", show expect, "of", qName $ elName parent]--showErr (ErrMsg msg) = msg+ unlines+ [ unwords ["Missing attribute", show expect, "of", show $ qName $ elName parent]+ , unwords ["at line:", showLine $ Elem parent]+ ]+showErr (ErrMsg msg) =+ unlines [msg] showErr (ErrNull expected) = - unwords ["Expected", expected]+ unlines [unwords ["Expected", expected]] showErr (ErrEnd found) =- unwords ["Unexpected:", take 60 $ XML.showContent found]+ unlines [unwords ["Unexpected:", take 60 $ XML.showContent found]]+++contentLine :: Content -> Maybe Integer+contentLine (Elem e) = elLine e+contentLine (Text t) = cdLine t+contentLine _ = Nothing++showLine :: Content -> String+showLine = maybe "??" show . contentLine
xml-extractors.cabal view
@@ -1,26 +1,27 @@ name: xml-extractors-version: 0.2.0.0-synopsis: Simple wrapper over xml (Text.XML.Light) to extract data from parsed xml+version: 0.2.1.0+synopsis: Simple wrapper over xml to extract data from parsed xml description:- This is a library to make it easier to extract data from parsed xml.+ This is a library to simplify extraction of data from parsed xml. . See the 'Text.XML.Light.Extractors' module for an example. . = Motivation .- The xml package provides functions to parse and get information- from xml data. You can parse an xml string into a generic xml tree- representation. Then to extract information from that tree and into- you own data types you can use this library.+ The `xml` package provides functions to parse and get information from+ xml data. It will parse an xml string into a generic tree+ representation. Extracting information from such a tree while+ keeping track of location to handle errors is tricky. This library+ helps with that. . If there is an error during extraction (expected information is- absent or wrong), you will get an error value with position information.+ absent or wrong), it will return an error value with position information. . = Some limitations .- * Only handles unqualified names, so far.+ * Only handles unqualified names. .- * No column or line number reference in error values.+ * No column number and sometimes no line number reference in error values. homepage: https://github.com/holmisen/xml-extractors license: BSD3@@ -28,7 +29,8 @@ author: Johan Holmquist maintainer: holmisen@gmail.com -- copyright: -category: Text, XML+category: XML+tested-with: GHC == 7.8.3 build-type: Simple extra-source-files: README.md cabal-version: >=1.10@@ -39,7 +41,7 @@ Text.XML.Light.Extractors.Internal.Result, Text.XML.Light.Extractors.Extra, Text.XML.Light.Extractors.ShowErr- -- other-modules: + -- other-modules: other-extensions: NoMonomorphismRestriction build-depends: base >=4.6 && <4.8, xml >=1.3 && <1.4,