-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathOMDocXml.hs
1558 lines (1495 loc) · 41.8 KB
/
OMDocXml.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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{- |
Module : $Header$
Copyright : (c) Hendrik Iben, Uni Bremen 2005-2007
License : similar to LGPL, see HetCATS/LICENSE.txt or LIZENZ.txt
Maintainer : hiben@tzi.de
Stability : provisional
Portability : portable
XmlRepresentations for the OMDoc modelled in OMDoc.OMDocInterface
-}
module OMDoc.OMDocXml where
import OMDoc.OMDocInterface
import Text.XML.HXT.Parser ( (.>), (+=), (+++), getValue )
import qualified Text.XML.HXT.Parser as HXT hiding (run, trace, when)
import qualified OMDoc.XmlHandling as XML
import qualified Network.URI as URI
import qualified Data.List as List
import Data.Char
import qualified Network.HTTP.Base64 as Base64
import qualified Numeric as Numeric
import qualified OMDoc.Util as Util
import Debug.Trace (trace)
{- debug -}
maybeGetXml::String->IO (Maybe HXT.XmlTrees)
maybeGetXml source =
do
xml <- HXT.run' $
HXT.parseDocument
[
(HXT.a_source, source)
, (HXT.a_issue_errors, HXT.v_0)
, (HXT.a_check_namespaces, HXT.v_1)
, (HXT.a_validate, HXT.v_0)
]
HXT.emptyRoot
return
(let
status = (read $ HXT.xshow $ XML.getValue "status" (head xml))::Int
result = if status < HXT.c_err then (Just xml) else Nothing
in
result)
{--}
{- |
this class defines functions an instance has to provide to write it to
or read it from a HXT-Xml-structure
-}
class XmlRepresentable a where
-- | render instance to an XmlFilter
toXml :: a -> HXT.XmlFilter
-- | try to construct an instance from a single node
fromXml :: HXT.XmlTree -> Maybe a
getAllFromXml::forall a . (XmlRepresentable a)=>HXT.XmlTrees->[a]
getAllFromXml trees =
foldl
(\as xa ->
case fromXml xa of
Nothing -> as
(Just a) -> as ++ [a]
)
[]
trees
instance (XmlRepresentable x1, XmlRepresentable x2)=>XmlRepresentable (Either x1 x2) where
toXml (Left l) = toXml l
toXml (Right r) = toXml r
fromXml t =
case fromXml t of
Nothing ->
case fromXml t of
Nothing -> Nothing
(Just r) -> Just $ Right r
(Just l) -> Just $ Left l
-- | XML-representable Structure (convenience class, mostly used via inference)
class XmlRepStructure a where
toXmlS::a->HXT.XmlFilter
fromXmlS::HXT.XmlTrees->Maybe a
-- | structure of two sequential lists
instance (XmlRepresentable x1, XmlRepresentable x2)=>XmlRepStructure ([x1], [x2]) where
toXmlS (t1l, t2l) =
let
t1f =
foldl
(\ts t ->
ts +++ toXml t +++ XML.xmlNL
)
XML.xmlNullFilter
t1l
t2f =
foldl
(\ts t ->
ts +++ toXml t +++ XML.xmlNL
)
XML.xmlNullFilter
t2l
in
t1f +++ t2f
fromXmlS xl =
let
(t1l, t2l, _) =
until
(\(_, _, (xl', _)) -> null xl' )
(\(t1l', t2l', (h:xl', get1)) ->
if get1
then
case fromXml h of
Nothing -> (t1l', t2l', (h:xl', False))
(Just t1) -> (t1l' ++ [t1::x1], t2l', (xl', get1))
else
case fromXml h of
Nothing -> (t1l', t2l', ([], get1))
(Just t2) -> (t1l', t2l' ++ [t2::x2], (xl', get1))
)
([],[],(XML.applyXmlFilter HXT.isXTag xl, True))
in
Just (t1l, t2l)
-- | structure of a tupel-list
instance (XmlRepresentable x1, XmlRepresentable x2) => XmlRepStructure [(x1, x2)] where
toXmlS tl =
foldl
(\xs (t1,t2) ->
xs +++ toXml t1 +++ toXml t2 +++ XML.xmlNL
)
XML.xmlNullFilter
tl
fromXmlS xl =
let
(l, _) =
until
(\(_, xl') -> length xl' < 2)
(\(l', (x1:x2:xr)) ->
case (fromXml x1, fromXml x2) of
(Just t1, Just t2) ->
(l' ++ [(t1, t2)], xr)
_ -> (l', [])
)
([], xl)
in
Just l
-- | Any list of XmlRepresentable-instances can be written in sequence
instance (XmlRepresentable x)=>XmlRepStructure [x] where
toXmlS tl =
foldl
(\ts t ->
ts +++ toXml t +++ XML.xmlNL
)
XML.xmlNullFilter
tl
fromXmlS = Just . getAllFromXml
-- | OMDoc
instance XmlRepresentable OMDoc where
toXml o =
HXT.etag "omdoc"
+= (
HXT.sattr "xmlns" "http://www.mathweb.org/omdoc"
+++
XML.qualattr "xml" "id" (omdocId o)
+++
foldl
(\ts t ->
ts +++ XML.xmlNL +++ toXml t
)
XML.xmlNullFilter
(omdocTheories o)
+++
foldl
(\is i ->
is +++ XML.xmlNL +++ toXml i
)
XML.xmlNullFilter
(omdocInclusions o)
)
fromXml t =
case (HXT.isTag "omdoc" t) of
[] -> Nothing
_ ->
let
children = HXT.getChildren t
ids = HXT.xshow $ XML.getQualValue "xml" "id" t
thes =
getAllFromXml children
incs =
getAllFromXml children
in
Just $ OMDoc ids thes incs
-- | Theory
instance XmlRepresentable Theory where
toXml the =
let
tId = theoryId the
(mTheoryPres, _, trempres) =
until
(\(_, rp, _) -> null rp)
(\(_, (p:pr), rl) ->
if tId == (presentationForId p)
then
(Just p, [], rl ++ pr)
else
(Nothing, pr, rl ++ [p])
)
(Nothing, (theoryPresentations the), [])
(conx, conrempres) =
foldl
(\(cx, rp) con ->
let
presnames =
getIdsForPresentation con
(thispres, rempres) =
List.partition
(\pres ->
elem (presentationForId pres) presnames
)
rp
in
(
cx
+++
toXml con
+++
toXmlS thispres
, rempres
)
)
(XML.xmlNullFilter, trempres)
(theoryConstitutives the)
in
HXT.etag "theory"
+= (
XML.qualattr "xml" "id" (theoryId the)
+++
case theoryComment the of
Nothing -> XML.xmlNullFilter
(Just s) -> HXT.cmt s +++ XML.xmlNL
+++
(
case mTheoryPres of
Nothing -> XML.xmlNullFilter
(Just p) -> toXml p
)
+++
conx
+++
toXmlS conrempres
)
fromXml t =
case (HXT.isTag "theory") t of
[] -> Nothing
_ ->
let
comments = HXT.getXCmt t
tcom =
case Util.trimString (HXT.xshow comments) of
[] -> Nothing
cs -> Just cs
children = HXT.getChildren t
ids = HXT.xshow $ XML.getQualValue "xml" "id" t
cons = getAllFromXml children
pres =
getAllFromXml children
in
Just $ (Theory ids cons pres tcom)
-- | Imports
instance XmlRepresentable Imports where
toXml imp =
HXT.etag "imports"
+= (
HXT.sattr "from" (showURI $ importsFrom imp)
+++
(
case (importsId imp) of
(Just i) -> XML.qualattr "xml" "id" i
_ -> XML.xmlNullFilter
)
{- +++
(
case importsMorphism imp of
Nothing -> XML.xmlNullFilter
(Just m) ->
case morphismHiding m of
[] -> XML.xmlNullFilter
h -> HXT.sattr "hiding" (Util.implode " " h)
{-
if null (importsHiding imp)
then
XML.xmlNullFilter
else
HXT.sattr "hiding" (Util.implode " " (importsHiding imp))
-}
)-}
+++
(
HXT.sattr
"type"
(
case (importsType imp) of
ITLocal -> "local"
ITGlobal -> "global"
)
)
+++
(
case (importsConservativity imp) of
CNone -> XML.xmlNullFilter
c -> HXT.sattr "conservativity" (show c)
)
+++
(
case (importsMorphism imp) of
Nothing ->
XML.xmlNullFilter
(Just m) -> XML.xmlNL +++ toXml m +++ XML.xmlNL
)
)
fromXml t =
case (HXT.isTag "imports") t of
[] -> Nothing
_ ->
let
froms = HXT.xshow $ HXT.getValue "from" t
mfromuri =
if length froms < 1
then
Nothing
else
URI.parseURIReference froms
{- hidings =
filter
(not . null)
$
map
Util.trimString
$
Util.explode
" "
$
HXT.xshow
$
HXT.getValue "hiding" t -}
mid =
case HXT.xshow $ XML.getQualValue "xml" "id" t of
[] -> Nothing
s -> Just s
itype =
case map toLower $ HXT.xshow $ HXT.getValue "type" t of
[] -> ITGlobal
"global" -> ITGlobal
"local" -> ITLocal
u -> trace ("Unknown Import-Type : " ++ u) ITGlobal
mm =
case (HXT.getChildren .> HXT.isTag "morphism") t of
[] -> Nothing
(xm:_) -> fromXml xm
conss = HXT.xshow $ HXT.getValue "conservativity" t
cons =
case readsPrec 0 conss of
[] -> CNone
((c,_):_) -> c
in
case mfromuri of
Nothing ->
trace ("No 'from' in Imports!") Nothing
(Just u) ->
Just $ Imports u mm mid itype cons
-- | Use
instance XmlRepresentable Use where
toXml u =
HXT.etag "use"
+= (
HXT.sattr "format" (useFormat u)
+++
HXT.txt (useValue u)
)
fromXml t =
case (HXT.isTag "use") t of
[] -> Nothing
_ ->
let
formats = HXT.xshow $ HXT.getValue "format" t
values = HXT.xshow $ HXT.getChildren t
in
Just $ Use formats values
-- | Presentation
instance XmlRepresentable Presentation where
toXml pres =
let
xuses =
if length (presentationUses pres) == 0
then
XML.xmlNullFilter
else
foldl
(\us u ->
us +++ toXml u +++ XML.xmlNL
)
XML.xmlNL
(presentationUses pres)
in
HXT.etag "presentation"
+= (
HXT.sattr "for" ("#" ++ (presentationForId pres))
+++
(
case (presentationSystem pres) of
Nothing -> XML.xmlNullFilter
(Just sys) -> HXT.sattr "system" sys
)
+++
xuses
)
fromXml t =
case (HXT.isTag "presentation") t of
[] -> Nothing
_ ->
let
fors' = HXT.xshow $ HXT.getValue "for" t
fors =
case fors' of
[] -> trace ("Missing 'for' attribute in presentation...") ""
('#':rid) -> rid
s -> trace ("Presentation 'for' attribute is no omdocref...") s
systems = case HXT.xshow $ HXT.getValue "system" t of
[] -> Nothing
s -> Just s
uses =
getAllFromXml (HXT.getChildren t)
in
Just $ Presentation fors systems uses
-- | Symbol
instance XmlRepresentable Symbol where
toXml s =
HXT.etag "symbol"
+= (
HXT.sattr "role" (show $ symbolRole s)
+++
HXT.sattr "name" (symbolId s)
+++
(
case (symbolGeneratedFrom s) of
Nothing -> XML.xmlNullFilter
(Just g) ->
HXT.sattr "generated-from" g
)
+++
(
case (symbolType s) of
Nothing -> XML.xmlNullFilter
(Just t) ->
XML.xmlNL
+++
toXml t
+++
XML.xmlNL
)
)
fromXml t =
case (HXT.isTag "symbol") t of
[] -> Nothing
_ ->
let
roles = HXT.xshow $ HXT.getValue "role" t
names = HXT.xshow $ HXT.getValue "name" t
ids = HXT.xshow $ XML.getQualValue "xml" "id" t
sname =
case names of
[] -> ids
_ -> names
mgf =
case HXT.xshow $ HXT.getValue "generated-from" t of
[] -> Nothing
gf -> Just gf
in
case readsPrec 0 roles of
[] ->
if Util.isPrefix "ymmud" (reverse sname)
then
Nothing
else
trace ("Unknown role \"" ++ roles ++ "\" for symbol " ++ sname)
Nothing
(sr,_):_ ->
let
typechilds = (HXT.getChildren .> HXT.isTag "type") t
in
case typechilds of
[] -> Just $ Symbol mgf sname sr Nothing
_ ->
case fromXml (head typechilds) of
Nothing -> trace ("cant parse type...") Nothing
jty -> Just $ Symbol mgf sname sr jty
-- | Type
instance XmlRepresentable Type where
toXml t =
HXT.etag "type"
+= (
(
case typeSystem t of
Nothing -> XML.xmlNullFilter
(Just ts) -> HXT.sattr "system" (showURI ts)
)
+++
XML.xmlNL
+++
toXml (typeOMDocMathObject t)
+++
XML.xmlNL
)
fromXml t =
case (HXT.isTag "type") t of
[] -> Nothing
_ ->
let
systems = HXT.xshow $ HXT.getValue "system" t
omchilds =
(
HXT.getChildren .>
(
HXT.isTag "OMOBJ"
+++
HXT.isTag "math"
+++
HXT.isTag "legacy"
)
) t
in
case omchilds of
[] -> trace ("No Math-Object in type!") Nothing
_ ->
case fromXml (head omchilds) of
Nothing -> trace "error parsing omobj..." Nothing
(Just omobj) ->
let
typebody = (\u -> Type { typeSystem = u, typeOMDocMathObject = omobj })
in
if length systems > 0
then
case URI.parseURIReference systems of
Nothing -> trace ("Error parsing system-URI") Nothing
jsuri -> Just $ typebody jsuri
else
Just $ typebody Nothing
-- | Any Constitutive
instance XmlRepresentable Constitutive where
toXml (CAx ax) = toXml ax
toXml (CDe de) = toXml de
toXml (CSy sy) = toXml sy
toXml (CIm im) = toXml im
toXml (CAd ad) = toXml ad
toXml (CCo { conComCmt = cmt, conComCon = con }) =
HXT.cmt cmt +++ XML.xmlNL +++ (toXml con)
fromXml t =
case fromXml t of
(Just a) -> Just $ CAx a
Nothing ->
case fromXml t of
(Just d) -> Just $ CDe d
Nothing ->
case fromXml t of
(Just s) -> Just $ CSy s
Nothing ->
case fromXml t of
(Just i) -> Just $ CIm i
Nothing ->
case fromXml t of
(Just a) -> Just $ CAd a
Nothing -> Nothing
-- | Axiom
instance XmlRepresentable Axiom where
toXml axiom =
HXT.etag "axiom"
+= (
XML.qualattr "xml" "id" (axiomName axiom)
+++
(
if null $ axiomCMPs axiom
then
XML.xmlNullFilter
else
toXmlS (axiomCMPs axiom)
)
+++
(
if null $ axiomFMPs axiom
then
XML.xmlNullFilter
else
toXmlS (axiomFMPs axiom)
)
)
fromXml t =
case (HXT.isTag "axiom") t of
[] -> Nothing
_ ->
let
ids = HXT.xshow $ XML.getQualValue "xml" "id" t
children = HXT.getChildren t
cmps = getAllFromXml children
fmps = getAllFromXml children
in
case ids of
[] -> trace "no id for axiom!" Nothing
_ -> Just $ Axiom ids cmps fmps
-- | CMP
instance XmlRepresentable CMP where
toXml cmp =
HXT.etag "CMP"
+= (
XML.xmlNL
+++
toXml (cmpContent cmp)
)
fromXml t =
case (HXT.isTag "CMP") t of
[] -> Nothing
_ -> Just $ CMP $ MTextText $ HXT.xshow $ HXT.getChildren t
-- | FMP
instance XmlRepresentable FMP where
toXml fmp =
HXT.etag "FMP"
+= (
(
case fmpLogic fmp of
Nothing -> XML.xmlNullFilter
(Just l) -> HXT.sattr "logic" l
)
+++
case fmpContent fmp of
(Left o) -> toXml o
(Right ac) -> toXmlS ac
)
fromXml t =
case (HXT.isTag "FMP") t of
[] -> Nothing
_ ->
let
logics = HXT.xshow $ HXT.getValue "logic" t
logic = if length logics < 1 then Nothing else Just logics
children = HXT.getChildren t
omchildren = (HXT.getChildren .> HXT.isTag "OMOBJ") t
in
case children of
[] -> trace ("empty FMP!") Nothing
_ ->
case omchildren of
[] ->
case fromXmlS children of
Nothing -> trace "Wierd!" Nothing
(Just ac) -> Just $ FMP logic (Right ac)
_ ->
case fromXml $ head omchildren of
Nothing -> Nothing
(Just o) -> Just $ FMP logic (Left o)
-- | Assumption
instance XmlRepresentable Assumption where
toXml _ = HXT.etag "assumption"
fromXml t =
case (HXT.isTag "assumption") t of
[] -> Nothing
_ -> Just Assumption
-- | Conclusion
instance XmlRepresentable Conclusion where
toXml _ = HXT.etag "conclusion"
fromXml t =
case (HXT.isTag "conclusion") t of
[] -> Nothing
_ -> Just Conclusion
-- | Definition
instance XmlRepresentable Definition where
toXml def =
HXT.etag "symbol"
+= (
XML.qualattr "xml" "id" ((definitionId def) ++ "-dummy")
)
+++
XML.xmlNL
+++
HXT.etag "definition"
+= (
XML.qualattr "xml" "id" (definitionId def)
+++
HXT.sattr "for" ((++) "#" $ (++) (definitionId def) "-dummy" )
+++
HXT.sattr "type" "implicit"
+++
toXmlS (definitionCMPs def)
+++
toXmlS (definitionFMPs def)
)
fromXml t =
case (HXT.isTag "definition") t of
[] -> Nothing
_ ->
let
ids = HXT.xshow $ XML.getQualValue "xml" "id" t
fors =
case HXT.xshow $ HXT.getValue "for" t of
'#':r -> r
r -> r
ids' = case ids of [] -> fors; _ -> ids
children = HXT.getChildren t
cmps = getAllFromXml children
fmps = getAllFromXml children
in
case ids' of
[] -> trace "no id in definition" Nothing
_ -> Just $ Definition ids' cmps fmps
-- | SortDef
instance XmlRepresentable SortDef where
toXml sd =
HXT.etag "sortdef"
+= (
HXT.sattr "name" (sortDefName sd)
+++
HXT.sattr "role" (show $ sortDefRole sd)
+++
HXT.sattr "type" (show $ sortDefType sd)
+++
toXmlS (sortDefConstructors sd)
+++
toXmlS (sortDefInsorts sd)
+++
toXmlS (sortDefRecognizers sd)
)
fromXml t =
case HXT.isTag "sortdef" t of
[] -> Nothing
_ ->
let
sdNameS = HXT.xshow $ HXT.getValue "name" t
sdRoleS = HXT.xshow $ HXT.getValue "role" t
sdTypeS = HXT.xshow $ HXT.getValue "type" t
sdRole =
case sdRoleS of
[] -> SRSort
_ ->
case readsPrec 0 sdRoleS of
[] -> trace ("Invalid Role : \"" ++ sdRoleS ++ "\"") SRSort
((sdR,_):_) -> sdR
sdType =
case sdTypeS of
[] -> STFree
_ ->
case readsPrec 0 sdTypeS of
[] -> trace ("Invalid Type : \"" ++ sdTypeS ++ "\"") STFree
((sdT,_):_) -> sdT
xchildren = HXT.getChildren t
cons = getAllFromXml xchildren
insorts = getAllFromXml xchildren
recognizers = getAllFromXml xchildren
in
case sdNameS of
[] -> trace ("SortDef for Nothing!") Nothing
_ ->
Just $ SortDef sdNameS sdRole sdType cons insorts recognizers
-- | Constructor
instance XmlRepresentable Constructor where
toXml con =
HXT.etag "constructor"
+= (
HXT.sattr "name" (constructorName con)
+++
HXT.sattr "role" (show $ constructorRole con)
+++
(
foldl
(\cx a ->
cx
+++
HXT.etag "argument"
+= (
toXml a
)
+++
XML.xmlNL
)
XML.xmlNullFilter
(constructorArguments con)
)
)
fromXml t =
case HXT.isTag "constructor" t of
[] -> Nothing
_ ->
let
cNameS = HXT.xshow $ HXT.getValue "name" t
cRoleS = HXT.xshow $ HXT.getValue "role" t
cRole =
case cRoleS of
[] -> SRObject
_ ->
case readsPrec 0 cRoleS of
[] -> trace ("Invalid Role : \"" ++ cRoleS ++ "\"") SRObject
((cR,_):_) -> cR
argsxml = (HXT.getChildren .> HXT.isTag "argument") t
args =
foldl
(\as at ->
let
typechilds = (HXT.getChildren .> HXT.isTag "type") at
in
case typechilds of
[] -> trace ("no type in argument!") as
[tc] ->
case fromXml tc of
Nothing -> trace ("could not parse type!") as
(Just ty) -> as ++ [ty]
(tc:_) ->
case fromXml tc of
Nothing -> trace ("could not parse type and there are more!") as
(Just ty) -> trace ("more than one type in argument!") (as ++ [ty])
)
[]
argsxml
in
case cNameS of
[] -> trace ("No Name for Constructor!") Nothing
_ -> Just $ Constructor cNameS cRole args
-- | Insort
instance XmlRepresentable Insort where
toXml i =
HXT.etag "insort"
+= (
HXT.sattr "for" (showURI $ insortFor i)
)
fromXml t =
case HXT.isTag "insort" t of
[] -> Nothing
_ ->
let
forS = HXT.xshow $ HXT.getValue "for" t
in
case URI.parseURIReference forS of
Nothing -> trace ("No for...") Nothing
(Just u) -> Just $ Insort u
-- | Recognizer
instance XmlRepresentable Recognizer where
toXml i =
HXT.etag "recognizer"
+= (
HXT.sattr "name" (recognizerName i)
)
fromXml t =
case HXT.isTag "recognizer" t of
[] -> Nothing
_ ->
let
nameS = HXT.xshow $ HXT.getValue "name" t
in
case nameS of
"" -> trace ("No name...") Nothing
_ -> Just $ Recognizer nameS
-- | ADT
instance XmlRepresentable ADT where
toXml adt =
HXT.etag "adt"
+= (
(
case adtId adt of
Nothing -> XML.xmlNullFilter
(Just aid) -> XML.qualattr "xml" "id" aid
)
+++
(
toXmlS (adtSortDefs adt)
)
)
fromXml t =
case (HXT.isTag "adt") t of
[] -> Nothing
_ ->
let
xchildren = HXT.getChildren t
maid =
case HXT.xshow $ XML.getQualValue "xml" "id" t of
[] -> Nothing
s -> Just s
sds = getAllFromXml xchildren
in
Just $ ADT maid sds
-- | Inclusion
instance XmlRepresentable Inclusion where
toXml ti@(TheoryInclusion {}) =
HXT.etag "theory-inclusion"
+= (incBody ti)
toXml ai@(AxiomInclusion {}) =
HXT.etag "axiom-inclusion"
+= (incBody ai)
fromXml t =
case (HXT.isTag "axiom-inclusion") t of
[] ->
case (HXT.isTag "theory-inclusion") t of
[] -> Nothing
_ ->
case getIncBody of
Nothing -> Nothing
(Just (from,to,mm,mi,c)) -> Just $ TheoryInclusion from to mm mi c
_ ->
case getIncBody of
Nothing -> Nothing
(Just (from,to,mm,mi,c)) -> Just $ AxiomInclusion from to mm mi c
where
getIncBody::Maybe (URI.URI, URI.URI, Maybe Morphism, Maybe XmlId, Conservativity)
getIncBody =
let
froms = HXT.xshow $ HXT.getValue "from" t
tos = HXT.xshow $ HXT.getValue "to" t
fromuri = URI.parseURIReference froms
touri = URI.parseURIReference tos
ids = HXT.xshow $ XML.getQualValue "xml" "id" t
mid = if (length ids) > 0 then Just ids else Nothing
conss = HXT.xshow $ HXT.getValue "conservativity" t
cons =
case readsPrec 0 conss of
[] -> CNone
((c,_):_) -> c
mm = case (HXT.getChildren .> HXT.isTag "morphism") t of
[] -> Nothing
(xm:_) -> fromXml xm
in
if ( (length froms) < 1 || (length tos) < 1 )
then
trace
("No 'from' or no 'to' in Inclusion!")
Nothing
else
case (fromuri, touri) of
(Just fu, Just tu) ->
Just (fu, tu, mm, mid, cons)
_ -> trace ("Error parsing unclusion source or target!") Nothing
-- | used by Inclusion
incBody::
Inclusion
->HXT.XmlFilter
incBody tinc =
(
HXT.sattr "from" (showURI $ inclusionFrom tinc)
+++
HXT.sattr "to" (showURI $ inclusionTo tinc)
+++
(
case (inclusionId tinc) of
(Just i) -> XML.qualattr "xml" "id" i
_ -> XML.xmlNullFilter
)
-- conservativity has been removed from OMDoc-RNG
{-
+++
(
case (inclusionConservativity tinc) of
CNone -> XML.xmlNullFilter
c -> HXT.sattr "conservativity" (show c)
)
-}
+++
(
case (inclusionMorphism tinc) of
(Just m) -> XML.xmlNL +++ toXml m +++ XML.xmlNL
_ -> XML.xmlNullFilter
)
)