Xalan-C++ API Reference 1.12.0
XalanXMLSerializerBase.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(XALANXMLSERIALIZERBASE_HEADER_GUARD_1357924680)
19#define XALANXMLSERIALIZERBASE_HEADER_GUARD_1357924680
20
21
22
23
24// Base include file. Must be first.
26
27
28
30
31
32
34
35
36
37// Base class header file.
39
40
41
42namespace XALAN_CPP_NAMESPACE {
43
44
45
46class XalanOutputStream;
47
48
49
50using xercesc::MemoryManager;
51
52
53
54/**
55 * XalanXMLSerializerBase serves as a base class for XML serializers based on
56 * FormatterListener events.
57 */
59{
60public:
61
62 /**
63 * Perform static initialization. See class XMLSupportInit.
64 */
65 static void
66 initialize(MemoryManager& theManager);
67
68 /**
69 * Perform static shut down. See class XMLSupportInit.
70 */
71 static void
73
74
75 /**
76 * Constructor
77 *
78 * @param theManager The MemoryManager instance to use for any memory
79 * allocations
80 * @param doctypeSystem system identifier to be used in the document
81 * type declaration
82 * @param doctypePublic public identifier to be used in the document
83 * type declaration
84 * @param xmlDecl true if the XSLT processor should output an XML
85 * declaration
86 * @param theStandalone The string the XSLT processor should output for
87 * the standalone document declaration
88 */
90 MemoryManager& theManager,
95 bool xmlDecl,
97
98 virtual
100
101 MemoryManager&
103 {
104 return m_elemStack.getMemoryManager();
105 }
106
107
108 // These methods are inherited from FormatterListener ...
109
110 virtual void
111 setDocumentLocator(const Locator* const locator);
112
113 virtual void
115
116 virtual void
118 const XMLCh* const name,
119 AttributeList& attrs) = 0;
120
121 virtual void
122 endElement(const XMLCh* const name) = 0;
123
124 virtual void
126 const XMLCh* const chars,
127 const size_type length);
128
129 virtual void
131 const XMLCh* const chars,
132 const size_type length) = 0;
133
134 virtual void
135 entityReference(const XMLCh* const name) = 0;
136
137 virtual void
139 const XMLCh* const chars,
140 const size_type length);
141
142 virtual void
144 const XMLCh* const target,
145 const XMLCh* const data);
146
147 virtual void
149
150 virtual void
151 comment(const XMLCh* const data) = 0;
152
153 virtual void
155 const XMLCh* const ch,
156 const size_type length);
157
158 virtual const XalanDOMString&
160
161 virtual const XalanDOMString&
163
164 virtual const XalanDOMString&
165 getEncoding() const;
166
167 const XalanDOMString&
169 {
170 return m_version;
171 }
172
173 const XalanDOMString&
175 {
176 return m_standalone;
177 }
178
179 bool
181 {
182 return m_shouldWriteXMLHeader;
183 }
184
185 void
187 {
188 m_shouldWriteXMLHeader = b;
189 }
190
192
193
195
197
198
200 {
201 public:
202
203 // Static data members...
204 /**
205 * The string "UTF-8".
206 */
208
209 /**
210 * The string "<!DOCTYPE ".
211 */
212 static const char s_doctypeHeaderStartString[];
213
215
216 /**
217 * The string " PUBLIC \"".
218 */
219 static const char s_doctypeHeaderPublicString[];
220
222
223 /**
224 * The string " SYSTEM \"".
225 */
226 static const char s_doctypeHeaderSystemString[];
227
229
230 /**
231 * The string "<?xml version=\"".
232 */
233 static const char s_xmlHeaderStartString[];
234
236
237 /**
238 * The string "\" encoding=\"".
239 */
240 static const char s_xmlHeaderEncodingString[];
241
243
244 /**
245 * The string "\" standalone=\"".
246 */
247 static const char s_xmlHeaderStandaloneString[];
248
250
251 /**
252 * The string "\"?>".
253 */
254 static const char s_xmlHeaderEndString[];
255
257
258 /**
259 * The string "1.0".
260 */
261 static const char s_defaultVersionString[];
262
264
265 /**
266 * The string "-//W3C//DTD XHTML".
267 */
268 static const XalanDOMChar s_xhtmlDocTypeString[];
269
271
272 /**
273 * The string "<![CDATA[".
274 */
275 static const char s_cdataOpenString[];
276
278
279 /**
280 * The string "]]>".
281 */
282 static const char s_cdataCloseString[];
283
285
286 /**
287 * The string "&lt;".
288 */
289 static const char s_lessThanEntityString[];
290
292
293 /**
294 * The string "&gt;".
295 */
296 static const char s_greaterThanEntityString[];
297
299
300 /**
301 * The string "&amp;".
302 */
303 static const char s_ampersandEntityString[];
304
306
307 /**
308 * The string "&quot;".
309 */
310 static const char s_quoteEntityString[];
311
313 };
314
316 {
317 public:
318
319 /**
320 * The string "UTF-16".
321 */
323
324 /**
325 * The string "<!DOCTYPE ".
326 */
327 static const XalanDOMChar s_doctypeHeaderStartString[];
328
330
331 /**
332 * The string " PUBLIC \"".
333 */
334 static const XalanDOMChar s_doctypeHeaderPublicString[];
335
337
338 /**
339 * The string " SYSTEM \"".
340 */
341 static const XalanDOMChar s_doctypeHeaderSystemString[];
342
344
345 /**
346 * The string "<?xml version=\"".
347 */
348 static const XalanDOMChar s_xmlHeaderStartString[];
349
351
352 /**
353 * The string "\" encoding=\"".
354 */
355 static const XalanDOMChar s_xmlHeaderEncodingString[];
356
358
359 /**
360 * The string "\" standalone=\"".
361 */
362 static const XalanDOMChar s_xmlHeaderStandaloneString[];
363
365
366 /**
367 * The string "\"?>".
368 */
369 static const XalanDOMChar s_xmlHeaderEndString[];
370
372
373 /**
374 * The string "1.0".
375 */
376 static const XalanDOMChar s_defaultVersionString[];
377
379
380 /**
381 * The string "-//W3C//DTD XHTML".
382 */
383 static const XalanDOMChar s_xhtmlDocTypeString[];
384
386
387 /**
388 * The string "<![CDATA[".
389 */
390 static const XalanDOMChar s_cdataOpenString[];
391
393
394 /**
395 * The string "]]>".
396 */
397 static const XalanDOMChar s_cdataCloseString[];
398
400
401 /**
402 * The string "&lt;".
403 */
404 static const XalanDOMChar s_lessThanEntityString[];
405
407
408 /**
409 * The string "&gt;".
410 */
411 static const XalanDOMChar s_greaterThanEntityString[];
412
414
415 /**
416 * The string "&amp;".
417 */
418 static const XalanDOMChar s_ampersandEntityString[];
419
421
422 /**
423 * The string "&quot;".
424 */
425 static const XalanDOMChar s_quoteEntityString[];
426
428 };
429
430 enum
431 {
432 eBufferSize = 512 // The size of the buffer
433 };
434
436 {
437 public:
438
439 bool
441 {
442 return theChar > s_lastSpecial ?
443 false :
444 s_specialChars[theChar] > eNone;
445 }
446
447 bool
449 {
450 return theChar > s_lastSpecial ?
451 false :
452 s_specialChars[theChar] > eAttr;
453 }
454
455 bool
457 {
458 assert(theChar > 0);
459
460 return theChar > s_lastSpecial;
461 }
462
463 bool
465 {
466 return theChar > s_lastSpecial ?
467 false :
468 s_specialChars[theChar] == eForb;
469 }
470
471 bool
473 {
474 return theChar > s_lastSpecial ?
475 false :
476 s_specialChars[theChar] == eForb;
477 }
478
479
480 private:
481 static const size_t s_lastSpecial;
482
483 static const char s_specialChars[];
484 };
485
486
487
489 {
490 public:
491
492 bool
494 {
495 return theChar > s_lastSpecial ?
496 false :
497 s_specialChars[theChar] > eNone;
498 }
499
500 bool
502 {
503 return theChar > s_lastSpecial ?
504 false :
505 s_specialChars[theChar] > eAttr;
506 }
507
508 bool
510 {
511 assert(theChar > 0);
512
513 return theChar > s_lastSpecial;
514 }
515
516 bool
518 {
519 return theChar > s_lastSpecial ?
520 false :
521 s_specialChars[theChar] == eForb;
522 }
523
524 bool
526 {
527 return theChar > s_lastSpecial ?
528 false :
529 s_specialChars[theChar] == eCRFb;
530 }
531
532 private:
533 static const size_t s_lastSpecial;
534
535 static const char s_specialChars[];
536
537 };
538
539
540
541
542 enum
543 {
544 eNone = 0u,
545 eAttr = 1u, // A flag to indicate a value in s_specialChars applies to attributes
546 eBoth = 2u, // A flag to indicate a value in s_specialChars applies to both content and attributes
547 eForb = 4u, // A flag to indicate a forbidden value in s_specialChars
548 // XML1.1 put a requirement to output chars #x1...#x1F and #x7F...#x9F as charRefs only
549 // In the comments , PI and CDATA usage of charRefs is forbidden, so we will report an error in
550 eCRFb = 5u // that case. For the elemets and attributes is should work the same as eBoth
551 };
552
553
554
555protected:
556
557 virtual void
559
560 virtual void
562
563 virtual void
565
566 virtual void
568 const XMLCh* target,
569 const XMLCh* data) = 0;
570
571 virtual void
573 const XMLCh* chars,
574 size_type length) = 0;
575
576 virtual void
578 const XMLCh* chars,
579 size_type length) = 0;
580
581 virtual void
583
584 /**
585 * Mark the parent element as having a child. If this
586 * is the first child, return true, otherwise, return
587 * false. This allows the child element to determine
588 * if the parent tag has already been closed.
589 *
590 * @return true if the parent element has not been previously marked for children.
591 */
592 bool
594 {
595 if(!m_elemStack.empty())
596 {
597 // See if the parent element has already been flagged as having children.
598 if(false == m_elemStack.back())
599 {
600 m_elemStack.back() = true;
601
602 return true;
603 }
604 }
605
606 return false;
607 }
608
609 /**
610 * Determine if it a DOCTYPE declaration needs to
611 * be written.
612 */
613 bool
615 {
616 return m_needToOutputDoctypeDecl;
617 }
618
619 /**
620 * Open an element for possibile children
621 */
622 void
624 {
625 m_elemStack.push_back(false);
626 }
627
628 bool
630 {
631 return m_elemStack.empty();
632 }
633
634 /**
635 * Determine if an element ever had any children added.
636 *
637 * @return true if the children were added, false if not.
638 */
639 bool
641 {
642 bool fResult = false;
643
644 if (m_elemStack.empty() == false)
645 {
646 fResult = m_elemStack.back();
647
648 m_elemStack.pop_back();
649 }
650
651 return fResult;
652 }
653
654 void
656 {
657 if(true == m_needToOutputDoctypeDecl)
658 {
659 assert(m_doctypeSystem.empty() == false);
660
661 writeDoctypeDecl(name);
662
663 m_needToOutputDoctypeDecl = false;
664 }
665 }
666
667 /**
668 * Tell if the next text should be raw.
669 */
671
672 /**
673 * Add space before '/>' for XHTML.
674 */
676
677 /**
678 * The System ID for the doc type.
679 */
681
682 /**
683 * The public ID for the doc type.
684 */
686
687 /**
688 * Tells the XML version, for writing out to the XML decl.
689 */
691
692 /**
693 * Text for standalone part of header.
694 */
696
698
699 static bool
701 {
702 return 0xD800u <= theChar && theChar <= 0xDBFFu ? true : false;
703 }
704
705 static bool
707 {
708 return 0xDC00u <= theChar && theChar <= 0xDFFFu ? true : false;
709 }
710
711 static XalanUnicodeChar
715 MemoryManager& theManager);
716
717 /**
718 * Throw an exception when an invalid
719 * surrogate is encountered.
720 * @param ch The first character in the surrogate
721 */
722 static void
725 MemoryManager& theManager);
726
727 /**
728 * Throw an exception when an invalid
729 * surrogate is encountered.
730 * @param ch The first character in the surrogate
731 * @param next The next character in the surrogate
732 */
733 static void
736 XalanDOMChar next,
737 MemoryManager& theManager);
738
739 /**
740 * Throw an exception when an invalid
741 * character is encountered.
742 * @param ch The first character in the surrogate
743 * @param next The next character in the surrogate
744 */
745 static void
748 MemoryManager& theManager);
749
750
751 /**
752 * Throw an exception when an invalid
753 * character for the specific XML version is encountered.
754 * @param ch The first character in the surrogate
755 * @param next The next character in the surrogate
756 */
757 static void
761 MemoryManager& theManager);
762private:
763
764 // These are not implemented.
766
768 operator=(const XalanXMLSerializerBase&);
769
770 bool
772
773 // Data members...
774 /**
775 * Flag to tell that we need to add the doctype decl,
776 * which we can't do until the first element is
777 * encountered.
778 */
779 bool m_needToOutputDoctypeDecl;
780
781 /**
782 * If true, XML header should be written to output.
783 */
784 bool m_shouldWriteXMLHeader;
785
786 /**
787 * A stack of Boolean objects that tell if the given element
788 * has children.
789 */
790 BoolStackType m_elemStack;
791
792 /**
793 * The string "-//W3C//DTD XHTML".
794 */
795 static const XalanDOMChar s_xhtmlDocTypeString[];
796
797 static const size_type s_xhtmlDocTypeStringLength;
798};
799
800
801
802}
803
804
805
806#endif // XALANXMLSERIALIZERBASE_HEADER_GUARD_1357924680
#define XALAN_XMLSUPPORT_EXPORT
#define XALAN_CPP_NAMESPACE
Xalan-C++ namespace, including major and minor version.
A SAX-based formatter interface for the XSL processor.
static const XalanDOMString & s_encodingString
The string "UTF-16".
static const XalanDOMString & s_encodingString
The string "UTF-8".
XalanXMLSerializerBase serves as a base class for XML serializers based on FormatterListener events.
virtual void outputNewline()=0
bool childNodesWereAdded()
Determine if an element ever had any children added.
static void throwInvalidCharacterException(XalanUnicodeChar ch, MemoryManager &theManager)
Throw an exception when an invalid character is encountered.
virtual void entityReference(const XMLCh *const name)=0
Receive notification of a entityReference.
static bool isUTF16HighSurrogate(XalanDOMChar theChar)
static void initialize(MemoryManager &theManager)
Perform static initialization.
virtual void characters(const XMLCh *const chars, const size_type length)
const XalanDOMString m_standalone
Text for standalone part of header.
virtual const XalanDOMString & getDoctypeSystem() const
virtual void writeProcessingInstruction(const XMLCh *target, const XMLCh *data)=0
virtual void writeXMLHeader()=0
static const XalanDOMString & s_1_1String
const XalanDOMString m_doctypePublic
The public ID for the doc type.
virtual void ignorableWhitespace(const XMLCh *const chars, const size_type length)
static bool isUTF16LowSurrogate(XalanDOMChar theChar)
bool m_nextIsRaw
Tell if the next text should be raw.
void openElementForChildren()
Open an element for possibile children.
virtual const XalanDOMString & getEncoding() const
static XalanUnicodeChar decodeUTF16SurrogatePair(XalanDOMChar theHighSurrogate, XalanDOMChar theLowSurrogate, MemoryManager &theManager)
const XalanDOMString & getVersion() const
virtual void startElement(const XMLCh *const name, AttributeList &attrs)=0
virtual void comment(const XMLCh *const data)=0
Called when a Comment is to be constructed.
bool m_spaceBeforeClose
Add space before '/>' for XHTML.
virtual const XalanDOMString & getDoctypePublic() const
virtual void writeCDATA(const XMLCh *chars, size_type length)=0
static void terminate()
Perform static shut down.
const XalanDOMString & m_version
Tells the XML version, for writing out to the XML decl.
virtual void setDocumentLocator(const Locator *const locator)
bool getNeedToOutputDoctypeDecl() const
Determine if it a DOCTYPE declaration needs to be written.
virtual void charactersRaw(const XMLCh *const chars, const size_type length)=0
virtual void writeDoctypeDecl(const XalanDOMChar *name)=0
bool markParentForChildren()
Mark the parent element as having a child.
virtual void cdata(const XMLCh *const ch, const size_type length)
XalanXMLSerializerBase(MemoryManager &theManager, eXMLVersion theXMLVersion, const XalanDOMString &theEncoding, const XalanDOMString &theDoctypeSystem, const XalanDOMString &theDoctypePublic, bool xmlDecl, const XalanDOMString &theStandalone)
Constructor.
const XalanDOMString m_doctypeSystem
The System ID for the doc type.
static void throwInvalidUTF16SurrogateException(XalanDOMChar ch, XalanDOMChar next, MemoryManager &theManager)
Throw an exception when an invalid surrogate is encountered.
static void throwInvalidXMLCharacterException(XalanUnicodeChar ch, const XalanDOMString &theXMLversion, MemoryManager &theManager)
Throw an exception when an invalid character for the specific XML version is encountered.
virtual void processingInstruction(const XMLCh *const target, const XMLCh *const data)
virtual void endElement(const XMLCh *const name)=0
void generateDoctypeDecl(const XalanDOMChar *name)
const XalanDOMString & getStandalone() const
static void throwInvalidUTF16SurrogateException(XalanDOMChar ch, MemoryManager &theManager)
Throw an exception when an invalid surrogate is encountered.
static const XalanDOMString & s_1_0String
virtual void writeCharacters(const XMLCh *chars, size_type length)=0
size_t size_type
Definition XalanMap.hpp:46
bool operator==(const XalanVector< Type > &theLHS, const XalanVector< Type > &theRHS)