Xerces-C++ 3.2.0

XMLFormatter.hpp
Go to the documentation of this file.
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 /*
19 * $Id: XMLFormatter.hpp 932889 2010-04-11 13:10:10Z borisk $
20 */
21
22 #if !defined(XERCESC_INCLUDE_GUARD_XMLFORMATTER_HPP)
23 #define XERCESC_INCLUDE_GUARD_XMLFORMATTER_HPP
24
25 #include <xercesc/util/PlatformUtils.hpp>
26
27 XERCES_CPP_NAMESPACE_BEGIN
28
29 class XMLFormatTarget;
30 class XMLTranscoder;
31
41 class XMLPARSER_EXPORT XMLFormatter : public XMemory
42 {
43 public:
44 // -----------------------------------------------------------------------
45 // Class types
46 // -----------------------------------------------------------------------
124 enum EscapeFlags
125 {
126 NoEscapes
127 , StdEscapes
128 , AttrEscapes
129 , CharEscapes
130
131 // Special values, don't use directly
132 , EscapeFlags_Count
133 , DefaultEscape = 999
134 };
135
152 enum UnRepFlags
153 {
154 UnRep_Fail
155 , UnRep_CharRef
156 , UnRep_Replace
157
158 , DefaultUnRep = 999
159 };
161
162
163 // -----------------------------------------------------------------------
164 // Constructors and Destructor
165 // -----------------------------------------------------------------------
177 XMLFormatter
178 (
179 const XMLCh* const outEncoding
180 , const XMLCh* const docVersion
181 , XMLFormatTarget* const target
182 , const EscapeFlags escapeFlags = NoEscapes
183 , const UnRepFlags unrepFlags = UnRep_Fail
184 , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
185 );
186
187 XMLFormatter
188 (
189 const char* const outEncoding
190 , const char* const docVersion
191 , XMLFormatTarget* const target
192 , const EscapeFlags escapeFlags = NoEscapes
193 , const UnRepFlags unrepFlags = UnRep_Fail
194 , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
195 );
196
197 XMLFormatter
198 (
199 const XMLCh* const outEncoding
200 , XMLFormatTarget* const target
201 , const EscapeFlags escapeFlags = NoEscapes
202 , const UnRepFlags unrepFlags = UnRep_Fail
203 , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
204 );
205
206 XMLFormatter
207 (
208 const char* const outEncoding
209 , XMLFormatTarget* const target
210 , const EscapeFlags escapeFlags = NoEscapes
211 , const UnRepFlags unrepFlags = UnRep_Fail
212 , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager
213 );
214
215 ~XMLFormatter();
217
218
219 // -----------------------------------------------------------------------
220 // Formatting methods
221 // -----------------------------------------------------------------------
231 void formatBuf
232 (
233 const XMLCh* const toFormat
234 , const XMLSize_t count
235 , const EscapeFlags escapeFlags = DefaultEscape
236 , const UnRepFlags unrepFlags = DefaultUnRep
237 );
238
242 XMLFormatter& operator<<
243 (
244 const XMLCh* const toFormat
245 );
246
247 XMLFormatter& operator<<
248 (
249 const XMLCh toFormat
250 );
251
252 void writeBOM(const XMLByte* const toFormat
253 , const XMLSize_t count);
254
256
257 // -----------------------------------------------------------------------
258 // Getter methods
259 // -----------------------------------------------------------------------
266 const XMLCh* getEncodingName() const;
267
271 inline const XMLTranscoder* getTranscoder() const;
272
276 inline XMLTranscoder* getTranscoder();
277
279
280 // -----------------------------------------------------------------------
281 // Setter methods
282 // -----------------------------------------------------------------------
288 void setEscapeFlags
289 (
290 const EscapeFlags newFlags
291 );
292
296 void setUnRepFlags
297 (
298 const UnRepFlags newFlags
299 );
300
305 XMLFormatter& operator<<
306 (
307 const EscapeFlags newFlags
308 );
309
314 XMLFormatter& operator<<
315 (
316 const UnRepFlags newFlags
317 );
319
320 // -----------------------------------------------------------------------
321 // Getter methods
322 // -----------------------------------------------------------------------
328 EscapeFlags getEscapeFlags() const;
329
333 UnRepFlags getUnRepFlags() const;
335
336 private :
337 // -----------------------------------------------------------------------
338 // Unimplemented constructors and operators
339 // -----------------------------------------------------------------------
340 XMLFormatter();
341 XMLFormatter(const XMLFormatter&);
342 XMLFormatter& operator=(const XMLFormatter&);
343
344
345 // -----------------------------------------------------------------------
346 // Private class constants
347 // -----------------------------------------------------------------------
348 enum Constants
349 {
350 kTmpBufSize = 16 * 1024
351 };
352
353
354 // -----------------------------------------------------------------------
355 // Private helper methods
356 // -----------------------------------------------------------------------
357 const XMLByte* getCharRef(XMLSize_t &count,
358 XMLByte* &ref,
359 const XMLCh * stdRef);
360
361 void writeCharRef(const XMLCh &toWrite);
362 void writeCharRef(XMLSize_t toWrite);
363
364 bool inEscapeList(const XMLFormatter::EscapeFlags escStyle
365 , const XMLCh toCheck);
366
367
368 XMLSize_t handleUnEscapedChars(const XMLCh * srcPtr,
369 const XMLSize_t count,
370 const UnRepFlags unrepFlags);
371
372 void specialFormat
373 (
374 const XMLCh* const toFormat
375 , const XMLSize_t count
376 , const EscapeFlags escapeFlags
377 );
378
379
380 // -----------------------------------------------------------------------
381 // Private, non-virtual methods
382 //
383 // fEscapeFlags
384 // The escape flags we were told to use in formatting. These are
385 // defaults set in the ctor, which can be overridden on a particular
386 // call.
387 //
388 // fOutEncoding
389 // This the name of the output encoding. Saved mainly for meaningful
390 // error messages.
391 //
392 // fTarget
393 // This is the target object for the formatting operation.
394 //
395 // fUnRepFlags
396 // The unrepresentable flags that indicate how to react when a
397 // character cannot be represented in the target encoding.
398 //
399 // fXCoder
400 // This the transcoder that we will use. It is created using the
401 // encoding name we were told to use.
402 //
403 // fTmpBuf
404 // An output buffer that we use to transcode chars into before we
405 // send them off to be output.
406 //
407 // fAposRef
408 // fAmpRef
409 // fGTRef
410 // fLTRef
411 // fQuoteRef
412 // These are character refs for the standard char refs, in the
413 // output encoding. They are faulted in as required, by transcoding
414 // them from fixed Unicode versions.
415 //
416 // fIsXML11
417 // for performance reason, we do not store the actual version string
418 // and do the string comparison again and again.
419 //
420 // -----------------------------------------------------------------------
421 EscapeFlags fEscapeFlags;
422 XMLCh* fOutEncoding;
423 XMLFormatTarget* fTarget;
424 UnRepFlags fUnRepFlags;
425 XMLTranscoder* fXCoder;
426 XMLByte fTmpBuf[kTmpBufSize + 4];
427 XMLByte* fAposRef;
428 XMLSize_t fAposLen;
429 XMLByte* fAmpRef;
430 XMLSize_t fAmpLen;
431 XMLByte* fGTRef;
432 XMLSize_t fGTLen;
433 XMLByte* fLTRef;
434 XMLSize_t fLTLen;
435 XMLByte* fQuoteRef;
436 XMLSize_t fQuoteLen;
437 bool fIsXML11;
438 MemoryManager* fMemoryManager;
439 };
440
441
442 class XMLPARSER_EXPORT XMLFormatTarget : public XMemory
443 {
444 public:
445 // -----------------------------------------------------------------------
446 // Constructors and Destructor
447 // -----------------------------------------------------------------------
448 virtual ~XMLFormatTarget() {}
449
450
451 // -----------------------------------------------------------------------
452 // Virtual interface
453 // -----------------------------------------------------------------------
454 virtual void writeChars
455 (
456 const XMLByte* const toWrite
457 , const XMLSize_t count
458 , XMLFormatter* const formatter
459 ) = 0;
460
461 virtual void flush() {};
462
463
464 protected :
465 // -----------------------------------------------------------------------
466 // Hidden constructors and operators
467 // -----------------------------------------------------------------------
468 XMLFormatTarget() {};
469
470 private:
471 // -----------------------------------------------------------------------
472 // Unimplemented constructors and operators
473 // -----------------------------------------------------------------------
474 XMLFormatTarget(const XMLFormatTarget&);
475 XMLFormatTarget& operator=(const XMLFormatTarget&);
476 };
477
478
479 // ---------------------------------------------------------------------------
480 // XMLFormatter: Getter methods
481 // ---------------------------------------------------------------------------
482 inline const XMLCh* XMLFormatter::getEncodingName() const
483 {
484 return fOutEncoding;
485 }
486
487 inline const XMLTranscoder* XMLFormatter::getTranscoder() const
488 {
489 return fXCoder;
490 }
491
492 inline XMLTranscoder* XMLFormatter::getTranscoder()
493 {
494 return fXCoder;
495 }
496
497 // ---------------------------------------------------------------------------
498 // XMLFormatter: Setter methods
499 // ---------------------------------------------------------------------------
500 inline void XMLFormatter::setEscapeFlags(const EscapeFlags newFlags)
501 {
502 fEscapeFlags = newFlags;
503 }
504
505 inline void XMLFormatter::setUnRepFlags(const UnRepFlags newFlags)
506 {
507 fUnRepFlags = newFlags;
508 }
509
510
511 inline XMLFormatter& XMLFormatter::operator<<(const EscapeFlags newFlags)
512 {
513 fEscapeFlags = newFlags;
514 return *this;
515 }
516
517 inline XMLFormatter& XMLFormatter::operator<<(const UnRepFlags newFlags)
518 {
519 fUnRepFlags = newFlags;
520 return *this;
521 }
522
523 // ---------------------------------------------------------------------------
524 // XMLFormatter: Getter methods
525 // ---------------------------------------------------------------------------
526 inline XMLFormatter::EscapeFlags XMLFormatter::getEscapeFlags() const
527 {
528 return fEscapeFlags;
529 }
530
531 inline XMLFormatter::UnRepFlags XMLFormatter::getUnRepFlags() const
532 {
533 return fUnRepFlags;
534 }
535
536 XERCES_CPP_NAMESPACE_END
537
538 #endif
XMLFormatTarget::flush
virtual void flush()
Definition: XMLFormatter.hpp:461
XMLFormatter::setEscapeFlags
void setEscapeFlags(const EscapeFlags newFlags)
Definition: XMLFormatter.hpp:500
XMemory
This class makes it possible to override the C++ memory management by adding new/delete operators to ...
Definition: XMemory.hpp:40
XMLPlatformUtils::fgMemoryManager
static MemoryManager * fgMemoryManager
The configurable memory manager.
Definition: PlatformUtils.hpp:121
XMLFormatter::operator<<
XMLFormatter & operator<<(const XMLCh *const toFormat)
XMLFormatTarget::~XMLFormatTarget
virtual ~XMLFormatTarget()
Definition: XMLFormatter.hpp:448
XMLFormatter::getEncodingName
const XMLCh * getEncodingName() const
Definition: XMLFormatter.hpp:482
XMLFormatter::setUnRepFlags
void setUnRepFlags(const UnRepFlags newFlags)
Definition: XMLFormatter.hpp:505
XMLFormatter
This class provides the basic formatting capabilities that are required to turn the Unicode based XML...
Definition: XMLFormatter.hpp:41
XMLFormatter::getEscapeFlags
EscapeFlags getEscapeFlags() const
Definition: XMLFormatter.hpp:526
XMLFormatTarget::XMLFormatTarget
XMLFormatTarget()
Definition: XMLFormatter.hpp:468
XMLPARSER_EXPORT
#define XMLPARSER_EXPORT
Definition: XercesDefs.hpp:163
XMLTranscoder
XMLTranscoder is for transcoding non-local code page encodings, i.e.
Definition: TransService.hpp:215
PlatformUtils.hpp
MemoryManager
Configurable memory manager.
Definition: MemoryManager.hpp:39
XMLCh
uint16_t XMLCh
Definition: Xerces_autoconf_config.hpp:118
XERCES_CPP_NAMESPACE_BEGIN
#define XERCES_CPP_NAMESPACE_BEGIN
Definition: XercesDefs.hpp:112
XMLFormatter::getUnRepFlags
UnRepFlags getUnRepFlags() const
Definition: XMLFormatter.hpp:531
XMLFormatter::EscapeFlags
EscapeFlags
EscapeFlags - Different styles of escape flags to control various formatting.
Definition: XMLFormatter.hpp:124
XMLByte
unsigned char XMLByte
Definition: XercesDefs.hpp:65
XMLSize_t
size_t XMLSize_t
Definition: Xerces_autoconf_config.hpp:110
XERCES_CPP_NAMESPACE_END
#define XERCES_CPP_NAMESPACE_END
Definition: XercesDefs.hpp:113
XMLFormatter::UnRepFlags
UnRepFlags
UnRepFlags.
Definition: XMLFormatter.hpp:152
XMLFormatTarget
Definition: XMLFormatter.hpp:442
XMLFormatter::getTranscoder
const XMLTranscoder * getTranscoder() const
Definition: XMLFormatter.hpp:487