]> gitweb.michael.orlitzky.com - dead/htsn-import.git/blob - src/Xml.hs
Add a test for unpickling large Odds_XML files.
[dead/htsn-import.git] / src / Xml.hs
1 {-# LANGUAGE TypeFamilies #-}
2
3 -- | General XML stuff.
4 --
5 module Xml (
6 DtdName(..),
7 FromXml(..),
8 parse_opts,
9 pickle_unpickle,
10 unpickleable )
11 where
12
13 -- System imports.
14 import Control.Exception ( SomeException(..), catch )
15 import Text.XML.HXT.Core (
16 (>>>),
17 (/>),
18 PU,
19 SysConfigList,
20 isElem,
21 no,
22 readDocument,
23 runX,
24 withRemoveWS,
25 withValidate,
26 xpickleVal,
27 xunpickleVal,
28 yes )
29
30
31 -- | A typeclass for XML types that can be converted into an
32 -- associated database type. The story behind this is long, but
33 -- basically, we need to different types most XML thingies we're
34 -- going to import: a database type and an XML type.
35 --
36 -- Both Groundhog and HXT are very particular about the types that
37 -- they can use, and there's no way to reuse e.g. a type that HXT
38 -- can pickle in Groundhog. This typeclass gives us a standard way
39 -- to get the database type from the XML type that we have to define
40 -- for HXT.
41 --
42 class FromXml a where
43 -- | Each instance @a@ must declare its associated database type @Db a@.
44 type Db a :: *
45
46 -- | And provide a function for getting a @Db a@ out of an @a@.
47 from_xml :: a -> Db a
48
49
50 -- | Represents the DTD filename (\"SYSTEM\") part of the DOCTYPE
51 -- definition.
52 newtype DtdName = DtdName String
53
54 -- | A list of options passed to 'readDocument' when we parse an XML
55 -- document. All cosmetic whitespace should be removed, otherwise we
56 -- would have to parse whitespace in each (un)pickler.
57 --
58 parse_opts :: SysConfigList
59 parse_opts = [ withRemoveWS yes, withValidate no ]
60
61
62 -- | Given an @unpickler@ and a @filepath@, attempt to unpickle the
63 -- root element of @filepath@ using @unpickler@ and return both the
64 -- original unpickled object and one constructed by pickling and
65 -- unpickling that original. This is used in a number of XML tests
66 -- which pickle/unpickle and then make sure that the output is the
67 -- same as the input.
68 --
69 -- We return the object instead of an XmlTree (which would save us
70 -- an unpickle call) because otherwise the type of @a@ in the call
71 -- to 'xpickle' would be ambiguous. By returning some @a@s, we allow
72 -- the caller to annotate its type.
73 --
74 -- Note that this will happily pickle nothing to nothing and then
75 -- unpickle it back to more nothing. So the fact that the
76 -- before/after results from this function agree does not mean that
77 -- the document was successfully unpickled!
78 --
79 pickle_unpickle :: PU a -- ^ @unpickler@ returning an @a@
80 -> FilePath -- ^ Path to the document to unpickle.
81 -> IO ([a], [a])
82 pickle_unpickle unpickler filepath = do
83 -- We need to check only the root message element since
84 -- readDocument produces a bunch of other junk.
85 expected <- runX arr_getobj
86 actual <- runX $ arr_getobj
87 >>>
88 xpickleVal unpickler
89 >>>
90 xunpickleVal unpickler
91
92 return (expected, actual)
93 where
94 arr_getobj = readDocument parse_opts filepath
95 />
96 isElem -- Drop the extra junk readDocument pulls in.
97 >>>
98 xunpickleVal unpickler
99
100
101
102 -- | Is the given XML file unpickleable? Unpickling will be attempted
103 -- using the @unpickler@ argument. If we unilaterally used the
104 -- generic 'xpickle' function for our unpickler, a type ambiguity
105 -- would result. By taking the unpickler as an argument, we allow
106 -- the caller to indirectly specify a concrete type.
107 --
108 -- Apologies the the name; unpickleable means \"we can unpickle
109 -- it\", not \"not pickleable.\"
110 --
111 unpickleable :: FilePath -> PU a -> IO Bool
112 unpickleable filepath unpickler = do
113 xmldoc <- try_unpickle `catch` (\(SomeException _) -> return [])
114 return $ (not . null) xmldoc
115 where
116 try_unpickle = runX $ readDocument parse_opts filepath
117 >>>
118 xunpickleVal unpickler