1 {-# LANGUAGE TypeFamilies #-}
3 -- | General XML stuff.
13 import Control.Exception ( SomeException(..), catch )
14 import Database.Groundhog ( AutoKey )
15 import Text.XML.HXT.Core (
34 -- | A typeclass for types which can be converted into an associated
35 -- XML type. The story behind this is long, but basically, we need
36 -- to different types for each XML thingie we're going to import: a
37 -- database type and an XML type. Both Groundhog and HXT are very
38 -- particular about the types that they can use, and there's no way
39 -- to reuse e.g. a type that HXT can pickle in Groundhog. So this
40 -- typeclass gives us a way to get the XML type from the Groundhog
43 -- At first there appears to be an equally-valid approach, getting the
44 -- Groundhog type from the XML one. But Groundhog won't use type family
45 -- instances, so here we are.
47 class ToFromXml a where
48 -- | Each instance a must declare its associated XML type (Xml a)
52 -- | And provide a function for getting an (Xml a) out of an "a."
55 -- | And provide a function for getting an "a" out of an (Xml a).
56 from_xml :: Xml a -> a
58 -- | Often we need to provide a key to use as a foreign key into
59 -- some container. If the instance "belongs" to some other object,
60 -- then it might need to be passed a key before it can un-XML
61 -- itself. For example, the XML version of 'NewsTeam' doesn't
62 -- contain a message ID which is part of its database type.
63 from_xml_fk :: AutoKey (Container a) -> Xml a -> a
64 from_xml_fk _ = from_xml
67 -- | Represents the DTD filename ("SYSTEM") part of the DOCTYPE
69 newtype DtdName = DtdName String
71 -- | A list of options passed to 'readDocument' when we parse an XML
72 -- document. We don't validate because the DTDs from TSN are
73 -- wrong. As a result, we don't want to keep useless DTDs
74 -- areound. Thus we disable 'withSubstDTDEntities' which, when
75 -- combined with "withValidate no", prevents HXT from trying to read
78 parse_opts :: SysConfigList
80 [ withPreserveComment no,
82 withSubstDTDEntities no,
86 -- | Given a root element name and a file path, return both the
87 -- original unpickled root "object" and the one that was constructed
88 -- by pickled and unpickling the original. This is used in a number
89 -- of XML tests which pickle/unpickle and then make sure that the
90 -- output is the same as the input.
92 -- We return the object instead of an XmlTree (which would save us
93 -- an unpickle call) because otherwise the type of @a@ in the call
94 -- to 'xpickle' would be ambiguous. By returning some @a@s, we allow
95 -- the caller to annotate its type.
97 -- Note that this will happily pickle nothing to nothing and then
98 -- unpickle it back to more nothing. So the fact that the
99 -- before/after results from this function agree does not mean that
100 -- the document was successfully unpickled!
102 pickle_unpickle :: XmlPickler a
106 pickle_unpickle root_element filepath = do
107 -- We need to check only the root message element since
108 -- readDocument produces a bunch of other junk.
109 expected <- runX arr_getobj
110 actual <- runX $ arr_getobj
116 return (expected, actual)
118 arr_getobj = readDocument parse_opts filepath
126 -- | Is the given XML file unpickleable? Unpickling will be attempted
127 -- using the @unpickler@ argument. If we unilaterally used the
128 -- generic 'xpickle' function for our unpickler, a type ambiguity
129 -- would result. By taking the unpickler as an argument, we allow
130 -- the caller to indirectly specify a concrete type.
132 unpickleable :: XmlPickler a => FilePath -> PU a -> IO Bool
133 unpickleable filepath unpickler = do
134 xmldoc <- try_unpickle `catch` (\(SomeException _) -> return [])
135 return $ (not . null) xmldoc
137 try_unpickle = runX $ readDocument parse_opts filepath
139 xunpickleVal unpickler