xmluri.cpp
来自「IBM的解析xml的工具Xerces的源代码」· C++ 代码 · 共 2,222 行 · 第 1/5 页
CPP
2,222 行
{ return false; } // Check that this segment is not greater than 255. else if (numDigits == 3) { XMLCh first = addr[i-2]; XMLCh second = addr[i-1]; XMLCh last = addr[i]; if (!(first < chDigit_2 || (first == chDigit_2 && (second < chDigit_5 || (second == chDigit_5 && last <= chDigit_5))))) { return false; } } } //for return (numDots == 3);}//// IPv6reference = "[" IPv6address "]"//bool XMLUri::isWellFormedIPv6Reference(const XMLCh* const addr, const int length){ int index = 1; int end = length-1; // Check if string is a potential match for IPv6reference. if (!(length > 2 && addr[0] == chOpenSquare && addr[end] == chCloseSquare)) { return false; } // Counter for the number of 16-bit sections read in the address. int counter = 0; // Scan hex sequence before possible '::' or IPv4 address. index = scanHexSequence(addr, index, end, counter); if (index == -1) { return false; } // Address must contain 128-bits of information. else if (index == end) { return (counter == 8); } if (index+1 < end && addr[index] == chColon) { if (addr[index+1] == chColon) { // '::' represents at least one 16-bit group of zeros. if (++counter > 8) { return false; } index += 2; // Trailing zeros will fill out the rest of the address. if (index == end) { return true; } } // If the second character wasn't ':', in order to be valid, // the remainder of the string must match IPv4Address, // and we must have read exactly 6 16-bit groups. else { if (counter == 6) return isWellFormedIPv4Address(addr+index+1, end-index-1); else return false; } } else { return false; } // 3. Scan hex sequence after '::'. int prevCount = counter; index = scanHexSequence(addr, index, end, counter); if (index == -1) { return false; } // If this is the end of the address then // we've got 128-bits of information. else if (index == end) { return true; } // The address ends in an IPv4 address, or it is invalid. // scanHexSequence has already made sure that we have the right number of bits. int shiftCount = (counter > prevCount) ? index+1 : index; return isWellFormedIPv4Address(addr + shiftCount, end - shiftCount);}//// For use with isWellFormedIPv6Reference only.//int XMLUri::scanHexSequence (const XMLCh* const addr, int index, int end, int& counter){ XMLCh testChar = chNull; int numDigits = 0; int start = index; // Trying to match the following productions: // hexseq = hex4 *( ":" hex4) // hex4 = 1*4HEXDIG for (; index < end; ++index) { testChar = addr[index]; if (testChar == chColon) { // IPv6 addresses are 128-bit, so there can be at most eight sections. if (numDigits > 0 && ++counter > 8) { return -1; } // This could be '::'. if (numDigits == 0 || ((index+1 < end) && addr[index+1] == chColon)) { return index; } numDigits = 0; } // This might be invalid or an IPv4address. If it's potentially an IPv4address, // backup to just after the last valid character that matches hexseq. else if (!XMLString::isHex(testChar)) { if (testChar == chPeriod && numDigits < 4 && numDigits > 0 && counter <= 6) { int back = index - numDigits - 1; return (back >= start) ? back : start; } return -1; } // There can be at most 4 hex digits per group. else if (++numDigits > 4) { return -1; } } return (numDigits > 0 && ++counter <= 8) ? end : -1;}bool XMLUri::isGenericURI(){ return (getHost() != 0);}//// This method will take the broken out parts of the URI and build up the// full text. We don't do this unless someone asks us to, since its often// never required.//void XMLUri::buildFullText(){ // Calculate the worst case size of the buffer required unsigned int bufSize = XMLString::stringLen(fScheme) + 1 + XMLString::stringLen(fFragment) + 1 + XMLString::stringLen(fHost ? fHost : fRegAuth) + 2 + XMLString::stringLen(fPath) + XMLString::stringLen(fQueryString) + 1 + XMLString::stringLen(fUserInfo) + 1 + 32; // Clean up the existing buffer and allocate another fMemoryManager->deallocate(fURIText);//delete [] fURIText; fURIText = (XMLCh*) fMemoryManager->allocate(bufSize * sizeof(XMLCh));//new XMLCh[bufSize]; *fURIText = 0; XMLCh* outPtr = fURIText; if (fScheme != 0) { XMLString::catString(fURIText, getScheme()); outPtr += XMLString::stringLen(fURIText); *outPtr++ = chColon; } // Authority if (fHost || fRegAuth) { *outPtr++ = chForwardSlash; *outPtr++ = chForwardSlash; // Server based authority. if (fHost) { if (fUserInfo) { XMLString::copyString(outPtr, fUserInfo); outPtr += XMLString::stringLen(fUserInfo); *outPtr++ = chAt; } XMLString::copyString(outPtr, fHost); outPtr += XMLString::stringLen(fHost); // // If the port is -1, then we don't put it in. Else we need // to because it was explicitly provided. // if (fPort != -1) { *outPtr++ = chColon; XMLCh tmpBuf[17]; XMLString::binToText(fPort, tmpBuf, 16, 10, fMemoryManager); XMLString::copyString(outPtr, tmpBuf); outPtr += XMLString::stringLen(tmpBuf); } } // Registry based authority. else { XMLString::copyString(outPtr, fRegAuth); outPtr += XMLString::stringLen(fRegAuth); } } if (fPath) { XMLString::copyString(outPtr, fPath); outPtr += XMLString::stringLen(fPath); } if (fQueryString) { *outPtr++ = chQuestion; XMLString::copyString(outPtr, fQueryString); outPtr += XMLString::stringLen(fQueryString); } if (fFragment) { *outPtr++ = chPound; XMLString::copyString(outPtr, fFragment); outPtr += XMLString::stringLen(fFragment); } // Cap it off in case the last op was not a string copy *outPtr = 0;}// NOTE: no check for NULL value of uriStr (caller responsiblilty)bool XMLUri::isValidURI(const XMLUri* const baseURI , const XMLCh* const uriStr){ // get a trimmed version of uriStr // uriStr will NO LONGER be used in this function. const XMLCh* trimmedUriSpec = uriStr; while (XMLChar1_0::isWhitespace(*trimmedUriSpec)) trimmedUriSpec++; int trimmedUriSpecLen = XMLString::stringLen(trimmedUriSpec); while (trimmedUriSpecLen) { if (XMLChar1_0::isWhitespace(trimmedUriSpec[trimmedUriSpecLen-1])) trimmedUriSpecLen--; else break; } if (trimmedUriSpecLen == 0) { if (!baseURI) return false; else return true; } 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)) return false; } else { if (!processScheme(trimmedUriSpec, index)) return false; foundScheme = true; ++index; } // It's an error if we stop here if (index == trimmedUriSpecLen || (foundScheme && (trimmedUriSpec[index] == chPound))) return false; // two slashes means generic URI syntax, so we get the authority const XMLCh* authUriSpec = trimmedUriSpec + index; if (((index+1) < trimmedUriSpecLen) && XMLString::startsWith(authUriSpec, DOUBLE_SLASH)) { index += 2; int startPos = index; // get authority - everything up to path, query or fragment XMLCh testChar; while (index < trimmedUriSpecLen) { testChar = trimmedUriSpec[index]; if (testChar == chForwardSlash || testChar == chQuestion || testChar == chPound ) { break; } index++; } // if we found authority, parse it out, otherwise we set the // host to empty string if (index > startPos) { if (!processAuthority(trimmedUriSpec + startPos, index - startPos)) return false; } } // we need to check if index has exceed the lenght or not if (index < trimmedUriSpecLen) { if (!processPath(trimmedUriSpec + index, trimmedUriSpecLen - index, foundScheme)) return false; } return true;}// NOTE: no check for NULL value of uriStr (caller responsiblilty)// NOTE: this routine is the same as above, but it uses a flag to// indicate the existance of a baseURI rather than an XMLuri.bool XMLUri::isValidURI(bool haveBaseURI, const XMLCh* const uriStr){ // get a trimmed version of uriStr // uriStr will NO LONGER be used in this function. const XMLCh* trimmedUriSpec = uriStr; while (XMLChar1_0::isWhitespace(*trimmedUriSpec)) trimmedUriSpec++; int trimmedUriSpecLen = XMLString::stringLen(trimmedUriSpec); while (trimmedUriSpecLen) { if (XMLChar1_0::isWhitespace(trimmedUriSpec[trimmedUriSpecLen-1])) trimmedUriSpecLen--; else break; } if (trimmedUriSpecLen == 0) { if (!haveBaseURI) return false; else return true; return true; } 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 || (!haveBaseURI && fragmentIdx != 0)) return false; } else { if (!processScheme(trimmedUriSpec, index)) return false; foundScheme = true; ++index; } // It's an error if we stop here if (index == trimmedUriSpecLen || (foundScheme && (trimmedUriSpec[index] == chPound))) return false; // two slashes means generic URI syntax, so we get the authority const XMLCh* authUriSpec = trimmedUriSpec + index; if (((index+1) < trimmedUriSpecLen) && XMLString::startsWith(authUriSpec, DOUBLE_SLASH)) { index += 2; int startPos = index; // get authority - everything up to path, query or fragment XMLCh testChar; while (index < trimmedUriSpecLen) { testChar = trimmedUriSpec[index]; if (testChar == chForwardSlash || testChar == chQuestion || testChar == chPound ) { break; } index++; } // if we found authority, parse it out, otherwise we set the // host to empty string if (index > startPos) { if (!processAuthority(trimmedUriSpec + startPos, index - startP
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?