-
Notifications
You must be signed in to change notification settings - Fork 0
/
test8.hs
401 lines (353 loc) · 11.8 KB
/
test8.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
module Exam where
import Data.Char
import Data.Maybe
type Name = String
type Attributes = [(Name, String)]
data XML = Text String | Element Name Attributes [XML]
deriving (Eq, Show)
type Stack = [XML]
-----------------------------------------------------------------------
-- Some useful show/print functions
-- The 'show' function for XML objects
showXML :: XML -> String
showXML (Text t)
= t
showXML (Element n as es)
= "<" ++ n ++ showAtts as ++ ">" ++ concatMap showXML es ++ "</" ++ n ++ ">"
where
showAtts as = concatMap showAtt as
showAtt (n, v) = " " ++ n ++ "=" ++ "\"" ++ v ++ "\""
-- The 'show' function for lists of XML objects
showXMLs :: [XML] -> String
showXMLs
= concatMap showXML
-- Prints an XML object to the terminal
printXML :: XML -> IO()
printXML
= putStrLn . showXML
-- Prints a list of XML objects to the terminal (useful for testing the
-- output from expandXML')
printXMLs :: [XML] -> IO()
printXMLs
= mapM_ printXML
-----------------------------------------------------------------------
-----------------------------------------------------------------------
-- Part I
skipSpace :: String -> String
skipSpace
= dropWhile (isSpace)
getAttribute :: String -> XML -> String
getAttribute att (Text _)
= ""
getAttribute att (Element _ atts _)
| isNothing attVal = ""
| otherwise = fromJust attVal
where
attVal = lookup att atts
getChildren :: String -> XML -> [XML]
getChildren _ (Text _)
= []
getChildren s (Element _ _ xmls)
= filter (isElementName) xmls
where
isElementName (Text _) = False
isElementName (Element s' _ _) = s == s'
getChild :: String -> XML -> XML
getChild s xml
| null child = Text ""
| otherwise = c
where
child = getChildren s xml
(c : _) = child
addChild :: XML -> XML -> XML
-- Pre: the second argument is an Element
addChild xml (Element name att xml')
= Element name att (xml' ++ [xml])
getValue :: XML -> XML
getValue (Text s)
= Text s
getValue (Element _ _ xmls)
= join (map (getValue) xmls)
where
join xs = Text ((concatMap (\(Text s') -> s') xs))
-------------------------------------------------------------------------
-- Part II
-- Parses an element/attribute name
parseName :: String -> (Name, String)
parseName []
= error "Error: attempt to read empty name"
parseName s@(c : cs)
| isAlpha c = break (not . isNameChar) s
| otherwise = error ("parseName error: name " ++ show s ++
" must begin with a letter")
where
isNameChar c = isAlpha c || isDigit c || elem c "-."
sentinel :: XML
sentinel
= Element "" [] []
addText :: String -> Stack -> Stack
-- Pre: There is at least one Element on the stack
addText s (e : es)
= ((addChild (Text s) e) : es)
popAndAdd :: Stack -> Stack
-- Pre: There are at least two Elements on the stack
popAndAdd (e : e' : es)
= ((addChild e e') : es)
parseAttributes :: String -> (Attributes, String)
-- Pre: The XML attributes string is well-formed
parseAttributes s
| c == '>' = ([], cs)
| otherwise = (((name, attribute) : remAtt), remText)
where
s'@(c:cs) = skipSpace s
(name, rest) = parseName s'
(attribute, rest') = b (tail (skipSpace attText))
(_,attText) = b (skipSpace rest)
b = break (\x -> x == '"')
(remAtt, remText) = parseAttributes (tail rest')
parse :: String -> XML
-- Pre: The XML string is well-formed
parse s
= parse' (skipSpace s) [sentinel]
parse' :: String -> Stack -> XML
parse' [] ((Element _ _ (xml : _)) : es)
= xml
parse' s@(x : x' : xs) stack
| (x == '<') && (x' == '/') = parse' (tail rest) (popAndAdd stack)
| (x == '<') && (x' /= '/') = parse' (rest'') ((Element name att []) : stack)
| otherwise = parse' (rest''') (addText (text) stack)
where
(_, rest) = parseName xs
(name, rest') = parseName (x' : xs)
(att, rest'') = parseAttributes rest'
(text, rest''') = break (\x -> x == '<') s
-------------------------------------------------------------------------
-- Part III
type Context = XML
type XSL = XML
-- Parses XSL and XML source documents and transforms the latter using the
-- former. The output is written to the given file (String).
-- Example use:
-- output "out.html" filmsXSL films
-- To render output.html in a browser, type this at the Linux prompt:
-- firefox output.html &
output :: String -> XML -> XML -> IO()
output file xsl source
= writeFile file (showXMLs (expandXSL xsl source))
expandXSL :: XSL -> XML -> [XML]
expandXSL xsl source
= expandXSL' root xsl
where
root = Element "/" [] [source]
expandXSL' :: Context -> XSL -> [XML]
expandXSL'
= undefined
-------------------------------------------------------------------------
-- Test data for Parts I and II
-- Simple test cases (no whitespace)
s1, s2, s3 :: String
s1
= "<a>A</a>"
s2
= "<a x=\"1\"><b>A</b><b>B</b></a>"
s3
= "<a>\
\<b>\
\<c att=\"att1\">text1</c>\
\<c att=\"att2\">text2</c>\
\</b>\
\<b>\
\<c att=\"att3\">text3</c>\
\<d>text4</d>\
\</b>\
\</a>"
-- Parsed versions of the above
x1, x2, x3 :: XML
x1
= Element "a" [] [Text "A"]
x2
= Element "a"
[("x","1")]
[Element "b" [] [Text "A"],
Element "b" [] [Text "B"]]
x3
= Element "a"
[]
[Element "b"
[]
[Element "c"
[("att","att1")]
[Text "text1"],
Element "c"
[("att","att2")]
[Text "text2"]],
Element "b"
[]
[Element "c"
[("att","att3")]
[Text "text3"],
Element "d"
[]
[Text "text4"]]]
casablanca :: String
casablanca
= "<film title=\"Casablanca\">\n <director>Michael Curtiz</director>\n <year>1942\
\</year>\n</film>\n\n\n"
casablancaParsed :: XML
casablancaParsed
= Element "film"
[("title","Casablanca")]
[Text "\n ",
Element "director" [] [Text "Michael Curtiz"],
Text "\n ",
Element "year" [] [Text "1942"],
Text "\n"]
-- Films mark-up of Figure 1
films :: String
films
= "<filmlist>\n\
\ <film title = \"Rear Window\">\n\
\ <director>Alfred Hitchcock</director>\n\
\ <composer>Franz Waxman</composer>\n\
\ <year>1954</year>\n\
\ </film>\n\
\ <film title = \"2001: A Space Odyssey\">\n\
\ <director>Stanley Kubrick</director>\n\
\ <composer>Richard Strauss</composer>\n\
\ <composer>Gyorgy Ligeti</composer>\n\
\ <composer>Johann Strauss</composer>\n\
\ <year>1968</year>\n\
\ </film>\n\
\ <film title=\"Lawrence of Arabia\" >\n\
\ <duration>228</duration>\n\
\ <director>David Lean</director>\n\
\ <composer>Maurice Jarre</composer>\n\
\ </film>\n\
\</filmlist>\n\n\n"
-- Parsed version of films ('parse films'), suitably formatted
filmsParsed :: XML
filmsParsed
= Element "filmlist"
[]
[Text "\n ",
Element "film" [("title","Rear Window")]
[Text "\n ",
Element "director" [] [Text "Alfred Hitchcock"],
Text "\n ",
Element "composer" [] [Text "Franz Waxman"],
Text "\n ",
Element "year" [] [Text "1954"],
Text "\n "],
Text "\n ",
Element "film" [("title","2001: A Space Odyssey")]
[Text "\n ",
Element "director" [] [Text "Stanley Kubrick"],
Text "\n ",
Element "composer" [] [Text "Richard Strauss"],
Text "\n ",
Element "composer" [] [Text "Gyorgy Ligeti"],
Text "\n ",
Element "composer" [] [Text "Johann Strauss"],
Text "\n ",
Element "year" [] [Text "1968"],
Text "\n "],
Text "\n ",
Element "film" [("title","Lawrence of Arabia")]
[Text "\n ",
Element "duration" [] [Text "228"],
Text "\n ",
Element "director" [] [Text "David Lean"],
Text "\n ",
Element "composer" [] [Text "Maurice Jarre"],
Text "\n "],
Text "\n"]
-------------------------------------------------------------------------
-- XSL tests
-- value-of test cases
xsl1, xsl2, xsl3, xsl4, xsl5, xsl6, xsl7,
xsl8, xsl9 :: String
xsl1
= "<value-of select = \"a/b/c\"></value-of>"
xsl2
= "<value-of select = \"a/b\"></value-of>"
xsl3
= "<value-of select = \"a/b/d\"></value-of>"
xsl4
= "<value-of select = \"a/b/c/@att\"></value-of>"
xsl5
= "<value-of select = \"./a/./b/c/./.\"></value-of>"
xsl6
= "<t1><t2>Preamble</t2><t3><value-of select = \"a/b/c\"></value-of></t3></t1>"
-- for-each test cases
xsl7
= "<for-each select=\"a/b/c\"><value-of select=\"./@att\"></value-of>\
\</for-each>"
xsl8
= "<for-each select=\"a/b\"><t><value-of select=\"c\"></value-of></t>\
\</for-each>"
xsl9
= "<for-each select=\"a/b\"><t1><value-of select=\"absent\"></value-of>\
\</t1></for-each>"
-- Parsed versions of the above
xsl1Parsed, xsl2Parsed, xsl3Parsed, xsl4Parsed, xsl5Parsed,
xsl6Parsed, xsl7Parsed, xsl8Parsed, xsl9Parsed :: XML
xsl1Parsed
= Element "value-of" [("select","a/b/c")] []
xsl2Parsed
= Element "value-of" [("select","a/b")] []
xsl3Parsed
= Element "value-of" [("select","a/b/d")] []
xsl4Parsed
= Element "value-of" [("select","a/b/c/@att")] []
xsl5Parsed
= Element "value-of" [("select","./a/./b/c/./.")] []
xsl6Parsed
= Element "t1"
[]
[Element "t2" [] [Text "Preamble"],
Element "t3" [] [Element "value-of" [("select","a/b/c")] []]]
xsl7Parsed
= Element "for-each"
[("select","a/b/c")]
[Element "value-of" [("select","./@att")] []]
xsl8Parsed
= Element "for-each"
[("select","a/b")]
[Element "t" [] [Element "value-of" [("select","c")] []]]
xsl9Parsed
= Element "for-each"
[("select","a/b")]
[Element "t1" [] [Element "value-of" [("select","absent")] []]]
-- XSL template for building a films summary (example from spec.)
filmsXSL :: String
filmsXSL
= "<html>\n\
\<body>\n\
\ <h2>Film List</h2>\n\
\ <table border=\"1\">\n\
\ <tr>\n\
\ <th align=\"left\">Title</th>\n\
\ <th align=\"left\">Director</th>\n\
\ <th align=\"left\">Principal composer</th>\n\
\ </tr>\n\
\ <for-each select=\"filmlist/film\">\n\
\ <tr>\n\
\ <td><value-of select=\"@title\"></value-of></td>\n\
\ <td><value-of select=\"director\"></value-of></td>\n\
\ <td><value-of select=\"composer\"></value-of></td>\n\
\ </tr>\n\
\ </for-each>\n\
\ </table>\n\
\</body>\n\
\</html>"
-- XSL template for building a list of composers (example from spec.)
composersXSL :: String
composersXSL
= "<for-each select=\"filmlist/film\">\
\<h2><value-of select=\"@title\"></value-of> composers</h2>\
\<ul>\
\<for-each select=\"composer\">\
\<li><value-of select=\".\"></value-of></li>\
\</for-each>\
\</ul>\
\</for-each>"