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 ]
85 -- | Given a root element name and a file path, return both the
86 -- original unpickled root "object" and the one that was constructed
87 -- by pickled and unpickling the original. This is used in a number
88 -- of XML tests which pickle/unpickle and then make sure that the
89 -- output is the same as the input.
91 -- We return the object instead of an XmlTree (which would save us
92 -- an unpickle call) because otherwise the type of @a@ in the call
93 -- to 'xpickle' would be ambiguous. By returning some @a@s, we allow
94 -- the caller to annotate its type.
96 -- Note that this will happily pickle nothing to nothing and then
97 -- unpickle it back to more nothing. So the fact that the
98 -- before/after results from this function agree does not mean that
99 -- the document was successfully unpickled!
101 pickle_unpickle :: XmlPickler a
105 pickle_unpickle root_element filepath = do
106 -- We need to check only the root message element since
107 -- readDocument produces a bunch of other junk.
108 expected <- runX arr_getobj
109 actual <- runX $ arr_getobj
115 return (expected, actual)
117 arr_getobj = readDocument parse_opts filepath
125 -- | Is the given XML file unpickleable? Unpickling will be attempted
126 -- using the @unpickler@ argument. If we unilaterally used the
127 -- generic 'xpickle' function for our unpickler, a type ambiguity
128 -- would result. By taking the unpickler as an argument, we allow
129 -- the caller to indirectly specify a concrete type.
131 unpickleable :: XmlPickler a => FilePath -> PU a -> IO Bool
132 unpickleable filepath unpickler = do
133 xmldoc <- try_unpickle `catch` (\(SomeException _) -> return [])
134 return $ (not . null) xmldoc
136 try_unpickle = runX $ readDocument parse_opts filepath
138 xunpickleVal unpickler