xmlurl.cpp

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

CPP
1,467
字号
/* * Copyright 1999-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: XMLURL.cpp,v 1.15 2004/09/08 13:56:24 peiyongz Exp $ */// ---------------------------------------------------------------------------//  Includes// ---------------------------------------------------------------------------#include <xercesc/util/BinFileInputStream.hpp>#include <xercesc/util/Janitor.hpp>#include <xercesc/util/PlatformUtils.hpp>#include <xercesc/util/RuntimeException.hpp>#include <xercesc/util/TransService.hpp>#include <xercesc/util/XMLURL.hpp>#include <xercesc/util/XMLNetAccessor.hpp>#include <xercesc/util/XMLString.hpp>#include <xercesc/util/XMLUniDefs.hpp>#include <xercesc/util/XMLUni.hpp>#include <xercesc/util/XMLUri.hpp>#include <xercesc/util/OutOfMemoryException.hpp>XERCES_CPP_NAMESPACE_BEGIN// ---------------------------------------------------------------------------//  Local types////  TypeEntry//      This structure defines a single entry in the list of URL types. Each//      entry indicates the prefix for that type of URL, and the SourceTypes//      value it maps to.// ---------------------------------------------------------------------------struct ProtoEntry{    XMLURL::Protocols   protocol;    const XMLCh*        prefix;    unsigned int        defPort;};// ---------------------------------------------------------------------------//  Local data////  gXXXString//      These are the strings for our prefix types. They all have to be//      Unicode strings all the time, so we can't just do regular strings.////  gProtoList//      The list of URL types that we support and some info related to each//      one.////  gMaxProtoLen//      The length of the longest protocol string////      NOTE:!!! Be sure to keep this up to date if new protocols are added!// ---------------------------------------------------------------------------static const XMLCh  gFileString[] ={        chLatin_f, chLatin_i, chLatin_l, chLatin_e, chNull};static const XMLCh gFTPString[]  ={        chLatin_f, chLatin_t, chLatin_p, chNull};static const XMLCh gHTTPString[] ={        chLatin_h, chLatin_t, chLatin_t, chLatin_p, chNull};static ProtoEntry gProtoList[XMLURL::Protocols_Count] ={        { XMLURL::File     , gFileString    , 0  }    ,   { XMLURL::HTTP     , gHTTPString    , 80 }    ,   { XMLURL::FTP      , gFTPString     , 21 }};// !!! Keep these up to date with list above!static const unsigned int gMaxProtoLen = 4;static const XMLCh gListOne[]    = { chColon, chForwardSlash, chNull };static const XMLCh gListTwo[]    = { chAt, chNull };static const XMLCh gListThree[]  = { chColon, chNull };static const XMLCh gListFour[]   = { chForwardSlash, chNull };static const XMLCh gListFive[]   = { chPound, chQuestion, chNull };static const XMLCh gListSix[]    = { chPound, chNull };// ---------------------------------------------------------------------------//  Local methods// ---------------------------------------------------------------------------static bool isHexDigit(const XMLCh toCheck){    if ((toCheck >= chDigit_0) && (toCheck <= chDigit_9)    ||  (toCheck >= chLatin_A) && (toCheck <= chLatin_Z)    ||  (toCheck >= chLatin_a) && (toCheck <= chLatin_z))    {        return true;    }    return false;}static unsigned int xlatHexDigit(const XMLCh toXlat){    if ((toXlat >= chDigit_0) && (toXlat <= chDigit_9))        return (unsigned int)(toXlat - chDigit_0);    if ((toXlat >= chLatin_A) && (toXlat <= chLatin_Z))        return (unsigned int)(toXlat - chLatin_A) + 10;    return (unsigned int)(toXlat - chLatin_a) + 10;}// ---------------------------------------------------------------------------//  XMLURL: Public, static methods// ---------------------------------------------------------------------------XMLURL::Protocols XMLURL::lookupByName(const XMLCh* const protoName){    for (unsigned int index = 0; index < XMLURL::Protocols_Count; index++)    {        if (!XMLString::compareIString(gProtoList[index].prefix, protoName))            return gProtoList[index].protocol;    }    return XMLURL::Unknown;}// ---------------------------------------------------------------------------//  XMLURL: Constructors and Destructor// ---------------------------------------------------------------------------XMLURL::XMLURL(MemoryManager* const manager) :    fMemoryManager(manager)    , fFragment(0)    , fHost(0)    , fPassword(0)    , fPath(0)    , fPortNum(0)    , fProtocol(XMLURL::Unknown)    , fQuery(0)    , fUser(0)    , fURLText(0)    , fHasInvalidChar(false){}XMLURL::XMLURL(const XMLCh* const    baseURL             , const XMLCh* const    relativeURL             , MemoryManager* const manager) :    fMemoryManager(manager)    , fFragment(0)    , fHost(0)    , fPassword(0)    , fPath(0)    , fPortNum(0)    , fProtocol(XMLURL::Unknown)    , fQuery(0)    , fUser(0)    , fURLText(0)    , fHasInvalidChar(false){	try	{	        setURL(baseURL, relativeURL);	}    catch(const OutOfMemoryException&)    {        throw;    }    catch(...)    {        cleanup();        throw;    }}XMLURL::XMLURL(const XMLCh* const  baseURL             , const char* const   relativeURL             , MemoryManager* const manager) :    fMemoryManager(manager)    , fFragment(0)    , fHost(0)    , fPassword(0)    , fPath(0)    , fPortNum(0)    , fProtocol(XMLURL::Unknown)    , fQuery(0)    , fUser(0)    , fURLText(0)    , fHasInvalidChar(false){    XMLCh* tmpRel = XMLString::transcode(relativeURL, fMemoryManager);    ArrayJanitor<XMLCh> janRel(tmpRel, fMemoryManager);	try	{		setURL(baseURL, tmpRel);	}    catch(const OutOfMemoryException&)    {        throw;    }    catch(...)    {        cleanup();        throw;    }}XMLURL::XMLURL(const XMLURL&         baseURL             , const XMLCh* const    relativeURL) :    fMemoryManager(baseURL.fMemoryManager)    , fFragment(0)    , fHost(0)    , fPassword(0)    , fPath(0)    , fPortNum(0)    , fProtocol(XMLURL::Unknown)    , fQuery(0)    , fUser(0)    , fURLText(0)    , fHasInvalidChar(false){	try	{		setURL(baseURL, relativeURL);	}    catch(const OutOfMemoryException&)    {        throw;    }    catch(...)    {        cleanup();        throw;    }}XMLURL::XMLURL(const  XMLURL&        baseURL             , const char* const     relativeURL) :    fMemoryManager(baseURL.fMemoryManager)    , fFragment(0)    , fHost(0)    , fPassword(0)    , fPath(0)    , fPortNum(0)    , fProtocol(XMLURL::Unknown)    , fQuery(0)    , fUser(0)    , fURLText(0)    , fHasInvalidChar(false){    XMLCh* tmpRel = XMLString::transcode(relativeURL, fMemoryManager);    ArrayJanitor<XMLCh> janRel(tmpRel, fMemoryManager);	try	{		setURL(baseURL, tmpRel);	}    catch(const OutOfMemoryException&)    {        throw;    }    catch(...)    {        cleanup();        throw;    }}XMLURL::XMLURL(const XMLCh* const urlText,               MemoryManager* const manager) :    fMemoryManager(manager)    , fFragment(0)    , fHost(0)    , fPassword(0)    , fPath(0)    , fPortNum(0)    , fProtocol(XMLURL::Unknown)    , fQuery(0)    , fUser(0)    , fURLText(0)    , fHasInvalidChar(false){	try	{	    setURL(urlText);	}    catch(const OutOfMemoryException&)    {        throw;    }    catch(...)    {        cleanup();        throw;    }}XMLURL::XMLURL(const char* const urlText,               MemoryManager* const manager) :    fMemoryManager(manager)    , fFragment(0)    , fHost(0)    , fPassword(0)    , fPath(0)    , fPortNum(0)    , fProtocol(XMLURL::Unknown)    , fQuery(0)    , fUser(0)    , fURLText(0)    , fHasInvalidChar(false){    XMLCh* tmpText = XMLString::transcode(urlText, fMemoryManager);    ArrayJanitor<XMLCh> janRel(tmpText, fMemoryManager);	try	{	    setURL(tmpText);	}    catch(const OutOfMemoryException&)    {        throw;    }    catch(...)    {        cleanup();        throw;    }}XMLURL::XMLURL(const XMLURL& toCopy) :    fMemoryManager(toCopy.fMemoryManager)    , fFragment(0)    , fHost(0)    , fPassword(0)    , fPath(0)    , fPortNum(toCopy.fPortNum)    , fProtocol(toCopy.fProtocol)    , fQuery(0)    , fUser(0)    , fURLText(0)    , fHasInvalidChar(toCopy.fHasInvalidChar){    try    {        fFragment = XMLString::replicate(toCopy.fFragment, fMemoryManager);        fHost = XMLString::replicate(toCopy.fHost, fMemoryManager);        fPassword = XMLString::replicate(toCopy.fPassword, fMemoryManager);        fPath = XMLString::replicate(toCopy.fPath, fMemoryManager);        fQuery = XMLString::replicate(toCopy.fQuery, fMemoryManager);        fUser = XMLString::replicate(toCopy.fUser, fMemoryManager);        fURLText = XMLString::replicate(toCopy.fURLText, fMemoryManager);    }    catch(const OutOfMemoryException&)    {        throw;    }    catch(...)    {        cleanup();        throw;    }}XMLURL::~XMLURL(){    cleanup();}// ---------------------------------------------------------------------------//  XMLURL: Public operators// ---------------------------------------------------------------------------XMLURL& XMLURL::operator=(const XMLURL& toAssign){    if (this == &toAssign)        return *this;    // Clean up our stuff    cleanup();    // And copy his stuff    fMemoryManager = toAssign.fMemoryManager;    fFragment = XMLString::replicate(toAssign.fFragment, fMemoryManager);    fHost = XMLString::replicate(toAssign.fHost, fMemoryManager);    fPassword = XMLString::replicate(toAssign.fPassword, fMemoryManager);    fPath = XMLString::replicate(toAssign.fPath, fMemoryManager);    fPortNum = toAssign.fPortNum;    fProtocol = toAssign.fProtocol;    fQuery = XMLString::replicate(toAssign.fQuery, fMemoryManager);    fUser = XMLString::replicate(toAssign.fUser, fMemoryManager);    fURLText = XMLString::replicate(toAssign.fURLText, fMemoryManager);    fHasInvalidChar = toAssign.fHasInvalidChar;    return *this;}bool XMLURL::operator==(const XMLURL& toCompare) const{    //    //  Compare the two complete URLs (which have been processed the same    //  way so they should now be the same even if they came in via different    //  relative parts.    //    if (!XMLString::equals(getURLText(), toCompare.getURLText()))        return false;    return true;}// ---------------------------------------------------------------------------//  XMLURL: Getter methods// ---------------------------------------------------------------------------unsigned int XMLURL::getPortNum() const{    //    //  If it was not provided explicitly, then lets return the default one    //  for the protocol.    //    if (!fPortNum)    {        if (fProtocol == Unknown)            return 0;        return gProtoList[fProtocol].defPort;    }    return fPortNum;}const XMLCh* XMLURL::getProtocolName() const{    // Check to see if its ever been set    if (fProtocol == Unknown)        ThrowXMLwithMemMgr(MalformedURLException, XMLExcepts::URL_NoProtocolPresent, fMemoryManager);    return gProtoList[fProtocol].prefix;}// ---------------------------------------------------------------------------//  XMLURL: Setter methods// ---------------------------------------------------------------------------void XMLURL::setURL(const XMLCh* const urlText){    //    //  Try to parse the URL.    //    cleanup();    parse(urlText);}void XMLURL::setURL(const XMLCh* const    baseURL                  , const XMLCh* const    relativeURL){    cleanup();    // Parse our URL string    parse(relativeURL);	//	//  If its relative and the base is non-null and non-empty, then

⌨️ 快捷键说明

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