]> gitweb.michael.orlitzky.com - dead/htsn-import.git/blob - Xml.hs
dfb6d055ac0e297b94d42d92eff810e1464d42ec
[dead/htsn-import.git] / Xml.hs
1 {-# LANGUAGE TypeFamilies #-}
2
3 -- | General XML stuff.
4 --
5 module Xml (
6 ToFromXml(..),
7 parse_opts,
8 pickle_unpickle )
9 where
10
11 import Database.Groundhog ( AutoKey )
12 import Text.XML.HXT.Core (
13 (>>>),
14 (/>),
15 SysConfigList,
16 XmlPickler(..),
17 hasName,
18 no,
19 readDocument,
20 runX,
21 withPreserveComment,
22 withRemoveWS,
23 withSubstDTDEntities,
24 withValidate,
25 xpickleVal,
26 xunpickleVal,
27 yes )
28
29
30 -- | A typeclass for types which can be converted into an associated
31 -- XML type. The story behind this is long, but basically, we need
32 -- to different types for each XML thingie we're going to import: a
33 -- database type and an XML type. Both Groundhog and HXT are very
34 -- particular about the types that they can use, and there's no way
35 -- to reuse e.g. a type that HXT can pickle in Groundhog. So this
36 -- typeclass gives us a way to get the XML type from the Groundhog
37 -- type.
38 --
39 -- At first there appears to be an equally-valid approach, getting the
40 -- Groundhog type from the XML one. But Groundhog won't use type family
41 -- instances, so here we are.
42 --
43 class ToFromXml a where
44 -- | Each instance a must declare its associated XML type (Xml a)
45 type Xml a :: *
46 type Container a :: *
47
48 -- | And provide a function for getting an (Xml a) out of an "a."
49 to_xml :: a -> Xml a
50
51 -- | And provide a function for getting an "a" out of an (Xml a).
52 from_xml :: Xml a -> a
53
54 -- | Often we need to provide a key to use as a foreign key into
55 -- some container. If the instance "belongs" to some other object,
56 -- then it might need to be passed a key before it can un-XML
57 -- itself. For example, the XML version of 'NewsTeam' doesn't
58 -- contain a message ID which is part of its database type.
59 from_xml_fk :: AutoKey (Container a) -> Xml a -> a
60 from_xml_fk _ = from_xml
61
62 -- | A list of options passed to 'readDocument' when we parse an XML
63 -- document. We don't validate because the DTDs from TSN are
64 -- wrong. As a result, we don't want to keep useless DTDs
65 -- areound. Thus we disable 'withSubstDTDEntities' which, when
66 -- combined with "withValidate no", prevents HXT from trying to read
67 -- the DTD at all.
68 --
69 parse_opts :: SysConfigList
70 parse_opts =
71 [ withPreserveComment no,
72 withRemoveWS yes,
73 withSubstDTDEntities no,
74 withValidate no ]
75
76
77 -- | Given a root element name and a file path, return both the
78 -- original unpickled root "object" and the one that was constructed
79 -- by pickled and unpickling the original. This is used in a number
80 -- of XML tests which pickle/unpickle and then make sure that the
81 -- output is the same as the input.
82 --
83 -- We return the object instead of an XmlTree (which would save us
84 -- an unpickle call) because otherwise the type of @a@ in the call
85 -- to 'xpickle' would be ambiguous. By returning some @a@s, we allow
86 -- the caller to annotate its type.
87 --
88 pickle_unpickle :: XmlPickler a
89 => String
90 -> FilePath
91 -> IO ([a], [a])
92 pickle_unpickle root_element filepath = do
93 -- We need to check only the root message element since
94 -- readDocument produces a bunch of other junk.
95 expected <- runX $ arr_getobj
96 actual <- runX $ arr_getobj
97 >>>
98 xpickleVal xpickle
99 >>>
100 xunpickleVal xpickle
101
102 return (expected, actual)
103 where
104 arr_getobj = readDocument parse_opts filepath
105 />
106 hasName root_element
107 >>>
108 xunpickleVal xpickle