dbertoni    2003/07/31 15:55:59

  Modified:    c/src/xalanc/XMLSupport FormatterToXML_UTF16.cpp
                        FormatterToXML_UTF16.hpp FormatterToXML_UTF8.cpp
                        FormatterToXML_UTF8.hpp
  Added:       c/src/xalanc/XMLSupport FormatterToXMLBase.cpp
                        FormatterToXMLBase.hpp
  Log:
  Factored common code out into base class.
  
  Revision  Changes    Path
  1.4       +137 -361  
xml-xalan/c/src/xalanc/XMLSupport/FormatterToXML_UTF16.cpp
  
  Index: FormatterToXML_UTF16.cpp
  ===================================================================
  RCS file: 
/home/cvs/xml-xalan/c/src/xalanc/XMLSupport/FormatterToXML_UTF16.cpp,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- FormatterToXML_UTF16.cpp  17 Jul 2003 01:54:03 -0000      1.3
  +++ FormatterToXML_UTF16.cpp  31 Jul 2003 22:55:59 -0000      1.4
  @@ -62,13 +62,11 @@
   
   
   #include <xercesc/sax/AttributeList.hpp>
  -#include <xercesc/sax/SAXException.hpp>
   
   
   
   #include <xalanc/PlatformSupport/DOMStringHelper.hpp>
   #include <xalanc/PlatformSupport/Writer.hpp>
  -#include <xalanc/PlatformSupport/XalanOutputStream.hpp>
   #include <xalanc/PlatformSupport/XalanTranscodingServices.hpp>
   #include <xalanc/PlatformSupport/XalanUnicode.hpp>
   
  @@ -82,75 +80,6 @@
   
   
   
  -const XalanDOMChar   FormatterToXML_UTF16::s_specialChars[kSpecialsSize] =
  -{
  -     kNotSpecial,            // 0
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kBothSpecial,           // 0xA -- linefeed  Special because we 
normalize as requested.
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,            // 0x10
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,            // 0x20
  -     kNotSpecial,
  -     kAttributeSpecial,      // 0x22 '"'
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kBothSpecial,           // 0x26 -- '&'
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,            // 0x30
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kBothSpecial,           // 0x3C '<'
  -     kNotSpecial,
  -     kBothSpecial            // 0x3E '>'
  -};
  -
  -
  -
   FormatterToXML_UTF16::FormatterToXML_UTF16(
                        Writer&                                 writer,
                        const XalanDOMString&   version,
  @@ -159,52 +88,18 @@
                        const XalanDOMString&   doctypePublic,
                        bool                                    xmlDecl,
                        const XalanDOMString&   standalone) :
  -     FormatterListener(OUTPUT_METHOD_XML),
  -     m_writer(&writer),
  -     m_shouldWriteXMLHeader(xmlDecl),
  -     m_needToOutputDocTypeDecl(true),
  -     m_nextIsRaw(false),
  -     m_doctypeSystem(doctypeSystem),
  -     m_doctypePublic(doctypePublic),
  -     m_spaceBeforeClose(false),
  -     m_version(version),
  -     m_standalone(standalone),
  -     m_mediaType(mediaType),
  -     m_elemStack(),
  -     m_newlineString(0),
  -     m_newlineStringLength(0),
  +     FormatterToXMLBase(
  +             writer,
  +             version,
  +             mediaType,
  +             doctypeSystem,
  +             doctypePublic,
  +             xmlDecl,
  +             standalone),
        m_buffer(),
        m_bufferPosition(m_buffer),
        m_bufferRemaining(kBufferSize)
   {
  -     if(isEmpty(m_doctypePublic) == false)
  -     {
  -             if(startsWith(
  -                     m_doctypePublic,
  -                     s_xhtmlDocTypeString,
  -                     s_xhtmlDocTypeStringLength) == true)
  -             {
  -                     m_spaceBeforeClose = true;
  -             }
  -     }
  -
  -     const XalanOutputStream* const  theStream = writer.getStream();
  -
  -     if (theStream == 0)
  -     {
  -             m_newlineString = XalanOutputStream::defaultNewlineString();
  -     }
  -     else
  -     {
  -             m_newlineString = theStream->getNewlineString();
  -     }
  -
  -     assert(m_newlineString != 0);
  -
  -     m_newlineStringLength = length(m_newlineString);
  -
  -     assert(m_newlineString != 0);
  -
        const XalanTranscodingServices::XalanXMLByte*   theProlog =
                XalanTranscodingServices::getStreamProlog(s_utf16String);
        assert(theProlog != 0);
  @@ -278,7 +173,7 @@
   
   
   void
  -FormatterToXML_UTF16::outputDocTypeDecl(const XalanDOMChar*  name)
  +FormatterToXML_UTF16::writeDoctypeDecl(const XalanDOMChar*   name)
   {
        // "<!DOCTYPE "
        write(s_doctypeHeaderStartString, s_doctypeHeaderStartStringLength);
  @@ -334,9 +229,6 @@
   
   
   
  -
  -
  -
   void
   FormatterToXML_UTF16::writeDefaultEscape(XalanDOMChar        ch)
   {
  @@ -385,78 +277,22 @@
   
   
   void
  -FormatterToXML_UTF16::flushWriter()
  -{
  -     m_writer->flush();
  -}
  -
  -
  -
  -void
  -FormatterToXML_UTF16::setDocumentLocator(const LocatorType* const    /* 
locator */)
  -{
  -}
  -
  -
  -
  -void
  -FormatterToXML_UTF16::startDocument()
  +FormatterToXML_UTF16::writeParentTagEnd()
   {
  -     m_needToOutputDocTypeDecl = true;
  -
  -     if(m_shouldWriteXMLHeader == true)
  +     if(markParentForChildren() == true)
        {
  -             // "<?xml version=\""
  -             write(s_xmlHeaderStartString, s_xmlHeaderStartStringLength);
  -
  -             if (length(m_version) != 0)
  -             {
  -                     write(m_version);
  -             }
  -             else
  -             {
  -                     write(s_defaultVersionString, 
s_defaultVersionStringLength);
  -             }
  -
  -             // "\" encoding=\""
  -             write(s_xmlHeaderEncodingString, 
s_xmlHeaderEncodingStringLength);
  -
  -             write(s_utf16String);
  -
  -             if (length(m_standalone) != 0)
  -             {
  -                     write(s_xmlHeaderStandaloneString, 
s_xmlHeaderStandaloneStringLength);
  -                     write(m_standalone);
  -             }
  -
  -             write(s_xmlHeaderEndString, s_xmlHeaderEndStringLength);
  -
  -             outputLineSep();
  +             write(char(XalanUnicode::charGreaterThanSign));
        }
   }
   
   
   
   void
  -FormatterToXML_UTF16::endDocument()
  -{
  -     flushBuffer();
  -}
  -
  -
  -
  -void
   FormatterToXML_UTF16::startElement(
                        const XMLCh* const      name,
                        AttributeListType&      attrs)
   {
  -     if(true == m_needToOutputDocTypeDecl &&
  -        isEmpty(m_doctypeSystem) == false)
  -     {
  -             outputDocTypeDecl(name);
  -
  -             m_needToOutputDocTypeDecl = false;
  -     }
  +     generateDoctypeDecl(name);
   
        writeParentTagEnd();
   
  @@ -503,89 +339,70 @@
   
   
   void
  -FormatterToXML_UTF16::processingInstruction(
  -                     const XMLCh* const      target,
  -                     const XMLCh* const      data)
  +FormatterToXML_UTF16::writeProcessingInstruction(
  +                     const XMLCh*    target,
  +                     const XMLCh*    data)
   {
  -     // Use a fairly nasty hack to tell if the next node is supposed to be 
  -     // unescaped text.
  -     if(equals(target, length(target), s_piTarget, s_piTargetLength) == true 
&&
  -        equals(data, length(data), s_piData, s_piDataLength) == true)
  -     {
  -             m_nextIsRaw = true;
  -     }
  -     else    
  -     {
  -             writeParentTagEnd();
  +     writeParentTagEnd();
   
  -             write(XalanUnicode::charLessThanSign);
  -             write(XalanUnicode::charQuestionMark);
  -             writeName(target);
  +     write(XalanUnicode::charLessThanSign);
  +     write(XalanUnicode::charQuestionMark);
   
  -             const XalanDOMString::size_type         len = length(data);
  +     writeName(target);
   
  -             if ( len > 0 && !isXMLWhitespace(data[0]))
  -             {
  -                     write(XalanUnicode::charSpace);
  -             }
  +     const XalanDOMString::size_type         len = length(data);
   
  -             writeNormalizedPIData(data, len);
  +     // We need to make sure there is a least one whitespace character
  +     // between the target and the data.
  +     if ( len > 0 && !isXMLWhitespace(data[0]))
  +     {
  +             write(XalanUnicode::charSpace);
  +     }
   
  -             write(XalanUnicode::charQuestionMark);
  -             write(XalanUnicode::charGreaterThanSign);
  +     writeNormalizedPIData(data, len);
   
  -             // If outside of an element, then put in a new line.  This 
whitespace
  -             // is not significant.
  -             if (m_elemStack.empty() == true)
  -             {
  -                     outputLineSep();
  -             }
  +     write(XalanUnicode::charQuestionMark);
  +     write(XalanUnicode::charGreaterThanSign);
  +
  +     // If outside of the document element, then put in a new line.  This 
whitespace
  +     // is not significant.
  +     if (outsideDocumentElement() == true)
  +     {
  +             outputLineSep();
        }
   }
   
   
   
   void
  -FormatterToXML_UTF16::characters(
  -                     const XMLCh* const      chars,
  -                     const unsigned int      length)
  +FormatterToXML_UTF16::writeCharacters(
  +                     const XMLCh*    chars,
  +                     unsigned int    length)
   {
  -     if(length != 0)
  +     writeParentTagEnd();
  +
  +     unsigned int    i = 0;
  +     unsigned int    firstIndex = 0;
  +
  +     while(i < length) 
        {
  -             if(m_nextIsRaw)
  -             {
  -                     m_nextIsRaw = false;
  +             const XalanDOMChar      ch = chars[i];
   
  -                     charactersRaw(chars, length);
  +             if (isContentSpecial(ch) == false)
  +             {
  +                     ++i;
                }
                else
                {
  -                     writeParentTagEnd();
  -
  -                     unsigned int    i = 0;
  -                     unsigned int    firstIndex = 0;
  -
  -                     while(i < length) 
  -                     {
  -                             const XalanDOMChar      ch = chars[i];
  -
  -                             if (isContentSpecial(ch) == false)
  -                             {
  -                                     ++i;
  -                             }
  -                             else
  -                             {
  -                                     write(chars + firstIndex, i - 
firstIndex);
  -
  -                                     writeDefaultEscape(chars[i++]);
  +                     write(chars + firstIndex, i - firstIndex);
   
  -                                     firstIndex = i;
  -                             }
  -                     }
  +                     writeDefaultEscape(chars[i++]);
   
  -                     write(chars + firstIndex, i - firstIndex);
  +                     firstIndex = i;
                }
        }
  +
  +     write(chars + firstIndex, i - firstIndex);
   }
   
   
  @@ -602,30 +419,6 @@
   
   
   
  -Writer*
  -FormatterToXML_UTF16::getWriter() const
  -{
  -     return m_writer;
  -}
  -
  -
  -
  -const XalanDOMString&
  -FormatterToXML_UTF16::getDoctypeSystem() const
  -{
  -     return m_doctypeSystem;
  -}
  -
  -
  -
  -const XalanDOMString&
  -FormatterToXML_UTF16::getDoctypePublic() const
  -{
  -     return m_doctypePublic;
  -}
  -
  -
  -
   const XalanDOMString&
   FormatterToXML_UTF16::getEncoding() const
   {
  @@ -634,14 +427,6 @@
   
   
   
  -const XalanDOMString&
  -FormatterToXML_UTF16::getMediaType() const
  -{
  -     return m_mediaType;
  -}
  -
  -
  -
   void
   FormatterToXML_UTF16::writeAttrString(
                        const XalanDOMChar*                     theString,
  @@ -728,32 +513,63 @@
   {
        XalanDOMString::size_type i = 0;
   
  +     // enum for a cheezy little state machine.
  +     enum eState { eInitialState, eFirstRightSquareBracket, 
eSecondRightSquareBracket };
  +
  +     eState  theCurrentState = eInitialState;
  +
        while(i < length)
       {
                // If "]]>", which would close the CDATA appears in
                // the content, we have to put the first two characters
                // in the CDATA section, close the CDATA section, then
                // open a new one and add the last character.
  -             if (i < length - 2 &&
  -                     XalanUnicode::charRightSquareBracket == ch[i] &&
  -            XalanUnicode::charRightSquareBracket == ch[i + 1] &&
  -                     XalanUnicode::charGreaterThanSign == ch[ i + 2])
  +
  +             const XalanDOMChar      theChar = ch[i];
  +
  +             if (theChar == XalanUnicode::charRightSquareBracket)
                {
  -                     // "]]]]><![CDATA[>"
  -                     write(XalanUnicode::charRightSquareBracket);
  +                     if (theCurrentState == eInitialState)
  +                     {
  +                             theCurrentState = eFirstRightSquareBracket;
  +                     }
  +                     else if (theCurrentState == eFirstRightSquareBracket)
  +                     {
  +                             theCurrentState = eSecondRightSquareBracket;
  +                     }
  +
                        write(XalanUnicode::charRightSquareBracket);
  +             }
  +             else if (theChar == XalanUnicode::charGreaterThanSign)
  +             {
  +                     if (theCurrentState != eInitialState)
  +                     {
  +                             if (theCurrentState == eFirstRightSquareBracket)
  +                             {
  +                                     theCurrentState = eInitialState;
  +                             }
  +                             else
  +                             {
  +                                     theCurrentState = eInitialState;
   
  -                     write(s_cdataCloseString, s_cdataCloseStringLength);
  -                     write(s_cdataOpenString, s_cdataOpenStringLength);
  +                                     write(s_cdataCloseString, 
s_cdataCloseStringLength);
  +                                     write(s_cdataOpenString, 
s_cdataOpenStringLength);
  +                             }
  +                     }
   
                        write(XalanUnicode::charGreaterThanSign);
  -
  -                     i += 3;
                }
                else
                {
  -                     writeNormalizedChar(ch[i++]);
  +                     if (theCurrentState != eInitialState)
  +                     {
  +                             theCurrentState = eInitialState;
  +                     }
  +
  +                     writeNormalizedChar(theChar);
                }
  +
  +             ++i;
       }
   }
   
  @@ -772,27 +588,6 @@
   
   
   void
  -FormatterToXML_UTF16::ignorableWhitespace(
  -                     const XMLCh* const      chars,
  -                     const unsigned int      length)
  -{
  -     if (length > 0)
  -     {
  -             characters(chars, length);
  -     }
  -}
  -
  -
  -
  -void
  -FormatterToXML_UTF16::resetDocument()
  -{
  -     // I don't do anything with this yet.
  -}
  -
  -
  -
  -void
   FormatterToXML_UTF16::comment(const XMLCh* const     data)
   {
        writeParentTagEnd();
  @@ -812,71 +607,19 @@
   
   
   void
  -FormatterToXML_UTF16::cdata(
  -                     const XMLCh* const      ch,
  -                     const unsigned int      length)
  -{
  -     if(m_nextIsRaw == true)
  -     {
  -             m_nextIsRaw = false;
  -
  -             charactersRaw(ch, length);
  -     }
  -     else
  -     {
  -             writeParentTagEnd();
  -
  -             if(length > 0)
  -             {
  -                     write(s_cdataOpenString, s_cdataOpenStringLength);
  -
  -                     writeCDATAChars(ch, length);
  -
  -                     write(s_cdataCloseString, s_cdataCloseStringLength);
  -             }
  -     }
  -}
  -
  -
  -
  -void
  -FormatterToXML_UTF16::writeParentTagEnd()
  +FormatterToXML_UTF16::writeCDATA(
  +                     const XMLCh*    ch,
  +                     unsigned int    length)
   {
  -     if(!m_elemStack.empty())
  -     {
  -             // See if the parent element has already been flagged as having 
children.
  -             if(false == m_elemStack.back())
  -             {
  -                     write(char(XalanUnicode::charGreaterThanSign));
  -
  -                     m_elemStack.back() = true;
  -             }
  -     }
  -}
  -
  -
  -
  -void
  -FormatterToXML_UTF16::openElementForChildren()
  -{
  -     m_elemStack.push_back(false);
  -}
  +     assert(length != 0);
   
  +     writeParentTagEnd();
   
  +     write(s_cdataOpenString, s_cdataOpenStringLength);
   
  -bool
  -FormatterToXML_UTF16::childNodesWereAdded()
  -{
  -     bool    fResult = false;
  -
  -     if (m_elemStack.empty() == false)
  -     {
  -             fResult = m_elemStack.back();
  -
  -             m_elemStack.pop_back();
  -     }
  +     writeCDATAChars(ch, length);
   
  -     return fResult;
  +     write(s_cdataCloseString, s_cdataCloseStringLength);
   }
   
   
  @@ -900,6 +643,39 @@
                writeAttrString(value, length(value));
                write(char(XalanUnicode::charQuoteMark));
        }
  +}
  +
  +
  +
  +void
  +FormatterToXML_UTF16::writeXMLHeader()
  +{
  +     // "<?xml version=\""
  +     write(s_xmlHeaderStartString, s_xmlHeaderStartStringLength);
  +
  +     if (length(m_version) != 0)
  +     {
  +             write(m_version);
  +     }
  +     else
  +     {
  +             write(s_defaultVersionString, s_defaultVersionStringLength);
  +     }
  +
  +     // "\" encoding=\""
  +     write(s_xmlHeaderEncodingString, s_xmlHeaderEncodingStringLength);
  +
  +     write(s_utf16String);
  +
  +     if (length(m_standalone) != 0)
  +     {
  +             write(s_xmlHeaderStandaloneString, 
s_xmlHeaderStandaloneStringLength);
  +             write(m_standalone);
  +     }
  +
  +     write(s_xmlHeaderEndString, s_xmlHeaderEndStringLength);
  +
  +     outputLineSep();
   }
   
   
  
  
  
  1.5       +23 -208   
xml-xalan/c/src/xalanc/XMLSupport/FormatterToXML_UTF16.hpp
  
  Index: FormatterToXML_UTF16.hpp
  ===================================================================
  RCS file: 
/home/cvs/xml-xalan/c/src/xalanc/XMLSupport/FormatterToXML_UTF16.hpp,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- FormatterToXML_UTF16.hpp  15 Jul 2003 22:29:55 -0000      1.4
  +++ FormatterToXML_UTF16.hpp  31 Jul 2003 22:55:59 -0000      1.5
  @@ -65,16 +65,12 @@
   
   
   
  -#include <vector>
  -
  -
  -
   #include <xalanc/XalanDOM/XalanDOMString.hpp>
   
   
   
   // Base class header file.
  -#include <xalanc/PlatformSupport/FormatterListener.hpp>
  +#include "FormatterToXMLBase.hpp"
   
   
   
  @@ -82,14 +78,10 @@
   
   
   
  -class Writer;
  -
  -
  -
   /**
    * FormatterToXML_UTF16 formats SAX-style events into XML.
    */
  -class XALAN_XMLSUPPORT_EXPORT FormatterToXML_UTF16 : public 
FormatterListener 
  +class XALAN_XMLSUPPORT_EXPORT FormatterToXML_UTF16 : public 
FormatterToXMLBase 
   {
   public:
   
  @@ -136,15 +128,6 @@
        // These methods are inherited from FormatterListener ...
   
        virtual void
  -     setDocumentLocator(const LocatorType* const             locator);
  -
  -     virtual void
  -     startDocument();
  -
  -     virtual void
  -     endDocument();
  -
  -     virtual void
        startElement(
                        const XMLCh* const      name,
                        AttributeListType&      attrs);
  @@ -153,11 +136,6 @@
        endElement(const XMLCh* const   name);
   
       virtual void
  -     characters(
  -                     const XMLCh* const      chars,
  -                     const unsigned int      length);
  -
  -    virtual void
        charactersRaw(
                        const XMLCh* const      chars,
                        const unsigned int      length);
  @@ -166,86 +144,36 @@
        entityReference(const XMLCh* const      name);
   
        virtual void
  -     ignorableWhitespace(
  -                     const XMLCh* const      chars,
  -                     const unsigned int      length);
  -
  -     virtual void
  -     processingInstruction(
  -                     const XMLCh* const      target,
  -                     const XMLCh* const      data);
  -
  -
  -    virtual void
  -     resetDocument();
  -
  -     virtual void
        comment(const XMLCh* const      data);
   
  -     virtual void
  -     cdata(
  -                     const XMLCh* const      ch,
  -                     const unsigned int      length);
  -
  -     virtual Writer*
  -     getWriter() const;
  -
  -     virtual const XalanDOMString&
  -     getDoctypeSystem() const;
  -
  -     virtual const XalanDOMString&
  -     getDoctypePublic() const;
  -
        virtual const XalanDOMString&
        getEncoding() const;
   
  -     virtual const XalanDOMString&
  -     getMediaType() const;
  -
  -     const XalanDOMString&
  -     getVersion() const
  -     {
  -             return m_version;
  -     }
  +protected:
   
  -     const XalanDOMString&
  -     getStandalone() const
  -     {
  -             return m_standalone;
  -     }
  +     virtual void
  +     writeXMLHeader();
   
  -     bool
  -     getShouldWriteXMLHeader() const
  -     {
  -             return m_shouldWriteXMLHeader;
  -     }
  +     virtual void
  +     flushBuffer();
   
  -     void
  -     setShouldWriteXMLHeader(bool    b)
  -     {
  -             m_shouldWriteXMLHeader = b;
  -     }
  +     virtual void
  +     writeDoctypeDecl(const XalanDOMChar*    name);
   
  -#if defined(XALAN_NO_STD_NAMESPACE)
  -#if defined(XALAN_USE_DEQUE_FOR_VECTOR_BOOL)
  -     typedef deque<bool>                     BoolStackType;
  -#else
  -     typedef vector<bool>            BoolStackType;
  -#endif
  -#else
  -#if defined(XALAN_USE_DEQUE_FOR_VECTOR_BOOL)
  -     typedef std::deque<bool>        BoolStackType;
  -#else
  -     typedef std::vector<bool>       BoolStackType;
  -#endif
  -#endif
  +     virtual void
  +     writeProcessingInstruction(
  +                     const XMLCh*    target,
  +                     const XMLCh*    data);
   
  -protected:
  +     virtual void
  +     writeCharacters(
  +                     const XMLCh*    chars,
  +                     unsigned int    length);
   
  -     /** 
  -      * The writer.
  -      */
  -     Writer*                                 m_writer;
  +     virtual void
  +     writeCDATA(
  +                     const XMLCh*    chars,
  +                     unsigned int    length);
   
        /**
         * Output a line break.
  @@ -286,15 +214,6 @@
        void
        writeCommentData(const XalanDOMChar*    data);
   
  -     void
  -     flushWriter();
  -
  -     void
  -     openElementForChildren();
  -
  -     bool
  -     childNodesWereAdded();
  -
        /**
         * Check to see if a parent's ">" has been written, and, if 
         * it has not, write it.
  @@ -331,52 +250,6 @@
                        const XalanDOMChar*                     theString,
                        XalanDOMString::size_type       theStringLength);
   
  -     /**
  -      * Throw an exception when an invalid
  -      * surrogate is encountered.
  -      * @param ch The first character in the surrogate
  -      */
  -     static void
  -     throwInvalidUTF16SurrogateException(XalanDOMChar        ch);
  -
  -     /**
  -      * Throw an exception when an invalid
  -      * surrogate is encountered.
  -      * @param ch The first character in the surrogate
  -      * @param next The next character in the surrogate
  -      */
  -     static void
  -     throwInvalidUTF16SurrogateException(
  -                     XalanDOMChar    ch,
  -                     XalanDOMChar    next);
  -
  -     /**
  -      * If true, XML header should be written to output.
  -      */
  -     bool            m_shouldWriteXMLHeader;
  -
  -     /**
  -      * Flag to tell that we need to add the doctype decl, 
  -      * which we can't do until the first element is 
  -      * encountered.
  -      */
  -     bool            m_needToOutputDocTypeDecl;
  -  
  -     /**
  -      * Tell if the next text should be raw.
  -      */
  -     bool            m_nextIsRaw;
  -
  -     /**
  -      * The System ID for the doc type.
  -      */
  -     const XalanDOMString    m_doctypeSystem;
  -
  -     /**
  -      * The public ID for the doc type.
  -      */
  -     const XalanDOMString    m_doctypePublic;
  -
   private:
   
        // These are not implemented.
  @@ -442,30 +315,7 @@
                        const XalanDOMChar*                     theData,
                        XalanDOMString::size_type       theLength);
   
  -     void
  -     flushBuffer();
  -
        // Data members...
  -     /**
  -      * Add space before '/>' for XHTML.
  -      */
  -     bool            m_spaceBeforeClose;
  -
  -     /**
  -      * Tells the XML version, for writing out to the XML decl.
  -      */
  -     const XalanDOMString    m_version;
  -
  -     /**
  -      * Text for standalone part of header.
  -      */
  -     const XalanDOMString    m_standalone;
  -
  -     /**
  -      * The media type.  Not used right now.
  -      */
  -     const XalanDOMString    m_mediaType;
  -
   
        /**
         * The string "UTF-16".
  @@ -584,31 +434,9 @@
   
        static const XalanDOMString::size_type  s_linefeedNCRStringLength;
   
  -     /**
  -      * A stack of Boolean objects that tell if the given element 
  -      * has children.
  -      */
  -     BoolStackType   m_elemStack;
  -
  -     /**
  -      * The string of characters that represents the newline
  -      */
  -     const XalanDOMChar*             m_newlineString;
  -
  -     /**
  -      * The length of the the string of characters that represents the 
newline
  -      */
  -     XalanDOMString::size_type       m_newlineStringLength;
  -
  -
        enum
        {
  -                     kNotSpecial = 0,
  -                     kContentSpecial = 1,    // A flag to indicate a value 
in s_specialChars applies to content
  -                     kAttributeSpecial = 2,  // A flag to indicate a value 
in s_specialChars applies to attributes
  -                     kBothSpecial = 3,               // A flag t0 indicate a 
value in s_specialChars applies to both content and attributes
  -                     kSpecialsSize = 0x80,   // The size of s_specialChars
  -                     kBufferSize = 512               // The size of the 
buffer
  +             kBufferSize = 512               // The size of the buffer
        };
   
        static bool
  @@ -637,25 +465,12 @@
                }
        }
   
  -     static bool
  -     isUTF16HighSurrogate(XalanDOMChar       theChar)
  -     {
  -             return 0xD800u <= theChar && theChar <= 0xDBFFu ? true : false;
  -     }
  -
  -     static bool
  -     isUTF16LowSurrogate(XalanDOMChar        theChar)
  -     {
  -             return 0xDC00u <= theChar && theChar <= 0xDFFFu ? true : false;
  -     }
   
        XalanDOMChar                            m_buffer[kBufferSize];
   
        XalanDOMChar*                           m_bufferPosition;
   
  -     XalanDOMString::size_type                                       
m_bufferRemaining;
  -
  -     static const XalanDOMChar       s_specialChars[];
  +     XalanDOMString::size_type       m_bufferRemaining;
   };
   
   
  
  
  
  1.4       +140 -412  xml-xalan/c/src/xalanc/XMLSupport/FormatterToXML_UTF8.cpp
  
  Index: FormatterToXML_UTF8.cpp
  ===================================================================
  RCS file: 
/home/cvs/xml-xalan/c/src/xalanc/XMLSupport/FormatterToXML_UTF8.cpp,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- FormatterToXML_UTF8.cpp   17 Jul 2003 01:54:51 -0000      1.3
  +++ FormatterToXML_UTF8.cpp   31 Jul 2003 22:55:59 -0000      1.4
  @@ -62,14 +62,12 @@
   
   
   #include <xercesc/sax/AttributeList.hpp>
  -#include <xercesc/sax/SAXException.hpp>
   
   
   
   #include <xalanc/PlatformSupport/DOMStringHelper.hpp>
   #include <xalanc/PlatformSupport/DoubleSupport.hpp>
   #include <xalanc/PlatformSupport/Writer.hpp>
  -#include <xalanc/PlatformSupport/XalanOutputStream.hpp>
   #include <xalanc/PlatformSupport/XalanTranscodingServices.hpp>
   #include <xalanc/PlatformSupport/XalanUnicode.hpp>
   
  @@ -83,75 +81,6 @@
   
   
   
  -const XalanDOMChar   FormatterToXML_UTF8::s_specialChars[kSpecialsSize] =
  -{
  -     kNotSpecial,            // 0
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kBothSpecial,           // 0xA -- linefeed  Special because we 
normalize as requested.
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,            // 0x10
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,            // 0x20
  -     kNotSpecial,
  -     kAttributeSpecial,      // 0x22 '"'
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kBothSpecial,           // 0x26 -- '&'
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,            // 0x30
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kNotSpecial,
  -     kBothSpecial,           // 0x3C '<'
  -     kNotSpecial,
  -     kBothSpecial            // 0x3E '>'
  -};
  -
  -
  -
   FormatterToXML_UTF8::FormatterToXML_UTF8(
                        Writer&                                 writer,
                        const XalanDOMString&   version,
  @@ -160,53 +89,19 @@
                        const XalanDOMString&   doctypePublic,
                        bool                                    xmlDecl,
                        const XalanDOMString&   standalone) :
  -     FormatterListener(OUTPUT_METHOD_XML),
  -     m_writer(&writer),
  -     m_shouldWriteXMLHeader(xmlDecl),
  -     m_needToOutputDocTypeDecl(true),
  -     m_nextIsRaw(false),
  -     m_doctypeSystem(doctypeSystem),
  -     m_doctypePublic(doctypePublic),
  -     m_spaceBeforeClose(false),
  -     m_version(version),
  -     m_standalone(standalone),
  -     m_mediaType(mediaType),
  -     m_elemStack(),
  -     m_newlineString(0),
  -     m_newlineStringLength(0),
  +     FormatterToXMLBase(
  +             writer,
  +             version,
  +             mediaType,
  +             doctypeSystem,
  +             doctypePublic,
  +             xmlDecl,
  +             standalone),
        m_nameFunction(0),
        m_buffer(),
        m_bufferPosition(m_buffer),
        m_bufferRemaining(kBufferSize)
   {
  -     if(isEmpty(m_doctypePublic) == false)
  -     {
  -             if(startsWith(
  -                     m_doctypePublic,
  -                     s_xhtmlDocTypeString,
  -                     s_xhtmlDocTypeStringLength) == true)
  -             {
  -                     m_spaceBeforeClose = true;
  -             }
  -     }
  -
  -     const XalanOutputStream* const  theStream = writer.getStream();
  -
  -     if (theStream == 0)
  -     {
  -             m_newlineString = XalanOutputStream::defaultNewlineString();
  -     }
  -     else
  -     {
  -             m_newlineString = theStream->getNewlineString();
  -     }
  -
  -     assert(m_newlineString != 0);
  -
  -     m_newlineStringLength = length(m_newlineString);
  -
  -     assert(m_newlineString != 0);
  -
        if (m_version.empty() == true ||
                DoubleSupport::equal(DOMStringToDouble(m_version), 1.0) == true)
        {
  @@ -396,12 +291,7 @@
        }
        else
        {
  -             XALAN_USING_XERCES(SAXException)
  -
  -             const XalanDOMString    
theMessage(TranscodeFromLocalCodePage("Invalid character detected: ") +
  -                                                                             
   UnsignedLongToHexDOMString(theChar));
  -
  -             throw SAXException(c_wstr(theMessage));
  +             throwInvalidCharacterException(theChar);
        }
   }
   
  @@ -435,23 +325,6 @@
   
   
   
  -inline unsigned int
  -FormatterToXML_UTF8::decodeUTF16SurrogatePair(
  -                     XalanDOMChar    theHighSurrogate,
  -                     XalanDOMChar    theLowSurrogate)
  -{
  -     assert(isUTF16HighSurrogate(theHighSurrogate) == true);
  -
  -     if (isUTF16LowSurrogate(theLowSurrogate) == false)
  -     {
  -             throwInvalidUTF16SurrogateException(theHighSurrogate, 
theLowSurrogate);
  -     }
  -
  -     return ((theHighSurrogate - 0xD800u) << 10) + theLowSurrogate - 0xDC00u 
+ 0x00010000u;
  -}
  -
  -
  -
   void
   FormatterToXML_UTF8::writeName1_0(const XalanDOMChar*        theChars)
   {
  @@ -536,7 +409,7 @@
   
   
   void
  -FormatterToXML_UTF8::outputDocTypeDecl(const XalanDOMChar*   name)
  +FormatterToXML_UTF8::writeDoctypeDecl(const XalanDOMChar*    name)
   {
        // "<!DOCTYPE "
        write(s_doctypeHeaderStartString, s_doctypeHeaderStartStringLength);
  @@ -567,35 +440,6 @@
   
   
   
  -XALAN_USING_XERCES(SAXException)
  -
  -void
  -FormatterToXML_UTF8::throwInvalidUTF16SurrogateException(XalanDOMChar        
ch)
  -{
  -     const XalanDOMString    theMessage(TranscodeFromLocalCodePage("Invalid 
UTF-16 surrogate detected: ") +
  -                                                                        
UnsignedLongToHexDOMString(ch) +
  -                                                                        
TranscodeFromLocalCodePage(" ?"));
  -
  -     throw SAXException(c_wstr(theMessage));
  -}
  -
  -
  -
  -void
  -FormatterToXML_UTF8::throwInvalidUTF16SurrogateException(
  -                     XalanDOMChar    ch,
  -                     XalanDOMChar    next)
  -{
  -     const XalanDOMString    theMessage(TranscodeFromLocalCodePage("Invalid 
UTF-16 surrogate detected: ") +
  -                                                                        
UnsignedLongToHexDOMString(ch) +
  -                                                                        
UnsignedLongToHexDOMString(next) +
  -                                                                        
TranscodeFromLocalCodePage(" ?"));
  -
  -     throw SAXException(c_wstr(theMessage));
  -}
  -
  -
  -
   bool
   FormatterToXML_UTF8::writeDefaultEntity(XalanDOMChar ch)
   {
  @@ -684,78 +528,22 @@
   
   
   void
  -FormatterToXML_UTF8::flushWriter()
  -{
  -     m_writer->flush();
  -}
  -
  -
  -
  -void
  -FormatterToXML_UTF8::setDocumentLocator(const LocatorType* const     /* 
locator */)
  -{
  -}
  -
  -
  -
  -void
  -FormatterToXML_UTF8::startDocument()
  +FormatterToXML_UTF8::writeParentTagEnd()
   {
  -     m_needToOutputDocTypeDecl = true;
  -
  -     if(m_shouldWriteXMLHeader == true)
  +     if(markParentForChildren() == true)
        {
  -             // "<?xml version=\""
  -             write(s_xmlHeaderStartString, s_xmlHeaderStartStringLength);
  -
  -             if (length(m_version) != 0)
  -             {
  -                     write(m_version);
  -             }
  -             else
  -             {
  -                     write(s_defaultVersionString, 
s_defaultVersionStringLength);
  -             }
  -
  -             // "\" encoding=\""
  -             write(s_xmlHeaderEncodingString, 
s_xmlHeaderEncodingStringLength);
  -
  -             write(s_utf8String);
  -
  -             if (length(m_standalone) != 0)
  -             {
  -                     write(s_xmlHeaderStandaloneString, 
s_xmlHeaderStandaloneStringLength);
  -                     write(m_standalone);
  -             }
  -
  -             write(s_xmlHeaderEndString, s_xmlHeaderEndStringLength);
  -
  -             outputLineSep();
  +             write(char(XalanUnicode::charGreaterThanSign));
        }
   }
   
   
   
   void
  -FormatterToXML_UTF8::endDocument()
  -{
  -     flushBuffer();
  -}
  -
  -
  -
  -void
   FormatterToXML_UTF8::startElement(
                        const XMLCh* const      name,
                        AttributeListType&      attrs)
   {
  -     if(true == m_needToOutputDocTypeDecl &&
  -        isEmpty(m_doctypeSystem) == false)
  -     {
  -             outputDocTypeDecl(name);
  -
  -             m_needToOutputDocTypeDecl = false;
  -     }
  +     generateDoctypeDecl(name);
   
        writeParentTagEnd();
   
  @@ -784,6 +572,7 @@
        {
                write(char(XalanUnicode::charLessThanSign));
                write(char(XalanUnicode::charSolidus));
  +
                writeName(name);
        }
        else
  @@ -802,91 +591,73 @@
   
   
   void
  -FormatterToXML_UTF8::processingInstruction(
  -                     const XMLCh* const      target,
  -                     const XMLCh* const      data)
  +FormatterToXML_UTF8::writeProcessingInstruction(
  +                     const XMLCh*    target,
  +                     const XMLCh*    data)
   {
  -     // Use a fairly nasty hack to tell if the next node is supposed to be 
  -     // unescaped text.
  -     if(equals(target, length(target), s_piTarget, s_piTargetLength) == true 
&&
  -        equals(data, length(data), s_piData, s_piDataLength) == true)
  -     {
  -             m_nextIsRaw = true;
  -     }
  -     else    
  -     {
  -             writeParentTagEnd();
  +     writeParentTagEnd();
   
  -             write(char(XalanUnicode::charLessThanSign));
  -             write(char(XalanUnicode::charQuestionMark));
  -             writeName(target);
  +     write(char(XalanUnicode::charLessThanSign));
  +     write(char(XalanUnicode::charQuestionMark));
  +     writeName(target);
   
  -             const XalanDOMString::size_type         len = length(data);
  +     const XalanDOMString::size_type         len = length(data);
   
  -             if ( len > 0 && !isXMLWhitespace(data[0]))
  -             {
  -                     write(char(XalanUnicode::charSpace));
  -             }
  +     // We need to make sure there is a least one whitespace character
  +     // between the target and the data.
  +     if ( len > 0 && !isXMLWhitespace(data[0]))
  +     {
  +             write(char(XalanUnicode::charSpace));
  +     }
   
  -             writeNormalizedPIData(data, len);
  +     writeNormalizedPIData(data, len);
   
  -             write(char(XalanUnicode::charQuestionMark));
  -             write(char(XalanUnicode::charGreaterThanSign));
  +     write(char(XalanUnicode::charQuestionMark));
  +     write(char(XalanUnicode::charGreaterThanSign));
   
  -             // If outside of an element, then put in a new line.  This 
whitespace
  -             // is not significant.
  -             if (m_elemStack.empty() == true)
  -             {
  -                     outputLineSep();
  -             }
  +     // If outside of an element, then put in a new line.  This whitespace
  +     // is not significant.
  +     if (outsideDocumentElement() == true)
  +     {
  +             outputLineSep();
        }
   }
   
   
   
   void
  -FormatterToXML_UTF8::characters(
  -                     const XMLCh* const      chars,
  -                     const unsigned int      length)
  +FormatterToXML_UTF8::writeCharacters(
  +                     const XMLCh*    chars,
  +                     unsigned int    length)
   {
  -     if(length != 0)
  +     assert(length != 0);
  +
  +     writeParentTagEnd();
  +
  +     unsigned int    i = 0;
  +     unsigned int    firstIndex = 0;
  +
  +     while(i < length) 
        {
  -             if(m_nextIsRaw)
  -             {
  -                     m_nextIsRaw = false;
  +             const XalanDOMChar      ch = chars[i];
   
  -                     charactersRaw(chars, length);
  +             if (isContentSpecial(ch) == false)
  +             {
  +                     ++i;
                }
                else
                {
  -                     writeParentTagEnd();
  -
  -                     unsigned int    i = 0;
  -                     unsigned int    firstIndex = 0;
  -
  -                     while(i < length) 
  -                     {
  -                             const XalanDOMChar      ch = chars[i];
  -
  -                             if (isContentSpecial(ch) == false)
  -                             {
  -                                     ++i;
  -                             }
  -                             else
  -                             {
  -                                     safeWriteContent(chars + firstIndex, i 
- firstIndex);
  -
  -                                     i = writeDefaultEscape(chars[i], i, 
chars, length);
  +                     safeWriteContent(chars + firstIndex, i - firstIndex);
   
  -                                     ++i;
  +                     i = writeDefaultEscape(chars[i], i, chars, length);
   
  -                                     firstIndex = i;
  -                             }
  -                     }
  +                     ++i;
   
  -                     safeWriteContent(chars + firstIndex, i - firstIndex);
  +                     firstIndex = i;
                }
        }
  +
  +     safeWriteContent(chars + firstIndex, i - firstIndex);
   }
   
   
  @@ -903,30 +674,6 @@
   
   
   
  -Writer*
  -FormatterToXML_UTF8::getWriter() const
  -{
  -     return m_writer;
  -}
  -
  -
  -
  -const XalanDOMString&
  -FormatterToXML_UTF8::getDoctypeSystem() const
  -{
  -     return m_doctypeSystem;
  -}
  -
  -
  -
  -const XalanDOMString&
  -FormatterToXML_UTF8::getDoctypePublic() const
  -{
  -     return m_doctypePublic;
  -}
  -
  -
  -
   const XalanDOMString&
   FormatterToXML_UTF8::getEncoding() const
   {
  @@ -935,14 +682,6 @@
   
   
   
  -const XalanDOMString&
  -FormatterToXML_UTF8::getMediaType() const
  -{
  -     return m_mediaType;
  -}
  -
  -
  -
   void
   FormatterToXML_UTF8::writeAttrString(
                        const XalanDOMChar*                     theString,
  @@ -1046,34 +785,63 @@
   {
        XalanDOMString::size_type i = 0;
   
  +     // enum for a cheezy little state machine.
  +     enum eState { eInitialState, eFirstRightSquareBracket, 
eSecondRightSquareBracket };
  +
  +     eState  theCurrentState = eInitialState;
  +
        while(i < length)
       {
                // If "]]>", which would close the CDATA appears in
                // the content, we have to put the first two characters
                // in the CDATA section, close the CDATA section, then
                // open a new one and add the last character.
  -             if (i < length - 2 &&
  -                     XalanUnicode::charRightSquareBracket == ch[i] &&
  -            XalanUnicode::charRightSquareBracket == ch[i + 1] &&
  -                     XalanUnicode::charGreaterThanSign == ch[ i + 2])
  +
  +             const XalanDOMChar      theChar = ch[i];
  +
  +             if (theChar == XalanUnicode::charRightSquareBracket)
                {
  -                     // "]]]]><![CDATA[>"
  -                     write(char(XalanUnicode::charRightSquareBracket));
  +                     if (theCurrentState == eInitialState)
  +                     {
  +                             theCurrentState = eFirstRightSquareBracket;
  +                     }
  +                     else if (theCurrentState == eFirstRightSquareBracket)
  +                     {
  +                             theCurrentState = eSecondRightSquareBracket;
  +                     }
  +
                        write(char(XalanUnicode::charRightSquareBracket));
  +             }
  +             else if (theChar == XalanUnicode::charGreaterThanSign)
  +             {
  +                     if (theCurrentState != eInitialState)
  +                     {
  +                             if (theCurrentState == eFirstRightSquareBracket)
  +                             {
  +                                     theCurrentState = eInitialState;
  +                             }
  +                             else
  +                             {
  +                                     theCurrentState = eInitialState;
   
  -                     write(s_cdataCloseString, s_cdataCloseStringLength);
  -                     write(s_cdataOpenString, s_cdataOpenStringLength);
  +                                     write(s_cdataCloseString, 
s_cdataCloseStringLength);
  +                                     write(s_cdataOpenString, 
s_cdataOpenStringLength);
  +                             }
  +                     }
   
                        write(char(XalanUnicode::charGreaterThanSign));
  -
  -                     i += 3;
                }
                else
                {
  -                     i = writeNormalizedChar(ch[i], ch, i, length);
  +                     if (theCurrentState != eInitialState)
  +                     {
  +                             theCurrentState = eInitialState;
  +                     }
   
  -                     ++i;
  +                     i = writeNormalizedChar(theChar, ch, i, length);
                }
  +
  +             ++i;
       }
   }
   
  @@ -1092,27 +860,6 @@
   
   
   void
  -FormatterToXML_UTF8::ignorableWhitespace(
  -                     const XMLCh* const      chars,
  -                     const unsigned int      length)
  -{
  -     if (length > 0)
  -     {
  -             characters(chars, length);
  -     }
  -}
  -
  -
  -
  -void
  -FormatterToXML_UTF8::resetDocument()
  -{
  -     // I don't do anything with this yet.
  -}
  -
  -
  -
  -void
   FormatterToXML_UTF8::comment(const XMLCh* const      data)
   {
        writeParentTagEnd();
  @@ -1132,71 +879,19 @@
   
   
   void
  -FormatterToXML_UTF8::cdata(
  -                     const XMLCh* const      ch,
  -                     const unsigned int      length)
  +FormatterToXML_UTF8::writeCDATA(
  +                     const XMLCh*    ch,
  +                     unsigned int    length)
   {
  -     if(m_nextIsRaw == true)
  -     {
  -             m_nextIsRaw = false;
  -
  -             charactersRaw(ch, length);
  -     }
  -     else
  -     {
  -             writeParentTagEnd();
  -
  -             if(length > 0)
  -             {
  -                     write(s_cdataOpenString, s_cdataOpenStringLength);
  -
  -                     writeCDATAChars(ch, length);
  -
  -                     write(s_cdataCloseString, s_cdataCloseStringLength);
  -             }
  -     }
  -}
  -
  -
  -
  -void
  -FormatterToXML_UTF8::writeParentTagEnd()
  -{
  -     if(!m_elemStack.empty())
  -     {
  -             // See if the parent element has already been flagged as having 
children.
  -             if(false == m_elemStack.back())
  -             {
  -                     write(char(XalanUnicode::charGreaterThanSign));
  -
  -                     m_elemStack.back() = true;
  -             }
  -     }
  -}
  -
  -
  -
  -void
  -FormatterToXML_UTF8::openElementForChildren()
  -{
  -     m_elemStack.push_back(false);
  -}
  -
  +     assert(length != 0);
   
  +     writeParentTagEnd();
   
  -bool
  -FormatterToXML_UTF8::childNodesWereAdded()
  -{
  -     bool    fResult = false;
  +     write(s_cdataOpenString, s_cdataOpenStringLength);
   
  -     if (m_elemStack.empty() == false)
  -     {
  -             fResult = m_elemStack.back();
  +     writeCDATAChars(ch, length);
   
  -             m_elemStack.pop_back();
  -     }
  -
  -     return fResult;
  +     write(s_cdataCloseString, s_cdataCloseStringLength);
   }
   
   
  @@ -1220,6 +915,39 @@
                writeAttrString(value, length(value));
                write(char(XalanUnicode::charQuoteMark));
        }
  +}
  +
  +
  +
  +void
  +FormatterToXML_UTF8::writeXMLHeader()
  +{
  +             // "<?xml version=\""
  +     write(s_xmlHeaderStartString, s_xmlHeaderStartStringLength);
  +
  +     if (length(m_version) != 0)
  +     {
  +             write(m_version);
  +     }
  +     else
  +     {
  +             write(s_defaultVersionString, s_defaultVersionStringLength);
  +     }
  +
  +     // "\" encoding=\""
  +     write(s_xmlHeaderEncodingString, s_xmlHeaderEncodingStringLength);
  +
  +     write(s_utf8String);
  +
  +     if (length(m_standalone) != 0)
  +     {
  +             write(s_xmlHeaderStandaloneString, 
s_xmlHeaderStandaloneStringLength);
  +             write(m_standalone);
  +     }
  +
  +     write(s_xmlHeaderEndString, s_xmlHeaderEndStringLength);
  +
  +     outputLineSep();
   }
   
   
  
  
  
  1.5       +22 -220   xml-xalan/c/src/xalanc/XMLSupport/FormatterToXML_UTF8.hpp
  
  Index: FormatterToXML_UTF8.hpp
  ===================================================================
  RCS file: 
/home/cvs/xml-xalan/c/src/xalanc/XMLSupport/FormatterToXML_UTF8.hpp,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- FormatterToXML_UTF8.hpp   17 Jul 2003 01:54:51 -0000      1.4
  +++ FormatterToXML_UTF8.hpp   31 Jul 2003 22:55:59 -0000      1.5
  @@ -65,16 +65,12 @@
   
   
   
  -#include <vector>
  -
  -
  -
   #include <xalanc/XalanDOM/XalanDOMString.hpp>
   
   
   
   // Base class header file.
  -#include <xalanc/PlatformSupport/FormatterListener.hpp>
  +#include "FormatterToXMLBase.hpp"
   
   
   
  @@ -82,14 +78,10 @@
   
   
   
  -class Writer;
  -
  -
  -
   /**
    * FormatterToXML_UTF8 formats SAX-style events into XML.
    */
  -class XALAN_XMLSUPPORT_EXPORT FormatterToXML_UTF8 : public FormatterListener 
  +class XALAN_XMLSUPPORT_EXPORT FormatterToXML_UTF8 : public FormatterToXMLBase
   {
   public:
   
  @@ -136,15 +128,6 @@
        // These methods are inherited from FormatterListener ...
   
        virtual void
  -     setDocumentLocator(const LocatorType* const             locator);
  -
  -     virtual void
  -     startDocument();
  -
  -     virtual void
  -     endDocument();
  -
  -     virtual void
        startElement(
                        const XMLCh* const      name,
                        AttributeListType&      attrs);
  @@ -153,11 +136,6 @@
        endElement(const XMLCh* const   name);
   
       virtual void
  -     characters(
  -                     const XMLCh* const      chars,
  -                     const unsigned int      length);
  -
  -    virtual void
        charactersRaw(
                        const XMLCh* const      chars,
                        const unsigned int      length);
  @@ -166,86 +144,36 @@
        entityReference(const XMLCh* const      name);
   
        virtual void
  -     ignorableWhitespace(
  -                     const XMLCh* const      chars,
  -                     const unsigned int      length);
  -
  -     virtual void
  -     processingInstruction(
  -                     const XMLCh* const      target,
  -                     const XMLCh* const      data);
  -
  -
  -    virtual void
  -     resetDocument();
  -
  -     virtual void
        comment(const XMLCh* const      data);
   
  -     virtual void
  -     cdata(
  -                     const XMLCh* const      ch,
  -                     const unsigned int      length);
  -
  -     virtual Writer*
  -     getWriter() const;
  -
  -     virtual const XalanDOMString&
  -     getDoctypeSystem() const;
  -
  -     virtual const XalanDOMString&
  -     getDoctypePublic() const;
  -
        virtual const XalanDOMString&
        getEncoding() const;
   
  -     virtual const XalanDOMString&
  -     getMediaType() const;
  -
  -     const XalanDOMString&
  -     getVersion() const
  -     {
  -             return m_version;
  -     }
  +protected:
   
  -     const XalanDOMString&
  -     getStandalone() const
  -     {
  -             return m_standalone;
  -     }
  +     virtual void
  +     writeXMLHeader();
   
  -     bool
  -     getShouldWriteXMLHeader() const
  -     {
  -             return m_shouldWriteXMLHeader;
  -     }
  +     virtual void
  +     flushBuffer();
   
  -     void
  -     setShouldWriteXMLHeader(bool    b)
  -     {
  -             m_shouldWriteXMLHeader = b;
  -     }
  +     virtual void
  +     writeDoctypeDecl(const XalanDOMChar*    name);
   
  -#if defined(XALAN_NO_STD_NAMESPACE)
  -#if defined(XALAN_USE_DEQUE_FOR_VECTOR_BOOL)
  -     typedef deque<bool>                     BoolStackType;
  -#else
  -     typedef vector<bool>            BoolStackType;
  -#endif
  -#else
  -#if defined(XALAN_USE_DEQUE_FOR_VECTOR_BOOL)
  -     typedef std::deque<bool>        BoolStackType;
  -#else
  -     typedef std::vector<bool>       BoolStackType;
  -#endif
  -#endif
  +     virtual void
  +     writeProcessingInstruction(
  +                     const XMLCh*    target,
  +                     const XMLCh*    data);
   
  -protected:
  +     virtual void
  +     writeCharacters(
  +                     const XMLCh*    chars,
  +                     unsigned int    length);
   
  -     /** 
  -      * The writer.
  -      */
  -     Writer*                                 m_writer;
  +     virtual void
  +     writeCDATA(
  +                     const XMLCh*    chars,
  +                     unsigned int    length);
   
        /**
         * Output a line break.
  @@ -294,15 +222,6 @@
        void
        writeCommentData(const XalanDOMChar*    data);
   
  -     void
  -     flushWriter();
  -
  -     void
  -     openElementForChildren();
  -
  -     bool
  -     childNodesWereAdded();
  -
        /**
         * Check to see if a parent's ">" has been written, and, if 
         * it has not, write it.
  @@ -345,52 +264,6 @@
                        const XalanDOMChar*                     theString,
                        XalanDOMString::size_type       theStringLength);
   
  -     /**
  -      * Throw an exception when an invalid
  -      * surrogate is encountered.
  -      * @param ch The first character in the surrogate
  -      */
  -     static void
  -     throwInvalidUTF16SurrogateException(XalanDOMChar        ch);
  -
  -     /**
  -      * Throw an exception when an invalid
  -      * surrogate is encountered.
  -      * @param ch The first character in the surrogate
  -      * @param next The next character in the surrogate
  -      */
  -     static void
  -     throwInvalidUTF16SurrogateException(
  -                     XalanDOMChar    ch,
  -                     XalanDOMChar    next);
  -
  -     /**
  -      * If true, XML header should be written to output.
  -      */
  -     bool            m_shouldWriteXMLHeader;
  -
  -     /**
  -      * Flag to tell that we need to add the doctype decl, 
  -      * which we can't do until the first element is 
  -      * encountered.
  -      */
  -     bool            m_needToOutputDocTypeDecl;
  -  
  -     /**
  -      * Tell if the next text should be raw.
  -      */
  -     bool            m_nextIsRaw;
  -
  -     /**
  -      * The System ID for the doc type.
  -      */
  -     const XalanDOMString    m_doctypeSystem;
  -
  -     /**
  -      * The public ID for the doc type.
  -      */
  -     const XalanDOMString    m_doctypePublic;
  -
   private:
   
        // These are not implemented.
  @@ -454,14 +327,6 @@
        }
   
        /**
  -      * Output the doc type declaration.
  -      *
  -      * @param name the name of the doctype.
  -      */
  -     void
  -     outputDocTypeDecl(const XalanDOMChar*   name);
  -
  -     /**
         * Process an attribute.
         * @param name The name of the attribute.
         * @param value The value of the attribute.
  @@ -481,31 +346,8 @@
                        const XalanDOMChar*                     theData,
                        XalanDOMString::size_type       theLength);
   
  -     void
  -     flushBuffer();
  -
        // Data members...
        /**
  -      * Add space before '/>' for XHTML.
  -      */
  -     bool            m_spaceBeforeClose;
  -
  -     /**
  -      * Tells the XML version, for writing out to the XML decl.
  -      */
  -     const XalanDOMString    m_version;
  -
  -     /**
  -      * Text for standalone part of header.
  -      */
  -     const XalanDOMString    m_standalone;
  -
  -     /**
  -      * The media type.  Not used right now.
  -      */
  -     const XalanDOMString    m_mediaType;
  -
  -     /**
         * The string "UTF-8".
         */
        static const XalanDOMString&    s_utf8String;
  @@ -622,22 +464,6 @@
   
        static const XalanDOMString::size_type  s_linefeedNCRStringLength;
   
  -     /**
  -      * A stack of Boolean objects that tell if the given element 
  -      * has children.
  -      */
  -     BoolStackType   m_elemStack;
  -
  -     /**
  -      * The string of characters that represents the newline
  -      */
  -     const XalanDOMChar*             m_newlineString;
  -
  -     /**
  -      * The length of the the string of characters that represents the 
newline
  -      */
  -     XalanDOMString::size_type       m_newlineStringLength;
  -
        typedef void (FormatterToXML_UTF8::*NameFunctionType)(const 
XalanDOMChar*);
   
        NameFunctionType        m_nameFunction;
  @@ -652,12 +478,7 @@
   
        enum
        {
  -                     kNotSpecial = 0,
  -                     kContentSpecial = 1,    // A flag to indicate a value 
in s_specialChars applies to content
  -                     kAttributeSpecial = 2,  // A flag to indicate a value 
in s_specialChars applies to attributes
  -                     kBothSpecial = 3,               // A flag t0 indicate a 
value in s_specialChars applies to both content and attributes
  -                     kSpecialsSize = 0x80,   // The size of s_specialChars
  -                     kBufferSize = 512               // The size of the 
buffer
  +             kBufferSize = 512               // The size of the buffer
        };
   
        static bool
  @@ -686,30 +507,11 @@
                }
        }
   
  -     static bool
  -     isUTF16HighSurrogate(XalanDOMChar       theChar)
  -     {
  -             return 0xD800u <= theChar && theChar <= 0xDBFFu ? true : false;
  -     }
  -
  -     static bool
  -     isUTF16LowSurrogate(XalanDOMChar        theChar)
  -     {
  -             return 0xDC00u <= theChar && theChar <= 0xDFFFu ? true : false;
  -     }
  -
  -     static unsigned int
  -     decodeUTF16SurrogatePair(
  -                     XalanDOMChar    theHighSurrogate,
  -                     XalanDOMChar    theLowSurrogate);
  -
        char                                            m_buffer[kBufferSize];
   
        char*                                           m_bufferPosition;
   
        XalanDOMString::size_type       m_bufferRemaining;
  -
  -     static const XalanDOMChar       s_specialChars[];
   };
   
   
  
  
  
  1.1                  xml-xalan/c/src/xalanc/XMLSupport/FormatterToXMLBase.cpp
  
  Index: FormatterToXMLBase.cpp
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   *
   * Copyright (c) 1999-2003 The Apache Software Foundation.  All rights 
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *      notice, this list of conditions and the following disclaimer. 
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *      notice, this list of conditions and the following disclaimer in
   *      the documentation and/or other materials provided with the
   *      distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *      if any, must include the following acknowledgment:  
   *             "This product includes software developed by the
   *              Apache Software Foundation (http://www.apache.org/)."
   *      Alternately, this acknowledgment may appear in the software itself,
   *      if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Xalan" and "Apache Software Foundation" must
   *      not be used to endorse or promote products derived from this
   *      software without prior written permission. For written 
   *      permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache",
   *      nor may "Apache" appear in their name, without prior written
   *      permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.        IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation and was
   * originally based on software copyright (c) 1999, International
   * Business Machines, Inc., http://www.ibm.com.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  
  // Class header file.
  #include "FormatterToXMLBase.hpp"
  
  
  
  #include <xercesc/sax/SAXException.hpp>
  
  
  
  #include <xalanc/PlatformSupport/DOMStringHelper.hpp>
  #include <xalanc/PlatformSupport/Writer.hpp>
  #include <xalanc/PlatformSupport/XalanOutputStream.hpp>
  
  
  
  XALAN_CPP_NAMESPACE_BEGIN
  
  
  
  const XalanDOMChar    FormatterToXMLBase::s_specialChars[kSpecialsSize] =
  {
        kNotSpecial,            // 0
        kNotSpecial,
        kNotSpecial,
        kNotSpecial,
        kNotSpecial,
        kNotSpecial,
        kNotSpecial,
        kNotSpecial,
        kNotSpecial,
        kNotSpecial,
        kBothSpecial,           // 0xA -- linefeed  Special because we 
normalize as requested.
        kNotSpecial,
        kNotSpecial,
        kNotSpecial,
        kNotSpecial,
        kNotSpecial,
        kNotSpecial,            // 0x10
        kNotSpecial,
        kNotSpecial,
        kNotSpecial,
        kNotSpecial,
        kNotSpecial,
        kNotSpecial,
        kNotSpecial,
        kNotSpecial,
        kNotSpecial,
        kNotSpecial,
        kNotSpecial,
        kNotSpecial,
        kNotSpecial,
        kNotSpecial,
        kNotSpecial,
        kNotSpecial,            // 0x20
        kNotSpecial,
        kAttributeSpecial,      // 0x22 '"'
        kNotSpecial,
        kNotSpecial,
        kNotSpecial,
        kBothSpecial,           // 0x26 -- '&'
        kNotSpecial,
        kNotSpecial,
        kNotSpecial,
        kNotSpecial,
        kNotSpecial,
        kNotSpecial,
        kNotSpecial,
        kNotSpecial,
        kNotSpecial,
        kNotSpecial,            // 0x30
        kNotSpecial,
        kNotSpecial,
        kNotSpecial,
        kNotSpecial,
        kNotSpecial,
        kNotSpecial,
        kNotSpecial,
        kNotSpecial,
        kNotSpecial,
        kNotSpecial,
        kNotSpecial,
        kBothSpecial,           // 0x3C '<'
        kNotSpecial,
        kBothSpecial            // 0x3E '>'
  };
  
  
  
  FormatterToXMLBase::FormatterToXMLBase(
                        Writer&                                 writer,
                        const XalanDOMString&   version,
                        const XalanDOMString&   mediaType,
                        const XalanDOMString&   doctypeSystem,
                        const XalanDOMString&   doctypePublic,
                        bool                                    xmlDecl,
                        const XalanDOMString&   standalone) :
        FormatterListener(OUTPUT_METHOD_XML),
        m_writer(&writer),
        m_nextIsRaw(false),
        m_doctypeSystem(doctypeSystem),
        m_doctypePublic(doctypePublic),
        m_spaceBeforeClose(false),
        m_version(version),
        m_standalone(standalone),
        m_mediaType(mediaType),
        m_newlineString(0),
        m_newlineStringLength(0),
        m_needToOutputDoctypeDecl(false),
        m_shouldWriteXMLHeader(xmlDecl),
        m_elemStack()
  {
        if(isEmpty(m_doctypePublic) == false)
        {
                if(startsWith(
                        m_doctypePublic,
                        s_xhtmlDocTypeString) == true)
                {
                        m_spaceBeforeClose = true;
                }
        }
  
        const XalanOutputStream* const  theStream = writer.getStream();
  
        if (theStream == 0)
        {
                m_newlineString = XalanOutputStream::defaultNewlineString();
        }
        else
        {
                m_newlineString = theStream->getNewlineString();
        }
  
        assert(m_newlineString != 0);
  
        m_newlineStringLength = length(m_newlineString);
  
        assert(m_newlineString != 0);
  }
  
  
  
  FormatterToXMLBase::~FormatterToXMLBase()
  {
  }
  
  
  
  inline unsigned int
  FormatterToXMLBase::decodeUTF16SurrogatePair(
                        XalanDOMChar    theHighSurrogate,
                        XalanDOMChar    theLowSurrogate)
  {
        assert(isUTF16HighSurrogate(theHighSurrogate) == true);
  
        if (isUTF16LowSurrogate(theLowSurrogate) == false)
        {
                throwInvalidUTF16SurrogateException(theHighSurrogate, 
theLowSurrogate);
        }
  
        return ((theHighSurrogate - 0xD800u) << 10) + theLowSurrogate - 0xDC00u 
+ 0x00010000u;
  }
  
  
  
  XALAN_USING_XERCES(SAXException)
  
  void
  FormatterToXMLBase::throwInvalidUTF16SurrogateException(XalanDOMChar  ch)
  {
        const XalanDOMString    theMessage(TranscodeFromLocalCodePage("Invalid 
UTF-16 surrogate detected: ") +
                                                                           
UnsignedLongToHexDOMString(ch) +
                                                                           
TranscodeFromLocalCodePage(" ?"));
  
        throw SAXException(c_wstr(theMessage));
  }
  
  
  
  void
  FormatterToXMLBase::throwInvalidUTF16SurrogateException(
                        XalanDOMChar    ch,
                        XalanDOMChar    next)
  {
        const XalanDOMString    theMessage(TranscodeFromLocalCodePage("Invalid 
UTF-16 surrogate detected: ") +
                                                                           
UnsignedLongToHexDOMString(ch) +
                                                                           
UnsignedLongToHexDOMString(next) +
                                                                           
TranscodeFromLocalCodePage(" ?"));
  
        throw SAXException(c_wstr(theMessage));
  }
  
  
  
  void
  FormatterToXMLBase::throwInvalidCharacterException(unsigned int               
ch)
  {
        const XalanDOMString    theMessage(TranscodeFromLocalCodePage("Invalid 
character detected: ") +
                                                                           
UnsignedLongToHexDOMString(ch));
  
        throw SAXException(c_wstr(theMessage));
  }
  
  
  
  void
  FormatterToXMLBase::flushWriter()
  {
        m_writer->flush();
  }
  
  
  
  void
  FormatterToXMLBase::setDocumentLocator(const LocatorType* const       /* 
locator */)
  {
  }
  
  
  
  void
  FormatterToXMLBase::startDocument()
  {
        if (m_doctypeSystem.empty() == false)
        {
                m_needToOutputDoctypeDecl = true;
        }
  
        if(m_shouldWriteXMLHeader == true)
        {
                writeXMLHeader();
        }
  }
  
  
  
  void
  FormatterToXMLBase::endDocument()
  {
        m_needToOutputDoctypeDecl = false;
  
        flushBuffer();
  }
  
  
  
  void
  FormatterToXMLBase::characters(
                        const XMLCh* const      chars,
                        const unsigned int      length)
  {
        if(length != 0)
        {
                if(m_nextIsRaw)
                {
                        m_nextIsRaw = false;
  
                        charactersRaw(chars, length);
                }
                else
                {
                        writeCharacters(chars, length);
                }
        }
  }
  
  
  
  void
  FormatterToXMLBase::cdata(
                        const XMLCh* const      ch,
                        const unsigned int      length)
  {
        if (length != 0)
        {
                if(m_nextIsRaw == true)
                {
                        m_nextIsRaw = false;
  
                        charactersRaw(ch, length);
                }
                else
                {
                        writeCDATA(ch, length);
                }
        }
  }
  
  
  
  void
  FormatterToXMLBase::processingInstruction(
                        const XMLCh* const      target,
                        const XMLCh* const      data)
  {
        // Use a fairly nasty hack to tell if the next node is supposed to be 
        // unescaped text.
        if(equals(target, length(target), s_piTarget, s_piTargetLength) == true 
&&
           equals(data, length(data), s_piData, s_piDataLength) == true)
        {
                m_nextIsRaw = true;
        }
        else    
        {
                writeProcessingInstruction(target, data);
        }
  }
  
  
  
  void
  FormatterToXMLBase::ignorableWhitespace(
                        const XMLCh* const      chars,
                        const unsigned int      length)
  {
        if (length > 0)
        {
                characters(chars, length);
        }
  }
  
  
  
  Writer*
  FormatterToXMLBase::getWriter() const
  {
        return m_writer;
  }
  
  
  
  const XalanDOMString&
  FormatterToXMLBase::getDoctypeSystem() const
  {
        return m_doctypeSystem;
  }
  
  
  
  const XalanDOMString&
  FormatterToXMLBase::getDoctypePublic() const
  {
        return m_doctypePublic;
  }
  
  
  
  const XalanDOMString&
  FormatterToXMLBase::getMediaType() const
  {
        return m_mediaType;
  }
  
  
  
  void
  FormatterToXMLBase::resetDocument()
  {
        // I don't do anything with this yet.
  }
  
  
  
  #define FXML_SIZE(str)        ((sizeof(str) / sizeof(str[0]) - 1))
  
  const XalanDOMChar    FormatterToXMLBase::s_xhtmlDocTypeString[] =
  {
        XalanUnicode::charHyphenMinus,
        XalanUnicode::charSolidus,
        XalanUnicode::charSolidus,
        XalanUnicode::charLetter_W,
        XalanUnicode::charDigit_3,
        XalanUnicode::charLetter_C,
        XalanUnicode::charSolidus,
        XalanUnicode::charSolidus,
        XalanUnicode::charLetter_D,
        XalanUnicode::charLetter_T,
        XalanUnicode::charLetter_D,
        XalanUnicode::charSpace,
        XalanUnicode::charLetter_X,
        XalanUnicode::charLetter_H,
        XalanUnicode::charLetter_T,
        XalanUnicode::charLetter_M,
        XalanUnicode::charLetter_L,
        XalanDOMChar(0)
  };
  
  const XalanDOMString::size_type               
FormatterToXMLBase::s_xhtmlDocTypeStringLength =
                FXML_SIZE(s_xhtmlDocTypeString);
  
  
  XALAN_CPP_NAMESPACE_END
  
  
  
  1.1                  xml-xalan/c/src/xalanc/XMLSupport/FormatterToXMLBase.hpp
  
  Index: FormatterToXMLBase.hpp
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   *
   * Copyright (c) 1999-2003 The Apache Software Foundation.  All rights 
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer. 
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:  
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Xalan" and "Apache Software Foundation" must
   *    not be used to endorse or promote products derived from this
   *    software without prior written permission. For written 
   *    permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache",
   *    nor may "Apache" appear in their name, without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation and was
   * originally based on software copyright (c) 1999, International
   * Business Machines, Inc., http://www.ibm.com.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  #if !defined(FORMATTERTOXMLBASE_HEADER_GUARD_1357924680)
  #define FORMATTERTOXMLBASE_HEADER_GUARD_1357924680
  
  
  
  
  // Base include file.  Must be first.
  #include <xalanc/XMLSupport/XMLSupportDefinitions.hpp>
  
  
  
  #include <vector>
  
  
  
  #include <xalanc/XalanDOM/XalanDOMString.hpp>
  
  
  
  // Base class header file.
  #include <xalanc/PlatformSupport/FormatterListener.hpp>
  
  
  
  XALAN_CPP_NAMESPACE_BEGIN
  
  
  
  class Writer;
  
  
  
  /**
   * FormatterToXML_UTF8 formats SAX-style events into XML.
   */
  class XALAN_XMLSUPPORT_EXPORT FormatterToXMLBase : public FormatterListener 
  {
  public:
  
        /**
         * Constructor
         *
         * @param writer            the writer.
         * @param version           the string to write for the XML version 
number.
         * @param mediaType         media type (MIME content type) of the data
         * @param doctypeSystem     system identifier to be used in the document
         *                          type declaration
         * @param doctypePublic     public identifier to be used in the document
         *                          type declaration
         * @param xmlDecl           true if the XSLT processor should output an 
XML
         *                          declaration
         * @param standalone        The string the XSLT processor should output 
for
         *                          the standalone document declaration
         */
        FormatterToXMLBase(
                        Writer&                                 writer,
                        const XalanDOMString&   version,
                        const XalanDOMString&   mediaType,
                        const XalanDOMString&   doctypeSystem,
                        const XalanDOMString&   doctypePublic,
                        bool                                    xmlDecl,
                        const XalanDOMString&   standalone);
  
        virtual
        ~FormatterToXMLBase();
  
        // These methods are inherited from FormatterListener ...
  
        virtual void
        setDocumentLocator(const LocatorType* const     locator);
  
        virtual void
        startDocument();
  
        virtual void
        endDocument();
  
        virtual void
        startElement(
                        const XMLCh* const      name,
                        AttributeListType&      attrs) = 0;
  
      virtual void
        endElement(const XMLCh* const   name) = 0;
  
      virtual void
        characters(
                        const XMLCh* const      chars,
                        const unsigned int      length);
  
      virtual void
        charactersRaw(
                        const XMLCh* const      chars,
                        const unsigned int      length) = 0;
  
        virtual void
        entityReference(const XMLCh* const      name) = 0;
  
        virtual void
        ignorableWhitespace(
                        const XMLCh* const      chars,
                        const unsigned int      length);
  
        virtual void
        processingInstruction(
                        const XMLCh* const      target,
                        const XMLCh* const      data);
  
      virtual void
        resetDocument();
  
        virtual void
        comment(const XMLCh* const      data) = 0;
  
        virtual void
        cdata(
                        const XMLCh* const      ch,
                        const unsigned int      length);
  
        virtual Writer*
        getWriter() const;
  
        virtual const XalanDOMString&
        getDoctypeSystem() const;
  
        virtual const XalanDOMString&
        getDoctypePublic() const;
  
        virtual const XalanDOMString&
        getEncoding() const = 0;
  
        virtual const XalanDOMString&
        getMediaType() const;
  
        const XalanDOMString&
        getVersion() const
        {
                return m_version;
        }
  
        const XalanDOMString&
        getStandalone() const
        {
                return m_standalone;
        }
  
        bool
        getShouldWriteXMLHeader() const
        {
                return m_shouldWriteXMLHeader;
        }
  
        void
        setShouldWriteXMLHeader(bool    b)
        {
                m_shouldWriteXMLHeader = b;
        }
  
  #if defined(XALAN_NO_STD_NAMESPACE)
  #if defined(XALAN_USE_DEQUE_FOR_VECTOR_BOOL)
        typedef deque<bool>                     BoolStackType;
  #else
        typedef vector<bool>            BoolStackType;
  #endif
  #else
  #if defined(XALAN_USE_DEQUE_FOR_VECTOR_BOOL)
        typedef std::deque<bool>        BoolStackType;
  #else
        typedef std::vector<bool>       BoolStackType;
  #endif
  #endif
  
  protected:
  
        virtual void
        writeXMLHeader() = 0;
  
        virtual void
        flushBuffer() = 0;
  
        virtual void
        writeDoctypeDecl(const XalanDOMChar*    name) = 0;
  
        virtual void
        writeProcessingInstruction(
                        const XMLCh*    target,
                        const XMLCh*    data) = 0;
  
        virtual void
        writeCharacters(
                        const XMLCh*    chars,
                        unsigned int    length) = 0;
  
        virtual void
        writeCDATA(
                        const XMLCh*    chars,
                        unsigned int    length) = 0;
  
        /**
         * Mark the parent element as having a child.  If this
         * is the first child, return true, otherwise, return
         * false.  This allows the child element to determine
         * if the parent tag has already been closed.
         *
         * @return true if the parent element has not been previously marked 
for children.
         */
        bool
        markParentForChildren()
        {
                if(!m_elemStack.empty())
                {
                        // See if the parent element has already been flagged 
as having children.
                        if(false == m_elemStack.back())
                        {
                                m_elemStack.back() = true;
  
                                return true;
                        }
                }
  
                return false;
        }
  
        /**
         * Open an element for possibile children
         */
        void
        openElementForChildren()
        {
                m_elemStack.push_back(false);
        }
  
        bool
        outsideDocumentElement() const
        {
                return m_elemStack.empty();
        }
  
        /**
         * Determine if an element ever had any children added.
         *
         * @return true if the children were added, false if not.
         */
        bool
        childNodesWereAdded()
        {
                bool    fResult = false;
  
                if (m_elemStack.empty() == false)
                {
                        fResult = m_elemStack.back();
  
                        m_elemStack.pop_back();
                }
  
                return fResult;
        }
  
        void
        generateDoctypeDecl(const XalanDOMChar*         name)
        {
                if(true == m_needToOutputDoctypeDecl)              
                {
                        assert(m_doctypeSystem.empty() == false);
  
                        writeDoctypeDecl(name);
  
                        m_needToOutputDoctypeDecl = false;
                }
        }
  
        /** 
         * The writer.
         */
        Writer*                                 m_writer;
  
        void
        flushWriter();
  
        /**
         * Tell if the next text should be raw.
         */
        bool            m_nextIsRaw;
  
        /**
         * Add space before '/>' for XHTML.
         */
        bool            m_spaceBeforeClose;
  
        /**
         * The System ID for the doc type.
         */
        const XalanDOMString    m_doctypeSystem;
  
        /**
         * The public ID for the doc type.
         */
        const XalanDOMString    m_doctypePublic;
  
        /**
         * Tells the XML version, for writing out to the XML decl.
         */
        const XalanDOMString    m_version;
  
        /**
         * Text for standalone part of header.
         */
        const XalanDOMString    m_standalone;
  
        /**
         * The media type.  Not used right now.
         */
        const XalanDOMString    m_mediaType;
  
        /**
         * The string of characters that represents the newline
         */
        const XalanDOMChar*             m_newlineString;
  
        /**
         * The length of the the string of characters that represents the 
newline
         */
        XalanDOMString::size_type       m_newlineStringLength;
  
        static bool
        isUTF16HighSurrogate(XalanDOMChar       theChar)
        {
                return 0xD800u <= theChar && theChar <= 0xDBFFu ? true : false;
        }
  
        static bool
        isUTF16LowSurrogate(XalanDOMChar        theChar)
        {
                return 0xDC00u <= theChar && theChar <= 0xDFFFu ? true : false;
        }
  
        static unsigned int
        decodeUTF16SurrogatePair(
                        XalanDOMChar    theHighSurrogate,
                        XalanDOMChar    theLowSurrogate);
  
        /**
         * Throw an exception when an invalid
         * surrogate is encountered.
         * @param ch The first character in the surrogate
         */
        static void
        throwInvalidUTF16SurrogateException(XalanDOMChar        ch);
  
        /**
         * Throw an exception when an invalid
         * surrogate is encountered.
         * @param ch The first character in the surrogate
         * @param next The next character in the surrogate
         */
        static void
        throwInvalidUTF16SurrogateException(
                        XalanDOMChar    ch,
                        XalanDOMChar    next);
  
        /**
         * Throw an exception when an invalid
         * character is encountered.
         * @param ch The first character in the surrogate
         * @param next The next character in the surrogate
         */
        static void
        throwInvalidCharacterException(unsigned int             ch);
  
        enum
        {
                        kNotSpecial = 0,
                        kContentSpecial = 1,    // A flag to indicate a value 
in s_specialChars applies to content
                        kAttributeSpecial = 2,  // A flag to indicate a value 
in s_specialChars applies to attributes
                        kBothSpecial = 3,               // A flag t0 indicate a 
value in s_specialChars applies to both content and attributes
                        kSpecialsSize = 0x80,   // The size of s_specialChars
                        kBufferSize = 512               // The size of the 
buffer
        };
  
        static const XalanDOMChar       s_specialChars[];
  
  private:
  
        // These are not implemented.
        FormatterToXMLBase(const FormatterToXMLBase&);
  
        FormatterToXMLBase&
        operator=(const FormatterToXMLBase&);
  
        bool
        operator==(const FormatterToXMLBase&) const;
  
        // Data members...
        /**
         * Flag to tell that we need to add the doctype decl, 
         * which we can't do until the first element is 
         * encountered.
         */
        bool            m_needToOutputDoctypeDecl;
  
        /**
         * If true, XML header should be written to output.
         */
        bool            m_shouldWriteXMLHeader;
  
        /**
         * A stack of Boolean objects that tell if the given element 
         * has children.
         */
        BoolStackType   m_elemStack;
  
        /**
         * The string "-//W3C//DTD XHTML".
         */
        static const XalanDOMChar                               
s_xhtmlDocTypeString[];
  
        static const XalanDOMString::size_type  s_xhtmlDocTypeStringLength;
  };
  
  
  
  XALAN_CPP_NAMESPACE_END
  
  
  
  #endif        // FORMATTERTOXMLBASE_HEADER_GUARD_1357924680
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to