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
36namespace XALAN_CPP_NAMESPACE {
37
38
39
40using xercesc::MemoryManager;
41
42
43
45{
46public:
47
49
50
51 template <class WriterType>
53 {
54 public:
55
57
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:
109
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
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
157public:
158
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
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
223 {
224 return 0xD800u <= theChar && theChar <= 0xDBFFu ? true : false;
225 }
226
227 static bool
229 {
230 return 0xDC00u <= theChar && theChar <= 0xDFFFu ? true : false;
231 }
232
233 static XalanUnicodeChar
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
252 MemoryManager& theManager)
253 {
256
257 XalanMessageLoader::getMessage(
259 XalanMessages::InvalidScalar_1Param,
261
262 using xercesc::SAXException;
263
264 throw SAXException(theMessage.c_str(), &theManager);
265 }
266
267 void
270 MemoryManager& theManager)
271 {
273
274 const XalanOutputStream* const theStream =
275 m_writer.getStream();
276
278 ch,
279 theStream->getOutputEncoding(),
280 theBuffer);
281 }
282
283 static void
286 XalanDOMChar next,
287 MemoryManager& theManager)
288 {
289
291
293
295
297
299
300 XalanMessageLoader::getMessage(
302 XalanMessages::InvalidSurrogatePair_2Param,
304 chStr,
305 nextStr);
306
307 using xercesc::SAXException;
308
309 throw SAXException(theMessage.c_str(),&theManager);
310 }
311
312protected:
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 */
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&
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
357private:
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
#define XALAN_CPP_NAMESPACE
Xalan-C++ namespace, including major and minor version.
void push_back(XalanDOMChar theChar)
static void throwInvalidUTF16SurrogateException(XalanDOMChar ch, XalanDOMChar next, MemoryManager &theManager)
MemoryManager & m_memoryManager
The MemoryManager instance to use for any dynamically- allocated memory.
static bool isUTF16HighSurrogate(XalanDOMChar theChar)
XalanFormatterWriter(Writer &theWriter, MemoryManager &theMemoryManager)
static XalanUnicodeChar decodeUTF16SurrogatePair(XalanDOMChar theHighSurrogate, XalanDOMChar theLowSurrogate, MemoryManager &theManager)
const XalanOutputStream * getStream() const
size_type m_newlineStringLength
The length of the the string of characters that represents the newline.
static void throwInvalidCharacterException(XalanUnicodeChar ch, MemoryManager &theManager)
void throwUnrepresentableCharacterException(XalanUnicodeChar ch, MemoryManager &theManager)
static bool isUTF16LowSurrogate(XalanDOMChar theChar)
FormatterListener::size_type size_type
const XalanDOMString & formatNumericCharacterReference(XalanUnicodeChar theChar)
Format a code point as a numeric character reference.
const XalanDOMChar * m_newlineString
The string of characters that represents the newline.
XalanDOMString::size_type length(const XalanDOMString &theString)
Get the length of a XalanDOMString.
NumberToDOMString(double theValue, XalanDOMString &theResult)
Converts a double value into a XalanDOMString.
NumberToHexDOMString(XMLUInt64 theValue, XalanDOMString &theResult)
Converts an 64-bit unsigned int value into a XalanDOMString.