X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=src%2FXml.hs;h=5c846648a85d8a2f7b267db22b6b9b200dc67d78;hb=2dd1ab7a375b55632f6c8165dce97a2df3cc1907;hp=3f7bb62cae43fc0cb8dbe42bf54741e448980fbd;hpb=6c1e96b2a155336038c010ebd4f28795b152e5b0;p=dead%2Fhtsn-import.git diff --git a/src/Xml.hs b/src/Xml.hs index 3f7bb62..5c84664 100644 --- a/src/Xml.hs +++ b/src/Xml.hs @@ -7,10 +7,13 @@ module Xml ( DtdName(..), FromXml(..), FromXmlFk(..), + FromXmlFkTeams(..), ToDb(..), parse_opts, pickle_unpickle, - unpickleable ) + unpickleable, + unsafe_read_document, + unsafe_unpickle ) where -- System imports. @@ -21,6 +24,7 @@ import Text.XML.HXT.Core ( (/>), PU, SysConfigList, + XmlTree, isElem, no, readDocument, @@ -29,9 +33,15 @@ import Text.XML.HXT.Core ( withSubstDTDEntities, withValidate, xpickleVal, + xunpickleDocument, xunpickleVal, yes ) + +-- Local imports. +import TSN.Team ( Team(..) ) + + -- | Common associated type shared by 'FromXml' and 'FromXmlFk'. This -- basically just forces the client to define the \"database -- version\" of his type. @@ -72,6 +82,22 @@ class (ToDb a) => FromXmlFk a where from_xml_fk :: DefaultKey (Parent a) -> a -> Db a +-- | A further refinement of 'FromXmlFk'. These types need not only a +-- foreign key to a parent in order to make the XML -> DB +-- conversion, but also two foreign keys to away/home teams (as +-- represented in "TSN.Team"). +-- +class (ToDb a) => FromXmlFkTeams a where + -- | The function that produces a @Db a@ out of a parent foreign + -- key, two team foreign keys, and an @a@. The parameter order makes + -- it easier to map this function over a bunch of things. + from_xml_fk_teams :: DefaultKey (Parent a) + -> DefaultKey Team -- ^ The away team FK + -> DefaultKey Team -- ^ The home team FK + -> a + -> Db a + + -- | Represents the DTD filename (\"SYSTEM\") part of the DOCTYPE -- definition. newtype DtdName = DtdName String @@ -143,3 +169,19 @@ unpickleable filepath unpickler = do try_unpickle = runX $ readDocument parse_opts filepath >>> xunpickleVal unpickler + + +-- | Unpickle from a 'FilePath' using the given pickler. Explode if it +-- doesn't work. +-- +unsafe_unpickle :: FilePath -> PU a -> IO a +unsafe_unpickle filepath unpickler = + fmap head $ runX $ xunpickleDocument unpickler parse_opts filepath + + +-- | Read an XML document from a 'FilePath' into an XmlTree. Explode if it +-- doesn't work. +-- +unsafe_read_document :: FilePath -> IO XmlTree +unsafe_read_document filepath = + fmap head $ runX $ readDocument parse_opts filepath