]> gitweb.michael.orlitzky.com - dead/htsn-import.git/blob - src/Xml.hs
Begin validating the XML.
[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
84
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.
90 --
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.
95 --
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!
100 --
101 pickle_unpickle :: XmlPickler a
102 => String
103 -> FilePath
104 -> IO ([a], [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
110 >>>
111 xpickleVal xpickle
112 >>>
113 xunpickleVal xpickle
114
115 return (expected, actual)
116 where
117 arr_getobj = readDocument parse_opts filepath
118 />
119 hasName root_element
120 >>>
121 xunpickleVal xpickle
122
123
124
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.
130 --
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
135 where
136 try_unpickle = runX $ readDocument parse_opts filepath
137 >>>
138 xunpickleVal unpickler