source: to-imperative/trunk/java/refal/refal/plus/XML.rf @ 3989

Last change on this file since 3989 was 3986, checked in by yura, 12 years ago
  • ReadXML fails if Open_File fails.
File size: 3.0 KB
Line 
1$module "refal.plus.XML";
2
3$use Access Convert Stream StdIO;
4
5$func? IsBlank t.term = ;
6IsBlank \{ ' '; '\n'; '\r'; '\t'; };
7
8$func? IsBlankOrDelim t.delim t.term = ;
9IsBlankOrDelim t.delim t.term, t.term : \{ ' '; '\n'; '\r'; '\t'; t.delim; };
10
11$func GetUntilDelim s.stream t.delim = e.expr; // no e.delim at the end
12GetUntilDelim s.stream t.delim =
13  () $iter {
14    <Get_While s.stream &IsBlank> :: e, {
15      <Scanc s.stream t.delim> : v = (e.expr) Stop;
16      <Get_Until s.stream &IsBlankOrDelim t.delim> : v.head = (e.expr ' ' v.head);
17      (e.expr) Stop;
18    };
19  } :: (e.expr) e.isStop, e.isStop : v =
20  { e.expr : ' ' e.head = e.head; e.expr; };
21
22$func? GetXMLFormat s.stream = t.comment;
23GetXMLFormat s.stream, <Scanc s.stream '?'> : v = ((XML_FORMAT) <Middle 0 2 <Get_Delims s.stream '?>'>>);
24
25$func? GetComment s.stream = t.comment;
26GetComment s.stream, <Scans s.stream '!--'> : v = ((COMMENT) <Middle 0 3 <Get_Delims s.stream '-->'>>);
27
28$func? GetCDATA s.stream = t.cdata;
29GetCDATA s.stream, <Scans s.stream '![CDATA['> : v = ((CDATA) <Middle 0 3 <Get_Delims s.stream ']]>'>>);
30
31$func FindTag s.stream = e.text (v.tag);
32FindTag s.stream = <GetUntilDelim s.stream '<'> :: e.text, {
33  \{ <GetXMLFormat s.stream>; <GetComment s.stream>; <GetCDATA s.stream>; } :: e.text1 = e.text e.text1 <FindTag s.stream>;
34  e.text ('<' <GetUntilDelim s.stream '>'> '>');
35};
36
37$func SplitAttrs e.text = e.attrs;
38SplitAttrs e.attrs,
39  () (e.attrs) $iter {
40    e.attrs : e.key '=' '"' e.value '"' e.tail =
41      { e.tail : ' ' e.tail1 = e.tail1; e.tail; } :: e.tail,     
42      (e.keyvalues (<ToWord e.key> <ToWord e.value>)) (e.tail);
43  } :: (e.keyvalues) (e.attrs), e.attrs : /* empty */ =
44  e.keyvalues;
45
46$func SplitName e.text = e.name (e.attrs);
47SplitName {
48  e.name ' ' e.attrs = e.name (<SplitAttrs e.attrs>);
49  e.name = e.name ();
50};
51
52$func ReadTag s.stream v.tag = t;
53ReadTag s.stream v.tag, v.tag : {
54  '<' e.name '/>' =
55    <SplitName e.name> :: e.name (e.attrs),
56    (<ToWord e.name> ((ATTRIBUTES) e.attrs));
57  '<' e.name '>' =
58    <SplitName e.name> :: e.name (e.attrs),
59    <FindTag s.stream> $iter e.text <ReadTag s.stream v.tag> <FindTag s.stream> :: e.text (v.tag),
60    v.tag : '</' e.name '>',
61    (<ToWord e.name> ((ATTRIBUTES) e.attrs) e.text);
62};
63
64$public $func Read s.stream = e.text_then_XML_tag;
65Read s.stream = <FindTag s.stream> : e.text (v.tag), e.text <ReadTag s.stream v.tag>;
66
67$public $func? GetAttribute t.tag s.attrName = s.attrValue;
68GetAttribute (s (t e (s.attrName s.attrValue) e) e) s.attrName = s.attrValue;
69
70$public $func? GetTag t.tag t.innerTagName = t.innerTag;
71GetTag (s t e t.innerTag e) t.innerTagName, t.innerTag : (t.innerTagName e) = t.innerTag;
72
73$public $func? GetValue t.tag = e.value;
74GetValue (s t e.value) = e.value;
75
76$public $func? ReadXML e.fileName = e.xml;
77ReadXML e.fileName =
78  $trap <File_Open e.fileName> :: s.stream, {
79    <XML.Read s.stream> :: e.xml = <File_Close s.stream>, e.xml;
80    = <File_Close s.stream>, $fail;
81  } $with $fail;
82
83$func Main = e;
84Main =
85  <WriteLn <Read <Expr_Open '<Z>as<C/><B>b<!--<Z></Z>--></B><C>c</C></Z>'>>>;
Note: See TracBrowser for help on using the repository browser.