xml-helpers-1.0.0/0000755000000000000000000000000011546177733012154 5ustar0000000000000000xml-helpers-1.0.0/LICENSE0000644000000000000000000000271711546177733013170 0ustar0000000000000000Copyright (c) 2011 Adam Wick All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name Adam Wick nor the names of any contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. xml-helpers-1.0.0/Setup.hs0000644000000000000000000000005611546177733013611 0ustar0000000000000000import Distribution.Simple main = defaultMain xml-helpers-1.0.0/xml-helpers.cabal0000644000000000000000000000114211546177733015376 0ustar0000000000000000Name: xml-helpers Version: 1.0.0 Build-Type: Simple Cabal-Version: >= 1.6 License: BSD3 License-File: LICENSE Author: Adam Wick Maintainer: Adam Wick Homepage: http://github.com/acw/xml-helpers Category: Text, XML Synopsis: Some useful helper functions for the xml library. Description: Included are some folds and maps I've found useful in parsing XML data. Library Build-Depends: base >= 4.0 && < 5.0, xml >= 1.3 && < 1.4 Exposed-Modules: Text.XML.Light.Helpers source-repository head type: git location: http://github.com/acw/xml-helpers xml-helpers-1.0.0/Text/0000755000000000000000000000000011546177733013100 5ustar0000000000000000xml-helpers-1.0.0/Text/XML/0000755000000000000000000000000011546177733013540 5ustar0000000000000000xml-helpers-1.0.0/Text/XML/Light/0000755000000000000000000000000011546177733014607 5ustar0000000000000000xml-helpers-1.0.0/Text/XML/Light/Helpers.hs0000644000000000000000000001010111546177733016536 0ustar0000000000000000-- |A set of useful helper functions for dealing with XML data. module Text.XML.Light.Helpers where import Control.Monad import Data.Maybe import Text.XML.Light -- |Map the given function over the children of the given element with the -- given name. mapChildren :: String -> Element -> (Element -> Maybe a) -> Maybe [a] mapChildren s e f = sequence $ map f $ findChildren (unqual s) e -- |Fold the function over the children of the given element with the given -- name. foldChildren :: String -> Element -> a -> (a -> Element -> Maybe a) -> Maybe a foldChildren s e b f = foldM f b $ findChildren (unqual s) e -- |Map the given function over all subelements of the given element with -- the given name. mapElements :: String -> Element -> (Element -> Maybe a) -> Maybe [a] mapElements s e f = sequence $ map f $ findElements (unqual s) e -- |Fold the given function over the children of the given element with -- the given name. foldElements :: String -> Element -> a -> (a -> Element -> Maybe a) -> Maybe a foldElements s e b f = foldM f b $ findElements (unqual s) e -- -- |Map the given function over the children of the given element that -- have an attribute "name" matching the given string. mapChildrenWithAttName :: String -> Element -> (Element -> Maybe a) -> Maybe [a] mapChildrenWithAttName s e f = mapM f $ findChildrenWithAttName s e -- |Map the given function over the subelements of the given element that -- have an attribute "name" matching the given string. mapElementsWithAttName :: String -> Element -> (Element -> Maybe a) -> Maybe [a] mapElementsWithAttName s e f = mapM f $ findElementsWithAttName s e -- |Fold the given function over the children of the given element that -- have an attribute "name" matching the given string. foldChildrenWithAttName :: String -> Element -> a -> (a -> Element -> Maybe a) -> Maybe a foldChildrenWithAttName s e b f = foldM f b $ findChildrenWithAttName s e -- |Fold the given function over the subelements of the given element that -- have an attribute "name" matching the given string. foldElementsWithAttName :: String -> Element -> a -> (a -> Element -> Maybe a) -> Maybe a foldElementsWithAttName s e b f = foldM f b $ findElementsWithAttName s e -- -- |Get the string contents of the child of the given element with the given -- name. getChildData :: String -> Element -> Maybe String getChildData s x = strContent `fmap` findChild (unqual s) x -- |Get the string contents of the subelement of the given element with the -- given name. getElementData :: String -> Element -> Maybe String getElementData s x = strContent `fmap` findElement (unqual s) x -- -- |Find a child of the given element with that has an attribute "name" -- equal to the given string. findChildWithAttName :: String -> Element -> Maybe Element findChildWithAttName s = listToMaybe . findChildrenWithAttName s -- |Find all the children of the given element that have an attribute "name" -- equal to the given string. findChildrenWithAttName :: String -> Element -> [Element] findChildrenWithAttName s = filterChildren (elementHasNameAttr s) -- |Find a subelement of the given element that has an attribute "name" -- equal to the given string. findElementWithAttName :: String -> Element -> Maybe Element findElementWithAttName s = listToMaybe . findElementsWithAttName s -- |Find all the subelements of the given element that have an attribute -- "name" equal to the given string. findElementsWithAttName :: String -> Element -> [Element] findElementsWithAttName s = filterElements (elementHasNameAttr s) -- |Returns True iff the given alement has an attribute "name" equal to -- the given string. elementHasNameAttr :: String -> Element -> Bool elementHasNameAttr s e = case findAttr (unqual "name") e of Nothing -> False Just v -> s == v -- |Convert a list of rows (subelement with the name "row") into a Haskell -- datatype using the given function.s parseRows :: (Element -> Maybe a) -> Element -> Maybe [a] parseRows f xml = sequence $ map f $ findElements (unqual "row") xml