aboutsummaryrefslogtreecommitdiff
path: root/src/inprogress/xmlreader.cpp
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/inprogress/xmlreader.cpp274
1 files changed, 0 insertions, 274 deletions
diff --git a/src/inprogress/xmlreader.cpp b/src/inprogress/xmlreader.cpp
deleted file mode 100644
index 5c755bb..0000000
--- a/src/inprogress/xmlreader.cpp
+++ /dev/null
@@ -1,274 +0,0 @@
1/*
2 * Copyright (C) 2007-2008 Xagasoft, All rights reserved.
3 *
4 * This file is part of the libbu++ library and is released under the
5 * terms of the license contained in the file LICENSE.
6 */
7
8#include "xmlreader.h"
9
10Bu::XmlReader::XmlReader( Bu::Stream &sIn ) :
11 sIn( sIn )
12{
13}
14
15Bu::XmlReader::~XmlReader()
16{
17}
18
19const char *Bu::XmlReader::lookahead( int nAmnt )
20{
21 if( sBuf.getSize() >= nAmnt )
22 return sBuf.getStr();
23
24 int nNew = nAmnt - sBuf.getSize();
25 char *buf = new char[nNew];
26 sIn.read( buf, nNew );
27 sBuf.append( buf );
28
29 return sBuf.getStr();
30}
31
32void Bu::XmlReader::burn( int nAmnt )
33{
34 if( sBuf.getSize() < nAmnt )
35 {
36 lookahead( nAmnt );
37 }
38
39 //sBuf.remove( nAmnt );
40}
41
42void Bu::XmlReader::checkString( const char *str, int nLen )
43{
44 if( !strncmp( str, lookahead( nLen ), nLen ) )
45 {
46 burn( nLen );
47 return;
48 }
49
50 throw Bu::ExceptionBase("Expected string '%s'", str );
51}
52
53Bu::XmlNode *Bu::XmlReader::read()
54{
55 prolog();
56}
57
58void Bu::XmlReader::prolog()
59{
60 XMLDecl();
61 Misc();
62}
63
64void Bu::XmlReader::XMLDecl()
65{
66 checkString("<?xml", 5 );
67 S();
68 VersionInfo();
69 EncodingDecl();
70 SDDecl();
71 Sq();
72 checkString("?>", 2 );
73}
74
75void Bu::XmlReader::Misc()
76{
77 for(;;)
78 {
79 S();
80 if( !strncmp("<!--", lookahead( 4 ), 4 ) )
81 {
82 Comment();
83 }
84 else if( !strncmp("<?", lookahead( 2 ), 2 ) )
85 {
86 PI();
87 }
88 else
89 {
90 return;
91 }
92 }
93}
94
95void Bu::XmlReader::Comment()
96{
97 checkString("<!--", 4 );
98 for(;;)
99 {
100 unsigned char c = *lookahead(1);
101 if( c == '-' )
102 {
103 if( lookahead(2)[1] == '-' )
104 {
105 checkString("-->", 3 );
106 return;
107 }
108 }
109 burn( 1 );
110 }
111}
112
113void Bu::XmlReader::PI()
114{
115 checkString("<?", 2 );
116 FString sName = Name();
117 printf("PI: %s\n---\n", sName.getStr() );
118 S();
119 for(int j = 0;; j++ )
120 {
121 if( !strncmp( "?>", lookahead(j+2)+j, 2 ) )
122 {
123 burn( j+2 );
124 return;
125 }
126 }
127}
128
129void Bu::XmlReader::S()
130{
131 for( int j = 0;; j++ )
132 {
133 char c = *lookahead( 1 );
134 if( c == 0x20 || c == 0x9 || c == 0xD || c == 0xA )
135 continue;
136 if( j == 0 )
137 throw ExceptionBase("Expected whitespace.");
138 return;
139 }
140}
141
142void Bu::XmlReader::Sq()
143{
144 for(;;)
145 {
146 char c = *lookahead( 1 );
147 if( c == 0x20 || c == 0x9 || c == 0xD || c == 0xA )
148 continue;
149 return;
150 }
151}
152
153void Bu::XmlReader::VersionInfo()
154{
155 try
156 {
157 S();
158 checkString("version", 7 );
159 }
160 catch( ExceptionBase &e )
161 {
162 return;
163 }
164 Eq();
165 Bu::FString ver = AttValue();
166 if( ver != "1.1" )
167 throw ExceptionBase("Currently we only support xml version 1.1\n");
168}
169
170void Bu::XmlReader::Eq()
171{
172 Sq();
173 checkString("=", 1 );
174 Sq();
175}
176
177void Bu::XmlReader::EncodingDecl()
178{
179 S();
180 try
181 {
182 checkString("encoding", 8 );
183 }
184 catch( ExceptionBase &e )
185 {
186 return;
187 }
188
189 Eq();
190 AttValue();
191}
192
193void Bu::XmlReader::SDDecl()
194{
195 S();
196 try
197 {
198 checkString("standalone", 10 );
199 }
200 catch( ExceptionBase &e )
201 {
202 return;
203 }
204
205 Eq();
206 AttValue();
207}
208
209Bu::FString Bu::XmlReader::AttValue()
210{
211 char q = *lookahead(1);
212 if( q == '\"' )
213 {
214 for( int j = 2;; j++ )
215 {
216 if( lookahead(j)[j-1] == '\"' )
217 {
218 Bu::FString ret( lookahead(j)+1, j-2 );
219 burn( j );
220 return ret;
221 }
222 }
223 }
224 else if( q == '\'' )
225 {
226 for( int j = 2;; j++ )
227 {
228 if( lookahead(j)[j-1] == '\'' )
229 {
230 Bu::FString ret( lookahead(j)+1, j-2 );
231 burn( j );
232 return ret;
233 }
234 }
235 }
236
237 throw ExceptionBase("Excpected either \' or \".\n");
238}
239
240Bu::FString Bu::XmlReader::Name()
241{
242 unsigned char c = *lookahead( 1 );
243 if( c != ':' && c != '_' &&
244 (c < 'A' || c > 'Z') &&
245 (c < 'a' || c > 'z') &&
246 (c < 0xC0 || c > 0xD6 ) &&
247 (c < 0xD8 || c > 0xF6 ) &&
248 (c < 0xF8))
249 {
250 throw ExceptionBase("Invalid entity name starting character.");
251 }
252
253 for( int j = 1;; j++ )
254 {
255 unsigned char c = lookahead(j+1)[j];
256 if( isS( c ) )
257 {
258 FString ret( lookahead(j+1), j+1 );
259 burn( j+1 );
260 return ret;
261 }
262 if( c != ':' && c != '_' && c != '-' && c != '.' && c != 0xB7 &&
263 (c < 'A' || c > 'Z') &&
264 (c < 'a' || c > 'z') &&
265 (c < '0' || c > '9') &&
266 (c < 0xC0 || c > 0xD6 ) &&
267 (c < 0xD8 || c > 0xF6 ) &&
268 (c < 0xF8))
269 {
270 throw ExceptionBase("Invalid character in name.");
271 }
272 }
273}
274