packages feed

htsn-import-0.2.2: src/Xml.hs

{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TypeFamilies #-}

-- | General XML stuff.
--
module Xml (
  Child(..),
  DtdName(..),
  FromXml(..),
  FromXmlFk(..),
  ToDb(..),
  parse_opts,
  parse_opts_novalidate,
  pickle_unpickle,
  unpickleable,
  unsafe_read_document,
  unsafe_read_invalid_document,
  unsafe_unpickle )
where

-- System imports.
import Control.Exception ( SomeException(..), catch )
import Database.Groundhog.Core ( PersistEntity(..) )
import Text.XML.HXT.Core (
  (>>>),
  (/>),
  PU,
  SysConfigList,
  XmlTree,
  isElem,
  no,
  readDocument,
  runX,
  withRemoveWS,
  withSubstDTDEntities,
  withValidate,
  xpickleVal,
  xunpickleDocument,
  xunpickleVal,
  yes )



-- | Common associated type shared by 'FromXml' and 'FromXmlFk'. This
--   basically just forces the client to define the \"database
--   version\" of his type.
--
class ToDb a where
  -- | Each instance @a@ must declare its associated database type @Db a@.
  type Db a :: *

-- | A typeclass for XML types that can be converted into an
--   associated database type. The story behind this is long, but
--   basically, we need to different types most XML thingies we're
--   going to import: a database type and an XML type.
--
--   Both Groundhog and HXT are very particular about the types that
--   they can use, and there's no way to reuse e.g. a type that HXT
--   can pickle in Groundhog. This typeclass gives us a standard way
--   to get the database type from the XML type that we have to define
--   for HXT.
--
class (ToDb a) => FromXml a where
  -- | A function for getting a @Db a@ out of an @a@.
  from_xml :: a -> Db a


-- | A class for XML representations which are children of other
--   elements. The foal is to associate a child XML element with its
--   parent element's database type. This is required to construct the
--   database analogue of @a@ in 'FromXmlFk'.
--
class Child a where
  -- | The type of our parent object, i.e. to the type to whom our
  --   foreign key will point.
  type Parent a :: *


-- | Some database types cannot be constructed from the XML type
--   alone; they must be supplied a foreign key to a parent object
--   first. Members of this class can be converted from an XML
--   representation to a database representation in this manner.
--
class (Child a, ToDb a) => FromXmlFk a where
  -- | The function that produces a @Db a@ out of a foreign key and an
  --   @a@. The parameter order makes it easier to map this function
  --   over a bunch of things.
  from_xml_fk :: DefaultKey (Parent a) -> a -> Db a



-- | Represents the DTD filename (\"SYSTEM\") part of the DOCTYPE
--   definition.
newtype DtdName = DtdName String

-- | A list of options passed to 'readDocument' when we parse an XML
--   document. All cosmetic whitespace should be removed, otherwise we
--   would have to parse whitespace in each (un)pickler.
--
parse_opts :: SysConfigList
parse_opts = [ withRemoveWS yes,
               withSubstDTDEntities no ]

-- | Like 'parse_opts' except we don't validate the document against
--   its DTD. This is useful when we need to parse a document that we
--   /know/ is invalid so that we can deliver a better error message.
--
parse_opts_novalidate :: SysConfigList
parse_opts_novalidate = (withValidate no) : parse_opts


-- | Given an @unpickler@ and a @filepath@, attempt to unpickle the
--   root element of @filepath@ using @unpickler@ and return both the
--   original unpickled object and one constructed by pickling and
--   unpickling that original. This is used in a number of XML tests
--   which pickle/unpickle and then make sure that the output is the
--   same as the input.
--
--   We return the object instead of an XmlTree (which would save us
--   an unpickle call) because otherwise the type of @a@ in the call
--   to 'xpickle' would be ambiguous. By returning some @a@s, we allow
--   the caller to annotate its type.
--
--   Note that this will happily pickle nothing to nothing and then
--   unpickle it back to more nothing. So the fact that the
--   before/after results from this function agree does not mean that
--   the document was successfully unpickled!
--
pickle_unpickle :: PU a -- ^ @unpickler@ returning an @a@
                -> FilePath -- ^ Path to the document to unpickle.
                -> IO ([a], [a])
pickle_unpickle unpickler filepath = do
  -- We need to check only the root message element since
  -- readDocument produces a bunch of other junk.
    expected <- runX arr_getobj
    actual <- runX $ arr_getobj
                     >>>
                     xpickleVal unpickler
                     >>>
                     xunpickleVal unpickler

    return (expected, actual)
  where
    arr_getobj = readDocument parse_opts filepath
                   />
                   isElem -- Drop the extra junk readDocument pulls in.
                   >>>
                   xunpickleVal unpickler



-- | Is the given XML file unpickleable? Unpickling will be attempted
--   using the @unpickler@ argument. If we unilaterally used the
--   generic 'xpickle' function for our unpickler, a type ambiguity
--   would result. By taking the unpickler as an argument, we allow
--   the caller to indirectly specify a concrete type.
--
--   Apologies the the name; unpickleable means \"we can unpickle
--   it\", not \"not pickleable.\"
--
unpickleable :: FilePath -> PU a -> IO Bool
unpickleable filepath unpickler = do
  xmldoc <- try_unpickle `catch` (\(SomeException _) -> return [])
  return $ (not . null) xmldoc
  where
    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

-- | The same as 'unsafe_read_document', except it allows you to read
--   documents which don't validate against their DTDs.
--
unsafe_read_invalid_document :: FilePath -> IO XmlTree
unsafe_read_invalid_document filepath =
  fmap head $ runX $ readDocument parse_opts_novalidate filepath