xmluri.cpp

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

CPP
2,222
字号
/* * Copyright 2001-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: XMLUri.cpp,v 1.26 2004/09/08 13:56:24 peiyongz Exp $ */// ---------------------------------------------------------------------------//  Includes// ---------------------------------------------------------------------------#include <xercesc/util/Janitor.hpp>#include <xercesc/util/XMLURL.hpp>#include <xercesc/util/XMLUri.hpp>#include <xercesc/util/XMLChar.hpp>#include <xercesc/util/OutOfMemoryException.hpp>XERCES_CPP_NAMESPACE_BEGIN// ---------------------------------------------------------------------------//  XMLUri: static data// ---------------------------------------------------------------------------//      Amended by RFC2732//      reserved      = ";" | "/" | "?" | ":" | "@" | "&" | "=" | "+" |//                      "$" | "," | "[" | "]"//const XMLCh XMLUri::RESERVED_CHARACTERS[] ={    chSemiColon, chForwardSlash, chQuestion, chColon, chAt,    chAmpersand, chEqual, chPlus, chDollarSign, chComma, chOpenSquare,    chCloseSquare, chNull};////      mark          = "-" | "_" | "." | "!" | "~" | "*" | "'" |//                      "(" | ")"//const XMLCh XMLUri::MARK_CHARACTERS[] ={    chDash, chUnderscore, chPeriod, chBang, chTilde,    chAsterisk, chSingleQuote, chOpenParen, chCloseParen, chNull};// combination of MARK and RESERVEDconst XMLCh XMLUri::MARK_OR_RESERVED_CHARACTERS[] ={    chDash, chUnderscore, chPeriod, chBang, chTilde,    chAsterisk, chSingleQuote, chOpenParen, chCloseParen,    chSemiColon, chForwardSlash, chQuestion, chColon, chAt,    chAmpersand, chEqual, chPlus, chDollarSign, chComma, chOpenSquare,    chCloseSquare, chNull};////      scheme        = alpha *( alpha | digit | "+" | "-" | "." )//const XMLCh XMLUri::SCHEME_CHARACTERS[] ={    chPlus, chDash, chPeriod, chNull};////      userinfo      = *( unreserved | escaped |//                         ";" | ":" | "&" | "=" | "+" | "$" | "," )//const XMLCh XMLUri::USERINFO_CHARACTERS[] ={    chSemiColon, chColon, chAmpersand, chEqual, chPlus,    chDollarSign, chPeriod, chNull};////      reg_name     = 1*( unreserved | escaped | "$" | "," |//                         ";" | ":" | "@" | "&" | "=" | "+" )//const XMLCh XMLUri::REG_NAME_CHARACTERS[] ={    chDollarSign, chComma, chSemiColon, chColon, chAt,    chAmpersand, chEqual, chPlus, chNull};//      pchar plus ';' and '/'.//      pchar         = unreserved | escaped |//                      ":" | "@" | "&" | "=" | "+" | "$" | ","const XMLCh XMLUri::PATH_CHARACTERS[] ={    chSemiColon, chForwardSlash, chColon, chAt, chAmpersand,     chEqual, chPlus, chDollarSign, chComma, chNull};// ---------------------------------------------------------------------------//  Local methods and data// ---------------------------------------------------------------------------static const int BUF_LEN = 64;//// "Scheme"// "SchemeSpecificPart"// "Parameters"// "UserInfo"// "Host"// "Port"// "RegName"// "Path"// "Query"// "Fragment"//static const XMLCh errMsg_SCHEME[] ={    chLatin_S, chLatin_c, chLatin_h, chLatin_e,    chLatin_m, chLatin_e, chNull};static const XMLCh errMsg_SCHEMESPART[] ={    chLatin_S, chLatin_c, chLatin_h, chLatin_e, chLatin_m, chLatin_e,    chLatin_S, chLatin_p, chLatin_e, chLatin_c, chLatin_i, chLatin_f,    chLatin_i, chLatin_c, chLatin_P, chLatin_a, chLatin_r, chLatin_t,    chNull};static const XMLCh errMsg_PARAMS[] ={    chLatin_P, chLatin_a, chLatin_r, chLatin_a, chLatin_m,    chLatin_e, chLatin_t, chLatin_e, chLatin_r, chLatin_s, chNull};static const XMLCh errMsg_USERINFO[] ={    chLatin_U, chLatin_s, chLatin_e, chLatin_r,    chLatin_i, chLatin_n, chLatin_f, chLatin_o, chNull};static const XMLCh errMsg_HOST[] ={    chLatin_H, chLatin_o, chLatin_s, chLatin_t, chNull};static const XMLCh errMsg_PORT[] ={    chLatin_P, chLatin_o, chLatin_r, chLatin_t, chNull};static const XMLCh errMsg_REGNAME[] ={    chLatin_R, chLatin_e, chLatin_g,     chLatin_N, chLatin_a, chLatin_m, chLatin_e, chNull};static const XMLCh errMsg_PATH[] ={    chLatin_P, chLatin_a, chLatin_t, chLatin_h, chNull};static const XMLCh errMsg_QUERY[] ={    chLatin_Q, chLatin_u, chLatin_e, chLatin_r, chLatin_y, chNull};static const XMLCh errMsg_FRAGMENT[] ={    chLatin_F, chLatin_r, chLatin_a, chLatin_g,    chLatin_m, chLatin_e, chLatin_n, chLatin_t, chNull};////  "//"//  "/"//  "./"//  "/."//  "/../"//  "/.."//static const XMLCh DOUBLE_SLASH[] ={    chForwardSlash, chForwardSlash, chNull};static const XMLCh SINGLE_SLASH[] ={    chForwardSlash, chNull};static const XMLCh SLASH_DOT_SLASH[] ={    chForwardSlash, chPeriod, chForwardSlash, chNull};static const XMLCh SLASH_DOT[] ={    chForwardSlash, chPeriod, chNull};static const XMLCh SLASH_DOTDOT_SLASH[] ={    chForwardSlash, chPeriod, chPeriod, chForwardSlash, chNull};static const XMLCh SLASH_DOTDOT[] ={    chForwardSlash, chPeriod, chPeriod, chNull};////  ":/?#"//// REVISIT: why?static const XMLCh SCHEME_SEPARATORS[] ={    chColon, chForwardSlash, chQuestion, chPound, chNull};////  "?#"//static const XMLCh PATH_SEPARATORS[] ={    chQuestion, chPound, chNull};// ---------------------------------------------------------------------------//  XMLUri: Constructors and Helper methods// ---------------------------------------------------------------------------// ctor# 2XMLUri::XMLUri(const XMLCh* const uriSpec,               MemoryManager* const manager): fScheme(0), fUserInfo(0), fHost(0), fPort(-1), fRegAuth(0), fPath(0), fQueryString(0), fFragment(0), fURIText(0), fMemoryManager(manager){    try {        initialize((XMLUri *)0, uriSpec);    }    catch(const OutOfMemoryException&)    {        throw;    }    catch (...)    {        cleanUp();        throw;    }}// ctor# 7 relative ctorXMLUri::XMLUri(const XMLUri* const      baseURI              , const XMLCh* const   uriSpec              , MemoryManager* const manager): fScheme(0), fUserInfo(0), fHost(0), fPort(-1), fRegAuth(0), fPath(0), fQueryString(0), fFragment(0), fURIText(0), fMemoryManager(manager){    try {        initialize(baseURI, uriSpec);    }    catch(const OutOfMemoryException&)    {        throw;    }    catch (...)    {        cleanUp();        throw;    }}//Copy constructorXMLUri::XMLUri(const XMLUri& toCopy): fScheme(0), fUserInfo(0), fHost(0), fPort(-1), fRegAuth(0), fPath(0), fQueryString(0), fFragment(0), fURIText(0), fMemoryManager(toCopy.fMemoryManager){    try {        initialize(toCopy);    }    catch(const OutOfMemoryException&)    {        throw;    }    catch (...)    {        cleanUp();        throw;    }}XMLUri& XMLUri::operator=(const XMLUri& toAssign){    try {        initialize(toAssign);    }    catch(const OutOfMemoryException&)    {        throw;    }    catch (...)    {        cleanUp();        throw;    }    return *this;}XMLUri::~XMLUri(){    cleanUp();}void XMLUri::cleanUp(){    if (fScheme)        fMemoryManager->deallocate(fScheme);//delete[] fScheme;    if (fUserInfo)        fMemoryManager->deallocate(fUserInfo);//delete[] fUserInfo;    if (fHost)        fMemoryManager->deallocate(fHost);//delete[] fHost;            if (fRegAuth)        fMemoryManager->deallocate(fRegAuth);//delete[] fRegAuth;    if (fPath)        fMemoryManager->deallocate(fPath);//delete[] fPath;    if (fQueryString)        fMemoryManager->deallocate(fQueryString);//delete[] fQueryString;    if (fFragment)        fMemoryManager->deallocate(fFragment);//delete[] fFragment;    fMemoryManager->deallocate(fURIText);//delete[] fURIText;}void XMLUri::initialize(const XMLUri& toCopy){    //    // assuming that all fields from the toCopy are valid,    // therefore need NOT to go through various setXXX() methods    //    fMemoryManager = toCopy.fMemoryManager;    fScheme = XMLString::replicate(toCopy.fScheme, fMemoryManager);    fUserInfo = XMLString::replicate(toCopy.fUserInfo, fMemoryManager);    fHost = XMLString::replicate(toCopy.fHost, fMemoryManager);    fPort = toCopy.fPort;    fRegAuth = XMLString::replicate(toCopy.fRegAuth, fMemoryManager);    fPath = XMLString::replicate(toCopy.fPath, fMemoryManager);    fQueryString = XMLString::replicate(toCopy.fQueryString, fMemoryManager);    fFragment = XMLString::replicate(toCopy.fFragment, fMemoryManager);}void XMLUri::initialize(const XMLUri* const baseURI                      , const XMLCh*  const uriSpec){    // get a trimmed version of uriSpec    // uriSpec will NO LONGER be used in this function.    //    XMLCh* trimmedUriSpec = XMLString::replicate(uriSpec, fMemoryManager);    XMLString::trim(trimmedUriSpec);    ArrayJanitor<XMLCh> janName(trimmedUriSpec, fMemoryManager);    int trimmedUriSpecLen = XMLString::stringLen(trimmedUriSpec);    if ( !baseURI &&        (!trimmedUriSpec || trimmedUriSpecLen == 0))    {        ThrowXMLwithMemMgr1(MalformedURLException               , XMLExcepts::XMLNUM_URI_Component_Empty               , errMsg_PARAMS               , fMemoryManager);    }	// just make a copy of the base if spec is empty	if (!trimmedUriSpec || trimmedUriSpecLen == 0)    {        initialize(*baseURI);        return;	}	int index = 0;	bool foundScheme = false;	// Check for scheme, which must be before `/', '?' or '#'. 	// Also handle names with DOS drive letters ('D:'), 	// so 1-character schemes are not allowed.        int colonIdx = XMLString::indexOf(trimmedUriSpec, chColon);        int slashIdx = XMLString::indexOf(trimmedUriSpec, chForwardSlash);        int queryIdx = XMLString::indexOf(trimmedUriSpec, chQuestion);        int fragmentIdx = XMLString::indexOf(trimmedUriSpec, chPound);        if ((colonIdx < 2) ||            (colonIdx > slashIdx && slashIdx != -1) ||            (colonIdx > queryIdx && queryIdx != -1) ||            (colonIdx > fragmentIdx && fragmentIdx != -1))        {            // A standalone base is a valid URI according to spec            if ( colonIdx == 0 || (!baseURI && fragmentIdx != 0) )            {                ThrowXMLwithMemMgr(MalformedURLException, XMLExcepts::XMLNUM_URI_No_Scheme, fMemoryManager);            }        }        else        {            foundScheme = true;            initializeScheme(trimmedUriSpec);            index = XMLString::stringLen(fScheme)+1;        }    // It's an error if we stop here

⌨️ 快捷键说明

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