You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xerces.apache.org by ro...@locus.apache.org on 2000/04/05 02:20:17 UTC

cvs commit: xml-xerces/c/src/framework XMLFormatter.cpp XMLFormatter.hpp

roddey      00/04/04 17:20:17

  Modified:    c/src/framework XMLFormatter.cpp XMLFormatter.hpp
  Log:
  More updates for the low level formatted output support
  
  Revision  Changes    Path
  1.2       +336 -13   xml-xerces/c/src/framework/XMLFormatter.cpp
  
  Index: XMLFormatter.cpp
  ===================================================================
  RCS file: /home/cvs/xml-xerces/c/src/framework/XMLFormatter.cpp,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- XMLFormatter.cpp	2000/03/28 19:43:17	1.1
  +++ XMLFormatter.cpp	2000/04/05 00:20:16	1.2
  @@ -56,6 +56,9 @@
   
   /**
    * $Log: XMLFormatter.cpp,v $
  + * Revision 1.2  2000/04/05 00:20:16  roddey
  + * More updates for the low level formatted output support
  + *
    * Revision 1.1  2000/03/28 19:43:17  roddey
    * Fixes for signed/unsigned warnings. New work for two way transcoding
    * stuff.
  @@ -72,18 +75,92 @@
   #include <util/TranscodingException.hpp>
   #include <util/XMLExceptMsgs.hpp>
   #include <framework/XMLFormatter.hpp>
  +#include <memory.h>
  +
  +
  +// ---------------------------------------------------------------------------
  +//  Local data
  +//
  +//  gXXXRef
  +//      These are hard coded versions of the char refs we put out for the
  +//      standard char refs.
  +//
  +//  gEscapeChars
  +//      For each style of escape, we have a list of the chars that must
  +//      be escaped for that style. The first null hit in each list indicates
  +//      no more valid entries in that list. The first entry is a dummy for
  +//      the NoEscapes style.
  +// ---------------------------------------------------------------------------
  +static const XMLCh  gAmpRef[] =
  +{
  +    chAmpersand, chLatin_a, chLatin_m, chLatin_p, chSemiColon, chNull
  +};
  +
  +static const XMLCh  gAposRef[] =
  +{
  +    chAmpersand, chLatin_a, chLatin_p, chLatin_o, chLatin_s, chSemiColon, chNull
  +};
  +
  +static const XMLCh  gGTRef[] =
  +{
  +    chAmpersand, chLatin_g, chLatin_t, chSemiColon, chNull
  +};
  +
  +static const XMLCh  gLTRef[] =
  +{
  +    chAmpersand, chLatin_l, chLatin_t, chSemiColon, chNull
  +};
  +
  +static const XMLCh  gQuoteRef[] =
  +{
  +    chAmpersand, chLatin_q, chLatin_u, chLatin_o, chLatin_t, chSemiColon, chNull
  +};
  +
  +static const unsigned int kEscapeCount = 6;
  +static const XMLCh gEscapeChars[XMLFormatter::EscapeFlags_Count][kEscapeCount] =
  +{
  +        { chNull      , chNull       , chNull        , chNull       , chNull        , chNull }
  +    ,   { chAmpersand , chCloseAngle , chDoubleQuote , chOpenAngle  , chSingleQuote , chNull }
  +    ,   { chAmpersand , chOpenAngle  , chDoubleQuote , chNull       , chNull        , chNull }
  +    ,   { chAmpersand , chOpenAngle  , chNull        , chNull       , chNull        , chNull }
  +};
  +
  +
  +// ---------------------------------------------------------------------------
  +//  Local methods
  +// ---------------------------------------------------------------------------
  +static inline bool inEscapeList(const   XMLFormatter::EscapeFlags escStyle
  +                                , const XMLCh                     toCheck)
  +{
  +    const XMLCh* escList = gEscapeChars[escStyle];
  +    while (*escList)
  +    {
  +        if (*escList++ == toCheck)
  +            return true;
  +    }
  +    return false;
  +}
   
   
   // ---------------------------------------------------------------------------
   //  XMLFormatter: Constructors and Destructor
   // ---------------------------------------------------------------------------
   XMLFormatter::XMLFormatter( const   char* const             outEncoding
  +                            ,       XMLFormatTarget* const  target
                               , const EscapeFlags             escapeFlags
  -                            ,       XMLFormatTarget* const  target) :
  +                            , const UnRepFlags              unrepFlags) :
  +
       fEscapeFlags(escapeFlags)
       , fOutEncoding(0)
       , fTarget(target)
  +    , fUnRepFlags(unrepFlags)
       , fXCoder(0)
  +
  +    , fAposRef(0)
  +    , fAmpRef(0)
  +    , fGTRef(0)
  +    , fLTRef(0)
  +    , fQuoteRef(0)
   {
       // Transcode the encoding string
       fOutEncoding = XMLString::transcode(outEncoding);
  @@ -94,7 +171,7 @@
       (
           fOutEncoding
           , resCode
  -        , 64 * 1024
  +        , kTmpBufSize
       );
   
       if (!fXCoder)
  @@ -112,12 +189,20 @@
   
   
   XMLFormatter::XMLFormatter( const   XMLCh* const            outEncoding
  +                            ,       XMLFormatTarget* const  target
                               , const EscapeFlags             escapeFlags
  -                            ,       XMLFormatTarget* const  target) :
  +                            , const UnRepFlags              unrepFlags) :
       fEscapeFlags(escapeFlags)
       , fOutEncoding(0)
       , fTarget(target)
  +    , fUnRepFlags(unrepFlags)
       , fXCoder(0)
  +
  +    , fAposRef(0)
  +    , fAmpRef(0)
  +    , fGTRef(0)
  +    , fLTRef(0)
  +    , fQuoteRef(0)
   {
       // Copy the encoding string
       fOutEncoding = XMLString::replicate(outEncoding);
  @@ -146,6 +231,12 @@
   
   XMLFormatter::~XMLFormatter()
   {
  +    delete fAposRef;
  +    delete fAmpRef;
  +    delete fGTRef;
  +    delete fLTRef;
  +    delete fQuoteRef;
  +
       delete [] fOutEncoding;
       delete fXCoder;
   
  @@ -157,24 +248,34 @@
   //  XMLFormatter: Formatting methods
   // ---------------------------------------------------------------------------
   void
  -XMLFormatter::formatBuf(const XMLCh* const toFormat, const unsigned int count)
  +XMLFormatter::formatBuf(const   XMLCh* const    toFormat
  +                        , const unsigned int    count
  +                        , const EscapeFlags     escapeFlags
  +                        , const UnRepFlags      unrepFlags)
   {
       //
  +    //  Figure out the actual escape flag value. If the parameter is not
  +    //  the default, then take it. Else take the current default.
  +    //
  +    const EscapeFlags actualEsc = (escapeFlags == DefaultEscape)
  +                                ? fEscapeFlags : escapeFlags;
  +
  +    //
       //  If we don't have any escape flags set, then we can do the most
       //  efficient loop, else we have to do it the hard way.
       //
  -    unsigned int    srcCount = count;
       const XMLCh*    srcPtr = toFormat;
  -    if (fEscapeFlags == NoEscapes)
  +    const XMLCh*    endPtr = toFormat + count;
  +    unsigned int    charsEaten;
  +    if (actualEsc == NoEscapes)
       {
           //
           //  Just do a whole buffer at a time into the temp buffer, cap it
           //  off, and send it to the target.
           //
  -        while (srcCount)
  +        while (srcPtr < endPtr)
           {
  -            unsigned int charsEaten;
  -
  +            const unsigned int srcCount = endPtr - srcPtr;
               const unsigned srcChars = srcCount > kTmpBufSize ?
                                         kTmpBufSize : srcCount;
   
  @@ -196,17 +297,118 @@
               }
               #endif
   
  +            // If we get any bytes out, then write them
               if (outBytes)
               {
                   fTmpBuf[outBytes] = 0;
                   fTarget->writeChars(fTmpBuf);
               }
   
  -            srcCount -= charsEaten;
  +            // And bump up our pointer
  +            srcPtr += charsEaten;
           }
       }
        else
       {
  +        //
  +        //  This one just escapes the standard set of XML defined character
  +        //  refs: apos, amp, lt, gt, and quot.
  +        //
  +        //  For now, just whimp out and do it the simple but slow way in
  +        //  order to get this concept out for evaluation. Come back later
  +        //  and spiff it up.
  +        //
  +        while (srcPtr < endPtr)
  +        {
  +            //
  +            //  Run a temp pointer up until we hit a character that we have
  +            //  to escape. Then we can convert all the chars between our
  +            //  current source pointer and here all at once.
  +            //
  +            const XMLCh* tmpPtr = srcPtr;
  +            while (!inEscapeList(actualEsc, *tmpPtr) && (tmpPtr < endPtr))
  +                tmpPtr++;
  +
  +            //
  +            //  If we got any chars, then lets convert them and write them
  +            //  out.
  +            //
  +            if (tmpPtr > srcPtr)
  +            {
  +                const unsigned int srcCount = tmpPtr - srcPtr;
  +                const unsigned srcChars = srcCount > kTmpBufSize ?
  +                                          kTmpBufSize : srcCount;
  +
  +                const unsigned int outBytes = fXCoder->transcodeTo
  +                (
  +                    srcPtr
  +                    , srcChars
  +                    , fTmpBuf
  +                    , kTmpBufSize
  +                    , charsEaten
  +                    , XMLTranscoder::UnRep_Throw
  +                );
  +
  +                #if XML_DEBUG
  +                if ((outBytes > kTmpBufSize)
  +                ||  (charsEaten > srcCount))
  +                {
  +                    // <TBD> The transcoder is freakin out maaaannn
  +                }
  +                #endif
  +
  +                // If we get any bytes out, then write them
  +                if (outBytes)
  +                {
  +                    fTmpBuf[outBytes] = 0;
  +                    fTarget->writeChars(fTmpBuf);
  +                }
  +
  +                // And bump up our pointer
  +                srcPtr += charsEaten;
  +            }
  +             else if (tmpPtr < endPtr)
  +            {
  +                //
  +                //  Ok, so we've hit a char that must be escaped. So loop
  +                //  until we hit the end or a non-escaped char and put out
  +                //  char refs for these.
  +                //
  +                bool done = false;
  +                while ((srcPtr < endPtr) && !done)
  +                {
  +                    switch(*srcPtr)
  +                    {
  +                        case chAmpersand :
  +                            fTarget->writeChars(getAmpRef());
  +                            break;
  +
  +                        case chSingleQuote :
  +                            fTarget->writeChars(getAposRef());
  +                            break;
  +
  +                        case chDoubleQuote :
  +                            fTarget->writeChars(getQuoteRef());
  +                            break;
  +
  +                        case chCloseAngle :
  +                            fTarget->writeChars(getGTRef());
  +                            break;
  +
  +                        case chOpenAngle :
  +                            fTarget->writeChars(getLTRef());
  +                            break;
  +
  +                        default:
  +                            done = true;
  +                            break;
  +                    }
  +
  +                    if (!done)
  +                        srcPtr++;
  +                }
  +            }
  +        }
       }
   }
   
  @@ -217,8 +419,128 @@
       return *this;
   }
   
  -XMLFormatter& XMLFormatter::operator<<(const char* const toFormat)
  +XMLFormatter& XMLFormatter::operator<<(const XMLCh toFormat)
   {
  -    fTarget->writeChars((const XMLByte*)toFormat);
  +    // Make a temp string format that
  +    XMLCh szTmp[2];
  +    szTmp[0] = toFormat;
  +    szTmp[1] = 0;
  +
  +    formatBuf(szTmp, 1);
       return *this;
  +}
  +
  +
  +
  +// ---------------------------------------------------------------------------
  +//  XMLFormatter: Private helper methosd
  +// ---------------------------------------------------------------------------
  +const XMLByte* XMLFormatter::getAposRef()
  +{
  +    if (fAposRef)
  +        return fAposRef;
  +
  +    unsigned int charsEaten;
  +    const unsigned int outBytes = fXCoder->transcodeTo
  +    (
  +        gAposRef
  +        , XMLString::stringLen(gAposRef)
  +        , fTmpBuf
  +        , kTmpBufSize
  +        , charsEaten
  +        , XMLTranscoder::UnRep_Throw
  +    );
  +    fTmpBuf[outBytes] = 0;
  +   
  +    ((XMLFormatter*)this)->fAposRef = new XMLByte[outBytes + 1];
  +    memcpy(fAposRef, fTmpBuf, outBytes + 1);
  +    return fAposRef;
  +}
  +
  +const XMLByte* XMLFormatter::getAmpRef()
  +{
  +    if (fAmpRef)
  +        return fAmpRef;
  +
  +    unsigned int charsEaten;
  +    const unsigned int outBytes = fXCoder->transcodeTo
  +    (
  +        gAmpRef
  +        , XMLString::stringLen(gAmpRef)
  +        , fTmpBuf
  +        , kTmpBufSize
  +        , charsEaten
  +        , XMLTranscoder::UnRep_Throw
  +    );
  +    fTmpBuf[outBytes] = 0;
  +   
  +    ((XMLFormatter*)this)->fAmpRef = new XMLByte[outBytes + 1];
  +    memcpy(fAmpRef, fTmpBuf, outBytes + 1);
  +    return fAmpRef;
  +}
  +
  +const XMLByte* XMLFormatter::getGTRef()
  +{
  +    if (fGTRef)
  +        return fGTRef;
  +
  +    unsigned int charsEaten;
  +    const unsigned int outBytes = fXCoder->transcodeTo
  +    (
  +        gGTRef
  +        , XMLString::stringLen(gGTRef)
  +        , fTmpBuf
  +        , kTmpBufSize
  +        , charsEaten
  +        , XMLTranscoder::UnRep_Throw
  +    );
  +    fTmpBuf[outBytes] = 0;
  +   
  +    ((XMLFormatter*)this)->fGTRef = new XMLByte[outBytes + 1];
  +    memcpy(fGTRef, fTmpBuf, outBytes + 1);
  +    return fGTRef;
  +}
  +
  +const XMLByte* XMLFormatter::getLTRef()
  +{
  +    if (fLTRef)
  +        return fLTRef;
  +
  +    unsigned int charsEaten;
  +    const unsigned int outBytes = fXCoder->transcodeTo
  +    (
  +        gLTRef
  +        , XMLString::stringLen(gLTRef)
  +        , fTmpBuf
  +        , kTmpBufSize
  +        , charsEaten
  +        , XMLTranscoder::UnRep_Throw
  +    );
  +    fTmpBuf[outBytes] = 0;
  +   
  +    ((XMLFormatter*)this)->fLTRef = new XMLByte[outBytes + 1];
  +    memcpy(fLTRef, fTmpBuf, outBytes + 1);
  +    return fLTRef;
  +}
  +
  +const XMLByte* XMLFormatter::getQuoteRef()
  +{
  +    if (fQuoteRef)
  +        return fQuoteRef;
  +
  +    unsigned int charsEaten;
  +    const unsigned int outBytes = fXCoder->transcodeTo
  +    (
  +        gQuoteRef
  +        , XMLString::stringLen(gQuoteRef)
  +        , fTmpBuf
  +        , kTmpBufSize
  +        , charsEaten
  +        , XMLTranscoder::UnRep_Throw
  +    );
  +    fTmpBuf[outBytes] = 0;
  +   
  +    ((XMLFormatter*)this)->fQuoteRef = new XMLByte[outBytes + 1];
  +    memcpy(fQuoteRef, fTmpBuf, outBytes + 1);
  +    return fQuoteRef;
   }
  
  
  
  1.2       +125 -6    xml-xerces/c/src/framework/XMLFormatter.hpp
  
  Index: XMLFormatter.hpp
  ===================================================================
  RCS file: /home/cvs/xml-xerces/c/src/framework/XMLFormatter.hpp,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- XMLFormatter.hpp	2000/03/28 19:43:17	1.1
  +++ XMLFormatter.hpp	2000/04/05 00:20:16	1.2
  @@ -56,6 +56,9 @@
   
   /*
    * $Log: XMLFormatter.hpp,v $
  + * Revision 1.2  2000/04/05 00:20:16  roddey
  + * More updates for the low level formatted output support
  + *
    * Revision 1.1  2000/03/28 19:43:17  roddey
    * Fixes for signed/unsigned warnings. New work for two way transcoding
    * stuff.
  @@ -89,26 +92,40 @@
       {
           NoEscapes
           , StdEscapes
  -        , FailEscapes
  -        , AllEscapes
  +        , AttrEscapes
  +        , CharEscapes
  +
  +        // Special values, don't use directly
  +        , EscapeFlags_Count
  +        , DefaultEscape     = 999
       };
   
  +    enum UnRepFlags
  +    {
  +        UnRep_Fail
  +        , UnRep_CharRef
   
  +        , DefaultUnRep      = 999
  +    };
  +
  +
       // -----------------------------------------------------------------------
       //  Constructors and Destructor
       // -----------------------------------------------------------------------
       XMLFormatter
       (
           const   XMLCh* const            outEncoding
  -        , const EscapeFlags             escapeFlags
           ,       XMLFormatTarget* const  target
  +        , const EscapeFlags             escapeFlags = NoEscapes
  +        , const UnRepFlags              unrepFlags = UnRep_Fail
       );
   
       XMLFormatter
       (
           const   char* const             outEncoding
  -        , const EscapeFlags             escapeFlags
           ,       XMLFormatTarget* const  target
  +        , const EscapeFlags             escapeFlags = NoEscapes
  +        , const UnRepFlags              unrepFlags = UnRep_Fail
       );
   
       ~XMLFormatter();
  @@ -121,6 +138,8 @@
       (
           const   XMLCh* const    toFormat
           , const unsigned int    count
  +        , const EscapeFlags     escapeFlags = DefaultEscape
  +        , const UnRepFlags      unrepFlags = DefaultUnRep
       );
   
       XMLFormatter& operator<<
  @@ -130,10 +149,40 @@
   
       XMLFormatter& operator<<
       (
  -        const   char* const     toFormat
  +        const   XMLCh           toFormat
       );
   
   
  +    // -----------------------------------------------------------------------
  +    //  Getter methods
  +    // -----------------------------------------------------------------------
  +    const XMLCh* getEncodingName() const;
  +
  +
  +    // -----------------------------------------------------------------------
  +    //  Setter methods
  +    // -----------------------------------------------------------------------
  +    void setEscapeFlags
  +    (
  +        const   EscapeFlags     newFlags
  +    );
  +
  +    void setUnRepFlags
  +    (
  +        const   UnRepFlags      newFlags
  +    );
  +
  +    XMLFormatter& operator<<
  +    (
  +        const   EscapeFlags     newFlags
  +    );
  +
  +    XMLFormatter& operator<<
  +    (
  +        const   UnRepFlags      newFlags
  +    );
  +
  +
   private :
       // -----------------------------------------------------------------------
       //  Unimplemented constructors and operators
  @@ -153,6 +202,17 @@
   
   
       // -----------------------------------------------------------------------
  +    //  Private helper methods
  +    // -----------------------------------------------------------------------
  +    const XMLByte* getAposRef();
  +    const XMLByte* getAmpRef();
  +    const XMLByte* getGTRef();
  +    const XMLByte* getLTRef();
  +    const XMLByte* getQuoteRef();
  +
  +
  +
  +    // -----------------------------------------------------------------------
       //  Private, non-virtual methods
       //
       //  fEscapeFlags
  @@ -164,18 +224,39 @@
       //      This the name of the output encoding. Saved mainly for meaningful
       //      error messages.
       //
  +    //  fRef
       //  fTarget
       //      This is the target object for the formatting operation.
       //
  +    //  fUnRepFlags
  +    //      The unrepresentable flags that indicate how to react when a
  +    //      character cannot be represented in the target encoding.
  +    //
       //  fXCoder
       //      This the transcoder that we will use. It is created using the
       //      encoding name we were told to use.
  +    //
  +    //  fAposRef
  +    //  fAmpRef
  +    //  fGTRef
  +    //  fLTRef
  +    //  fQuoteRef
  +    //      These are character refs for the standard char refs, in the
  +    //      output encoding. They are faulted in as required, by transcoding
  +    //      them from fixed Unicode versions.
       // -----------------------------------------------------------------------
       EscapeFlags         fEscapeFlags;
       XMLCh*              fOutEncoding;
       XMLFormatTarget*    fTarget;
  +    UnRepFlags          fUnRepFlags;
       XMLTranscoder*      fXCoder;
       XMLByte             fTmpBuf[kTmpBufSize + 1];
  +
  +    XMLByte*            fAposRef;
  +    XMLByte*            fAmpRef;
  +    XMLByte*            fGTRef;
  +    XMLByte*            fLTRef;
  +    XMLByte*            fQuoteRef;
   };
   
   
  @@ -205,5 +286,42 @@
       XMLFormatTarget(const XMLFormatTarget&) {}
       void operator=(const XMLFormatTarget&) {}
   };
  +
  +
  +// ---------------------------------------------------------------------------
  +//  XMLFormatter: Getter methods
  +// ---------------------------------------------------------------------------
  +inline const XMLCh* XMLFormatter::getEncodingName() const
  +{
  +    return fOutEncoding;
  +}
  +
  +
  +// ---------------------------------------------------------------------------
  +//  XMLFormatter: Setter methods
  +// ---------------------------------------------------------------------------
  +inline void XMLFormatter::setEscapeFlags(const EscapeFlags newFlags)
  +{
  +    fEscapeFlags = newFlags;
  +}
  +
  +inline void XMLFormatter::setUnRepFlags(const UnRepFlags newFlags)
  +{
  +    fUnRepFlags = newFlags;
  +}
  +
  +
  +inline XMLFormatter& XMLFormatter::operator<<(const EscapeFlags newFlags)
  +{
  +    fEscapeFlags = newFlags;
  +    return *this;
  +}
  +
  +inline XMLFormatter& XMLFormatter::operator<<(const UnRepFlags newFlags)
  +{
  +    fUnRepFlags = newFlags;
  +    return *this;
  +}
  +
   
   #endif