]> gitweb.michael.orlitzky.com - dead/htsn-import.git/blob - src/Xml.hs
Add the FromXmlFk class, like FromXml except it requires an FK (old idea).
[dead/htsn-import.git] / src / Xml.hs
1 {-# LANGUAGE FlexibleContexts #-}
2 {-# LANGUAGE TypeFamilies #-}
3
4 -- | General XML stuff.
5 --
6 module Xml (
7 DtdName(..),
8 FromXml(..),
9 FromXmlFk(..),
10 ToDb(..),
11 parse_opts,
12 pickle_unpickle,
13 unpickleable )
14 where
15
16 -- System imports.
17 import Control.Exception ( SomeException(..), catch )
18 import Database.Groundhog.Core ( PersistEntity(..) )
19 import Text.XML.HXT.Core (
20 (>>>),
21 (/>),
22 PU,
23 SysConfigList,
24 isElem,
25 no,
26 readDocument,
27 runX,
28 withRemoveWS,
29 withValidate,
30 xpickleVal,
31 xunpickleVal,
32 yes )
33
34 -- | Common associated type shared by 'FromXml' and 'FromXmlFk'. This
35 -- basically just forces the client to define the \"database
36 -- version\" of his type.
37 --
38 class ToDb a where
39 -- | Each instance @a@ must declare its associated database type @Db a@.
40 type Db a :: *
41
42 -- | A typeclass for XML types that can be converted into an
43 -- associated database type. The story behind this is long, but
44 -- basically, we need to different types most XML thingies we're
45 -- going to import: a database type and an XML type.
46 --
47 -- Both Groundhog and HXT are very particular about the types that
48 -- they can use, and there's no way to reuse e.g. a type that HXT
49 -- can pickle in Groundhog. This typeclass gives us a standard way
50 -- to get the database type from the XML type that we have to define
51 -- for HXT.
52 --
53 class (ToDb a) => FromXml a where
54 -- | A function for getting a @Db a@ out of an @a@.
55 from_xml :: a -> Db a
56
57
58 -- | Some database types cannot be constructed from the XML type
59 -- alone; they must be supplied a foreign key to a parent object
60 -- first. Members of this class can be converted from an XML
61 -- representation to a database representation in this manner.
62 --
63 class (ToDb a) => FromXmlFk a where
64 -- | The type of our parent object, i.e. to the type to whom our
65 -- foreign key will point.
66 type Parent a :: *
67
68 -- | The function that produces a @Db a@ out of a foreign key and an
69 -- @a@. The parameter order makes it easier to map this function
70 -- over a bunch of things.
71 from_xml_fk :: DefaultKey (Parent a) -> a -> Db a
72
73
74 -- | Represents the DTD filename (\"SYSTEM\") part of the DOCTYPE
75 -- definition.
76 newtype DtdName = DtdName String
77
78 -- | A list of options passed to 'readDocument' when we parse an XML
79 -- document. All cosmetic whitespace should be removed, otherwise we
80 -- would have to parse whitespace in each (un)pickler.
81 --
82 parse_opts :: SysConfigList
83 parse_opts = [ withRemoveWS yes, withValidate no ]
84
85
86 -- | Given an @unpickler@ and a @filepath@, attempt to unpickle the
87 -- root element of @filepath@ using @unpickler@ and return both the
88 -- original unpickled object and one constructed by pickling and
89 -- unpickling that original. This is used in a number of XML tests
90 -- which pickle/unpickle and then make sure that the output is the
91 -- same as the input.
92 --
93 -- We return the object instead of an XmlTree (which would save us
94 -- an unpickle call) because otherwise the type of @a@ in the call
95 -- to 'xpickle' would be ambiguous. By returning some @a@s, we allow
96 -- the caller to annotate its type.
97 --
98 -- Note that this will happily pickle nothing to nothing and then
99 -- unpickle it back to more nothing. So the fact that the
100 -- before/after results from this function agree does not mean that
101 -- the document was successfully unpickled!
102 --
103 pickle_unpickle :: PU a -- ^ @unpickler@ returning an @a@
104 -> FilePath -- ^ Path to the document to unpickle.
105 -> IO ([a], [a])
106 pickle_unpickle unpickler 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 unpickler
113 >>>
114 xunpickleVal unpickler
115
116 return (expected, actual)
117 where
118 arr_getobj = readDocument parse_opts filepath
119 />
120 isElem -- Drop the extra junk readDocument pulls in.
121 >>>
122 xunpickleVal unpickler
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 -- Apologies the the name; unpickleable means \"we can unpickle
133 -- it\", not \"not pickleable.\"
134 --
135 unpickleable :: FilePath -> PU a -> IO Bool
136 unpickleable filepath unpickler = do
137 xmldoc <- try_unpickle `catch` (\(SomeException _) -> return [])
138 return $ (not . null) xmldoc
139 where
140 try_unpickle = runX $ readDocument parse_opts filepath
141 >>>
142 xunpickleVal unpickler