af43d0a51718511b1bccc809dac740d9d3022bc9
1 #ifndef __DOMSTREAM_H__
2 #define __DOMSTREAM_H__
3 /**
4 * Phoebe DOM Implementation.
5 *
6 * This is a C++ approximation of the W3C DOM model, which follows
7 * fairly closely the specifications in the various .idl files, copies of
8 * which are provided for reference. Most important is this one:
9 *
10 * http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/idl-definitions.html
11 *
12 * Authors:
13 * Bob Jamison
14 *
15 * Copyright (C) 2005 Bob Jamison
16 *
17 * This library is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU Lesser General Public
19 * License as published by the Free Software Foundation; either
20 * version 2.1 of the License, or (at your option) any later version.
21 *
22 * This library is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 * Lesser General Public License for more details.
26 *
27 * You should have received a copy of the GNU Lesser General Public
28 * License along with this library; if not, write to the Free Software
29 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30 */
34 #include "dom.h"
36 namespace org
37 {
38 namespace w3c
39 {
40 namespace dom
41 {
42 namespace io
43 {
47 class StreamException
48 {
49 public:
51 StreamException(const DOMString &theReason) throw()
52 { reason = theReason; }
53 ~StreamException() throw()
54 { }
55 char const *what()
56 { return reason.c_str(); }
58 private:
60 DOMString reason;
62 };
64 //#########################################################################
65 //# I N P U T S T R E A M
66 //#########################################################################
68 /**
69 * This interface is the base of all input stream classes. Users who wish
70 * to make an InputStream that is part of a chain should inherit from
71 * BasicInputStream. Inherit from this class to make a source endpoint,
72 * such as a URI or buffer.
73 *
74 */
75 class InputStream
76 {
78 public:
80 /**
81 * Constructor.
82 */
83 InputStream() {}
85 /**
86 * Destructor
87 */
88 virtual ~InputStream() {}
90 /**
91 * Return the number of bytes that are currently available
92 * to be read
93 */
94 virtual int available() = 0;
96 /**
97 * Do whatever it takes to 'close' this input stream
98 * The most likely implementation of this method will be
99 * for endpoints that use a resource for their data.
100 */
101 virtual void close() = 0;
103 /**
104 * Read one byte from this input stream. This is a blocking
105 * call. If no data is currently available, this call will
106 * not return until it exists. If the user does not want
107 * their code to block, then the usual solution is:
108 * if (available() > 0)
109 * myChar = get();
110 * This call returns -1 on end-of-file.
111 */
112 virtual int get() = 0;
114 }; // class InputStream
119 /**
120 * This is the class that most users should inherit, to provide
121 * their own streams.
122 *
123 */
124 class BasicInputStream : public InputStream
125 {
127 public:
129 BasicInputStream(const InputStream &sourceStream);
131 virtual ~BasicInputStream() {}
133 virtual int available();
135 virtual void close();
137 virtual int get();
139 protected:
141 bool closed;
143 InputStream &source;
145 private:
148 }; // class BasicInputStream
152 /**
153 * Convenience class for reading from standard input
154 */
155 class StdInputStream : public InputStream
156 {
157 public:
159 int available()
160 { return 0; }
162 void close()
163 { /* do nothing */ }
165 int get()
166 { return getchar(); }
168 };
175 //#########################################################################
176 //# O U T P U T S T R E A M
177 //#########################################################################
179 /**
180 * This interface is the base of all input stream classes. Users who wish
181 * to make an OutputStream that is part of a chain should inherit from
182 * BasicOutputStream. Inherit from this class to make a destination endpoint,
183 * such as a URI or buffer.
184 */
185 class OutputStream
186 {
188 public:
190 /**
191 * Constructor.
192 */
193 OutputStream() {}
195 /**
196 * Destructor
197 */
198 virtual ~OutputStream() {}
200 /**
201 * This call should
202 * 1. flush itself
203 * 2. close itself
204 * 3. close the destination stream
205 */
206 virtual void close() = 0;
208 /**
209 * This call should push any pending data it might have to
210 * the destination stream. It should NOT call flush() on
211 * the destination stream.
212 */
213 virtual void flush() = 0;
215 /**
216 * Send one byte to the destination stream.
217 */
218 virtual void put(XMLCh ch) = 0;
221 }; // class OutputStream
224 /**
225 * This is the class that most users should inherit, to provide
226 * their own output streams.
227 */
228 class BasicOutputStream : public OutputStream
229 {
231 public:
233 BasicOutputStream(const OutputStream &destinationStream);
235 virtual ~BasicOutputStream() {}
237 virtual void close();
239 virtual void flush();
241 virtual void put(XMLCh ch);
243 protected:
245 bool closed;
247 OutputStream &destination;
250 }; // class BasicOutputStream
254 /**
255 * Convenience class for writing to standard output
256 */
257 class StdOutputStream : public OutputStream
258 {
259 public:
261 void close()
262 { }
264 void flush()
265 { }
267 void put(XMLCh ch)
268 { putchar(ch); }
270 };
275 //#########################################################################
276 //# R E A D E R
277 //#########################################################################
280 /**
281 * This interface and its descendants are for unicode character-oriented input
282 *
283 */
284 class Reader
285 {
287 public:
289 /**
290 * Constructor.
291 */
292 Reader() {}
294 /**
295 * Destructor
296 */
297 virtual ~Reader() {}
300 virtual int available() = 0;
302 virtual void close() = 0;
304 virtual int get() = 0;
306 virtual DOMString readLine() = 0;
308 virtual DOMString readWord() = 0;
310 /* Input formatting */
311 virtual Reader& readBool (bool& val ) = 0;
313 virtual Reader& readShort (short &val) = 0;
315 virtual Reader& readUnsignedShort (unsigned short &val) = 0;
317 virtual Reader& readInt (int &val) = 0;
319 virtual Reader& readUnsignedInt (unsigned int &val) = 0;
321 virtual Reader& readLong (long &val) = 0;
323 virtual Reader& readUnsignedLong (unsigned long &val) = 0;
325 virtual Reader& readFloat (float &val) = 0;
327 virtual Reader& readDouble (double &val) = 0;
329 }; // interface Reader
333 /**
334 * This class and its descendants are for unicode character-oriented input
335 *
336 */
337 class BasicReader : public Reader
338 {
340 public:
342 BasicReader(Reader &sourceStream);
344 virtual ~BasicReader() {}
346 virtual int available();
348 virtual void close();
350 virtual int get();
352 virtual DOMString readLine();
354 virtual DOMString readWord();
356 /* Input formatting */
357 virtual Reader& readBool (bool& val );
359 virtual Reader& readShort (short &val) ;
361 virtual Reader& readUnsignedShort (unsigned short &val) ;
363 virtual Reader& readInt (int &val) ;
365 virtual Reader& readUnsignedInt (unsigned int &val) ;
367 virtual Reader& readLong (long &val) ;
369 virtual Reader& readUnsignedLong (unsigned long &val) ;
371 virtual Reader& readFloat (float &val) ;
373 virtual Reader& readDouble (double &val) ;
375 protected:
377 Reader *source;
379 BasicReader()
380 { source = NULL; }
382 private:
384 }; // class BasicReader
388 Reader& operator>> (Reader &reader, bool& val );
390 Reader& operator>> (Reader &reader, short &val);
392 Reader& operator>> (Reader &reader, unsigned short &val);
394 Reader& operator>> (Reader &reader, int &val);
396 Reader& operator>> (Reader &reader, unsigned int &val);
398 Reader& operator>> (Reader &reader, long &val);
400 Reader& operator>> (Reader &reader, unsigned long &val);
402 Reader& operator>> (Reader &reader, float &val);
404 Reader& operator>> (Reader &reader, double &val);
409 /**
410 * Class for placing a Reader on an open InputStream
411 *
412 */
413 class InputStreamReader : public BasicReader
414 {
415 public:
417 InputStreamReader(const InputStream &inputStreamSource);
419 /*Overload these 3 for your implementation*/
420 virtual int available();
422 virtual void close();
424 virtual int get();
427 private:
429 InputStream &inputStream;
432 };
434 /**
435 * Convenience class for reading formatted from standard input
436 *
437 */
438 class StdReader : public BasicReader
439 {
440 public:
442 StdReader();
444 ~StdReader();
446 /*Overload these 3 for your implementation*/
447 virtual int available();
449 virtual void close();
451 virtual int get();
454 private:
456 InputStream *inputStream;
459 };
465 //#########################################################################
466 //# W R I T E R
467 //#########################################################################
469 /**
470 * This interface and its descendants are for unicode character-oriented output
471 *
472 */
473 class Writer
474 {
476 public:
478 /**
479 * Constructor.
480 */
481 Writer() {}
483 /**
484 * Destructor
485 */
486 virtual ~Writer() {}
488 virtual void close() = 0;
490 virtual void flush() = 0;
492 virtual void put(XMLCh ch) = 0;
494 /* Formatted output */
495 virtual Writer& printf(char *fmt, ...) = 0;
497 virtual Writer& writeChar(char val) = 0;
499 virtual Writer& writeString(const DOMString &val) = 0;
501 virtual Writer& writeBool (bool val ) = 0;
503 virtual Writer& writeShort (short val ) = 0;
505 virtual Writer& writeUnsignedShort (unsigned short val ) = 0;
507 virtual Writer& writeInt (int val ) = 0;
509 virtual Writer& writeUnsignedInt (unsigned int val ) = 0;
511 virtual Writer& writeLong (long val ) = 0;
513 virtual Writer& writeUnsignedLong (unsigned long val ) = 0;
515 virtual Writer& writeFloat (float val ) = 0;
517 virtual Writer& writeDouble (double val ) = 0;
521 }; // interface Writer
524 /**
525 * This class and its descendants are for unicode character-oriented output
526 *
527 */
528 class BasicWriter : public Writer
529 {
531 public:
533 BasicWriter(const Writer &destinationWriter);
535 virtual ~BasicWriter() {}
537 /*Overload these 3 for your implementation*/
538 virtual void close();
540 virtual void flush();
542 virtual void put(XMLCh ch);
546 /* Formatted output */
547 virtual Writer &printf(char *fmt, ...);
549 virtual Writer& writeChar(char val);
551 virtual Writer& writeString(const DOMString &val);
553 virtual Writer& writeBool (bool val );
555 virtual Writer& writeShort (short val );
557 virtual Writer& writeUnsignedShort (unsigned short val );
559 virtual Writer& writeInt (int val );
561 virtual Writer& writeUnsignedInt (unsigned int val );
563 virtual Writer& writeLong (long val );
565 virtual Writer& writeUnsignedLong (unsigned long val );
567 virtual Writer& writeFloat (float val );
569 virtual Writer& writeDouble (double val );
572 protected:
574 Writer *destination;
576 BasicWriter()
577 { destination = NULL; }
579 private:
581 }; // class BasicWriter
585 Writer& operator<< (Writer &writer, char val);
587 Writer& operator<< (Writer &writer, const DOMString &val);
589 Writer& operator<< (Writer &writer, bool val);
591 Writer& operator<< (Writer &writer, short val);
593 Writer& operator<< (Writer &writer, unsigned short val);
595 Writer& operator<< (Writer &writer, int val);
597 Writer& operator<< (Writer &writer, unsigned int val);
599 Writer& operator<< (Writer &writer, long val);
601 Writer& operator<< (Writer &writer, unsigned long val);
603 Writer& operator<< (Writer &writer, float val);
605 Writer& operator<< (Writer &writer, double val);
610 /**
611 * Class for placing a Writer on an open OutputStream
612 *
613 */
614 class OutputStreamWriter : public BasicWriter
615 {
616 public:
618 OutputStreamWriter(OutputStream &outputStreamDest);
620 /*Overload these 3 for your implementation*/
621 virtual void close();
623 virtual void flush();
625 virtual void put(XMLCh ch);
628 private:
630 OutputStream &outputStream;
633 };
636 /**
637 * Convenience class for writing to standard output
638 */
639 class StdWriter : public BasicWriter
640 {
641 public:
642 StdWriter();
644 ~StdWriter();
647 virtual void close();
650 virtual void flush();
653 virtual void put(XMLCh ch);
656 private:
658 OutputStream *outputStream;
660 };
662 //#########################################################################
663 //# U T I L I T Y
664 //#########################################################################
666 void pipeStream(InputStream &source, OutputStream &dest);
670 } //namespace io
671 } //namespace dom
672 } //namespace w3c
673 } //namespace org
676 #endif /* __DOMSTREAM_H__ */
678 //#########################################################################
679 //# E N D O F F I L E
680 //#########################################################################