| 1 | // file : xsd/cxx/xml/std-sax-input-source.hxx |
|---|
| 2 | // author : Boris Kolpackov <boris@codesynthesis.com> |
|---|
| 3 | // copyright : Copyright (c) 2005-2008 Code Synthesis Tools CC |
|---|
| 4 | // license : GNU GPL v2 + exceptions; see accompanying LICENSE file |
|---|
| 5 | |
|---|
| 6 | #ifndef XSD_CXX_XML_SAX_STD_INPUT_SOURCE_HXX |
|---|
| 7 | #define XSD_CXX_XML_SAX_STD_INPUT_SOURCE_HXX |
|---|
| 8 | |
|---|
| 9 | #include <istream> |
|---|
| 10 | |
|---|
| 11 | #include <xsd/cxx/xml/string.hxx> |
|---|
| 12 | |
|---|
| 13 | #include <xercesc/sax/InputSource.hpp> |
|---|
| 14 | #include <xercesc/util/BinInputStream.hpp> |
|---|
| 15 | |
|---|
| 16 | namespace xsd |
|---|
| 17 | { |
|---|
| 18 | namespace cxx |
|---|
| 19 | { |
|---|
| 20 | namespace xml |
|---|
| 21 | { |
|---|
| 22 | namespace sax |
|---|
| 23 | { |
|---|
| 24 | class std_input_stream: public xercesc::BinInputStream |
|---|
| 25 | { |
|---|
| 26 | public : |
|---|
| 27 | std_input_stream (std::istream& is) |
|---|
| 28 | : is_ (is) |
|---|
| 29 | { |
|---|
| 30 | } |
|---|
| 31 | |
|---|
| 32 | #if _XERCES_VERSION >= 30000 |
|---|
| 33 | virtual XMLFilePos |
|---|
| 34 | curPos () const |
|---|
| 35 | { |
|---|
| 36 | return static_cast<XMLFilePos> (is_.tellg ()); |
|---|
| 37 | } |
|---|
| 38 | #else |
|---|
| 39 | virtual unsigned int |
|---|
| 40 | curPos () const |
|---|
| 41 | { |
|---|
| 42 | return static_cast<unsigned int> (is_.tellg ()); |
|---|
| 43 | } |
|---|
| 44 | #endif |
|---|
| 45 | |
|---|
| 46 | #if _XERCES_VERSION >= 30000 |
|---|
| 47 | virtual XMLSize_t |
|---|
| 48 | readBytes (XMLByte* const buf, const XMLSize_t size) |
|---|
| 49 | #else |
|---|
| 50 | virtual unsigned int |
|---|
| 51 | readBytes (XMLByte* const buf, const unsigned int size) |
|---|
| 52 | #endif |
|---|
| 53 | { |
|---|
| 54 | // Some implementations don't clear gcount if you |
|---|
| 55 | // call read() on a stream that is in the eof state. |
|---|
| 56 | // |
|---|
| 57 | if (is_.eof ()) |
|---|
| 58 | return 0; |
|---|
| 59 | |
|---|
| 60 | // Unset the exception failbit while we are working |
|---|
| 61 | // with the stream. |
|---|
| 62 | // |
|---|
| 63 | std::ios_base::iostate old (is_.exceptions ()); |
|---|
| 64 | is_.exceptions (old & ~std::ios_base::failbit); |
|---|
| 65 | |
|---|
| 66 | is_.read (reinterpret_cast<char*> (buf), |
|---|
| 67 | static_cast<std::streamsize> (size)); |
|---|
| 68 | |
|---|
| 69 | // Clear the fail bit if it was caused by eof and restore |
|---|
| 70 | // the original exception state. If there are any pending |
|---|
| 71 | // errors then the exception will be thrown now. |
|---|
| 72 | // |
|---|
| 73 | if (is_.fail () && is_.eof ()) |
|---|
| 74 | is_.clear (is_.rdstate () & ~std::ios_base::failbit); |
|---|
| 75 | |
|---|
| 76 | is_.exceptions (old); |
|---|
| 77 | |
|---|
| 78 | // Make sure that if we failed, readBytes won't be called |
|---|
| 79 | // again. |
|---|
| 80 | // |
|---|
| 81 | if (!(is_.bad () || is_.fail ())) |
|---|
| 82 | { |
|---|
| 83 | #if _XERCES_VERSION >= 30000 |
|---|
| 84 | return static_cast<XMLSize_t> (is_.gcount ()); |
|---|
| 85 | #else |
|---|
| 86 | return static_cast<unsigned int> (is_.gcount ()); |
|---|
| 87 | #endif |
|---|
| 88 | } |
|---|
| 89 | else |
|---|
| 90 | return 0; |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | private : |
|---|
| 94 | std::istream& is_; |
|---|
| 95 | }; |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | class std_input_source: public xercesc::InputSource |
|---|
| 99 | { |
|---|
| 100 | public : |
|---|
| 101 | std_input_source (std::istream& is) |
|---|
| 102 | : is_ (&is) |
|---|
| 103 | { |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | template <typename C> |
|---|
| 107 | std_input_source (std::istream& is, const C* system_id) |
|---|
| 108 | : xercesc::InputSource (xml::string (system_id).c_str ()), |
|---|
| 109 | is_ (&is) |
|---|
| 110 | { |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | template <typename C> |
|---|
| 114 | std_input_source (std::istream& is, |
|---|
| 115 | const std::basic_string<C>& system_id) |
|---|
| 116 | : xercesc::InputSource (xml::string (system_id).c_str ()), |
|---|
| 117 | is_ (&is) |
|---|
| 118 | { |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | template <typename C> |
|---|
| 122 | std_input_source (std::istream& is, |
|---|
| 123 | const C* system_id, |
|---|
| 124 | const C* public_id) |
|---|
| 125 | : xercesc::InputSource (xml::string (system_id).c_str (), |
|---|
| 126 | xml::string (public_id).c_str ()), |
|---|
| 127 | is_ (&is) |
|---|
| 128 | { |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | template <typename C> |
|---|
| 132 | std_input_source (std::istream& is, |
|---|
| 133 | const std::basic_string<C>& system_id, |
|---|
| 134 | const std::basic_string<C>& public_id) |
|---|
| 135 | : xercesc::InputSource (xml::string (system_id).c_str (), |
|---|
| 136 | xml::string (public_id).c_str ()), |
|---|
| 137 | is_ (&is) |
|---|
| 138 | { |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | struct copy {}; |
|---|
| 142 | |
|---|
| 143 | // Throws the copy exception if this function is called more |
|---|
| 144 | // than once. |
|---|
| 145 | // |
|---|
| 146 | virtual xercesc::BinInputStream* |
|---|
| 147 | makeStream () const |
|---|
| 148 | { |
|---|
| 149 | if (is_ == 0) |
|---|
| 150 | throw copy (); |
|---|
| 151 | |
|---|
| 152 | std::istream& is (*is_); |
|---|
| 153 | |
|---|
| 154 | is_ = 0; |
|---|
| 155 | |
|---|
| 156 | return new std_input_stream (is); |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | private : |
|---|
| 160 | mutable std::istream* is_; |
|---|
| 161 | }; |
|---|
| 162 | } |
|---|
| 163 | } |
|---|
| 164 | } |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | #endif // XSD_CXX_XML_SAX_STD_INPUT_SOURCE_HXX |
|---|