hxt-9.1.0: A collection of tools for processing XML with Haskell.

Portabilityportable
Stabilityexperimental
MaintainerUwe Schmidt (uwe@fh-wedel.de)

Text.XML.HXT.Arrow.Pickle

Description

Pickler functions for converting between user defined data types and XmlTree data. Usefull for persistent storage and retreival of arbitray data as XML documents

This module is an adaptation of the pickler combinators developed by Andrew Kennedy ( http://research.microsoft.com/~akenn/fun/picklercombinators.pdf )

The difference to Kennedys approach is that the target is not a list of Chars but a list of XmlTrees. The basic picklers will convert data into XML text nodes. New are the picklers for creating elements and attributes.

One extension was neccessary: The unpickling may fail. Therefore the unpickler has a Maybe result type. Failure is used to unpickle optional elements (Maybe data) and lists of arbitray length

There is an example program demonstrating the use of the picklers for a none trivial data structure. (see "examples/arrows/pickle" directory)

Synopsis

Documentation

xpickleDocument :: PU a -> SysConfigList -> String -> IOStateArrow s a XmlTreeSource

store an arbitray value in a persistent XML document

The pickler converts a value into an XML tree, this is written out with Text.XML.HXT.Arrow.writeDocument. The option list is passed to Text.XML.HXT.Arrow.writeDocument

An option evaluated by this arrow is a_addDTD. If a_addDTD is set (v_1), the pickler DTD is added as an inline DTD into the document.

xunpickleDocument :: PU a -> SysConfigList -> String -> IOStateArrow s b aSource

read an arbitray value from an XML document

The document is read with Text.XML.HXT.Arrow.readDocument. Options are passed to Text.XML.HXT.Arrow.readDocument. The conversion from XmlTree is done with the pickler.

xpickleDocument xp al dest >>> xunpickleDocument xp al' dest is the identity arrow when applied with the appropriate options. When during pickling indentation is switched on, the whitespace must be removed during unpickling.

xpickleWriteDTD :: PU b -> SysConfigList -> String -> IOStateArrow s b XmlTreeSource

Write out the DTD generated out of a pickler. Calls xpicklerDTD

xpickleDTD :: PU b -> IOStateArrow s b XmlTreeSource

The arrow for generating the DTD out of a pickler

A DTD is generated from a pickler and check for consistency. Errors concerning the DTD are issued.

checkPickler :: Eq a => PU a -> IOStateArrow s a aSource

An arrow for checking picklers

A value is transformed into an XML document by a given pickler, the associated DTD is extracted from the pickler and checked, the document including the DTD is tranlated into a string, this string is read and validated against the included DTD, and unpickled. The last step is the equality with the input.

If the check succeeds, the arrow works like this, else it fails.

xpickleVal :: ArrowXml a => PU b -> a b XmlTreeSource

The arrow version of the pickler function

xunpickleVal :: ArrowXml a => PU b -> a XmlTree bSource

The arrow version of the unpickler function

thePicklerDTD :: PU b -> XmlTreesSource

Compute the associated DTD of a pickler

a_addDTD :: StringSource

Option for generating and adding DTD when document is pickled

pickleDoc :: PU a -> a -> XmlTreeSource

conversion of an arbitrary value into an XML document tree.

The pickler, first parameter, controls the conversion process. Result is a complete document tree including a root node

unpickleDoc :: PU a -> XmlTree -> Maybe aSource

Conversion of an XML document tree into an arbitrary data type

The inverse of pickleDoc. This law should hold for all picklers: unpickle px . pickle px $ v == Just v . Not every possible combination of picklers make sense. For reconverting a value from an XML tree, is becomes neccessary, to introduce "enough" markup for unpickling the value

showPickled :: XmlPickler a => SysConfigList -> a -> StringSource

Pickles a value, then writes the document to a string.

data PU a Source

Constructors

PU 

Fields

appPickle :: (a, St) -> St
 
appUnPickle :: St -> (Maybe a, St)
 
theSchema :: Schema
 

class XmlPickler a whereSource

The class for overloading xpickle, the default pickler

Methods

xpickle :: PU aSource

xp4Tuple :: PU a -> PU b -> PU c -> PU d -> PU (a, b, c, d)Source

Like xpPair and xpTriple but for 4-tuples

xp5Tuple :: PU a -> PU b -> PU c -> PU d -> PU e -> PU (a, b, c, d, e)Source

Like xpPair and xpTriple but for 5-tuples

xp6Tuple :: PU a -> PU b -> PU c -> PU d -> PU e -> PU f -> PU (a, b, c, d, e, f)Source

Like xpPair and xpTriple but for 6-tuples

xp7Tuple :: PU a -> PU b -> PU c -> PU d -> PU e -> PU f -> PU g -> PU (a, b, c, d, e, f, g)Source

Like xpPair and xpTriple but for 7-tuples

Thanks to Tony Morris for doing xp7Tuple, ..., xp24Tuple.

xp8Tuple :: PU a -> PU b -> PU c -> PU d -> PU e -> PU f -> PU g -> PU h -> PU (a, b, c, d, e, f, g, h)Source

xp9Tuple :: PU a -> PU b -> PU c -> PU d -> PU e -> PU f -> PU g -> PU h -> PU i -> PU (a, b, c, d, e, f, g, h, i)Source

xp10Tuple :: PU a -> PU b -> PU c -> PU d -> PU e -> PU f -> PU g -> PU h -> PU i -> PU j -> PU (a, b, c, d, e, f, g, h, i, j)Source

xp11Tuple :: PU a -> PU b -> PU c -> PU d -> PU e -> PU f -> PU g -> PU h -> PU i -> PU j -> PU k -> PU (a, b, c, d, e, f, g, h, i, j, k)Source

xp12Tuple :: PU a -> PU b -> PU c -> PU d -> PU e -> PU f -> PU g -> PU h -> PU i -> PU j -> PU k -> PU l -> PU (a, b, c, d, e, f, g, h, i, j, k, l)Source

xp13Tuple :: PU a -> PU b -> PU c -> PU d -> PU e -> PU f -> PU g -> PU h -> PU i -> PU j -> PU k -> PU l -> PU m -> PU (a, b, c, d, e, f, g, h, i, j, k, l, m)Source

xp14Tuple :: PU a -> PU b -> PU c -> PU d -> PU e -> PU f -> PU g -> PU h -> PU i -> PU j -> PU k -> PU l -> PU m -> PU n -> PU (a, b, c, d, e, f, g, h, i, j, k, l, m, n)Source

xp15Tuple :: PU a -> PU b -> PU c -> PU d -> PU e -> PU f -> PU g -> PU h -> PU i -> PU j -> PU k -> PU l -> PU m -> PU n -> PU o -> PU (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o)Source

xp16Tuple :: PU a -> PU b -> PU c -> PU d -> PU e -> PU f -> PU g -> PU h -> PU i -> PU j -> PU k -> PU l -> PU m -> PU n -> PU o -> PU p -> PU (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p)Source

xp17Tuple :: PU a -> PU b -> PU c -> PU d -> PU e -> PU f -> PU g -> PU h -> PU i -> PU j -> PU k -> PU l -> PU m -> PU n -> PU o -> PU p -> PU q -> PU (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q)Source

xp18Tuple :: PU a -> PU b -> PU c -> PU d -> PU e -> PU f -> PU g -> PU h -> PU i -> PU j -> PU k -> PU l -> PU m -> PU n -> PU o -> PU p -> PU q -> PU r -> PU (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r)Source

xp19Tuple :: PU a -> PU b -> PU c -> PU d -> PU e -> PU f -> PU g -> PU h -> PU i -> PU j -> PU k -> PU l -> PU m -> PU n -> PU o -> PU p -> PU q -> PU r -> PU s -> PU (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s)Source

xp20Tuple :: PU a -> PU b -> PU c -> PU d -> PU e -> PU f -> PU g -> PU h -> PU i -> PU j -> PU k -> PU l -> PU m -> PU n -> PU o -> PU p -> PU q -> PU r -> PU s -> PU t -> PU (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t)Source

xp21Tuple :: PU a -> PU b -> PU c -> PU d -> PU e -> PU f -> PU g -> PU h -> PU i -> PU j -> PU k -> PU l -> PU m -> PU n -> PU o -> PU p -> PU q -> PU r -> PU s -> PU t -> PU u -> PU (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u)Source

xp22Tuple :: PU a -> PU b -> PU c -> PU d -> PU e -> PU f -> PU g -> PU h -> PU i -> PU j -> PU k -> PU l -> PU m -> PU n -> PU o -> PU p -> PU q -> PU r -> PU s -> PU t -> PU u -> PU v -> PU (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v)Source

xp23Tuple :: PU a -> PU b -> PU c -> PU d -> PU e -> PU f -> PU g -> PU h -> PU i -> PU j -> PU k -> PU l -> PU m -> PU n -> PU o -> PU p -> PU q -> PU r -> PU s -> PU t -> PU u -> PU v -> PU w -> PU (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w)Source

xp24Tuple :: PU a -> PU b -> PU c -> PU d -> PU e -> PU f -> PU g -> PU h -> PU i -> PU j -> PU k -> PU l -> PU m -> PU n -> PU o -> PU p -> PU q -> PU r -> PU s -> PU t -> PU u -> PU v -> PU w -> PU x -> PU (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x)Source

Hopefully no one needs a xp25Tuple

xpAddFixedAttr :: String -> String -> PU a -> PU aSource

Add an attribute with a fixed value.

Useful e.g. to declare namespaces. Is implemented by xpAttrFixed

xpAlt :: (a -> Int) -> [PU a] -> PU aSource

Pickler for sum data types.

Every constructor is mapped to an index into the list of picklers. The index is used only during pickling, not during unpickling, there the 1. match is taken

xpAttr :: String -> PU a -> PU aSource

convenient Pickler for xpAttrQN

 xpAttr n = xpAttrQN (mkName n)

xpTextAttr :: String -> PU StringSource

A text attribute.

xpAttrImplied :: String -> PU a -> PU (Maybe a)Source

Add an optional attribute for an optional value (Maybe a).

xpChoice :: PU b -> PU a -> (a -> PU b) -> PU bSource

combine tow picklers with a choice

Run two picklers in sequence like with xpSeq. When during unpickling the first one fails, an alternative pickler (first argument) is applied. This pickler is only used as combinator for unpickling.

xpCondSeq :: PU b -> (b -> a) -> PU a -> (a -> PU b) -> PU bSource

pickle/unpickle combinator for sequence and choice.

When the first unpickler fails, the second one is taken, else the third one configured with the result from the first is taken. This pickler is a generalisation for xpSeq and xpChoice .

The schema must be attached later, e.g. in xpPair or other higher level combinators

xpDefault :: Eq a => a -> PU a -> PU aSource

Optional conversion with default value

The default value is not encoded in the XML document, during unpickling the default value is inserted if the pickler fails

xpElem :: String -> PU a -> PU aSource

convenient Pickler for xpElemQN

 xpElem n = xpElemQN (mkName n)

xpElemWithAttrValue :: String -> String -> String -> PU a -> PU aSource

Pickler for wrapping/unwrapping data into an XML element with an attribute with given value

To make XML structures flexible but limit the number of different elements, it's sometimes useful to use a kind of generic element with a key value structure

Example:

 <attr name="key1">value1</attr>
 <attr name="key2">value2</attr>
 <attr name="key3">value3</attr>

the Haskell datatype may look like this

 type T = T { key1 :: Int ; key2 :: String ; key3 :: Double }

Then the picker for that type looks like this

 xpT :: PU T
 xpT = xpWrap ( uncurry3 T, \ t -> (key1 t, key2 t, key3 t) ) $
       xpTriple (xpElemWithAttrValue "attr" "name" "key1" $ xpickle)
                (xpElemWithAttrValue "attr" "name" "key2" $ xpText0)
                (xpElemWithAttrValue "attr" "name" "key3" $ xpickle)

xpLift :: a -> PU aSource

Lift a value to a pickler

When pickling, nothing is encoded, when unpickling, the given value is inserted. This pickler always succeeds.

xpLiftMaybe :: Maybe a -> PU aSource

Lift a Maybe value to a pickler.

Nothing is mapped to the zero pickler, Just x is pickled with xpLift x.

xpList :: PU a -> PU [a]Source

Encoding of list values by pickling all list elements sequentially.

Unpickler relies on failure for detecting the end of the list. The standard pickler for lists. Can also be used in combination with xpWrap for constructing set and map picklers

xpList1 :: PU a -> PU [a]Source

Encoding of a none empty list of values

Attention: when calling this pickler with an empty list, an internal error "head of empty list is raised".

xpMap :: Ord k => String -> String -> PU k -> PU v -> PU (Map k v)Source

Standard pickler for maps

This pickler converts a map into a list of pairs. All key value pairs are mapped to an element with name (1.arg), the key is encoded as an attribute named by the 2. argument, the 3. arg is the pickler for the keys, the last one for the values

xpOption :: PU a -> PU (Maybe a)Source

Encoding of optional data by ignoring the Nothing case during pickling and relying on failure during unpickling to recompute the Nothing case

The default pickler for Maybe types

xpPair :: PU a -> PU b -> PU (a, b)Source

pickle a pair of values sequentially

Used for pairs or together with wrap for pickling algebraic data types with two components

xpPrim :: (Read a, Show a) => PU aSource

Pickle an arbitrary value by applyling show during pickling and read during unpickling.

Real pickling is then done with xpText. One of the most often used pimitive picklers. Applicable for all types which are instances of Read and Show

xpInt :: PU IntSource

Pickle an unsigned

xpSeq :: (b -> a) -> PU a -> (a -> PU b) -> PU bSource

Combine two picklers sequentially.

If the first fails during unpickling, the whole unpickler fails

xpText :: PU StringSource

Pickle a string into an XML text node

One of the most often used primitive picklers. Attention: For pickling empty strings use xpText0. If the text has a more specific datatype than xsd:string, use xpTextDT

xpText0 :: PU StringSource

Pickle a possibly empty string into an XML node.

Must be used in all places, where empty strings are legal values. If the content of an element can be an empty string, this string disapears during storing the DOM into a document and reparse the document. So the empty text node becomes nothing, and the pickler must deliver an empty string, if there is no text node in the document.

xpTextDT :: Schema -> PU StringSource

Pickle a string into an XML text node

Text pickler with a description of the structure of the text by a schema. A schema for a data type can be defined by scDT. In Text.XML.HXT.Arrow.Pickle.Schema there are some more functions for creating simple datatype descriptions.

xpText0DT :: Schema -> PU StringSource

Pickle a possibly empty string with a datatype description into an XML node.

Like xpText0 but with extra Parameter for datatype description as in xpTextDT.

xpTree :: PU XmlTreeSource

Pickle an XmlTree by just adding it

Usefull for components of type XmlTree in other data structures

xpTrees :: PU [XmlTree]Source

Pickle a whole list of XmlTrees by just adding the list, unpickle is done by taking all element contents.

This pickler should always be combined with xpElem for taking the whole contents of an element.

xpTriple :: PU a -> PU b -> PU c -> PU (a, b, c)Source

Like xpPair but for triples

xpWrap :: (a -> b, b -> a) -> PU a -> PU bSource

map value into another domain and apply pickler there

One of the most often used picklers.

xpWrapMaybe :: (a -> Maybe b, b -> a) -> PU a -> PU bSource

like xpWrap, but if the inverse mapping is undefined, the unpickler fails

Map a value into another domain. If the inverse mapping is undefined (Nothing), the unpickler fails

xpXmlText :: PU StringSource

Pickle a string representing XML contents by inserting the tree representation into the XML document.

Unpickling is done by converting the contents with xshowEscapeXml into a string, this function will escape all XML special chars, such that pickling the value back becomes save. Pickling is done with xread

xpZero :: PU aSource

The zero pickler

Encodes nothing, fails always during unpickling

data Schema Source

The datatype for modelling the structure of an

Instances