root/trunk/Serial/XML.d

Revision 591, 2.1 kB (checked in by BCS, 3 years ago)

Added support for 3rd party types and arrays

Line 
1 module XML;
2
3 import Serialize;
4 import Interface;
5
6 /**
7  * Assuming not in a tag, read data untill a tag is found
8  * Params:
9  *     term = the char to terminate on
10  *     source = the data source
11  * Returns:
12  *     the read data with escaping removed.
13  */
14 char[] ReadContentXML(char[] term = "<")(Source!(char) source)
15 {
16     char[] ret = new char[16];
17     int i = 0;
18     char t;
19     while(-1 == std.string.find(term, source.Peek()))
20     {
21         source.Pick(t);
22         if(t == '&')
23         {
24             if(!source.Pick(t)) throw new Exception("Invalid Source");
25             root: switch(t)
26             {
27             case 'a':
28                 if(!source.Pick(t)) goto default;
29                 switch(t)
30                 {
31                 case 'm':
32                     if(!source.Pick("p;")) goto default;
33                     t = '&';
34                     break root;
35
36                 case 'p':
37                     if(!source.Pick("os;")) goto default;
38                     t = '\'';
39                     break root;
40
41                 default:
42                     throw new Exception("Invalid Source");
43                 }
44                 break;
45
46             case 'g':
47                 if(!source.Pick("t;")) goto default;
48                 t = '>';
49                 break;
50
51             case 'l':
52                 if(!source.Pick("t;")) goto default;
53                 t = '<';
54                 break;
55
56             case 'q':
57                 if(!source.Pick("uot;")) goto default;
58                 t = '\"';
59                 break;
60
61             default:
62                 throw new Exception("Invalid Source");
63             }
64         }
65         ret[i++] = t;
66
67         if(ret.length <= i) ret.length = i*2;
68     }
69     return ret[0..i];
70 }
71
72 void WriteContentXML(Sink!(char) sink, char[] content)
73 {
74     int from = 0;
75     foreach(int i, char c; content)
76         switch(c)
77         {
78         case '\"':
79             if(from != i) sink.Dump(content[from..i]);
80             sink.Dump("&quot;");
81             from = i+1;
82             break;
83
84         case '&':
85             if(from != i) sink.Dump(content[from..i]);
86             sink.Dump("&amp;");
87             from = i+1;
88             break;
89
90         case '\'':
91             if(from != i) sink.Dump(content[from..i]);
92             sink.Dump("&apos;");
93             from = i+1;
94             break;
95
96         case '<':
97             if(from != i) sink.Dump(content[from..i]);
98             sink.Dump("&gt;");
99             from = i+1;
100             break;
101
102         case '>':
103             if(from != i) sink.Dump(content[from..i]);
104             sink.Dump("&lt;");
105             from = i+1;
106             break;
107
108         default:
109             break;
110         }
111     if(from != content.length) sink.Dump(content[from..$]);
112
113     return;
114 }
Note: See TracBrowser for help on using the browser.