Xalan-C++ API Reference  1.12.0
XalanOutputStream.hpp
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 #if !defined(XALANOUTPUTSTREAM_HEADER_GUARD_1357924680)
19 #define XALANOUTPUTSTREAM_HEADER_GUARD_1357924680
20 
21 
22 
23 // Base include file. Must be first.
25 
26 
27 
29 
30 
31 
33 
34 
35 
38 
39 
40 
41 namespace XALAN_CPP_NAMESPACE {
42 
43 
44 
45 class XalanOutputTranscoder;
46 
47 
48 
50 {
51 public :
52 
53  enum { eDefaultBufferSize = 512u, eDefaultTranscoderBlockSize = 1024u };
54 
58 
59  /**
60  * Constructor.
61  *
62  * @param theBufferSize the size of the transcoding buffer
63  * @param theTranscoderBlockSize the size of the block used by the transcoder
64  * @param fThrowTranscodeException If true, an error transcoding will result in an exception being thrown.
65  */
66  explicit
68  MemoryManager& theManager,
69  size_type theBufferSize = eDefaultBufferSize,
70  size_type theTranscoderBlockSize = eDefaultTranscoderBlockSize,
71  bool fThrowTranscodeException = true);
72 
73  virtual
75 
76  MemoryManager&
78  {
79  return m_buffer.getMemoryManager();
80  }
81 
82  static const XalanDOMChar*
84  {
85 #if defined(XALAN_NEWLINE_IS_CRLF)
86  return s_nlCRString;
87 #else
88  return s_nlString;
89 #endif
90  }
91 
92  /**
93  * Write the appropriate newline character(s) to the stream.
94  */
95  virtual void
96  newline();
97 
98  /**
99  * Get the string which is appropriate for inserting a line feed in the stream.
100  */
101  virtual const XalanDOMChar*
102  getNewlineString() const;
103 
104  /**
105  * Flush the stream's transcoding buffer, but do not request
106  * the implementation class to flush its buffer.
107  * .
108  */
109  void
110  flushBuffer();
111 
112  /**
113  * Flush the stream's buffer.
114  */
115  void
117  {
118  flushBuffer();
119 
120  doFlush();
121  }
122 
123  /**
124  * Write a character to the output stream. The character
125  * will not be transcoded.
126  *
127  * @param theChar the character to write
128  */
129  void
130  write(char theChar)
131  {
132  write(&theChar, 1);
133  }
134 
135  /**
136  * Write a wide character to the output stream. The character
137  * will be transcoded, if an output encoding is specified.
138  *
139  * @param theChar the character to write
140  */
141  void
142  write(XalanDOMChar theChar)
143  {
144  assert(m_bufferSize > 0);
145 
146  if (m_buffer.size() == m_bufferSize)
147  {
148  flushBuffer();
149  }
150 
151  m_buffer.push_back(theChar);
152  }
153 
154  /**
155  * Write a null-terminated string to the output file. The character
156  * will not be transcoded. The caller is responsible for making sure the
157  * buffer is flushed before calling this member function.
158  *
159  * @param theBuffer character buffer to write
160  */
161  void
162  write(const char* theBuffer)
163  {
164  assert(theBuffer != 0);
165  assert(m_buffer.empty() == true);
166 
167  write(theBuffer, length(theBuffer));
168  }
169 
170  /**
171  * Write a null-terminated wide string to the output file. The string
172  * will be transcoded, if an output encoding is specified.
173  *
174  * @param theBuffer character buffer to write
175  */
176  void
177  write(const XalanDOMChar* theBuffer)
178  {
179  write(theBuffer, length(theBuffer));
180  }
181 
182  /**
183  * Write a specified number of characters to the output stream. The string
184  * will not be transcoded. The caller is responsible for making sure the
185  * buffer is flushed before calling this member function.
186  *
187  * @param theBuffer character buffer to write
188  * @param theBufferLength number of characters to write
189  */
190  void
192  const char* theBuffer,
193  size_type theBufferLength)
194  {
195  assert(theBuffer != 0);
196  assert(m_buffer.empty() == true);
197 
198  writeData(theBuffer,
199  theBufferLength);
200  }
201 
202  /**
203  * Write a specified number of characters to the output stream. The string
204  * will be transcoded, if an output encoding is specified.
205  *
206  * @param theBuffer character buffer to write
207  * @param theBufferLength number of characters to write
208  */
209  void
210  write(
211  const XalanDOMChar* theBuffer,
212  size_type theBufferLength);
213 
214  /**
215  * Get the output encoding for the stream.
216  *
217  * @return The encoding name
218  */
219  const XalanDOMString&
221  {
222  return m_encoding;
223  }
224 
225  /**
226  * Set the output encoding for the stream.
227  *
228  * @param theEncoding The encoding name
229  */
230  void
231  setOutputEncoding(const XalanDOMString& theEncoding);
232 
233  /**
234  * Determine if a given value can be represented in
235  * the output encoding.
236  *
237  * @return true if the value can be represented, and false if not.
238  */
239  bool
240  canTranscodeTo(XalanUnicodeChar theChar) const;
241 
242 
243  const XalanOutputTranscoder*
245  {
246  return m_transcoder;
247  }
248 
249  /**
250  * Set the flag that indicates whether a transcoding
251  * error should throw an exception. The default is
252  * to throw an exception. If this flag is false, and
253  * and an error occurs transcoding, then data will
254  * likely be lost.
255  *
256  * @return the value of the flag.
257  */
258  bool
260  {
261  return m_throwTranscodeException;
262  }
263 
264  /**
265  * Set the flag that indicates whether a transcoding
266  * error should throw an exception. The default is
267  * to throw an exception. If this flag is false, and
268  * and an error occurs transcoding, then data will
269  * likely be lost.
270  *
271  * @param the new value of the flag.
272  */
273  void
275  {
276  m_throwTranscodeException = flag;
277  }
278 
279  /**
280  * Set the size of the output buffer.
281  *
282  * @param theBufferSize The buffer size.
283  */
284  void
285  setBufferSize(size_type theBufferSize);
286 
287 
289  {
290  public:
291 
293  const XalanDOMString& theMessage,
294  MemoryManager& theManager,
295  const Locator* theLocator);
296 
298  const XalanDOMString& theMessage,
299  MemoryManager& theManager);
300 
302 
303  virtual
305 
306  virtual const XalanDOMChar*
307  getType() const;
308 
309  private:
310  };
311 
313  {
314  public:
315 
317  const XalanDOMString& theEncoding,
318  XalanDOMString& theBuffer,
319  const Locator* theLocator);
320 
322  const XalanDOMString& theEncoding,
323  XalanDOMString& theBuffer);
324 
327  m_encoding(
328  other.m_encoding,
329  other.m_memoryManager)
330  {
331  }
332 
333  virtual
335 
336  const XalanDOMString&
337  getEncoding() const
338  {
339  return m_encoding;
340  }
341 
342  virtual const XalanDOMChar*
343  getType() const;
344 
345  private:
346 
347  const XalanDOMString m_encoding;
348  };
349 
351  {
352  public:
353 
355  const XalanDOMString& theEncoding,
356  XalanDOMString& theBuffer,
357  const Locator* theLocator);
358 
360  const XalanDOMString& theEncoding,
361  XalanDOMString& theBuffer);
362 
364 
365  virtual
367 
368  virtual const XalanDOMChar*
369  getType() const;
370 
371  const XalanDOMString&
372  getEncoding() const
373  {
374  return m_encoding;
375  }
376 
377  private:
378 
379  const XalanDOMString m_encoding;
380  };
381 
383  {
384  public:
385 
387  XalanDOMString& theBuffer,
388  const Locator* theLocator);
389 
390  explicit
392 
394 
395  virtual
397 
398  virtual const XalanDOMChar*
399  getType() const;
400  };
401 
402  static XalanDOMString&
403  formatMessage(
404  const XalanDOMString& theMessage,
405  int theErrorCode,
406  XalanDOMString& theBuffer);
407 
408 protected:
409 
410  /**
411  * Transcode a wide string.
412  *
413  * @param theBuffer The string to transcode.
414  * @param theBufferLength The length of the string.
415  * @param theDestination The destination vector.
416  */
417  void
418  transcode(
419  const XalanDOMChar* theBuffer,
420  size_type theBufferLength,
421  TranscodeVectorType& theDestination);
422 
423  /**
424  * Write the data in the buffer
425  *
426  * @param theBuffer The data to write
427  * @param theBufferLength The length of theBuffer.
428  */
429  virtual void
430  writeData(
431  const char* theBuffer,
432  size_type theBufferLength) = 0;
433 
434  /**
435  * Flush the stream.
436  */
437  virtual void
438  doFlush() = 0;
439 
440  static const XalanDOMChar s_nlString[];
441  static const XalanDOMChar s_nlCRString[];
442 
445 
446 private:
447 
448  // These are not implemented...
450 
452  operator=(const XalanOutputStream&);
453 
454  bool
455  operator==(const XalanOutputStream&) const;
456 
457  void
458  doWrite(
459  const XalanDOMChar* theBuffer,
460  size_type theBufferLength);
461 
462 
463  const size_type m_transcoderBlockSize;
464 
465  XalanOutputTranscoder* m_transcoder;
466 
467  size_type m_bufferSize;
468 
469  BufferType m_buffer;
470 
471  XalanDOMString m_encoding;
472 
473  bool m_writeAsUTF16;
474 
475  bool m_throwTranscodeException;
476 
477  TranscodeVectorType m_transcodingBuffer;
478 };
479 
480 
481 
482 }
483 
484 
485 
486 #endif // XALANOUTPUTSTREAM_HEADER_GUARD_1357924680
xalanc::XalanOutputStream::s_nlCRStringLength
static const XalanDOMString::size_type s_nlCRStringLength
Definition: XalanOutputStream.hpp:444
xalanc::XalanOutputStream::UnsupportedEncodingException
Definition: XalanOutputStream.hpp:312
xalanc::XalanOutputStream::write
void write(const char *theBuffer, size_type theBufferLength)
Write a specified number of characters to the output stream.
Definition: XalanOutputStream.hpp:191
xalanc::XalanOutputStream::write
void write(const XalanDOMChar *theBuffer)
Write a null-terminated wide string to the output file.
Definition: XalanOutputStream.hpp:177
xalanc::XalanOutputTranscoder
Definition: XalanTranscodingServices.hpp:332
xalanc::XalanOutputStream::getMemoryManager
MemoryManager & getMemoryManager()
Definition: XalanOutputStream.hpp:77
XALAN_CPP_NAMESPACE
#define XALAN_CPP_NAMESPACE
Xalan-C++ namespace, including major and minor version.
Definition: XalanVersion.hpp:76
xalanc::XalanOutputStream::size_type
XalanTranscodingServices::size_type size_type
Definition: XalanOutputStream.hpp:57
XalanDOMString.hpp
xalanc::XalanOutputStream::UnsupportedEncodingException::UnsupportedEncodingException
UnsupportedEncodingException(const UnsupportedEncodingException &other)
Definition: XalanOutputStream.hpp:325
xalanc::XalanVector< XalanDOMChar >
xalanc::XalanOutputStream::TranscoderInternalFailureException::getEncoding
const XalanDOMString & getEncoding() const
Definition: XalanOutputStream.hpp:372
xalanc::size_type
size_t size_type
Definition: XalanMap.hpp:46
xalanc::XalanOutputStream::BufferType
XalanVector< XalanDOMChar > BufferType
Definition: XalanOutputStream.hpp:55
xalanc::XalanOutputStream::defaultNewlineString
static const XalanDOMChar * defaultNewlineString()
Definition: XalanOutputStream.hpp:83
XalanVector.hpp
xalanc::XalanOutputStream::write
void write(XalanDOMChar theChar)
Write a wide character to the output stream.
Definition: XalanOutputStream.hpp:142
xalanc::operator==
bool operator==(const XalanVector< Type > &theLHS, const XalanVector< Type > &theRHS)
Definition: XalanVector.hpp:1118
xalanc::XalanOutputStream::TranscodeVectorType
XalanVector< char > TranscodeVectorType
Definition: XalanOutputStream.hpp:56
XalanTranscodingServices.hpp
xalanc::XalanOutputStream::UnsupportedEncodingException::getEncoding
const XalanDOMString & getEncoding() const
Definition: XalanOutputStream.hpp:337
xalanc::XalanOutputStream::flush
void flush()
Flush the stream's buffer.
Definition: XalanOutputStream.hpp:116
xalanc::XalanOutputStream
Definition: XalanOutputStream.hpp:49
XALAN_PLATFORMSUPPORT_EXPORT
#define XALAN_PLATFORMSUPPORT_EXPORT
Definition: PlatformSupportDefinitions.hpp:35
PlatformSupportDefinitions.hpp
xalanc::XSLException
Definition: XSLException.hpp:42
xalanc::XalanOutputStream::getOutputEncoding
const XalanDOMString & getOutputEncoding() const
Get the output encoding for the stream.
Definition: XalanOutputStream.hpp:220
xalanc::XalanOutputStream::s_nlStringLength
static const XalanDOMString::size_type s_nlStringLength
Definition: XalanOutputStream.hpp:443
xalanc::XalanOutputStream::getThrowTranscodeException
bool getThrowTranscodeException() const
Set the flag that indicates whether a transcoding error should throw an exception.
Definition: XalanOutputStream.hpp:259
xalanc::length
XalanDOMString::size_type length(const XalanDOMString &theString)
Get the length of a XalanDOMString.
Definition: DOMStringHelper.hpp:235
xalanc::XalanOutputStream::XalanOutputStreamException
Definition: XalanOutputStream.hpp:288
XSLException.hpp
xalanc::XalanTranscodingServices::size_type
XalanSize_t size_type
Definition: XalanTranscodingServices.hpp:62
xalanc::XalanOutputStream::TranscodingException
Definition: XalanOutputStream.hpp:382
xalanc::XalanOutputStream::getTranscoder
const XalanOutputTranscoder * getTranscoder() const
Definition: XalanOutputStream.hpp:244
xalanc::XalanDOMString::size_type
XalanSize_t size_type
Definition: XalanDOMString.hpp:57
xalanc::XalanOutputStream::write
void write(const char *theBuffer)
Write a null-terminated string to the output file.
Definition: XalanOutputStream.hpp:162
xalanc::XalanDOMString
Definition: XalanDOMString.hpp:45
xalanc::XalanOutputStream::write
void write(char theChar)
Write a character to the output stream.
Definition: XalanOutputStream.hpp:130
xalanc::XalanOutputStream::TranscoderInternalFailureException
Definition: XalanOutputStream.hpp:350
xalanc::XalanOutputStream::setThrowTranscodeException
void setThrowTranscodeException(bool flag)
Set the flag that indicates whether a transcoding error should throw an exception.
Definition: XalanOutputStream.hpp:274