diff --git a/lib/Text/InvisiXML/Namespace.hs b/lib/Text/InvisiXML/Namespace.hs index 0cd7220a17507b71abe3d229dcbf691731a7b28a..9449141e4bd39a938d088a05c170c6586f9d6360 100644 --- a/lib/Text/InvisiXML/Namespace.hs +++ b/lib/Text/InvisiXML/Namespace.hs @@ -3,13 +3,12 @@ module Text.InvisiXML.Namespace ( uRI , prefix , ixml - --, at - --, to , addAttr + , getAttr , setChildren ) where ---import Data.PositionTree (Position(..)) +import Data.List (find) import Text.XML.Light (Attr(..), Content(..), Element(..), QName(..), add_attr) uRI :: String @@ -21,14 +20,6 @@ prefix = "ixml" ixml :: String -> QName ixml qName = QName {qName, qURI = Just uRI, qPrefix = Just prefix} -{- -at :: Position -> Attr -at (Position p) = Attr (ixml "at") (show p) - -to :: Position -> Attr -to (Position p) = Attr (ixml "to") (show p) --} - onContent :: (Element -> Element) -> Content -> Content onContent f (Elem e) = Elem (f e) onContent _ x = x @@ -36,5 +27,8 @@ onContent _ x = x addAttr :: Attr -> Content -> Content addAttr = onContent . add_attr +getAttr :: QName -> Element -> Maybe String +getAttr key = fmap attrVal . find ((==) key . attrKey) . elAttribs + setChildren :: [Content] -> Content -> Content setChildren elContent = onContent $ \e -> e {elContent} diff --git a/lib/Text/XML/Light/Serializer.hs b/lib/Text/XML/Light/Serializer.hs index 87ea39003d5cdd79fb620b0855ed9cc228a0b2e3..fd92aea5cfdf38dfd2786684a0f95ccd86270aa4 100644 --- a/lib/Text/XML/Light/Serializer.hs +++ b/lib/Text/XML/Light/Serializer.hs @@ -1,12 +1,15 @@ module Text.XML.Light.Serializer ( FromXML(..) , ToXML(..) - , (.=) , decode + , eitherDecode , encode + , (.=) + , expected ) where import Data.List (intercalate) +import Text.Printf (printf) import Text.XML.Light (Attr(..), Content, QName, parseXML, ppContent) import Text.XML.Light.Lexer (XmlSource) @@ -16,14 +19,18 @@ class FromXML a where class ToXML a where toXML :: a -> [Content] -encode :: ToXML a => a -> String -encode = intercalate "\n" . fmap ppContent . toXML - decode :: (XmlSource s, FromXML a) => s -> Maybe a decode = either (\_ -> Nothing) Just . eitherDecode eitherDecode :: (XmlSource s, FromXML a) => s -> Either String a eitherDecode = fromXML . parseXML +encode :: ToXML a => a -> String +encode = intercalate "\n" . fmap ppContent . toXML + (.=) :: Show v => QName -> v -> Attr k .= v = Attr k $ show v + +expected :: String -> [Content] -> Either String a +expected elemType = + Left . printf "Expected %s but got %s" elemType . concat . fmap ppContent