1 {-# LANGUAGE FlexibleContexts #-}
2 {-# LANGUAGE TypeFamilies #-}
4 -- | General XML stuff.
17 import Control.Exception ( SomeException(..), catch )
18 import Database.Groundhog.Core ( PersistEntity(..) )
19 import Text.XML.HXT.Core (
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.
39 -- | Each instance @a@ must declare its associated database type @Db a@.
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.
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
53 class (ToDb a) => FromXml a where
54 -- | A function for getting a @Db a@ out of an @a@.
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.
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.
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
74 -- | Represents the DTD filename (\"SYSTEM\") part of the DOCTYPE
76 newtype DtdName = DtdName String
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.
82 parse_opts :: SysConfigList
83 parse_opts = [ withRemoveWS yes, withValidate no ]
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
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.
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!
103 pickle_unpickle :: PU a -- ^ @unpickler@ returning an @a@
104 -> FilePath -- ^ Path to the document to unpickle.
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
114 xunpickleVal unpickler
116 return (expected, actual)
118 arr_getobj = readDocument parse_opts filepath
120 isElem -- Drop the extra junk readDocument pulls in.
122 xunpickleVal unpickler
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.
132 -- Apologies the the name; unpickleable means \"we can unpickle
133 -- it\", not \"not pickleable.\"
135 unpickleable :: FilePath -> PU a -> IO Bool
136 unpickleable filepath unpickler = do
137 xmldoc <- try_unpickle `catch` (\(SomeException _) -> return [])
138 return $ (not . null) xmldoc
140 try_unpickle = runX $ readDocument parse_opts filepath
142 xunpickleVal unpickler