1 {-# LANGUAGE TypeFamilies #-}
3 -- | General XML stuff.
14 import Control.Exception ( SomeException(..), catch )
15 import Text.XML.HXT.Core (
31 -- | A typeclass for XML types that can be converted into an
32 -- associated database type. The story behind this is long, but
33 -- basically, we need to different types most XML thingies we're
34 -- going to import: a database type and an XML type.
36 -- Both Groundhog and HXT are very particular about the types that
37 -- they can use, and there's no way to reuse e.g. a type that HXT
38 -- can pickle in Groundhog. This typeclass gives us a standard way
39 -- to get the database type from the XML type that we have to define
43 -- | Each instance @a@ must declare its associated database type @Db a@.
46 -- | And provide a function for getting a @Db a@ out of an @a@.
50 -- | Represents the DTD filename (\"SYSTEM\") part of the DOCTYPE
52 newtype DtdName = DtdName String
54 -- | A list of options passed to 'readDocument' when we parse an XML
55 -- document. All cosmetic whitespace should be removed, otherwise we
56 -- would have to parse whitespace in each (un)pickler.
58 parse_opts :: SysConfigList
59 parse_opts = [ withRemoveWS yes, withValidate no ]
62 -- | Given an @unpickler@ and a @filepath@, attempt to unpickle the
63 -- root element of @filepath@ using @unpickler@ and return both the
64 -- original unpickled object and one constructed by pickling and
65 -- unpickling that original. This is used in a number of XML tests
66 -- which pickle/unpickle and then make sure that the output is the
69 -- We return the object instead of an XmlTree (which would save us
70 -- an unpickle call) because otherwise the type of @a@ in the call
71 -- to 'xpickle' would be ambiguous. By returning some @a@s, we allow
72 -- the caller to annotate its type.
74 -- Note that this will happily pickle nothing to nothing and then
75 -- unpickle it back to more nothing. So the fact that the
76 -- before/after results from this function agree does not mean that
77 -- the document was successfully unpickled!
79 pickle_unpickle :: PU a -- ^ @unpickler@ returning an @a@
80 -> FilePath -- ^ Path to the document to unpickle.
82 pickle_unpickle unpickler filepath = do
83 -- We need to check only the root message element since
84 -- readDocument produces a bunch of other junk.
85 expected <- runX arr_getobj
86 actual <- runX $ arr_getobj
90 xunpickleVal unpickler
92 return (expected, actual)
94 arr_getobj = readDocument parse_opts filepath
96 isElem -- Drop the extra junk readDocument pulls in.
98 xunpickleVal unpickler
102 -- | Is the given XML file unpickleable? Unpickling will be attempted
103 -- using the @unpickler@ argument. If we unilaterally used the
104 -- generic 'xpickle' function for our unpickler, a type ambiguity
105 -- would result. By taking the unpickler as an argument, we allow
106 -- the caller to indirectly specify a concrete type.
108 -- Apologies the the name; unpickleable means \"we can unpickle
109 -- it\", not \"not pickleable.\"
111 unpickleable :: FilePath -> PU a -> IO Bool
112 unpickleable filepath unpickler = do
113 xmldoc <- try_unpickle `catch` (\(SomeException _) -> return [])
114 return $ (not . null) xmldoc
116 try_unpickle = runX $ readDocument parse_opts filepath
118 xunpickleVal unpickler