]> gitweb.michael.orlitzky.com - dead/htsn.git/blob - src/TSN/Xml.hs
Based on TSN documentation, split XML documents on the </message> tag instead of...
[dead/htsn.git] / src / TSN / Xml.hs
1 -- | Minimal XML functionality needed to parse each document's
2 -- XML_File_ID.
3 --
4 module TSN.Xml (
5 parse_xmlfid,
6 xml_tests )
7 where
8
9 import Data.Maybe ( listToMaybe, mapMaybe )
10 import Test.Tasty ( TestTree, testGroup )
11 import Test.Tasty.HUnit ( (@?=), Assertion, testCase )
12 import Text.Read ( readMaybe )
13 import Text.XML.HXT.Core (
14 (>>>),
15 (/>),
16 getChildren,
17 getText,
18 hasName,
19 runLA,
20 xreadDoc )
21
22 -- | A tiny parser written in HXT to extract the "XML_File_ID" element
23 -- from a document.
24 parse_xmlfid :: String -> Maybe Integer
25 parse_xmlfid =
26 listToMaybe . mapMaybe readMaybe . parse
27 where
28 parse :: String -> [String]
29 parse =
30 runLA (xreadDoc
31 >>> hasName "message"
32 /> hasName "XML_File_ID"
33 >>> getChildren
34 >>> getText)
35
36
37
38 -- * Tasty Tests
39 xml_tests :: TestTree
40 xml_tests =
41 testGroup
42 "XML tests"
43 [ xml_file_id_tests ]
44
45
46 xml_file_id_tests :: TestTree
47 xml_file_id_tests =
48 testCase "XML_File_ID is parsed correctly" $ do
49 let xmlfids = ["19908216", "19908216", "19908245", "19908246", "19908247"]
50 mapM_ check xmlfids
51 where
52 check :: String -> Assertion
53 check xmlfid = do
54 xml <- readFile ("test/xml/" ++ xmlfid ++ ".xml")
55 let actual = parse_xmlfid xml
56 let expected = readMaybe xmlfid
57 actual @?= expected