]> gitweb.michael.orlitzky.com - dead/htsn-import.git/blob - src/Xml.hs
Add 'unpickleable' to Xml for use in tests.
[dead/htsn-import.git] / src / Xml.hs
1 {-# LANGUAGE TypeFamilies #-}
2
3 -- | General XML stuff.
4 --
5 module Xml (
6 DtdName(..),
7 ToFromXml(..),
8 parse_opts,
9 pickle_unpickle,
10 unpickleable )
11 where
12
13 import Control.Exception ( SomeException(..), catch )
14 import Database.Groundhog ( AutoKey )
15 import Text.XML.HXT.Core (
16 (>>>),
17 (/>),
18 PU,
19 SysConfigList,
20 XmlPickler(..),
21 hasName,
22 no,
23 readDocument,
24 runX,
25 withPreserveComment,
26 withRemoveWS,
27 withSubstDTDEntities,
28 withValidate,
29 xpickleVal,
30 xunpickleVal,
31 yes )
32
33
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
41 -- type.
42 --
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.
46 --
47 class ToFromXml a where
48 -- | Each instance a must declare its associated XML type (Xml a)
49 type Xml a :: *
50 type Container a :: *
51
52 -- | And provide a function for getting an (Xml a) out of an "a."
53 to_xml :: a -> Xml a
54
55 -- | And provide a function for getting an "a" out of an (Xml a).
56 from_xml :: Xml a -> a
57
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
65
66
67 -- | Represents the DTD filename ("SYSTEM") part of the DOCTYPE
68 -- definition.
69 newtype DtdName = DtdName String
70
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
76 -- the DTD at all.
77 --
78 parse_opts :: SysConfigList
79 parse_opts =
80 [ withPreserveComment no,
81 withRemoveWS yes,
82 withSubstDTDEntities no,
83 withValidate no ]
84
85
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.
91 --
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.
96 --
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!
101 --
102 pickle_unpickle :: XmlPickler a
103 => String
104 -> FilePath
105 -> IO ([a], [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
111 >>>
112 xpickleVal xpickle
113 >>>
114 xunpickleVal xpickle
115
116 return (expected, actual)
117 where
118 arr_getobj = readDocument parse_opts filepath
119 />
120 hasName root_element
121 >>>
122 xunpickleVal xpickle
123
124
125
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.
131 --
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
136 where
137 try_unpickle = runX $ readDocument parse_opts filepath
138 >>>
139 xunpickleVal unpickler