source: NonGTP/Xerces/xerces/include/xercesc/framework/MemBufInputSource.hpp @ 358

Revision 358, 10.4 KB checked in by bittner, 19 years ago (diff)

xerces added

Line 
1/*
2 * Copyright 1999-2000,2004 The Apache Software Foundation.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
18 * $Log: MemBufInputSource.hpp,v $
19 * Revision 1.9  2004/09/28 02:14:13  cargilld
20 * Add support for validating annotations.
21 *
22 * Revision 1.8  2004/09/08 13:55:57  peiyongz
23 * Apache License Version 2.0
24 *
25 * Revision 1.7  2004/01/29 11:46:29  cargilld
26 * Code cleanup changes to get rid of various compiler diagnostic messages.
27 *
28 * Revision 1.6  2003/12/01 23:23:25  neilg
29 * fix for bug 25118; thanks to Jeroen Witmond
30 *
31 * Revision 1.5  2003/07/30 14:48:38  peiyongz
32 * Allow default value
33 *
34 * Revision 1.4  2003/05/16 21:36:55  knoaman
35 * Memory manager implementation: Modify constructors to pass in the memory manager.
36 *
37 * Revision 1.3  2002/11/04 15:00:21  tng
38 * C++ Namespace Support.
39 *
40 * Revision 1.2  2002/02/20 18:17:01  tng
41 * [Bug 5977] Warnings on generating apiDocs.
42 *
43 * Revision 1.1.1.1  2002/02/01 22:21:50  peiyongz
44 * sane_include
45 *
46 * Revision 1.6  2000/12/14 18:49:54  tng
47 * Fix API document generation warning: "Warning: end of member group without matching begin"
48 *
49 * Revision 1.5  2000/02/24 20:00:22  abagchi
50 * Swat for removing Log from API docs
51 *
52 * Revision 1.4  2000/02/15 23:59:06  roddey
53 * More updated documentation of Framework classes.
54 *
55 * Revision 1.3  2000/02/15 01:21:30  roddey
56 * Some initial documentation improvements. More to come...
57 *
58 * Revision 1.2  2000/02/06 07:47:46  rahulj
59 * Year 2K copyright swat.
60 *
61 * Revision 1.1  2000/01/12 00:13:26  roddey
62 * These were moved from internal/ to framework/, which was something that should have
63 * happened long ago. They are really framework type of classes.
64 *
65 * Revision 1.1.1.1  1999/11/09 01:08:10  twl
66 * Initial checkin
67 *
68 * Revision 1.2  1999/11/08 20:44:43  rahul
69 * Swat for adding in Product name and CVS comment log variable.
70 *
71 */
72
73
74#if !defined(MEMBUFINPUTSOURCE_HPP)
75#define MEMBUFINPUTSOURCE_HPP
76
77#include <xercesc/sax/InputSource.hpp>
78
79XERCES_CPP_NAMESPACE_BEGIN
80
81class BinInputStream;
82
83
84/**
85 *  This class is a derivative of the standard InputSource class. It provides
86 *  for the parser access to data stored in a memory buffer. The type of
87 *  buffer and its host specific attributes are of little concern here. The
88 *  only real requirement is that the memory be readable by the current
89 *  process.
90 *
91 *  Note that the memory buffer size is expressed in <b>bytes</b>, not in
92 *  characters. If you pass it text data, you must account for the bytes
93 *  per character when indicating the buffer size.
94 *
95 *  As with all InputSource derivatives. The primary objective of an input
96 *  source is to create an input stream via which the parser can spool in
97 *  data from the referenced source. In this case, there are two options
98 *  available.
99 *
100 *  The passed buffer can be adopted or merely referenced. If it is adopted,
101 *  then it must be dynamically allocated and will be destroyed when the
102 *  input source is destroyed (no reference counting!.) If not adopted, the
103 *  caller must insure that it remains valid until the input source object
104 *  is destroyed.
105 *
106 *  The other option indicates whether each stream created for this input
107 *  source should get its own copy of the data, or whether it should just
108 *  stream the data directly from this object's copy of the data. The same
109 *  rules apply here, in that the buffer must either be copied by the
110 *  stream or it must remain valid until the stream is destroyed.
111 */
112class XMLPARSER_EXPORT MemBufInputSource : public InputSource
113{
114public :
115    // -----------------------------------------------------------------------
116    //  Constructors and Destructor
117    // -----------------------------------------------------------------------
118
119    /** @name Constructors */
120    //@{
121
122    /**
123      * A memory buffer input source is constructed from a buffer of byte
124      * data, and the count of bytes in that buffer. The parser will parse
125      * from this memory buffer until it has eaten the indicated number of
126      * bytes.
127      *
128      * Note that the system id provided serves two purposes. Firstly it is
129      * going to be displayed in error messages as the source of the error.
130      * And secondly, any entities which are refered to from this entity
131      * via relative paths/URLs will be relative to this fake system id.
132      *
133      * @param  srcDocBytes     The actual data buffer to be parsed from.
134      * @param  byteCount       The count of bytes (not characters, bytes!)
135      *                         in the buffer.
136      * @param  bufId           A fake system id for the buffer.
137      * @param  adoptBuffer     Indicates whether this object should adopt
138      *                         the buffer (i.e. make a copy of it) or just
139      *                         use it in place.
140      * @param  manager         Pointer to the memory manager to be used to
141      *                         allocate objects.
142      */
143    MemBufInputSource
144    (
145        const   XMLByte* const  srcDocBytes
146        , const unsigned int    byteCount
147        , const XMLCh* const    bufId
148        , const bool            adoptBuffer = false
149        , MemoryManager* const  manager = XMLPlatformUtils::fgMemoryManager
150    );
151
152    /**
153      * This constructor is identical to the previous one, except that it takes
154      * the fake system id in local code page form and transcodes it internally.
155      */
156    MemBufInputSource
157    (
158        const   XMLByte* const  srcDocBytes
159        , const unsigned int    byteCount
160        , const char* const     bufId
161        , const bool            adoptBuffer = false
162        , MemoryManager* const  manager = XMLPlatformUtils::fgMemoryManager
163    );
164    //@}
165
166    /** @name Destructor */
167    //@{
168    /**
169      * If the buffer was adopted, the copy made during construction is deleted
170      * at this point.
171      */
172    ~MemBufInputSource();
173    //@}
174
175
176    // -----------------------------------------------------------------------
177    //  Virtual input source interface
178    // -----------------------------------------------------------------------
179
180    /** @name Virtual methods */
181    //@{
182
183    /**
184      * This method will return a binary input stream derivative that will
185      * parse from the memory buffer. If setCopyBufToStream() has been set,
186      * then the stream will make its own copy. Otherwise, it will use the
187      * buffer as is (in which case it must remain valid until the stream
188      * is no longer in use, i.e. the parse completes.)
189      *
190      * @return A dynamically allocated binary input stream derivative that
191      *         can parse from the memory buffer.
192      */
193    BinInputStream* makeStream() const;
194
195    //@}
196
197
198    // -----------------------------------------------------------------------
199    //  Setter methods
200    // -----------------------------------------------------------------------
201
202    /** @name Setter methods */
203
204    //@{
205
206    /**
207      * By default, for safety's sake, each newly created stream from this
208      * input source will make its own copy of the buffer to stream from. This
209      * avoids having to deal with aliasing of the buffer for simple work. But,
210      * for higher performance applications or for large buffers, this is
211      * obviously not optimal.
212      *
213      * In such cases, you can call this method to turn off that default
214      * action. Once turned off, the streams will just get a pointer to the
215      * buffer and parse directly from that. In this case, you must insure that
216      * the buffer remains valid for as long as any parse events are still
217      * using it.
218      *
219      * @param  newState    The new boolean flag state to set.
220      */
221    void setCopyBufToStream(const bool newState);
222
223    /**
224      * This methods allows the MemBufInputSource to be used for more than
225      * one input source, instead of destructing/constructing another
226      * MemBufInputSource.
227      *
228      * @param  srcDocBytes     The actual data buffer to be parsed from.
229      * @param  byteCount       The count of bytes (not characters, bytes!)
230      *                         in the buffer.     
231      */
232    void resetMemBufInputSource(const   XMLByte* const  srcDocBytes
233                                , const unsigned int    byteCount);
234    //@}
235
236
237private :
238    // -----------------------------------------------------------------------
239    //  Unimplemented constructors and operators
240    // -----------------------------------------------------------------------
241    MemBufInputSource(const MemBufInputSource&);
242    MemBufInputSource& operator=(const MemBufInputSource&);
243
244    // -----------------------------------------------------------------------
245    //  Private data members
246    //
247    //  fAdopted
248    //      Indicates whether the buffer is adopted or not. If so, then it
249    //      is destroyed when the input source is destroyed.
250    //
251    //  fByteCount
252    //      The size of the source document.
253    //
254    //  fCopyBufToStream
255    //      This defaults to true (the safe option), which causes it to
256    //      give a copy of the buffer to any streams it creates. If you set
257    //      it to false, it will allow the streams to just reference the
258    //      buffer (in which case this input source must stay alive as long
259    //      as the buffer is in use by the stream.)
260    //
261    //  fSrcBytes
262    //      The source memory buffer that is being spooled from. Whether it
263    //      belongs to the this input source or not is controlled by the
264    //      fAdopted flag.
265    // -----------------------------------------------------------------------
266    bool            fAdopted;
267    unsigned int    fByteCount;
268    bool            fCopyBufToStream;
269    const XMLByte*  fSrcBytes;
270};
271
272
273inline void MemBufInputSource::setCopyBufToStream(const bool newState)
274{
275    fCopyBufToStream = newState;
276}
277
278XERCES_CPP_NAMESPACE_END
279
280#endif
Note: See TracBrowser for help on using the repository browser.