xmlformatter.cpp

来自「IBM的解析xml的工具Xerces的源代码」· C++ 代码 · 共 706 行 · 第 1/2 页

CPP
706
字号
/* * Copyright 1999-2000,2004 The Apache Software Foundation. *  * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at *  *      http://www.apache.org/licenses/LICENSE-2.0 *  * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *//** * $Id: XMLFormatter.cpp,v 1.20 2004/09/08 13:55:59 peiyongz Exp $ */// ---------------------------------------------------------------------------//  Includes// ---------------------------------------------------------------------------#include <xercesc/util/PlatformUtils.hpp>#include <xercesc/util/XMLUniDefs.hpp>#include <xercesc/util/XMLString.hpp>#include <xercesc/util/TransService.hpp>#include <xercesc/util/TranscodingException.hpp>#include <xercesc/util/XMLExceptMsgs.hpp>#include <xercesc/framework/XMLFormatter.hpp>#include <xercesc/util/Janitor.hpp>#include <xercesc/util/XMLChar.hpp>#include <string.h>XERCES_CPP_NAMESPACE_BEGIN// ---------------------------------------------------------------------------//  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 , chLF         , chNull        , chNull }    ,   { chAmpersand , chOpenAngle  , chCloseAngle  , chNull       , chNull        , chNull }};// ---------------------------------------------------------------------------//  Local methods// ---------------------------------------------------------------------------bool XMLFormatter::inEscapeList(const XMLFormatter::EscapeFlags escStyle                              , const XMLCh                     toCheck){    const XMLCh* escList = gEscapeChars[escStyle];    while (*escList)    {        if (*escList++ == toCheck)            return true;    }    /***     *  XML1.1     *     *  Finally, there is considerable demand to define a standard representation of      *  arbitrary Unicode characters in XML documents. Therefore, XML 1.1 allows the      *  use of character references to the control characters #x1 through #x1F,      *  most of which are forbidden in XML 1.0. For reasons of robustness, however,      *  these characters still cannot be used directly in documents.     *  In order to improve the robustness of character encoding detection, the      *  additional control characters #x7F through #x9F, which were freely allowed in      *  XML 1.0 documents, now must also appear only as character references.      *  (Whitespace characters are of course exempt.) The minor sacrifice of backward      *  compatibility is considered not significant.      *  Due to potential problems with APIs, #x0 is still forbidden both directly and      *  as a character reference.     *    ***/    if (fIsXML11)    {        // for XML11        if ( XMLChar1_1::isControlChar(toCheck, 0) &&            !XMLChar1_1::isWhitespace(toCheck, 0)   )        {            return true;            }        else        {                return false;        }    }    else    {        return false;    }}// ---------------------------------------------------------------------------//  XMLFormatter: Constructors and Destructor// ---------------------------------------------------------------------------XMLFormatter::XMLFormatter( const   char* const             outEncoding                            , const char* const             docVersion                            ,       XMLFormatTarget* const  target                            , const EscapeFlags             escapeFlags                            , const UnRepFlags              unrepFlags                            ,       MemoryManager* const    manager)    : fEscapeFlags(escapeFlags)    , fOutEncoding(0)    , fTarget(target)    , fUnRepFlags(unrepFlags)    , fXCoder(0)      , fAposRef(0)    , fAposLen(0)    , fAmpRef(0)        , fAmpLen(0)        , fGTRef(0)    , fGTLen(0)    , fLTRef(0)    , fLTLen(0)    , fQuoteRef(0)    , fQuoteLen(0)     , fIsXML11(false)    , fMemoryManager(manager){    // Transcode the encoding string    fOutEncoding = XMLString::transcode(outEncoding, fMemoryManager);    // Try to create a transcoder for this encoding    XMLTransService::Codes resCode;    fXCoder = XMLPlatformUtils::fgTransService->makeNewTranscoderFor    (        fOutEncoding        , resCode        , kTmpBufSize        , fMemoryManager    );    if (!fXCoder)    {        fMemoryManager->deallocate(fOutEncoding); //delete [] fOutEncoding;        ThrowXMLwithMemMgr1        (            TranscodingException            , XMLExcepts::Trans_CantCreateCvtrFor            , outEncoding            , fMemoryManager        );    }    XMLCh* const tmpDocVer = XMLString::transcode(docVersion, fMemoryManager);    ArrayJanitor<XMLCh> jname(tmpDocVer, fMemoryManager);    fIsXML11 = XMLString::equals(tmpDocVer, XMLUni::fgVersion1_1);}XMLFormatter::XMLFormatter( const   XMLCh* const            outEncoding                            , const XMLCh* const            docVersion                            ,       XMLFormatTarget* const  target                            , const EscapeFlags             escapeFlags                            , const UnRepFlags              unrepFlags                            ,       MemoryManager* const    manager)    : fEscapeFlags(escapeFlags)    , fOutEncoding(0)    , fTarget(target)    , fUnRepFlags(unrepFlags)    , fXCoder(0)      , fAposRef(0)    , fAposLen(0)    , fAmpRef(0)        , fAmpLen(0)        , fGTRef(0)    , fGTLen(0)    , fLTRef(0)    , fLTLen(0)    , fQuoteRef(0)    , fQuoteLen(0)     , fIsXML11(false)    , fMemoryManager(manager){    // Try to create a transcoder for this encoding    XMLTransService::Codes resCode;    fXCoder = XMLPlatformUtils::fgTransService->makeNewTranscoderFor    (        outEncoding        , resCode        , kTmpBufSize        , fMemoryManager    );    if (!fXCoder)    {        ThrowXMLwithMemMgr1        (            TranscodingException            , XMLExcepts::Trans_CantCreateCvtrFor            , outEncoding            , fMemoryManager        );    }    // Copy the encoding string    fOutEncoding = XMLString::replicate(outEncoding, fMemoryManager);    fIsXML11 = XMLString::equals(docVersion, XMLUni::fgVersion1_1);}XMLFormatter::XMLFormatter( const   char* const             outEncoding                            ,       XMLFormatTarget* const  target                            , const EscapeFlags             escapeFlags                            , const UnRepFlags              unrepFlags                            ,       MemoryManager* const    manager)    : fEscapeFlags(escapeFlags)    , fOutEncoding(0)    , fTarget(target)    , fUnRepFlags(unrepFlags)    , fXCoder(0)      , fAposRef(0)    , fAposLen(0)    , fAmpRef(0)        , fAmpLen(0)        , fGTRef(0)    , fGTLen(0)    , fLTRef(0)    , fLTLen(0)    , fQuoteRef(0)    , fQuoteLen(0)     , fIsXML11(false)    , fMemoryManager(manager){    // this constructor uses "1.0" for the docVersion    // Transcode the encoding string    fOutEncoding = XMLString::transcode(outEncoding, fMemoryManager);    // Try to create a transcoder for this encoding    XMLTransService::Codes resCode;    fXCoder = XMLPlatformUtils::fgTransService->makeNewTranscoderFor    (        fOutEncoding        , resCode        , kTmpBufSize        , fMemoryManager    );    if (!fXCoder)    {        fMemoryManager->deallocate(fOutEncoding); //delete [] fOutEncoding;        ThrowXMLwithMemMgr1        (            TranscodingException            , XMLExcepts::Trans_CantCreateCvtrFor            , outEncoding            , fMemoryManager        );    }    //XMLCh* const tmpDocVer = XMLString::transcode("1.0", fMemoryManager);    //ArrayJanitor<XMLCh> jname(tmpDocVer, fMemoryManager);    //fIsXML11 = XMLString::equals(tmpDocVer, XMLUni::fgVersion1_1);    fIsXML11 = false;  // docVersion 1.0 is not 1.1!}XMLFormatter::XMLFormatter( const   XMLCh* const            outEncoding                            ,       XMLFormatTarget* const  target                            , const EscapeFlags             escapeFlags                            , const UnRepFlags              unrepFlags                            ,       MemoryManager* const    manager)    : fEscapeFlags(escapeFlags)    , fOutEncoding(0)    , fTarget(target)    , fUnRepFlags(unrepFlags)    , fXCoder(0)      , fAposRef(0)    , fAposLen(0)    , fAmpRef(0)        , fAmpLen(0)        , fGTRef(0)    , fGTLen(0)    , fLTRef(0)    , fLTLen(0)    , fQuoteRef(0)    , fQuoteLen(0)     , fIsXML11(false)    , fMemoryManager(manager){    // this constructor uses XMLUni::fgVersion1_0 for the docVersion    // Try to create a transcoder for this encoding    XMLTransService::Codes resCode;    fXCoder = XMLPlatformUtils::fgTransService->makeNewTranscoderFor    (        outEncoding        , resCode        , kTmpBufSize        , fMemoryManager    );    if (!fXCoder)    {        ThrowXMLwithMemMgr1        (            TranscodingException            , XMLExcepts::Trans_CantCreateCvtrFor            , outEncoding            , fMemoryManager        );    }    // Copy the encoding string    fOutEncoding = XMLString::replicate(outEncoding, fMemoryManager);    //fIsXML11 = XMLString::equals(docVersion, XMLUni::fgVersion1_1);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?