00001 /* 00002 * Licensed to the Apache Software Foundation (ASF) under one 00003 * or more contributor license agreements. See the NOTICE file 00004 * distributed with this work for additional information 00005 * regarding copyright ownership. The ASF licenses this file 00006 * to you under the Apache License, Version 2.0 (the "License"); 00007 * you may not use this file except in compliance with the License. 00008 * You may obtain a copy of the License at 00009 * 00010 * http://www.apache.org/licenses/LICENSE-2.0 00011 * 00012 * Unless required by applicable law or agreed to in writing, software 00013 * distributed under the License is distributed on an "AS IS" BASIS, 00014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00015 * See the License for the specific language governing permissions and 00016 * limitations under the License. 00017 */ 00018 #if !defined(XALANXMLSERIALIZERBASE_HEADER_GUARD_1357924680) 00019 #define XALANXMLSERIALIZERBASE_HEADER_GUARD_1357924680 00020 00021 00022 00023 00024 // Base include file. Must be first. 00025 #include "xalanc/XMLSupport/XMLSupportDefinitions.hpp" 00026 00027 00028 00029 #include "xalanc/Include/XalanVector.hpp" 00030 00031 00032 00033 #include "xalanc/XalanDOM/XalanDOMString.hpp" 00034 00035 00036 00037 // Base class header file. 00038 #include "xalanc/PlatformSupport/FormatterListener.hpp" 00039 00040 00041 00042 XALAN_CPP_NAMESPACE_BEGIN 00043 00044 00045 00046 class XalanOutputStream; 00047 00048 00049 00050 XALAN_USING_XERCES(MemoryManager) 00051 00052 00053 00054 /** 00055 * XalanXMLSerializerBase serves as a base class for XML serializers based on 00056 * FormatterListener events. 00057 */ 00058 class XALAN_XMLSUPPORT_EXPORT XalanXMLSerializerBase : public FormatterListener 00059 { 00060 public: 00061 00062 /** 00063 * Perform static initialization. See class XMLSupportInit. 00064 */ 00065 static void 00066 initialize(MemoryManager& theManager); 00067 00068 /** 00069 * Perform static shut down. See class XMLSupportInit. 00070 */ 00071 static void 00072 terminate(); 00073 00074 00075 /** 00076 * Constructor 00077 * 00078 * @param theManager The MemoryManager instance to use for any memory 00079 * allocations 00080 * @param doctypeSystem system identifier to be used in the document 00081 * type declaration 00082 * @param doctypePublic public identifier to be used in the document 00083 * type declaration 00084 * @param xmlDecl true if the XSLT processor should output an XML 00085 * declaration 00086 * @param theStandalone The string the XSLT processor should output for 00087 * the standalone document declaration 00088 */ 00089 XalanXMLSerializerBase( 00090 MemoryManager& theManager, 00091 eXMLVersion theXMLVersion, 00092 const XalanDOMString& theEncoding, 00093 const XalanDOMString& theDoctypeSystem, 00094 const XalanDOMString& theDoctypePublic, 00095 bool xmlDecl, 00096 const XalanDOMString& theStandalone); 00097 00098 virtual 00099 ~XalanXMLSerializerBase(); 00100 00101 MemoryManager& 00102 getMemoryManager() 00103 { 00104 return m_elemStack.getMemoryManager(); 00105 } 00106 00107 00108 // These methods are inherited from FormatterListener ... 00109 00110 virtual void 00111 setDocumentLocator(const Locator* const locator); 00112 00113 virtual void 00114 startDocument(); 00115 00116 virtual void 00117 startElement( 00118 const XMLCh* const name, 00119 AttributeList& attrs) = 0; 00120 00121 virtual void 00122 endElement(const XMLCh* const name) = 0; 00123 00124 virtual void 00125 characters( 00126 const XMLCh* const chars, 00127 const size_type length); 00128 00129 virtual void 00130 charactersRaw( 00131 const XMLCh* const chars, 00132 const size_type length) = 0; 00133 00134 virtual void 00135 entityReference(const XMLCh* const name) = 0; 00136 00137 virtual void 00138 ignorableWhitespace( 00139 const XMLCh* const chars, 00140 const size_type length); 00141 00142 virtual void 00143 processingInstruction( 00144 const XMLCh* const target, 00145 const XMLCh* const data); 00146 00147 virtual void 00148 resetDocument(); 00149 00150 virtual void 00151 comment(const XMLCh* const data) = 0; 00152 00153 virtual void 00154 cdata( 00155 const XMLCh* const ch, 00156 const size_type length); 00157 00158 virtual const XalanDOMString& 00159 getDoctypeSystem() const; 00160 00161 virtual const XalanDOMString& 00162 getDoctypePublic() const; 00163 00164 virtual const XalanDOMString& 00165 getEncoding() const; 00166 00167 const XalanDOMString& 00168 getVersion() const 00169 { 00170 return m_version; 00171 } 00172 00173 const XalanDOMString& 00174 getStandalone() const 00175 { 00176 return m_standalone; 00177 } 00178 00179 bool 00180 getShouldWriteXMLHeader() const 00181 { 00182 return m_shouldWriteXMLHeader; 00183 } 00184 00185 void 00186 setShouldWriteXMLHeader(bool b) 00187 { 00188 m_shouldWriteXMLHeader = b; 00189 } 00190 00191 typedef XalanVector<bool> BoolStackType; 00192 00193 00194 static const XalanDOMString& s_1_0String; 00195 00196 static const XalanDOMString& s_1_1String; 00197 00198 00199 class XALAN_XMLSUPPORT_EXPORT UTF8 00200 { 00201 public: 00202 00203 // Static data members... 00204 /** 00205 * The string "UTF-8". 00206 */ 00207 static const XalanDOMString& s_encodingString; 00208 00209 /** 00210 * The string "<!DOCTYPE ". 00211 */ 00212 static const char s_doctypeHeaderStartString[]; 00213 00214 static const size_type s_doctypeHeaderStartStringLength; 00215 00216 /** 00217 * The string " PUBLIC \"". 00218 */ 00219 static const char s_doctypeHeaderPublicString[]; 00220 00221 static const size_type s_doctypeHeaderPublicStringLength; 00222 00223 /** 00224 * The string " SYSTEM \"". 00225 */ 00226 static const char s_doctypeHeaderSystemString[]; 00227 00228 static const size_type s_doctypeHeaderSystemStringLength; 00229 00230 /** 00231 * The string "<?xml version=\"". 00232 */ 00233 static const char s_xmlHeaderStartString[]; 00234 00235 static const size_type s_xmlHeaderStartStringLength; 00236 00237 /** 00238 * The string "\" encoding=\"". 00239 */ 00240 static const char s_xmlHeaderEncodingString[]; 00241 00242 static const size_type s_xmlHeaderEncodingStringLength; 00243 00244 /** 00245 * The string "\" standalone=\"". 00246 */ 00247 static const char s_xmlHeaderStandaloneString[]; 00248 00249 static const size_type s_xmlHeaderStandaloneStringLength; 00250 00251 /** 00252 * The string "\"?>". 00253 */ 00254 static const char s_xmlHeaderEndString[]; 00255 00256 static const size_type s_xmlHeaderEndStringLength; 00257 00258 /** 00259 * The string "1.0". 00260 */ 00261 static const char s_defaultVersionString[]; 00262 00263 static const size_type s_defaultVersionStringLength; 00264 00265 /** 00266 * The string "-//W3C//DTD XHTML". 00267 */ 00268 static const XalanDOMChar s_xhtmlDocTypeString[]; 00269 00270 static const size_type s_xhtmlDocTypeStringLength; 00271 00272 /** 00273 * The string "<![CDATA[". 00274 */ 00275 static const char s_cdataOpenString[]; 00276 00277 static const size_type s_cdataOpenStringLength; 00278 00279 /** 00280 * The string "]]>". 00281 */ 00282 static const char s_cdataCloseString[]; 00283 00284 static const size_type s_cdataCloseStringLength; 00285 00286 /** 00287 * The string "<". 00288 */ 00289 static const char s_lessThanEntityString[]; 00290 00291 static const size_type s_lessThanEntityStringLength; 00292 00293 /** 00294 * The string ">". 00295 */ 00296 static const char s_greaterThanEntityString[]; 00297 00298 static const size_type s_greaterThanEntityStringLength; 00299 00300 /** 00301 * The string "&". 00302 */ 00303 static const char s_ampersandEntityString[]; 00304 00305 static const size_type s_ampersandEntityStringLength; 00306 00307 /** 00308 * The string """. 00309 */ 00310 static const char s_quoteEntityString[]; 00311 00312 static const size_type s_quoteEntityStringLength; 00313 }; 00314 00315 class XALAN_XMLSUPPORT_EXPORT UTF16 00316 { 00317 public: 00318 00319 /** 00320 * The string "UTF-16". 00321 */ 00322 static const XalanDOMString& s_encodingString; 00323 00324 /** 00325 * The string "<!DOCTYPE ". 00326 */ 00327 static const XalanDOMChar s_doctypeHeaderStartString[]; 00328 00329 static const size_type s_doctypeHeaderStartStringLength; 00330 00331 /** 00332 * The string " PUBLIC \"". 00333 */ 00334 static const XalanDOMChar s_doctypeHeaderPublicString[]; 00335 00336 static const size_type s_doctypeHeaderPublicStringLength; 00337 00338 /** 00339 * The string " SYSTEM \"". 00340 */ 00341 static const XalanDOMChar s_doctypeHeaderSystemString[]; 00342 00343 static const size_type s_doctypeHeaderSystemStringLength; 00344 00345 /** 00346 * The string "<?xml version=\"". 00347 */ 00348 static const XalanDOMChar s_xmlHeaderStartString[]; 00349 00350 static const size_type s_xmlHeaderStartStringLength; 00351 00352 /** 00353 * The string "\" encoding=\"". 00354 */ 00355 static const XalanDOMChar s_xmlHeaderEncodingString[]; 00356 00357 static const size_type s_xmlHeaderEncodingStringLength; 00358 00359 /** 00360 * The string "\" standalone=\"". 00361 */ 00362 static const XalanDOMChar s_xmlHeaderStandaloneString[]; 00363 00364 static const size_type s_xmlHeaderStandaloneStringLength; 00365 00366 /** 00367 * The string "\"?>". 00368 */ 00369 static const XalanDOMChar s_xmlHeaderEndString[]; 00370 00371 static const size_type s_xmlHeaderEndStringLength; 00372 00373 /** 00374 * The string "1.0". 00375 */ 00376 static const XalanDOMChar s_defaultVersionString[]; 00377 00378 static const size_type s_defaultVersionStringLength; 00379 00380 /** 00381 * The string "-//W3C//DTD XHTML". 00382 */ 00383 static const XalanDOMChar s_xhtmlDocTypeString[]; 00384 00385 static const size_type s_xhtmlDocTypeStringLength; 00386 00387 /** 00388 * The string "<![CDATA[". 00389 */ 00390 static const XalanDOMChar s_cdataOpenString[]; 00391 00392 static const size_type s_cdataOpenStringLength; 00393 00394 /** 00395 * The string "]]>". 00396 */ 00397 static const XalanDOMChar s_cdataCloseString[]; 00398 00399 static const size_type s_cdataCloseStringLength; 00400 00401 /** 00402 * The string "<". 00403 */ 00404 static const XalanDOMChar s_lessThanEntityString[]; 00405 00406 static const size_type s_lessThanEntityStringLength; 00407 00408 /** 00409 * The string ">". 00410 */ 00411 static const XalanDOMChar s_greaterThanEntityString[]; 00412 00413 static const size_type s_greaterThanEntityStringLength; 00414 00415 /** 00416 * The string "&". 00417 */ 00418 static const XalanDOMChar s_ampersandEntityString[]; 00419 00420 static const size_type s_ampersandEntityStringLength; 00421 00422 /** 00423 * The string """. 00424 */ 00425 static const XalanDOMChar s_quoteEntityString[]; 00426 00427 static const size_type s_quoteEntityStringLength; 00428 }; 00429 00430 enum 00431 { 00432 eBufferSize = 512 // The size of the buffer 00433 }; 00434 00435 class XALAN_XMLSUPPORT_EXPORT CharFunctor1_0 00436 { 00437 public: 00438 00439 bool 00440 attribute(XalanDOMChar theChar) const 00441 { 00442 return theChar > s_lastSpecial ? 00443 false : 00444 s_specialChars[theChar] > eNone; 00445 } 00446 00447 bool 00448 content(XalanDOMChar theChar) const 00449 { 00450 return theChar > s_lastSpecial ? 00451 false : 00452 s_specialChars[theChar] > eAttr; 00453 } 00454 00455 bool 00456 range(XalanDOMChar theChar) const 00457 { 00458 assert(theChar > 0); 00459 00460 return theChar > s_lastSpecial; 00461 } 00462 00463 bool 00464 isForbidden(XalanDOMChar theChar) const 00465 { 00466 return theChar > s_lastSpecial ? 00467 false : 00468 s_specialChars[theChar] == eForb; 00469 } 00470 00471 bool 00472 isCharRefForbidden(XalanDOMChar theChar) const 00473 { 00474 return theChar > s_lastSpecial ? 00475 false : 00476 s_specialChars[theChar] == eForb; 00477 } 00478 00479 00480 private: 00481 static const size_t s_lastSpecial; 00482 00483 static const char s_specialChars[]; 00484 }; 00485 00486 00487 00488 class XALAN_XMLSUPPORT_EXPORT CharFunctor1_1 00489 { 00490 public: 00491 00492 bool 00493 attribute(XalanDOMChar theChar) const 00494 { 00495 return theChar > s_lastSpecial ? 00496 false : 00497 s_specialChars[theChar] > eNone; 00498 } 00499 00500 bool 00501 content(XalanDOMChar theChar) const 00502 { 00503 return theChar > s_lastSpecial ? 00504 false : 00505 s_specialChars[theChar] > eAttr; 00506 } 00507 00508 bool 00509 range(XalanDOMChar theChar) const 00510 { 00511 assert(theChar > 0); 00512 00513 return theChar > s_lastSpecial; 00514 } 00515 00516 bool 00517 isForbidden(XalanDOMChar theChar) const 00518 { 00519 return theChar > s_lastSpecial ? 00520 false : 00521 s_specialChars[theChar] == eForb; 00522 } 00523 00524 bool 00525 isCharRefForbidden(XalanDOMChar theChar) const 00526 { 00527 return theChar > s_lastSpecial ? 00528 false : 00529 s_specialChars[theChar] == eCRFb; 00530 } 00531 00532 private: 00533 static const size_t s_lastSpecial; 00534 00535 static const char s_specialChars[]; 00536 00537 }; 00538 00539 00540 00541 00542 enum 00543 { 00544 eNone = 0u, 00545 eAttr = 1u, // A flag to indicate a value in s_specialChars applies to attributes 00546 eBoth = 2u, // A flag to indicate a value in s_specialChars applies to both content and attributes 00547 eForb = 4u, // A flag to indicate a forbidden value in s_specialChars 00548 // XML1.1 put a requirement to output chars #x1...#x1F and #x7F...#x9F as charRefs only 00549 // In the comments , PI and CDATA usage of charRefs is forbidden, so we will report an error in 00550 eCRFb = 5u // that case. For the elemets and attributes is should work the same as eBoth 00551 }; 00552 00553 00554 00555 protected: 00556 00557 virtual void 00558 writeXMLHeader() = 0; 00559 00560 virtual void 00561 flushBuffer() = 0; 00562 00563 virtual void 00564 writeDoctypeDecl(const XalanDOMChar* name) = 0; 00565 00566 virtual void 00567 writeProcessingInstruction( 00568 const XMLCh* target, 00569 const XMLCh* data) = 0; 00570 00571 virtual void 00572 writeCharacters( 00573 const XMLCh* chars, 00574 size_type length) = 0; 00575 00576 virtual void 00577 writeCDATA( 00578 const XMLCh* chars, 00579 size_type length) = 0; 00580 00581 virtual void 00582 outputNewline() = 0; 00583 00584 /** 00585 * Mark the parent element as having a child. If this 00586 * is the first child, return true, otherwise, return 00587 * false. This allows the child element to determine 00588 * if the parent tag has already been closed. 00589 * 00590 * @return true if the parent element has not been previously marked for children. 00591 */ 00592 bool 00593 markParentForChildren() 00594 { 00595 if(!m_elemStack.empty()) 00596 { 00597 // See if the parent element has already been flagged as having children. 00598 if(false == m_elemStack.back()) 00599 { 00600 m_elemStack.back() = true; 00601 00602 return true; 00603 } 00604 } 00605 00606 return false; 00607 } 00608 00609 /** 00610 * Determine if it a DOCTYPE declaration needs to 00611 * be written. 00612 */ 00613 bool 00614 getNeedToOutputDoctypeDecl() const 00615 { 00616 return m_needToOutputDoctypeDecl; 00617 } 00618 00619 /** 00620 * Open an element for possibile children 00621 */ 00622 void 00623 openElementForChildren() 00624 { 00625 m_elemStack.push_back(false); 00626 } 00627 00628 bool 00629 outsideDocumentElement() const 00630 { 00631 return m_elemStack.empty(); 00632 } 00633 00634 /** 00635 * Determine if an element ever had any children added. 00636 * 00637 * @return true if the children were added, false if not. 00638 */ 00639 bool 00640 childNodesWereAdded() 00641 { 00642 bool fResult = false; 00643 00644 if (m_elemStack.empty() == false) 00645 { 00646 fResult = m_elemStack.back(); 00647 00648 m_elemStack.pop_back(); 00649 } 00650 00651 return fResult; 00652 } 00653 00654 void 00655 generateDoctypeDecl(const XalanDOMChar* name) 00656 { 00657 if(true == m_needToOutputDoctypeDecl) 00658 { 00659 assert(m_doctypeSystem.empty() == false); 00660 00661 writeDoctypeDecl(name); 00662 00663 m_needToOutputDoctypeDecl = false; 00664 } 00665 } 00666 00667 /** 00668 * Tell if the next text should be raw. 00669 */ 00670 bool m_nextIsRaw; 00671 00672 /** 00673 * Add space before '/>' for XHTML. 00674 */ 00675 bool m_spaceBeforeClose; 00676 00677 /** 00678 * The System ID for the doc type. 00679 */ 00680 const XalanDOMString m_doctypeSystem; 00681 00682 /** 00683 * The public ID for the doc type. 00684 */ 00685 const XalanDOMString m_doctypePublic; 00686 00687 /** 00688 * Tells the XML version, for writing out to the XML decl. 00689 */ 00690 const XalanDOMString& m_version; 00691 00692 /** 00693 * Text for standalone part of header. 00694 */ 00695 const XalanDOMString m_standalone; 00696 00697 const XalanDOMString m_encoding; 00698 00699 static bool 00700 isUTF16HighSurrogate(XalanDOMChar theChar) 00701 { 00702 return 0xD800u <= theChar && theChar <= 0xDBFFu ? true : false; 00703 } 00704 00705 static bool 00706 isUTF16LowSurrogate(XalanDOMChar theChar) 00707 { 00708 return 0xDC00u <= theChar && theChar <= 0xDFFFu ? true : false; 00709 } 00710 00711 static XalanUnicodeChar 00712 decodeUTF16SurrogatePair( 00713 XalanDOMChar theHighSurrogate, 00714 XalanDOMChar theLowSurrogate, 00715 MemoryManager& theManager); 00716 00717 /** 00718 * Throw an exception when an invalid 00719 * surrogate is encountered. 00720 * @param ch The first character in the surrogate 00721 */ 00722 static void 00723 throwInvalidUTF16SurrogateException( 00724 XalanDOMChar ch, 00725 MemoryManager& theManager); 00726 00727 /** 00728 * Throw an exception when an invalid 00729 * surrogate is encountered. 00730 * @param ch The first character in the surrogate 00731 * @param next The next character in the surrogate 00732 */ 00733 static void 00734 throwInvalidUTF16SurrogateException( 00735 XalanDOMChar ch, 00736 XalanDOMChar next, 00737 MemoryManager& theManager); 00738 00739 /** 00740 * Throw an exception when an invalid 00741 * character is encountered. 00742 * @param ch The first character in the surrogate 00743 * @param next The next character in the surrogate 00744 */ 00745 static void 00746 throwInvalidCharacterException( 00747 XalanUnicodeChar ch, 00748 MemoryManager& theManager); 00749 00750 00751 /** 00752 * Throw an exception when an invalid 00753 * character for the specific XML version is encountered. 00754 * @param ch The first character in the surrogate 00755 * @param next The next character in the surrogate 00756 */ 00757 static void 00758 throwInvalidXMLCharacterException( 00759 XalanUnicodeChar ch, 00760 const XalanDOMString& theXMLversion, 00761 MemoryManager& theManager); 00762 private: 00763 00764 // These are not implemented. 00765 XalanXMLSerializerBase(const XalanXMLSerializerBase&); 00766 00767 XalanXMLSerializerBase& 00768 operator=(const XalanXMLSerializerBase&); 00769 00770 bool 00771 operator==(const XalanXMLSerializerBase&) const; 00772 00773 // Data members... 00774 /** 00775 * Flag to tell that we need to add the doctype decl, 00776 * which we can't do until the first element is 00777 * encountered. 00778 */ 00779 bool m_needToOutputDoctypeDecl; 00780 00781 /** 00782 * If true, XML header should be written to output. 00783 */ 00784 bool m_shouldWriteXMLHeader; 00785 00786 /** 00787 * A stack of Boolean objects that tell if the given element 00788 * has children. 00789 */ 00790 BoolStackType m_elemStack; 00791 00792 /** 00793 * The string "-//W3C//DTD XHTML". 00794 */ 00795 static const XalanDOMChar s_xhtmlDocTypeString[]; 00796 00797 static const size_type s_xhtmlDocTypeStringLength; 00798 }; 00799 00800 00801 00802 XALAN_CPP_NAMESPACE_END 00803 00804 00805 00806 #endif // XALANXMLSERIALIZERBASE_HEADER_GUARD_1357924680
Doxygen and GraphViz are used to generate this API documentation from the Xalan-C header files.
Xalan-C++ XSLT Processor Version 1.11 |
|