xmluri.cpp

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

CPP
2,222
字号
        fQueryString = 0;	}	else if (!isGenericURI())    {        ThrowXMLwithMemMgr2(MalformedURLException                , XMLExcepts::XMLNUM_URI_Component_for_GenURI_Only                , errMsg_QUERY                , newQueryString                , fMemoryManager);	}	else if ( !getPath() )    {        ThrowXMLwithMemMgr2(MalformedURLException                , XMLExcepts::XMLNUM_URI_NullPath                , errMsg_QUERY                , newQueryString                , fMemoryManager);	}	else if (!isURIString(newQueryString))    {        ThrowXMLwithMemMgr2(MalformedURLException               , XMLExcepts::XMLNUM_URI_Component_Invalid_Char               , errMsg_QUERY               , newQueryString               , fMemoryManager);	}	else    {        if (getQueryString())        {            fMemoryManager->deallocate(fQueryString);//delete [] fQueryString;        }        fQueryString = XMLString::replicate(newQueryString, fMemoryManager);	}}// ---------------------------------------------------------------------------//  XMLUri: Public, static methods// ---------------------------------------------------------------------------////  scheme = alpha *( alpha | digit | "+" | "-" | "." )//  alphanum = alpha | digit//bool XMLUri::isConformantSchemeName(const XMLCh* const scheme){	if ( !scheme )        return false;    const XMLCh* tmpStr = scheme;    if (!XMLString::isAlpha(*tmpStr))     // first: alpha        return false;    // second onwards: ( alpha | digit | "+" | "-" | "." )    tmpStr++;    while (*tmpStr)    {        if ( !XMLString::isAlphaNum(*tmpStr) &&             (XMLString::indexOf(SCHEME_CHARACTERS, *tmpStr) == -1))            return false;        tmpStr++;    }    return true;}//// userinfo = *( unreserved | escaped |//              ";" | ":" | "&" | "=" | "+" | "$" | "," )//void XMLUri::isConformantUserInfo(const XMLCh* const userInfo                                  , MemoryManager* const manager){	if ( !userInfo )        return;    const XMLCh* tmpStr = userInfo;    while (*tmpStr)    {        if ( isUnreservedCharacter(*tmpStr) ||            (XMLString::indexOf(USERINFO_CHARACTERS, *tmpStr) != -1))        {            tmpStr++;        }        else if (*tmpStr == chPercent)               // '%'        {            if (XMLString::isHex(*(tmpStr+1)) &&     // 1st hex                XMLString::isHex(*(tmpStr+2))  )     // 2nd hex            {                tmpStr+=3;            }            else            {                XMLCh value1[BUF_LEN+1];                value1[0] = chPercent;                value1[1] = *(tmpStr+1);                value1[2] = *(tmpStr+2);                value1[3] = chNull;                ThrowXMLwithMemMgr2(MalformedURLException                        , XMLExcepts::XMLNUM_URI_Component_Invalid_EscapeSequence                        , errMsg_USERINFO                        , value1                        , manager);            }        }        else        {	            ThrowXMLwithMemMgr2(MalformedURLException                    , XMLExcepts::XMLNUM_URI_Component_Invalid_Char                    , errMsg_USERINFO                    , userInfo                    , manager);        }    } //while    return;}bool XMLUri::isValidServerBasedAuthority(const XMLCh* const host,                                         const int hostLen,                                         const int port,                                         const XMLCh* const userinfo,                                         const int userLen){    // The order is important, do not change    if (!isWellFormedAddress(host, hostLen))        return false;    // check port number    if ((port > 65535) || (port < 0 && port != -1))        return false;    // check userinfo    int index = 0;    while (index < userLen)    {        if (isUnreservedCharacter(userinfo[index]) ||            (XMLString::indexOf(USERINFO_CHARACTERS, userinfo[index]) != -1))        {            index++;        }        else if (userinfo[index] == chPercent)               // '%'        {            if (XMLString::isHex(userinfo[index+1]) &&     // 1st hex                XMLString::isHex(userinfo[index+2])  )     // 2nd hex                index +=3;            else                return false;        }        else            return false;    } //while    return true;}bool XMLUri::isValidServerBasedAuthority(const XMLCh* const host                                         , const int port                                         , const XMLCh* const userinfo                                         , MemoryManager* const manager){    // The order is important, do not change    if (!isWellFormedAddress(host, manager))        return false;    // check port number    if ((port > 65535) || (port < 0 && port != -1))        return false;        // check userinfo    if (!userinfo)        return true;            const XMLCh* tmpStr = userinfo;    while (*tmpStr)    {        if ( isUnreservedCharacter(*tmpStr) ||            (XMLString::indexOf(USERINFO_CHARACTERS, *tmpStr) != -1))        {            tmpStr++;        }        else if (*tmpStr == chPercent)               // '%'        {            if (XMLString::isHex(*(tmpStr+1)) &&     // 1st hex                XMLString::isHex(*(tmpStr+2))  )     // 2nd hex            {                tmpStr+=3;            }            else                return false;        }        else            return false;    } //while        return true;}bool XMLUri::isValidRegistryBasedAuthority(const XMLCh* const authority,                                           const int authLen){    // check authority    int index = 0;    while (index < authLen)    {        if (isUnreservedCharacter(authority[index]) ||            (XMLString::indexOf(REG_NAME_CHARACTERS, authority[index]) != -1))        {            index++;        }        else if (authority[index] == chPercent)               // '%'        {            if (XMLString::isHex(authority[index+1]) &&     // 1st hex                XMLString::isHex(authority[index+2])  )     // 2nd hex                index +=3;            else                return false;        }        else            return false;    } //while    return true;}bool XMLUri::isValidRegistryBasedAuthority(const XMLCh* const authority){    // check authority    if (!authority)        return false;            const XMLCh* tmpStr = authority;    while (*tmpStr)    {        if (isUnreservedCharacter(*tmpStr) ||            (XMLString::indexOf(REG_NAME_CHARACTERS, *tmpStr) != -1))        {            tmpStr++;        }        else if (*tmpStr == chPercent)               // '%'        {            if (XMLString::isHex(*(tmpStr+1)) &&     // 1st hex                XMLString::isHex(*(tmpStr+2))  )     // 2nd hex            {                tmpStr+=3;            }            else                return false;        }        else            return false;    } //while        return true;}//// uric     = reserved | unreserved | escaped// escaped  = "%" hex hex// hex      = digit | "A" | "B" | "C" | "D" | "E" | "F" |//                    "a" | "b" | "c" | "d" | "e" | "f"//bool XMLUri::isURIString(const XMLCh* const uricString){	if (!uricString || !*uricString)        return false;    const XMLCh* tmpStr = uricString;    while (*tmpStr)    {        if (isReservedOrUnreservedCharacter(*tmpStr))                    {            tmpStr++;        }        else if (*tmpStr == chPercent)               // '%'        {            if (XMLString::isHex(*(tmpStr+1)) &&     // 1st hex                XMLString::isHex(*(tmpStr+2))  )     // 2nd hex            {                tmpStr+=3;            }            else            {                return false;            }        }        else        {	            return false;        }    }    return true;}////  host          = hostname | IPv4address////  hostname      = *( domainlabel "." ) toplabel [ "." ]//  domainlabel   = alphanum | alphanum *( alphanum | "-" ) alphanum//  toplabel      = alpha | alpha *( alphanum | "-" ) alphanum////  IPv4address   = 1*3DIGIT "." 1*3DIGIT "." 1*3DIGIT "." 1*3DIGIT//bool XMLUri::isWellFormedAddress(const XMLCh* const addrString                                 , MemoryManager* const manager){    // Check that we have a non-zero length string.    if (!addrString || !*addrString)        return false;            // Get address length.    int addrStrLen = XMLString::stringLen(addrString);        // Check if the host is a valid IPv6reference.    if (*addrString == chOpenSquare)    {        return isWellFormedIPv6Reference(addrString, addrStrLen);    }    //    // Cannot start with a '.', '-', or end with a '-'.    //    if (*addrString == chPeriod ||        *addrString == chDash ||        addrString[addrStrLen-1] == chDash)        return false;    // rightmost domain label starting with digit indicates IP address    // since top level domain label can only start with an alpha    // see RFC 2396 Section 3.2.2        int lastPeriodPos = XMLString::lastIndexOf(addrString, chPeriod);    // if the string ends with "."    // get the second last "."    if (lastPeriodPos + 1 == addrStrLen)    {        XMLCh* tmp2 = (XMLCh*) manager->allocate        (            addrStrLen * sizeof(XMLCh)        );//new XMLCh[addrStrLen];        XMLString::subString(tmp2, addrString, 0, lastPeriodPos, manager);        lastPeriodPos = XMLString::lastIndexOf(tmp2, chPeriod);        manager->deallocate(tmp2);//delete [] tmp2;        if ( XMLString::isDigit(addrString[lastPeriodPos + 1]))			return false;    }    if (XMLString::isDigit(addrString[lastPeriodPos + 1]))    {        return isWellFormedIPv4Address(addrString, addrStrLen);    } // end of IPv4address    else    {        //        //  hostname      = *( domainlabel "." ) toplabel [ "." ]        //  domainlabel   = alphanum | alphanum *( alphanum | "-" ) alphanum        //  toplabel      = alpha | alpha *( alphanum | "-" ) alphanum                // RFC 2396 states that hostnames take the form described in         // RFC 1034 (Section 3) and RFC 1123 (Section 2.1). According        // to RFC 1034, hostnames are limited to 255 characters.        if (addrStrLen > 255) {            return false;        }                unsigned int labelCharCount = 0;                // domain labels can contain alphanumerics and '-"        // but must start and end with an alphanumeric        for (int i = 0; i < addrStrLen; i++)        {            if (addrString[i] == chPeriod)            {              if (((i > 0)  &&                   (!XMLString::isAlphaNum(addrString[i-1]))) ||                  ((i + 1 < addrStrLen) &&                   (!XMLString::isAlphaNum(addrString[i+1])))  )                {                    return false;                }                labelCharCount = 0;            }            else if (!XMLString::isAlphaNum(addrString[i]) &&                      addrString[i] != chDash)            {                return false;            }            // RFC 1034: Labels must be 63 characters or less.            else if (++labelCharCount > 63) {                return false;            }        } //for    }    return true;}////  RFC 2732 amended RFC 2396 by replacing the definition //  of IPv4address with the one defined by RFC 2373.////  IPv4address   = 1*3DIGIT "." 1*3DIGIT "." 1*3DIGIT "." 1*3DIGIT//bool XMLUri::isWellFormedIPv4Address(const XMLCh* const addr, const int length){    int numDots = 0;    int numDigits = 0;        // IPv4address = 1*3DIGIT "." 1*3DIGIT "." 1*3DIGIT "." 1*3DIGIT    //    // make sure that    // 1) we see only digits and dot separators,    // 2) that any dot separator is preceded and followed by a digit    // 3) that we find 3 dots    // 4) that each segment contains 1 to 3 digits.    // 5) that each segment is not greater than 255.        for (int i = 0; i < length; ++i)    {        if (addr[i] == chPeriod)        {            if ((i == 0) ||                (i+1 == length) ||                !XMLString::isDigit(addr[i+1]))            {               return false;            }            numDigits = 0;            if (++numDots > 3)                return false;        }        else if (!XMLString::isDigit(addr[i]))        {            return false;        }        // Check that that there are no more than three digits        // in this segment.        else if (++numDigits > 3)

⌨️ 快捷键说明

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