- -- | Takes a 'Doctype', 'XmlTree' pair and uses the 'Doctype' to
- -- determine which function to call on the 'XmlTree'.
- import_with_dtd :: (String, XmlTree) -> IO (Maybe Int)
- import_with_dtd (dtd,xml) =
- -- We need NoMonomorphismRestriction here.
- if backend cfg == Postgres
- then withPostgresqlConn cs $ runDbConn $ importer xml
- else withSqliteConn cs $ runDbConn $ importer xml
- where
- -- | Pull the real connection String out of the configuration.
- cs :: String
- cs = get_connection_string $ connection_string cfg
-
- importer
- | dtd == "injuriesxml.dtd" =
- dbimport (undefined :: Injuries.Listing)
-
- | dtd == "Injuries_Detail_XML.dtd" =
- dbimport (undefined :: InjuriesDetail.PlayerListing)
-
- | dtd == "newsxml.dtd" =
- dbimport (undefined :: News.Message)
-
- | otherwise = \_ -> do -- Dummy arg simplifies the other cases.
- let errmsg = "Unrecognized DTD in " ++ path ++ ": " ++ dtd ++ "."
- liftIO $ report_info errmsg
- return Nothing
-
-
-
+ -- | Takes a ('DtdName', 'XmlTree') pair and uses the 'DtdName'
+ -- to determine which function to call on the 'XmlTree'.
+ import_with_dtd :: (DtdName, XmlTree) -> IO ImportResult
+ import_with_dtd (DtdName dtd,xml)
+ -- We special-case the heartbeat so it doesn't have to run in
+ -- the database monad.
+ | dtd == "Heartbeat.dtd" = Heartbeat.verify xml
+ | otherwise = do
+ -- We need NoMonomorphismRestriction here.
+ if backend cfg == Postgres
+ then withPostgresqlConn cs $ runDbConn $ importer
+ else withSqliteConn cs $ runDbConn $ importer
+ where
+ -- | Pull the real connection String out of the configuration.
+ cs :: String
+ cs = get_connection_string $ connection_string cfg
+
+ -- | Convenience; we use this everywhere below in 'importer'.
+ migrate_and_import m = dbmigrate m >> dbimport m
+
+ importer
+ | dtd == "injuriesxml.dtd" = do
+ let m = unpickleDoc xpickle xml :: Maybe Injuries.Message
+ let errmsg = "Could not unpickle injuriesxml."
+ maybe (return $ ImportFailed errmsg) migrate_and_import m
+
+ | dtd == "Injuries_Detail_XML.dtd" = do
+ let m = unpickleDoc xpickle xml :: Maybe InjuriesDetail.Message
+ let errmsg = "Could not unpickle Injuries_Detail_XML."
+ maybe (return $ ImportFailed errmsg) migrate_and_import m
+
+
+ | dtd == "newsxml.dtd" = do
+ let m = unpickleDoc xpickle xml :: Maybe News.Message
+ let errmsg = "Could not unpickle newsxml."
+ maybe (return $ ImportFailed errmsg) migrate_and_import m
+
+ -- | dtd == "Odds_XML.dtd" = undefined
+
+ | otherwise = do
+ let infomsg =
+ "Unrecognized DTD in " ++ path ++ ": " ++ dtd ++ "."
+ return $ ImportUnsupported infomsg
+
+
+-- | Entry point of the program. It twiddles some knobs for
+-- configuration options and then calls 'import_file' on each XML file
+-- given on the command-line.
+--
+-- Any file successfully processed is then removed, and we're done.
+--