-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathXmlParser.hs
51 lines (40 loc) · 1.34 KB
/
XmlParser.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
{-# LANGUAGE CPP, TypeSynonymInstances #-}
{- |
Module : $Header$
Description : Interface to the Xml Parsing Facility
Copyright : (c) Ewaryst Schulz, DFKI 2009
License : GPLv2 or higher, see LICENSE.txt
Maintainer : ewaryst.schulz@dfki.de
Stability : provisional
Portability : portable
Provides an xml parse function which depends on external libraries.
-}
module Common.XmlParser (XmlParseable (parseXml), readXmlFile) where
import Text.XML.Light
import qualified Data.ByteString.Lazy as BS
#ifdef HEXPAT
import qualified Common.XmlExpat as XE
#else
import Data.ByteString.Lazy.UTF8
#endif
readXmlFile :: FilePath -> IO BS.ByteString
readXmlFile fp = do
bs <- BS.readFile fp
if BS.length bs > 0 then return bs else
fail "Common.XmlParser.readXmlFile: empty file"
{- | This class provides an xml parsing function which is instantiated
by using the hexpat or the XML.Light library, dependent on the haskell
environment. -}
class XmlParseable a where
parseXml :: a -> Either String Element
#ifdef HEXPAT
instance XmlParseable BS.ByteString where
parseXml = XE.parseXml
-- 169MB on Basic/Algebra_I
#else
instance XmlParseable BS.ByteString where
parseXml s = case parseXMLDoc $ toString s of
Just x -> Right x
_ -> Left "parseXMLDoc: parse error"
-- 426MB on Basic/Algebra_I
#endif