1 /**
2 * Phoebe DOM Implementation.
3 *
4 * This is a C++ approximation of the W3C DOM model, which follows
5 * fairly closely the specifications in the various .idl files, copies of
6 * which are provided for reference. Most important is this one:
7 *
8 * http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/idl-definitions.html
9 *
10 * Authors:
11 * Bob Jamison
12 *
13 * Copyright (C) 2005 Bob Jamison
14 *
15 * This library is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU Lesser General Public
17 * License as published by the Free Software Foundation; either
18 * version 2.1 of the License, or (at your option) any later version.
19 *
20 * This library is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * Lesser General Public License for more details.
24 *
25 * You should have received a copy of the GNU Lesser General Public
26 * License along with this library; if not, write to the Free Software
27 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 */
30 /**
31 * Our base String stream classes. We implement these to
32 * be based on DOMString
33 *
34 * Authors:
35 * Bob Jamison <rjamison@titan.com>
36 *
37 * Copyright (C) 2004 Inkscape.org
38 *
39 * Released under GNU GPL, read the file 'COPYING' for more information
40 */
43 #include "uristream.h"
47 namespace org
48 {
49 namespace w3c
50 {
51 namespace dom
52 {
56 //#########################################################################
57 //# U R I I N P U T S T R E A M / R E A D E R
58 //#########################################################################
61 /**
62 *
63 */
64 UriInputStream::UriInputStream(const URI &source)
65 throw (StreamException): uri((URI &)source)
66 {
67 //get information from uri
68 scheme = uri.getScheme();
70 //printf("in schemestr:'%s' scheme:'%d'\n", schemestr, scheme);
71 char *cpath = NULL;
73 switch (scheme)
74 {
76 case URI::SCHEME_FILE:
77 {
78 cpath = (char *) uri.getPath().c_str();
79 //printf("in cpath:'%s'\n", cpath);
80 //inf = fopen_utf8name(cpath, FILE_READ);
81 inf = fopen(cpath, "rb");
82 //g_free(cpath);
83 if (!inf)
84 {
85 DOMString err = "UriInputStream cannot open file ";
86 err += cpath;
87 throw StreamException(err);
88 }
89 break;
90 }
92 case URI::SCHEME_DATA:
93 {
94 data = (unsigned char *) uri.getPath().c_str();
95 //printf("in data:'%s'\n", data);
96 dataPos = 0;
97 dataLen = strlen((const char *)data);
98 break;
99 }
101 }
103 closed = false;
104 }
106 /**
107 *
108 */
109 UriInputStream::~UriInputStream() throw(StreamException)
110 {
111 close();
112 }
114 /**
115 * Returns the number of bytes that can be read (or skipped over) from
116 * this input stream without blocking by the next caller of a method for
117 * this input stream.
118 */
119 int UriInputStream::available() throw(StreamException)
120 {
121 return 0;
122 }
125 /**
126 * Closes this input stream and releases any system resources
127 * associated with the stream.
128 */
129 void UriInputStream::close() throw(StreamException)
130 {
131 if (closed)
132 return;
134 switch (scheme)
135 {
137 case URI::SCHEME_FILE:
138 {
139 if (!inf)
140 return;
141 fflush(inf);
142 fclose(inf);
143 inf=NULL;
144 break;
145 }
147 case URI::SCHEME_DATA:
148 {
149 //do nothing
150 break;
151 }
153 }//switch
155 closed = true;
156 }
158 /**
159 * Reads the next byte of data from the input stream. -1 if EOF
160 */
161 int UriInputStream::get() throw(StreamException)
162 {
163 int retVal = -1;
164 if (closed)
165 {
166 return -1;
167 }
169 switch (scheme)
170 {
172 case URI::SCHEME_FILE:
173 {
174 if (!inf || feof(inf))
175 {
176 retVal = -1;
177 }
178 else
179 {
180 retVal = fgetc(inf);
181 }
182 break;
183 }
185 case URI::SCHEME_DATA:
186 {
187 if (dataPos >= dataLen)
188 {
189 retVal = -1;
190 }
191 else
192 {
193 retVal = data[dataPos++];
194 }
195 break;
196 }
198 }//switch
200 return retVal;
201 }
208 /**
209 *
210 */
211 UriReader::UriReader(const URI &uri) throw (StreamException)
212 {
213 inputStream = new UriInputStream(uri);
214 }
216 /**
217 *
218 */
219 UriReader::~UriReader() throw (StreamException)
220 {
221 delete inputStream;
222 }
224 /**
225 *
226 */
227 int UriReader::available() throw(StreamException)
228 {
229 return inputStream->available();
230 }
232 /**
233 *
234 */
235 void UriReader::close() throw(StreamException)
236 {
237 inputStream->close();
238 }
240 /**
241 *
242 */
243 int UriReader::get() throw(StreamException)
244 {
245 int ch = (int)inputStream->get();
246 return ch;
247 }
250 //#########################################################################
251 //# U R I O U T P U T S T R E A M / W R I T E R
252 //#########################################################################
254 /**
255 *
256 */
257 UriOutputStream::UriOutputStream(const URI &destination)
258 throw (StreamException): closed(false),
259 ownsFile(true),
260 outf(NULL),
261 uri((URI &)destination)
262 {
263 //get information from uri
264 scheme = uri.getScheme();
266 //printf("out schemestr:'%s' scheme:'%d'\n", schemestr, scheme);
267 char *cpath = NULL;
269 switch (scheme)
270 {
272 case URI::SCHEME_FILE:
273 {
274 cpath = (char *) uri.getPath().c_str();
275 //printf("out path:'%s'\n", cpath);
276 outf = fopen(cpath, "wb");
277 if (!outf)
278 {
279 DOMString err = "UriOutputStream cannot open file ";
280 err += cpath;
281 throw StreamException(err);
282 }
283 break;
284 }
286 case URI::SCHEME_DATA:
287 {
288 data = "data:";
289 break;
290 }
292 }//switch
293 }
296 /**
297 *
298 */
299 UriOutputStream::~UriOutputStream() throw(StreamException)
300 {
301 close();
302 }
304 /**
305 * Closes this output stream and releases any system resources
306 * associated with this stream.
307 */
308 void UriOutputStream::close() throw(StreamException)
309 {
310 if (closed)
311 return;
313 switch (scheme)
314 {
316 case URI::SCHEME_FILE:
317 {
318 if (!outf)
319 return;
320 fflush(outf);
321 if ( ownsFile )
322 fclose(outf);
323 outf=NULL;
324 break;
325 }
327 case URI::SCHEME_DATA:
328 {
329 uri = URI(data.c_str());
330 break;
331 }
333 }//switch
335 closed = true;
336 }
338 /**
339 * Flushes this output stream and forces any buffered output
340 * bytes to be written out.
341 */
342 void UriOutputStream::flush() throw(StreamException)
343 {
344 if (closed)
345 return;
347 switch (scheme)
348 {
350 case URI::SCHEME_FILE:
351 {
352 if (!outf)
353 return;
354 fflush(outf);
355 break;
356 }
358 case URI::SCHEME_DATA:
359 {
360 //nothing
361 break;
362 }
364 }//switch
366 }
368 /**
369 * Writes the specified byte to this output stream.
370 */
371 void UriOutputStream::put(XMLCh ch) throw(StreamException)
372 {
373 if (closed)
374 return;
376 switch (scheme)
377 {
379 case URI::SCHEME_FILE:
380 {
381 if (!outf)
382 return;
383 unsigned char uch = (unsigned char)(ch & 0xff);
384 fputc(uch, outf);
385 //fwrite(uch, 1, 1, outf);
386 break;
387 }
389 case URI::SCHEME_DATA:
390 {
391 data.push_back(ch);
392 break;
393 }
395 }//switch
397 }
403 /**
404 *
405 */
406 UriWriter::UriWriter(const URI &uri)
407 throw (StreamException)
408 {
409 outputStream = new UriOutputStream(uri);
410 }
412 /**
413 *
414 */
415 UriWriter::~UriWriter() throw (StreamException)
416 {
417 delete outputStream;
418 }
420 /**
421 *
422 */
423 void UriWriter::close() throw(StreamException)
424 {
425 outputStream->close();
426 }
428 /**
429 *
430 */
431 void UriWriter::flush() throw(StreamException)
432 {
433 outputStream->flush();
434 }
436 /**
437 *
438 */
439 void UriWriter::put(XMLCh ch) throw(StreamException)
440 {
441 int ich = (int)ch;
442 outputStream->put(ich);
443 }
449 } //namespace dom
450 } //namespace w3c
451 } //namespace org
454 //#########################################################################
455 //# E N D O F F I L E
456 //#########################################################################