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 {
45 class StreamException
46 {
47 public:
49 StreamException(const DOMString &theReason) throw()
50 { reason = theReason; }
51 ~StreamException() throw()
52 { }
53 char const *what()
54 { return reason.c_str(); }
56 private:
58 DOMString reason;
60 };
62 //#########################################################################
63 //# I N P U T S T R E A M
64 //#########################################################################
66 /**
67 * This interface is the base of all input stream classes. Users who wish
68 * to make an InputStream that is part of a chain should inherit from
69 * BasicInputStream. Inherit from this class to make a source endpoint,
70 * such as a URI or buffer.
71 *
72 */
73 class InputStream
74 {
76 public:
78 /**
79 * Constructor.
80 */
81 InputStream() {}
83 /**
84 * Destructor
85 */
86 virtual ~InputStream() {}
88 /**
89 * Return the number of bytes that are currently available
90 * to be read
91 */
92 virtual int available() = 0;
94 /**
95 * Do whatever it takes to 'close' this input stream
96 * The most likely implementation of this method will be
97 * for endpoints that use a resource for their data.
98 */
99 virtual void close() = 0;
101 /**
102 * Read one byte from this input stream. This is a blocking
103 * call. If no data is currently available, this call will
104 * not return until it exists. If the user does not want
105 * their code to block, then the usual solution is:
106 * if (available() > 0)
107 * myChar = get();
108 * This call returns -1 on end-of-file.
109 */
110 virtual int get() = 0;
112 }; // class InputStream
117 /**
118 * This is the class that most users should inherit, to provide
119 * their own streams.
120 *
121 */
122 class BasicInputStream : public InputStream
123 {
125 public:
127 BasicInputStream(const InputStream &sourceStream);
129 virtual ~BasicInputStream() {}
131 virtual int available();
133 virtual void close();
135 virtual int get();
137 protected:
139 bool closed;
141 InputStream &source;
143 private:
146 }; // class BasicInputStream
150 /**
151 * Convenience class for reading from standard input
152 */
153 class StdInputStream : public InputStream
154 {
155 public:
157 int available()
158 { return 0; }
160 void close()
161 { /* do nothing */ }
163 int get()
164 { return getchar(); }
166 };
173 //#########################################################################
174 //# O U T P U T S T R E A M
175 //#########################################################################
177 /**
178 * This interface is the base of all input stream classes. Users who wish
179 * to make an OutputStream that is part of a chain should inherit from
180 * BasicOutputStream. Inherit from this class to make a destination endpoint,
181 * such as a URI or buffer.
182 */
183 class OutputStream
184 {
186 public:
188 /**
189 * Constructor.
190 */
191 OutputStream() {}
193 /**
194 * Destructor
195 */
196 virtual ~OutputStream() {}
198 /**
199 * This call should
200 * 1. flush itself
201 * 2. close itself
202 * 3. close the destination stream
203 */
204 virtual void close() = 0;
206 /**
207 * This call should push any pending data it might have to
208 * the destination stream. It should NOT call flush() on
209 * the destination stream.
210 */
211 virtual void flush() = 0;
213 /**
214 * Send one byte to the destination stream.
215 */
216 virtual void put(XMLCh ch) = 0;
219 }; // class OutputStream
222 /**
223 * This is the class that most users should inherit, to provide
224 * their own output streams.
225 */
226 class BasicOutputStream : public OutputStream
227 {
229 public:
231 BasicOutputStream(const OutputStream &destinationStream);
233 virtual ~BasicOutputStream() {}
235 virtual void close();
237 virtual void flush();
239 virtual void put(XMLCh ch);
241 protected:
243 bool closed;
245 OutputStream &destination;
248 }; // class BasicOutputStream
252 /**
253 * Convenience class for writing to standard output
254 */
255 class StdOutputStream : public OutputStream
256 {
257 public:
259 void close()
260 { }
262 void flush()
263 { }
265 void put(XMLCh ch)
266 { putchar(ch); }
268 };
273 //#########################################################################
274 //# R E A D E R
275 //#########################################################################
278 /**
279 * This interface and its descendants are for unicode character-oriented input
280 *
281 */
282 class Reader
283 {
285 public:
287 /**
288 * Constructor.
289 */
290 Reader() {}
292 /**
293 * Destructor
294 */
295 virtual ~Reader() {}
298 virtual int available() = 0;
300 virtual void close() = 0;
302 virtual int get() = 0;
304 virtual DOMString readLine() = 0;
306 virtual DOMString readWord() = 0;
308 /* Input formatting */
309 virtual Reader& readBool (bool& val ) = 0;
311 virtual Reader& readShort (short &val) = 0;
313 virtual Reader& readUnsignedShort (unsigned short &val) = 0;
315 virtual Reader& readInt (int &val) = 0;
317 virtual Reader& readUnsignedInt (unsigned int &val) = 0;
319 virtual Reader& readLong (long &val) = 0;
321 virtual Reader& readUnsignedLong (unsigned long &val) = 0;
323 virtual Reader& readFloat (float &val) = 0;
325 virtual Reader& readDouble (double &val) = 0;
327 }; // interface Reader
331 /**
332 * This class and its descendants are for unicode character-oriented input
333 *
334 */
335 class BasicReader : public Reader
336 {
338 public:
340 BasicReader(Reader &sourceStream);
342 virtual ~BasicReader() {}
344 virtual int available();
346 virtual void close();
348 virtual int get();
350 virtual DOMString readLine();
352 virtual DOMString readWord();
354 /* Input formatting */
355 virtual Reader& readBool (bool& val );
357 virtual Reader& readShort (short &val) ;
359 virtual Reader& readUnsignedShort (unsigned short &val) ;
361 virtual Reader& readInt (int &val) ;
363 virtual Reader& readUnsignedInt (unsigned int &val) ;
365 virtual Reader& readLong (long &val) ;
367 virtual Reader& readUnsignedLong (unsigned long &val) ;
369 virtual Reader& readFloat (float &val) ;
371 virtual Reader& readDouble (double &val) ;
373 protected:
375 Reader *source;
377 BasicReader()
378 { source = NULL; }
380 private:
382 }; // class BasicReader
386 Reader& operator>> (Reader &reader, bool& val );
388 Reader& operator>> (Reader &reader, short &val);
390 Reader& operator>> (Reader &reader, unsigned short &val);
392 Reader& operator>> (Reader &reader, int &val);
394 Reader& operator>> (Reader &reader, unsigned int &val);
396 Reader& operator>> (Reader &reader, long &val);
398 Reader& operator>> (Reader &reader, unsigned long &val);
400 Reader& operator>> (Reader &reader, float &val);
402 Reader& operator>> (Reader &reader, double &val);
407 /**
408 * Class for placing a Reader on an open InputStream
409 *
410 */
411 class InputStreamReader : public BasicReader
412 {
413 public:
415 InputStreamReader(const InputStream &inputStreamSource);
417 /*Overload these 3 for your implementation*/
418 virtual int available();
420 virtual void close();
422 virtual int get();
425 private:
427 InputStream &inputStream;
430 };
432 /**
433 * Convenience class for reading formatted from standard input
434 *
435 */
436 class StdReader : public BasicReader
437 {
438 public:
440 StdReader();
442 ~StdReader();
444 /*Overload these 3 for your implementation*/
445 virtual int available();
447 virtual void close();
449 virtual int get();
452 private:
454 InputStream *inputStream;
457 };
463 //#########################################################################
464 //# W R I T E R
465 //#########################################################################
467 /**
468 * This interface and its descendants are for unicode character-oriented output
469 *
470 */
471 class Writer
472 {
474 public:
476 /**
477 * Constructor.
478 */
479 Writer() {}
481 /**
482 * Destructor
483 */
484 virtual ~Writer() {}
486 virtual void close() = 0;
488 virtual void flush() = 0;
490 virtual void put(XMLCh ch) = 0;
492 /* Formatted output */
493 virtual Writer& printf(char *fmt, ...) = 0;
495 virtual Writer& writeChar(char val) = 0;
497 virtual Writer& writeString(const DOMString &val) = 0;
499 virtual Writer& writeBool (bool val ) = 0;
501 virtual Writer& writeShort (short val ) = 0;
503 virtual Writer& writeUnsignedShort (unsigned short val ) = 0;
505 virtual Writer& writeInt (int val ) = 0;
507 virtual Writer& writeUnsignedInt (unsigned int val ) = 0;
509 virtual Writer& writeLong (long val ) = 0;
511 virtual Writer& writeUnsignedLong (unsigned long val ) = 0;
513 virtual Writer& writeFloat (float val ) = 0;
515 virtual Writer& writeDouble (double val ) = 0;
519 }; // interface Writer
522 /**
523 * This class and its descendants are for unicode character-oriented output
524 *
525 */
526 class BasicWriter : public Writer
527 {
529 public:
531 BasicWriter(const Writer &destinationWriter);
533 virtual ~BasicWriter() {}
535 /*Overload these 3 for your implementation*/
536 virtual void close();
538 virtual void flush();
540 virtual void put(XMLCh ch);
544 /* Formatted output */
545 virtual Writer &printf(char *fmt, ...);
547 virtual Writer& writeChar(char val);
549 virtual Writer& writeString(const DOMString &val);
551 virtual Writer& writeBool (bool val );
553 virtual Writer& writeShort (short val );
555 virtual Writer& writeUnsignedShort (unsigned short val );
557 virtual Writer& writeInt (int val );
559 virtual Writer& writeUnsignedInt (unsigned int val );
561 virtual Writer& writeLong (long val );
563 virtual Writer& writeUnsignedLong (unsigned long val );
565 virtual Writer& writeFloat (float val );
567 virtual Writer& writeDouble (double val );
570 protected:
572 Writer *destination;
574 BasicWriter()
575 { destination = NULL; }
577 private:
579 }; // class BasicWriter
583 Writer& operator<< (Writer &writer, char val);
585 Writer& operator<< (Writer &writer, const DOMString &val);
587 Writer& operator<< (Writer &writer, bool val);
589 Writer& operator<< (Writer &writer, short val);
591 Writer& operator<< (Writer &writer, unsigned short val);
593 Writer& operator<< (Writer &writer, int val);
595 Writer& operator<< (Writer &writer, unsigned int val);
597 Writer& operator<< (Writer &writer, long val);
599 Writer& operator<< (Writer &writer, unsigned long val);
601 Writer& operator<< (Writer &writer, float val);
603 Writer& operator<< (Writer &writer, double val);
608 /**
609 * Class for placing a Writer on an open OutputStream
610 *
611 */
612 class OutputStreamWriter : public BasicWriter
613 {
614 public:
616 OutputStreamWriter(OutputStream &outputStreamDest);
618 /*Overload these 3 for your implementation*/
619 virtual void close();
621 virtual void flush();
623 virtual void put(XMLCh ch);
626 private:
628 OutputStream &outputStream;
631 };
634 /**
635 * Convenience class for writing to standard output
636 */
637 class StdWriter : public BasicWriter
638 {
639 public:
640 StdWriter();
642 ~StdWriter();
645 virtual void close();
648 virtual void flush();
651 virtual void put(XMLCh ch);
654 private:
656 OutputStream *outputStream;
658 };
660 //#########################################################################
661 //# U T I L I T Y
662 //#########################################################################
664 void pipeStream(InputStream &source, OutputStream &dest);
669 } //namespace dom
670 } //namespace w3c
671 } //namespace org
674 #endif /* __DOMSTREAM_H__ */