Xalan-C++ API Reference  1.12.0
XalanFormatterWriter.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(XALANFORMATTERWRITER_HEADER_GUARD_1357924680)
19 #define XALANFORMATTERWRITER_HEADER_GUARD_1357924680
20 
21 
22 
23 // Base include file. Must be first.
25 
26 #include <xercesc/sax/SAXException.hpp>
27 
33 
34 
35 
36 namespace XALAN_CPP_NAMESPACE {
37 
38 
39 
40 using xercesc::MemoryManager;
41 
42 
43 
45 {
46 public:
47 
49 
50 
51  template <class WriterType>
53  {
54  public:
55 
56  typedef WriterType writer_type;
57 
58  NewLineWriterFunctor(WriterType& writer) :
59  m_writer(writer),
60  m_newlineString(0),
61  m_newlineStringLength(0)
62  {
63  XalanOutputStream* stream = writer.getStream();
64 
65  if(stream != 0)
66  {
67  m_newlineString = stream->getNewlineString();
68  }
69  else
70  {
71  m_newlineString = XalanOutputStream::defaultNewlineString();
72  }
73 
74  assert(m_newlineString != 0);
75 
76  m_newlineStringLength = length(m_newlineString);
77  }
78 
79  void
81  {
82  assert(m_newlineString != 0 && length(m_newlineString) == m_newlineStringLength);
83 
84  m_writer.write(m_newlineString, m_newlineStringLength);
85  }
86 
87  private:
88 
89  WriterType& m_writer;
90 
91  /**
92  * The string of characters that represents the newline
93  */
94  const XalanDOMChar* m_newlineString;
95 
96  /**
97  * The length of the the string of characters that represents the newline
98  */
99  size_type m_newlineStringLength;
100  };
101 
102  template<class WriterType>
104  {
105  typedef typename WriterType::value_type value_type;
106 
107  public:
108  typedef WriterType writer_type;
109 
110  WhiteSpaceWriterFunctor(WriterType& writer) :
111  m_writer(writer)
112  {
113  }
114 
115  void
117  {
118  for ( size_type i = 0 ; i < count ; i++ )
119  {
120  m_writer.write(value_type(XalanUnicode::charSpace));
121  }
122  }
123 
124  private:
125 
126  WriterType& m_writer;
127  };
128 
130  {
131  public:
132 
134  m_stream(stream)
135  {
136  assert(stream != 0);
137  }
138 
139  bool
140  operator()(XalanUnicodeChar theChar) const
141  {
142  bool result = true;
143 
144  if (m_stream != 0)
145  {
146  result = m_stream->canTranscodeTo(theChar);
147  }
148 
149  return result;
150  }
151 
152  private:
153 
154  const XalanOutputStream* const m_stream;
155  };
156 
157 public:
158 
160  Writer& theWriter,
161  MemoryManager& theMemoryManager) :
162  m_writer(theWriter),
163  m_memoryManager(theMemoryManager),
164  m_stringBuffer(5, 0, theMemoryManager)
165  {
166  const XalanOutputStream* const theStream =
167  theWriter.getStream();
168 
169  if (theStream == 0)
170  {
171  m_newlineString = XalanOutputStream::defaultNewlineString();
172  }
173  else
174  {
175  m_newlineString = theStream->getNewlineString();
176  }
177 
178  assert(m_newlineString != 0);
179 
180  m_newlineStringLength = length(m_newlineString);
181 
182  assert(m_newlineString != 0);
183  }
184 
185  MemoryManager&
187  {
188  return m_memoryManager;
189  }
190 
191  virtual
193  {
194  }
195 
196  Writer*
197  getWriter() const
198  {
199  return &m_writer;
200  }
201 
204  {
205  return m_writer.getStream();
206  }
207 
208  const XalanOutputStream*
209  getStream() const
210  {
211  return m_writer.getStream();
212  }
213 
214  void
216  {
217  m_writer.flush();
218  }
219 
220 
221  static bool
222  isUTF16HighSurrogate(XalanDOMChar theChar)
223  {
224  return 0xD800u <= theChar && theChar <= 0xDBFFu ? true : false;
225  }
226 
227  static bool
228  isUTF16LowSurrogate(XalanDOMChar theChar)
229  {
230  return 0xDC00u <= theChar && theChar <= 0xDFFFu ? true : false;
231  }
232 
233  static XalanUnicodeChar
235  XalanDOMChar theHighSurrogate,
236  XalanDOMChar theLowSurrogate,
237  MemoryManager& theManager)
238  {
239  assert(isUTF16HighSurrogate(theHighSurrogate) == true);
240 
241  if (isUTF16LowSurrogate(theLowSurrogate) == false)
242  {
243  throwInvalidUTF16SurrogateException(theHighSurrogate, theLowSurrogate, theManager);
244  }
245 
246  return ((theHighSurrogate - 0xD800u) << 10) + theLowSurrogate - 0xDC00u + 0x00010000u;
247  }
248 
249  static void
251  XalanUnicodeChar ch,
252  MemoryManager& theManager)
253  {
254  XalanDOMString theMessage(theManager);
255  XalanDOMString theBuffer(theManager);
256 
257  XalanMessageLoader::getMessage(
258  theMessage,
259  XalanMessages::InvalidScalar_1Param,
260  NumberToHexDOMString(ch, theBuffer));
261 
262  using xercesc::SAXException;
263 
264  throw SAXException(theMessage.c_str(), &theManager);
265  }
266 
267  void
269  XalanUnicodeChar ch,
270  MemoryManager& theManager)
271  {
272  XalanDOMString theBuffer(theManager);
273 
274  const XalanOutputStream* const theStream =
275  m_writer.getStream();
276 
278  ch,
279  theStream->getOutputEncoding(),
280  theBuffer);
281  }
282 
283  static void
285  XalanDOMChar ch,
286  XalanDOMChar next,
287  MemoryManager& theManager)
288  {
289 
290  XalanDOMString chStr(theManager);
291 
292  XalanDOMString nextStr(theManager);
293 
294  NumberToHexDOMString(ch, chStr);
295 
296  NumberToHexDOMString(next, nextStr);
297 
298  XalanDOMString theMessage(theManager);
299 
300  XalanMessageLoader::getMessage(
301  theMessage,
302  XalanMessages::InvalidSurrogatePair_2Param,
303  theMessage,
304  chStr,
305  nextStr);
306 
307  using xercesc::SAXException;
308 
309  throw SAXException(theMessage.c_str(),&theManager);
310  }
311 
312 protected:
313 
314  /**
315  * The writer.
316  */
318 
319  /**
320  * The MemoryManager instance to use for any dynamically-
321  * allocated memory.
322  */
323  MemoryManager& m_memoryManager;
324 
326 
327  /**
328  * The string of characters that represents the newline
329  */
330  const XalanDOMChar* m_newlineString;
331 
332  /**
333  * The length of the the string of characters that represents the newline
334  */
336 
337  /**
338  * Format a code point as a numeric character reference.
339  *
340  * @param theChar A Unicode code point.
341  */
342  const XalanDOMString&
343  formatNumericCharacterReference(XalanUnicodeChar theChar)
344  {
345  m_stringBuffer.clear();
346 
347  m_stringBuffer.push_back(XalanDOMChar(XalanUnicode::charAmpersand));
348  m_stringBuffer.push_back(XalanDOMChar(XalanUnicode::charNumberSign));
349 
350  NumberToDOMString(theChar, m_stringBuffer);
351 
352  m_stringBuffer.push_back(XalanDOMChar(XalanUnicode::charSemicolon));
353 
354  return m_stringBuffer;
355  }
356 
357 private:
358 
359  // These are not implemented.
361 
363  operator=(const XalanFormatterWriter&);
364 };
365 
366 
367 
368 }
369 
370 
371 
372 #endif // XALANFORMATTERWRITER_HEADER_GUARD_1357924680
xalanc::XalanFormatterWriter::XalanFormatterWriter
XalanFormatterWriter(Writer &theWriter, MemoryManager &theMemoryManager)
Definition: XalanFormatterWriter.hpp:159
xalanc::XalanOutputStream::getNewlineString
virtual const XalanDOMChar * getNewlineString() const
Get the string which is appropriate for inserting a line feed in the stream.
XALAN_CPP_NAMESPACE
#define XALAN_CPP_NAMESPACE
Xalan-C++ namespace, including major and minor version.
Definition: XalanVersion.hpp:76
xalanc::Writer::getStream
virtual XalanOutputStream * getStream()
Get the stream associated with the writer...
xalanc::XalanFormatterWriter::isUTF16LowSurrogate
static bool isUTF16LowSurrogate(XalanDOMChar theChar)
Definition: XalanFormatterWriter.hpp:228
xalanc::XalanFormatterWriter::flushWriter
void flushWriter()
Definition: XalanFormatterWriter.hpp:215
xalanc::XalanFormatterWriter::CommonRepresentableCharFunctor
Definition: XalanFormatterWriter.hpp:129
Writer.hpp
xalanc::XalanFormatterWriter::getWriter
Writer * getWriter() const
Definition: XalanFormatterWriter.hpp:197
xalanc::XalanFormatterWriter::NewLineWriterFunctor::writer_type
WriterType writer_type
Definition: XalanFormatterWriter.hpp:56
xalanc::XalanFormatterWriter::WhiteSpaceWriterFunctor::WhiteSpaceWriterFunctor
WhiteSpaceWriterFunctor(WriterType &writer)
Definition: XalanFormatterWriter.hpp:110
xalanc::XalanFormatterWriter::NewLineWriterFunctor::operator()
void operator()()
Definition: XalanFormatterWriter.hpp:80
xalanc::XalanFormatterWriter::m_memoryManager
MemoryManager & m_memoryManager
The MemoryManager instance to use for any dynamically- allocated memory.
Definition: XalanFormatterWriter.hpp:323
XalanOutputStream.hpp
FormatterListener.hpp
xalanc::XalanFormatterWriter::getStream
const XalanOutputStream * getStream() const
Definition: XalanFormatterWriter.hpp:209
xalanc::NumberToHexDOMString
NumberToHexDOMString(XMLUInt64 theValue, XalanDOMString &theResult)
Converts an 64-bit unsigned int value into a XalanDOMString.
xalanc::XalanFormatterWriter::m_newlineString
const XalanDOMChar * m_newlineString
The string of characters that represents the newline.
Definition: XalanFormatterWriter.hpp:330
XalanMessageLoader.hpp
xalanc::XalanFormatterWriter::getMemoryManager
MemoryManager & getMemoryManager()
Definition: XalanFormatterWriter.hpp:186
xalanc::FormatterListener::size_type
XalanSize_t size_type
Definition: FormatterListener.hpp:63
xalanc::XalanOutputStream
Definition: XalanOutputStream.hpp:49
DOMStringHelper.hpp
xalanc::XalanOutputStream::getOutputEncoding
const XalanDOMString & getOutputEncoding() const
Get the output encoding for the stream.
Definition: XalanOutputStream.hpp:220
xalanc::XalanFormatterWriter::CommonRepresentableCharFunctor::operator()
bool operator()(XalanUnicodeChar theChar) const
Definition: XalanFormatterWriter.hpp:140
xalanc::XalanFormatterWriter::getStream
XalanOutputStream * getStream()
Definition: XalanFormatterWriter.hpp:203
xalanc::XalanFormatterWriter::size_type
FormatterListener::size_type size_type
Definition: XalanFormatterWriter.hpp:48
xalanc::length
XalanDOMString::size_type length(const XalanDOMString &theString)
Get the length of a XalanDOMString.
Definition: DOMStringHelper.hpp:235
xalanc::XalanFormatterWriter::NewLineWriterFunctor::NewLineWriterFunctor
NewLineWriterFunctor(WriterType &writer)
Definition: XalanFormatterWriter.hpp:58
xalanc::NumberToDOMString
NumberToDOMString(double theValue, XalanDOMString &theResult)
Converts a double value into a XalanDOMString.
xalanc::XalanFormatterWriter::WhiteSpaceWriterFunctor::operator()
void operator()(size_type count)
Definition: XalanFormatterWriter.hpp:116
xalanc::XalanFormatterWriter
Definition: XalanFormatterWriter.hpp:44
xalanc::XalanFormatterWriter::throwUnrepresentableCharacterException
void throwUnrepresentableCharacterException(XalanUnicodeChar ch, MemoryManager &theManager)
Definition: XalanFormatterWriter.hpp:268
xalanc::XalanFormatterWriter::throwInvalidCharacterException
static void throwInvalidCharacterException(XalanUnicodeChar ch, MemoryManager &theManager)
Definition: XalanFormatterWriter.hpp:250
xalanc::XalanDOMString::clear
void clear()
Definition: XalanDOMString.hpp:257
xalanc::XalanFormatterWriter::CommonRepresentableCharFunctor::CommonRepresentableCharFunctor
CommonRepresentableCharFunctor(const XalanOutputStream *stream)
Definition: XalanFormatterWriter.hpp:133
xalanc::XalanFormatterWriter::decodeUTF16SurrogatePair
static XalanUnicodeChar decodeUTF16SurrogatePair(XalanDOMChar theHighSurrogate, XalanDOMChar theLowSurrogate, MemoryManager &theManager)
Definition: XalanFormatterWriter.hpp:234
xalanc::XalanDOMString::c_str
const XalanDOMChar * c_str() const
Definition: XalanDOMString.hpp:344
xalanc::XalanFormatterWriter::m_stringBuffer
XalanDOMString m_stringBuffer
Definition: XalanFormatterWriter.hpp:325
xalanc::XalanFormatterWriter::WhiteSpaceWriterFunctor
Definition: XalanFormatterWriter.hpp:103
xalanc::XalanFormatterWriter::m_writer
Writer & m_writer
The writer.
Definition: XalanFormatterWriter.hpp:317
xalanc::XalanTranscodingServices::UnrepresentableCharacterException
Definition: XalanTranscodingServices.hpp:280
xalanc::XalanFormatterWriter::isUTF16HighSurrogate
static bool isUTF16HighSurrogate(XalanDOMChar theChar)
Definition: XalanFormatterWriter.hpp:222
xalanc::XalanDOMString
Definition: XalanDOMString.hpp:45
xalanc::XalanFormatterWriter::m_newlineStringLength
size_type m_newlineStringLength
The length of the the string of characters that represents the newline.
Definition: XalanFormatterWriter.hpp:335
xalanc::XalanFormatterWriter::formatNumericCharacterReference
const XalanDOMString & formatNumericCharacterReference(XalanUnicodeChar theChar)
Format a code point as a numeric character reference.
Definition: XalanFormatterWriter.hpp:343
xalanc::XalanFormatterWriter::WhiteSpaceWriterFunctor::writer_type
WriterType writer_type
Definition: XalanFormatterWriter.hpp:108
XMLSupportDefinitions.hpp
xalanc::XalanFormatterWriter::throwInvalidUTF16SurrogateException
static void throwInvalidUTF16SurrogateException(XalanDOMChar ch, XalanDOMChar next, MemoryManager &theManager)
Definition: XalanFormatterWriter.hpp:284
xalanc::XalanFormatterWriter::NewLineWriterFunctor
Definition: XalanFormatterWriter.hpp:52
xalanc::XalanFormatterWriter::~XalanFormatterWriter
virtual ~XalanFormatterWriter()
Definition: XalanFormatterWriter.hpp:192
xalanc::XalanDOMString::push_back
void push_back(XalanDOMChar theChar)
Definition: XalanDOMString.hpp:529
xalanc::Writer
Definition: Writer.hpp:44