packages feed

xml-extractors 0.3.0.0 → 0.4.0.0

raw patch · 7 files changed

+108/−41 lines, 7 filesPVP ok

version bump matches the API change (PVP)

API changes (from Hackage documentation)

- Text.XML.Light.Extractors: ErrAttr :: String -> Element -> Err
- Text.XML.Light.Extractors: ErrExpect :: String -> Content -> Err
- Text.XML.Light.Extractors: expected :: Err -> String
- Text.XML.Light.Extractors: found :: Err -> Content
- Text.XML.Light.Extractors.Internal: ErrAttr :: String -> Element -> Err
- Text.XML.Light.Extractors.Internal: ErrExpect :: String -> Content -> Err
- Text.XML.Light.Extractors.Internal: expected :: Err -> String
- Text.XML.Light.Extractors.Internal: found :: Err -> Content
+ Text.XML.Light.Extractors: ErrAttribValue :: String -> String -> Element -> Err
+ Text.XML.Light.Extractors: ErrExpectAttrib :: String -> Element -> Err
+ Text.XML.Light.Extractors: ErrExpectContent :: String -> Content -> Err
+ Text.XML.Light.Extractors: eitherMessageOrValue :: Either ExtractionErr a -> Either String a
+ Text.XML.Light.Extractors: expectedAttrib :: Err -> String
+ Text.XML.Light.Extractors: expectedContent :: Err -> String
+ Text.XML.Light.Extractors: expectedValue :: Err -> String
+ Text.XML.Light.Extractors: float :: (Floating a, Read a) => String -> Either String a
+ Text.XML.Light.Extractors: foundContent :: Err -> Content
+ Text.XML.Light.Extractors: foundValue :: Err -> String
+ Text.XML.Light.Extractors: integer :: (Integral a, Read a) => String -> Either String a
+ Text.XML.Light.Extractors: showExtractionErr :: ExtractionErr -> String
+ Text.XML.Light.Extractors.Internal: ErrAttribValue :: String -> String -> Element -> Err
+ Text.XML.Light.Extractors.Internal: ErrExpectAttrib :: String -> Element -> Err
+ Text.XML.Light.Extractors.Internal: ErrExpectContent :: String -> Content -> Err
+ Text.XML.Light.Extractors.Internal: expectedAttrib :: Err -> String
+ Text.XML.Light.Extractors.Internal: expectedContent :: Err -> String
+ Text.XML.Light.Extractors.Internal: expectedValue :: Err -> String
+ Text.XML.Light.Extractors.Internal: foundContent :: Err -> Content
+ Text.XML.Light.Extractors.Internal: foundValue :: Err -> String
- Text.XML.Light.Extractors: attribAs :: String -> (String -> Either Err a) -> ElementExtractor a
+ Text.XML.Light.Extractors: attribAs :: String -> (String -> Either String a) -> ElementExtractor a
- Text.XML.Light.Extractors.Extra: float :: (Floating a, Read a) => String -> Either Err a
+ Text.XML.Light.Extractors.Extra: float :: (Floating a, Read a) => String -> Either String a
- Text.XML.Light.Extractors.Extra: integer :: (Integral a, Read a) => String -> Either Err a
+ Text.XML.Light.Extractors.Extra: integer :: (Integral a, Read a) => String -> Either String a
- Text.XML.Light.Extractors.Internal: attribAs :: String -> (String -> Either Err a) -> ElementExtractor a
+ Text.XML.Light.Extractors.Internal: attribAs :: String -> (String -> Either String a) -> ElementExtractor a

Files

README.md view
@@ -1,5 +1,5 @@-xml-extractor-=============+xml-extractors+==============  Simple wrapper over [xml](http://hackage.haskell.org/package/xml) to extract data from parsed xml.
changelog.md view
@@ -1,3 +1,27 @@+0.4.0.0++  * Added several functions to the `Extractors` module:++    * `eitherMessageOrValue` from the `ShowErr` module and some others.++	* Both conversion functions from `Extra`.++    This means less imports in many cases.++  * Some changes were made to the `Err` datatype:++    * `ErrExpect` was renamed to `ErrExpectContent`.++    * `ErrAttr` was renamed to `ErrExpectAttrib` and used for signalling a missing attribute.++    * `ErrAttribValue` was added to cater for erroneous attribute values.++    * Some field names have been given more descriptive names.++    The conversion/validation function given to `attribAs` does no+    longer need to return an `Err` value for errors, but only a string+    description of the expected value format.+ 0.3.0.0    * Added `anyContent`
src/Text/XML/Light/Extractors.hs view
@@ -49,15 +49,14 @@ -- -- = Notes --+--  * The 'only' combinator can be used to exhaustively extract contents.+-- --  * 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   (    -- * Errors@@ -84,6 +83,12 @@   , anyContent   , eoc   , only++  -- * Utils+  , showExtractionErr+  , eitherMessageOrValue+  , integer+  , float   )  where @@ -92,6 +97,8 @@ import           Text.XML.Light.Types as XML import qualified Text.XML.Light.Proc  as XML +import           Text.XML.Light.Extractors.Extra+import           Text.XML.Light.Extractors.ShowErr  (showExtractionErr) import           Text.XML.Light.Extractors.Internal (ExtractionErr, Err, Path) import qualified Text.XML.Light.Extractors.Internal as Internal import           Text.XML.Light.Extractors.Internal.Result hiding (throwError, throwFatal)@@ -120,7 +127,11 @@  -- | @attribAs name f@ extracts the value of attribute @name@ and runs -- it through a conversion/validation function.-attribAs :: String -> (String -> Either Err a) -> ElementExtractor a+--+-- The conversion function takes a string with the value and returns+-- either a description of the expected format of the value or the+-- converted value.+attribAs :: String -> (String -> Either String a) -> ElementExtractor a attribAs name = ElementExtractor . (Internal.attribAs name)  @@ -182,3 +193,11 @@ -- | Extracts one 'Content' item. anyContent :: ContentsExtractor Content anyContent = ContentsExtractor Internal.anyContent+++-- | Convenience function to convert extraction errors to string+-- messages using 'showExtractionErr'.+--+-- > eitherMessageOrValue = either (Left . showExtractionErr) Right+eitherMessageOrValue :: Either ExtractionErr a -> Either String a+eitherMessageOrValue = either (Left . showExtractionErr) Right
src/Text/XML/Light/Extractors/Extra.hs view
@@ -1,14 +1,15 @@ module Text.XML.Light.Extractors.Extra where  import Safe (readMay)-import Text.XML.Light.Extractors  -float :: (Floating a, Read a) => String -> Either Err a-float s = -  maybe (Left $ ErrMsg ("Expected float, found: " ++ show s)) return (readMay s)+-- | Reads a floating point value or return @'Left' "float"@ if+-- the read fails.+float :: (Floating a, Read a) => String -> Either String a+float = maybe (Left "float") return . readMay  -integer :: (Integral a, Read a) => String -> Either Err a-integer s = -  maybe (Left $ ErrMsg ("Expected integer, found: " ++ show s)) return (readMay s)+-- | Reads an integer value or return @'Left' "integer"@ if the read+-- fails.+integer :: (Integral a, Read a) => String -> Either String a+integer = maybe (Left "integer") return . readMay
src/Text/XML/Light/Extractors/Internal.hs view
@@ -43,8 +43,9 @@  -- | Location for some content. ----- For now it is a reversed list of content indices and element--- names. This may change to something less \"stringly typed\".+-- For now it is a reversed list of content indices (starting at 1)+-- and element names. This may change to something less \"stringly+-- typed\". type Path = [String]  @@ -65,19 +66,24 @@   -- | Extraction errors.-data Err = ErrExpect-           { expected :: String      -- ^ expected content-           , found    :: XML.Content -- ^ found content+data Err = ErrExpectContent+           { expectedContent :: String+           , foundContent    :: XML.Content            } -- ^ Some expected content is missing-         | ErrAttr -           { expected  :: String       -- ^ expected attribute-           , atElement :: XML.Element  -- ^ element with missing attribute+         | ErrExpectAttrib +           { expectedAttrib :: String       -- ^ name of expected attribute+           , atElement      :: XML.Element  -- ^ element with missing attribute            } -- ^ An expected attribute is missing+         | ErrAttribValue+           { expectedValue  :: String       -- ^ description of expected value+           , foundValue     :: String       -- ^ the value found+           , atElement      :: XML.Element  -- ^ element with bad attribute+           } -- ^ An attribute value was bad          | ErrEnd -           { found    :: XML.Content+           { foundContent   :: XML.Content            } -- ^ Expected end of contents          | ErrNull-           { expected :: String  -- ^ expected content+           { expectedContent :: String            } -- ^ Unexpected end of contents          | ErrMsg String   deriving Show@@ -109,16 +115,16 @@   attribAs :: String -- ^ name of attribute to extract-         -> (String -> Either Err a)+         -> (String -> Either String a) -- ^ function returning given string to some value or an error message          -> ElementExtractor a attribAs name f = do   (path,x) <- ask   let path' = pushAttrib name path   case XML.lookupAttr (XML.unqual name) (elAttribs x) of-    Nothing -> throwError $ ExtractionErr (ErrAttr name x) path+    Nothing -> throwError $ ExtractionErr (ErrExpectAttrib name x) path     Just s  ->       case f s of-        Left e  -> throwFatal $ ExtractionErr e path'+        Left e  -> throwFatal $ ExtractionErr (ErrAttribValue e s x) path'         Right a -> return a  @@ -136,13 +142,13 @@   makeElementExtractor $ fmap fst r  --- | Lift a string function to an element extractor.-liftToElement :: (String -> Either Err a) -> String -> ElementExtractor a-liftToElement f s = do-  (path,_) <- ask-  case f s of-    Left e   -> throwError (ExtractionErr e path)-    Right a  -> return a+-- -- | Lift a string function to an element extractor.+-- liftToElement :: (String -> Either Err a) -> String -> ElementExtractor a+-- liftToElement f s = do+--   (path,_) <- ask+--   case f s of+--     Left e   -> throwError (ExtractionErr e path)+--     Right a  -> return a    -------------------------------------------------------------------------------- @@ -174,7 +180,7 @@   where     go (Elem x) path       | elemName x == name = escalate $ runElementExtractor p x (pushElem x path)-    go c        path       = Fail (ExtractionErr (ErrExpect expect c) path)+    go c        path       = Fail (ExtractionErr (ErrExpectContent expect c) path)      expect = "element " ++ show name @@ -186,7 +192,7 @@       case f (cdData x) of         Left e  -> Fatal $ ExtractionErr e path         Right s -> return s-    go c path = Fail $ ExtractionErr (ErrExpect "text" c) path+    go c path = Fail $ ExtractionErr (ErrExpectContent "text" c) path   text :: ContentsExtractor String
src/Text/XML/Light/Extractors/ShowErr.hs view
@@ -13,31 +13,48 @@ import           Text.XML.Light.Extractors.Internal (ExtractionErr(..), Err(..), Path)  +-- | Converts an extraction error to a multi line string message.+--+-- Paths are shown according to 'showPath'. showExtractionErr :: ExtractionErr -> String showExtractionErr (ExtractionErr e path) =   unlines [showErr e ++ "in path: " ++ showPath path] ++-- | Paths will show like this:+--+-- @1\/foo\/2\/bar\/\@x@+--+-- which represents the \"x\" attribute of the \"bar\" element, which+-- is the second content of the \"foo\" element which is the first+-- content from the root. showPath :: Path -> String showPath = intercalate "/" . reverse   showErr :: Err -> String-showErr (ErrExpect expect found) =+showErr (ErrExpectContent expect found) =   unlines-  [ unwords ["Expected", expect, "found"]-  , take 60 $ XML.showContent found+  [ unwords ["Expected:", expect]+  , unwords ["Found:", take 60 $ XML.showContent found]   , unwords ["at line:", showLine found]   ]-showErr (ErrAttr expect parent) =+showErr (ErrExpectAttrib expect parent) =   unlines   [ unwords ["Missing attribute", show expect, "of", show $ qName $ elName parent]   , unwords ["at line:", showLine $ Elem parent]   ]+showErr (ErrAttribValue expect found parent) =+  unlines+  [ unwords ["Expected:", expect]+  , unwords ["Found:", show found]+  , unwords ["line:", showLine $ Elem parent]+  ] showErr (ErrMsg msg) =   unlines [msg]  showErr (ErrNull expected) = -  unlines [unwords ["Expected", expected]]+  unlines [unwords ["Expected:", expected]]  showErr (ErrEnd found) =   unlines [unwords ["Unexpected:", take 60 $ XML.showContent found]]
xml-extractors.cabal view
@@ -1,5 +1,5 @@ name:                xml-extractors-version:             0.3.0.0+version:             0.4.0.0 synopsis:            Wrapper over xml to extract data from parsed xml description:   This is a library to simplify extraction of data from parsed xml.