2
* Phoebe DOM Implementation.
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:
8
* http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/idl-definitions.html
13
* Copyright (C) 2005-2008 Bob Jamison
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.
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.
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
32
#include "uristream.h"
48
//#########################################################################
49
//# U R I I N P U T S T R E A M / R E A D E R
50
//#########################################################################
56
UriInputStream::UriInputStream(const URI &source)
57
throw (StreamException): uri((URI &)source)
65
void UriInputStream::init() throw (StreamException)
67
//get information from uri
68
scheme = uri.getScheme();
73
case URI::SCHEME_FILE:
75
DOMString npath = uri.getNativePath();
76
inf = fopen(npath.c_str(), "rb");
79
DOMString err = "UriInputStream cannot open file ";
81
throw StreamException(err);
86
case URI::SCHEME_DATA:
89
//printf("in data:'%s'\n", data.c_str());
91
dataLen = uri.getPath().size();
95
case URI::SCHEME_HTTP:
96
case URI::SCHEME_HTTPS:
98
if (!httpClient.openGet(uri))
100
DOMString err = "UriInputStream cannot open URL ";
101
err.append(uri.toString());
102
throw StreamException(err);
119
UriInputStream::~UriInputStream() throw(StreamException)
125
* Returns the number of bytes that can be read (or skipped over) from
126
* this input stream without blocking by the next caller of a method for
129
int UriInputStream::available() throw(StreamException)
136
* Closes this input stream and releases any system resources
137
* associated with the stream.
139
void UriInputStream::close() throw(StreamException)
147
case URI::SCHEME_FILE:
157
case URI::SCHEME_DATA:
163
case URI::SCHEME_HTTP:
164
case URI::SCHEME_HTTPS:
176
* Reads the next byte of data from the input stream. -1 if EOF
178
int UriInputStream::get() throw(StreamException)
189
case URI::SCHEME_FILE:
191
if (!inf || feof(inf))
202
case URI::SCHEME_DATA:
204
if (dataPos >= dataLen)
210
retVal = data[dataPos++];
215
case URI::SCHEME_HTTP:
216
case URI::SCHEME_HTTPS:
218
retVal = httpClient.read();
235
UriReader::UriReader(const URI &uri) throw (StreamException)
237
inputStream = new UriInputStream(uri);
243
UriReader::~UriReader() throw (StreamException)
251
int UriReader::available() throw(StreamException)
253
return inputStream->available();
259
void UriReader::close() throw(StreamException)
261
inputStream->close();
267
int UriReader::get() throw(StreamException)
269
int ch = (int)inputStream->get();
274
//#########################################################################
275
//# U R I O U T P U T S T R E A M / W R I T E R
276
//#########################################################################
281
UriOutputStream::UriOutputStream(const URI &destination)
282
throw (StreamException): closed(false),
285
uri((URI &)destination)
294
void UriOutputStream::init() throw(StreamException)
296
//get information from uri
297
scheme = uri.getScheme();
299
//printf("out schemestr:'%s' scheme:'%d'\n", schemestr, scheme);
305
case URI::SCHEME_FILE:
307
cpath = (char *) uri.getNativePath().c_str();
308
//printf("out path:'%s'\n", cpath);
309
outf = fopen(cpath, "wb");
312
DOMString err = "UriOutputStream cannot open file ";
314
throw StreamException(err);
319
case URI::SCHEME_DATA:
331
UriOutputStream::~UriOutputStream() throw(StreamException)
337
* Closes this output stream and releases any system resources
338
* associated with this stream.
340
void UriOutputStream::close() throw(StreamException)
348
case URI::SCHEME_FILE:
359
case URI::SCHEME_DATA:
372
* Flushes this output stream and forces any buffered output
373
* bytes to be written out.
375
void UriOutputStream::flush() throw(StreamException)
383
case URI::SCHEME_FILE:
391
case URI::SCHEME_DATA:
402
* Writes the specified byte to this output stream.
404
int UriOutputStream::put(XMLCh ch) throw(StreamException)
412
case URI::SCHEME_FILE:
416
unsigned char uch = (unsigned char)(ch & 0xff);
418
//fwrite(uch, 1, 1, outf);
422
case URI::SCHEME_DATA:
439
UriWriter::UriWriter(const URI &uri)
440
throw (StreamException)
442
outputStream = new UriOutputStream(uri);
448
UriWriter::~UriWriter() throw (StreamException)
456
void UriWriter::close() throw(StreamException)
458
outputStream->close();
464
void UriWriter::flush() throw(StreamException)
466
outputStream->flush();
472
int UriWriter::put(XMLCh ch) throw(StreamException)
475
if (outputStream->put(ich) < 0)
490
//#########################################################################
491
//# E N D O F F I L E
492
//#########################################################################