Newer
Older
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
{
// Refer this oneto the transcoding service
return XMLPlatformUtils::fgTransService->compareNIString(str1, str2, maxChars);
}
int XMLString::compareString( const XMLCh* const str1
, const XMLCh* const str2)
{
const XMLCh* psz1 = str1;
const XMLCh* psz2 = str2;
if (psz1 == 0 || psz2 == 0) {
if (psz1 == 0) {
return 0 - XMLString::stringLen(psz2);
}
else if (psz2 == 0) {
return XMLString::stringLen(psz1);
}
}
while (true)
{
// If an inequality, then return the difference
if (*psz1 != *psz2)
return int(*psz1) - int(*psz2);
// If either has ended, then they both ended, so equal
if (!*psz1 || !*psz2)
break;
// Move upwards for the next round
psz1++;
psz2++;
}
return 0;
}
bool XMLString::regionMatches(const XMLCh* const str1
, const int offset1
, const XMLCh* const str2
, const int offset2
, const unsigned int charCount)
{
if (!validateRegion(str1, offset1,str2, offset2, charCount))
return false;
if (compareNString(str1+offset1, str2+offset2, charCount) != 0)
return false;
return true;
}
bool XMLString::regionIMatches(const XMLCh* const str1
, const int offset1
, const XMLCh* const str2
, const int offset2
, const unsigned int charCount)
{
if (!validateRegion(str1, offset1,str2, offset2, charCount))
return false;
if (compareNIString(str1+offset1, str2+offset2, charCount) != 0)
return false;
return true;
}
void XMLString::copyString(XMLCh* const target, const XMLCh* const src)
{
if (!src)
{
*target = 0;
return;
}
XMLCh* pszOut = target;
const XMLCh* pszIn = src;
while (*pszIn)
*pszOut++ = *pszIn++;
// Capp off the target where we ended
*pszOut = 0;
}
bool XMLString::copyNString( XMLCh* const target
, const XMLCh* const src
, const unsigned int maxChars)
{
XMLCh* outPtr = target;
const XMLCh* srcPtr = src;
const XMLCh* endPtr = target + maxChars - 1;
while (*srcPtr && (outPtr <= endPtr))
*outPtr++ = *srcPtr++;
// Cap it off here
*outPtr = 0;
// Return whether we copied it all or hit the max
return (*srcPtr == 0);
}
unsigned int XMLString::hash( const XMLCh* const tohash
, const unsigned int hashModulus)
{
if (!hashModulus)
ThrowXML(IllegalArgumentException, XMLExcepts::Pool_ZeroModulus);
unsigned int hashVal = 0;
if (tohash) {
const XMLCh* curCh = tohash;
while (*curCh)
{
unsigned int top = hashVal >> 24;
hashVal += (hashVal * 37) + top + (unsigned int)(*curCh);
curCh++;
}
}
// Divide by modulus
return hashVal % hashModulus;
}
const XMLCh* XMLString::findAny(const XMLCh* const toSearch
, const XMLCh* const searchList)
{
const XMLCh* srcPtr = toSearch;
while (*srcPtr)
{
const XMLCh* listPtr = searchList;
const XMLCh curCh = *srcPtr;
while (*listPtr)
{
if (curCh == *listPtr++)
return srcPtr;
}
srcPtr++;
}
return 0;
}
XMLCh* XMLString::findAny( XMLCh* const toSearch
, const XMLCh* const searchList)
{
XMLCh* srcPtr = toSearch;
while (*srcPtr)
{
const XMLCh* listPtr = searchList;
const XMLCh curCh = *srcPtr;
while (*listPtr)
{
if (curCh == *listPtr++)
return srcPtr;
}
srcPtr++;
}
return 0;
}
int XMLString::patternMatch( const XMLCh* const toSearch
if (!toSearch || !*toSearch )
return -1;
const int patnLen = XMLString::stringLen(pattern);
if ( !patnLen )
return -1;
const XMLCh* srcPtr = toSearch;
const XMLCh* patnStart = toSearch;
if ( !(*srcPtr++ == pattern[patnIndex++]))
srcPtr = ++patnStart;
{
if (patnIndex == patnLen)
// full pattern match found
return (srcPtr - patnLen - toSearch);
}
}
return -1;
}
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
unsigned int XMLString::hashN( const XMLCh* const tohash
, const unsigned int n
, const unsigned int hashModulus)
{
if (!hashModulus)
ThrowXML(IllegalArgumentException, XMLExcepts::Pool_ZeroModulus);
unsigned int hashVal = 0;
if (tohash) {
const XMLCh* curCh = tohash;
int i = n;
while (i--)
{
unsigned int top = hashVal >> 24;
hashVal += (hashVal * 37) + top + (unsigned int)(*curCh);
curCh++;
}
}
// Divide by modulus
return hashVal % hashModulus;
}
int XMLString::indexOf(const XMLCh* const toSearch, const XMLCh ch)
{
const XMLCh* srcPtr = toSearch;
while (*srcPtr)
{
if (ch == *srcPtr)
return (int)(srcPtr - toSearch);
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
}
return -1;
}
int XMLString::indexOf( const XMLCh* const toSearch
, const XMLCh ch
, const unsigned int fromIndex)
{
const int len = stringLen(toSearch);
// Make sure the start index is within the XMLString bounds
if ((int)fromIndex > len-1)
ThrowXML(ArrayIndexOutOfBoundsException, XMLExcepts::Str_StartIndexPastEnd);
for (int i = (int)fromIndex; i < len; i++)
{
if (toSearch[i] == ch)
return i;
}
return -1;
}
int XMLString::lastIndexOf(const XMLCh* const toSearch, const XMLCh ch)
{
const int len = stringLen(toSearch);
for (int i = len-1; i >= 0; i--)
{
if (toSearch[i] == ch)
return i;
}
return -1;
}
int XMLString::lastIndexOf( const XMLCh* const toSearch
, const XMLCh ch
, const unsigned int fromIndex)
{
const int len = stringLen(toSearch);
if ((int)fromIndex > len-1)
ThrowXML(ArrayIndexOutOfBoundsException, XMLExcepts::Str_StartIndexPastEnd);
for (int i = (int)fromIndex; i >= 0; i--)
{
if (toSearch[i] == ch)
return i;
}
return -1;
}
XMLCh*
XMLString::makeUName(const XMLCh* const pszURI, const XMLCh* const pszName)
{
//
// If there is a URI, then format out the full name in the {uri}name
// form. Otherwise, just set it to the same thing as the base name.
//
XMLCh* pszRet = 0;
const unsigned int uriLen = stringLen(pszURI);
if (uriLen)
{
pszRet = new XMLCh[uriLen + stringLen(pszName) + 3];
XMLCh szTmp[2];
szTmp[1] = 0;
szTmp[0] = chOpenCurly;
copyString(pszRet, szTmp);
catString(pszRet, pszURI);
szTmp[0] = chCloseCurly;
catString(pszRet, szTmp);
catString(pszRet, pszName);
}
else
{
pszRet = replicate(pszName);
}
return pszRet;
}
bool XMLString::textToBin(const XMLCh* const toConvert, unsigned int& toFill)
{
toFill = 0;
// If no string, then its a failure
XMLCh* trimmedStr = XMLString::replicate(toConvert);
ArrayJanitor<XMLCh> jan1(trimmedStr);
XMLString::trim(trimmedStr);
unsigned int trimmedStrLen = XMLString::stringLen(trimmedStr);
// we don't allow '-' sign
if (XMLString::indexOf(trimmedStr, chDash, 0) != -1)
return false;
//the errno set by previous run is NOT automatically cleared
errno = 0;
char *nptr = XMLString::transcode(trimmedStr);
ArrayJanitor<char> jan2(nptr);
char *endptr;
//
// REVISIT: conversion of (unsigned long) to (unsigned int)
// may truncate value on IA64
toFill = (unsigned int) strtoul(nptr, &endptr, 10);
// check if all chars are valid char
// check if overflow/underflow occurs
if ( ( (endptr - nptr) != (int) trimmedStrLen) ||
return true;
}
int XMLString::parseInt(const XMLCh* const toConvert)
{
// If no string, or empty string, then it is a failure
if ((!toConvert) || (!*toConvert))
ThrowXML(NumberFormatException, XMLExcepts::XMLNUM_null_ptr);
XMLCh* trimmedStr = XMLString::replicate(toConvert);
ArrayJanitor<XMLCh> jan1(trimmedStr);
XMLString::trim(trimmedStr);
unsigned int trimmedStrLen = XMLString::stringLen(trimmedStr);
if ( !trimmedStrLen )
ThrowXML(NumberFormatException, XMLExcepts::XMLNUM_null_ptr);
//the errno set by previous run is NOT automatically cleared
errno = 0;
char *nptr = XMLString::transcode(trimmedStr);
ArrayJanitor<char> jan2(nptr);
char *endptr;
long retVal = strtol(nptr, &endptr, 10);
if ( (endptr - nptr) != (int) trimmedStrLen)
ThrowXML(NumberFormatException, XMLExcepts::XMLNUM_Inv_chars);
// check if overflow/underflow occurs
if (errno == ERANGE)
ThrowXML(NumberFormatException, XMLExcepts::Str_ConvertOverflow);
//
// REVISIT: conversion of (long) to (int)
// may truncate value on IA64
return (int) retVal;
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
void XMLString::trim(XMLCh* const toTrim)
{
const unsigned int len = stringLen(toTrim);
unsigned int skip, scrape;
for (skip = 0; skip < len; skip++)
{
if (!XMLPlatformUtils::fgTransService->isSpace(toTrim[skip]))
break;
}
for (scrape = len; scrape > skip; scrape--)
{
if (!XMLPlatformUtils::fgTransService->isSpace(toTrim[scrape - 1]))
break;
}
// Cap off at the scrap point
if (scrape != len)
toTrim[scrape] = 0;
if (skip)
{
// Copy the chars down
unsigned int index = 0;
while (toTrim[skip])
toTrim[index++] = toTrim[skip++];
toTrim[index] = 0;
}
}
void XMLString::upperCase(XMLCh* const toUpperCase)
{
// Refer this one to the transcoding service
XMLPlatformUtils::fgTransService->upperCase(toUpperCase);
}
void XMLString::lowerCase(XMLCh* const toLowerCase)
{
// Refer this one to the transcoding service
XMLPlatformUtils::fgTransService->lowerCase(toLowerCase);
}
void XMLString::subString(XMLCh* const targetStr, const XMLCh* const srcStr
, const int startIndex, const int endIndex)
{
//if (startIndex < 0 || endIndex < 0)
// ThrowXML(ArrayIndexOutOfBoundsException, XMLExcepts::Str_NegativeIndex);
if (targetStr == 0)
ThrowXML(IllegalArgumentException, XMLExcepts::Str_ZeroSizedTargetBuf);
const int srcLen = stringLen(srcStr);
const int copySize = endIndex - startIndex;
// Make sure the start index is within the XMLString bounds
if ( startIndex < 0 || startIndex > endIndex || endIndex > srcLen)
ThrowXML(ArrayIndexOutOfBoundsException, XMLExcepts::Str_StartIndexPastEnd);
for (int i= startIndex; i < endIndex; i++) {
targetStr[i-startIndex] = srcStr[i];
}
targetStr[copySize] = 0;
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
}
RefVectorOf<XMLCh>* XMLString::tokenizeString(const XMLCh* const tokenizeSrc)
{
XMLCh* orgText = replicate(tokenizeSrc);
ArrayJanitor<XMLCh> janText(orgText);
XMLCh* tokenizeStr = orgText;
RefVectorOf<XMLCh>* tokenStack = new RefVectorOf<XMLCh>(16, true);
unsigned int len = stringLen(tokenizeStr);
unsigned int skip;
unsigned int index = 0;
while (index != len) {
// find the first non-space character
for (skip = index; skip < len; skip++)
{
if (!XMLPlatformUtils::fgTransService->isSpace(tokenizeStr[skip]))
break;
}
index = skip;
// find the delimiter (space character)
for (; skip < len; skip++)
{
if (XMLPlatformUtils::fgTransService->isSpace(tokenizeStr[skip]))
break;
}
// we reached the end of the string
if (skip == index)
break;
// these tokens are adopted in the RefVector and will be deleted
// when the vector is deleted by the caller
XMLCh* token = new XMLCh[skip+1-index];
XMLString::subString(token, tokenizeStr, index, skip);
tokenStack->addElement(token);
index = skip;
}
return tokenStack;
}
//
// This method is called when we get a notation or enumeration type attribute
// to validate. We have to confirm that the passed value to find is one of
// the values in the passed list. The list is a space separated string of
// values to match against.
//
bool XMLString::isInList(const XMLCh* const toFind, const XMLCh* const enumList)
{
//
// We loop through the values in the list via this outer loop. We end
// when we hit the end of the enum list or get a match.
//
const XMLCh* listPtr = enumList;
const unsigned int findLen = XMLString::stringLen(toFind);
while (*listPtr)
{
unsigned int testInd;
for (testInd = 0; testInd < findLen; testInd++)
{
//
// If they don't match, then reset and try again. Note that
// hitting the end of the current item will cause a mismatch
// because there can be no spaces in the toFind string.
//
if (listPtr[testInd] != toFind[testInd])
break;
}
//
// If we went the distance, see if we matched. If we did, the current
// list character has to be null or space.
//
if (testInd == findLen)
{
if ((listPtr[testInd] == chSpace) || !listPtr[testInd])
return true;
}
// Run the list pointer up to the next substring
while ((*listPtr != chSpace) && *listPtr)
listPtr++;
// If we hit the end, then we failed
if (!*listPtr)
return false;
// Else move past the space and try again
listPtr++;
}
// We never found it
return false;
}
//
// a string is whitespace:replaced, is having no
// #xD Carriage Return
// #xA Line Feed
// #x9 TAB
//
bool XMLString::isWSReplaced(const XMLCh* const toCheck)
{
// If no string, then its a OK
if (( !toCheck ) || ( !*toCheck ))
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
const XMLCh* startPtr = toCheck;
while ( *startPtr )
{
if ( ( *startPtr == chCR) ||
( *startPtr == chLF) ||
( *startPtr == chHTab))
return false;
startPtr++;
}
return true;
}
//
// to replace characters listed below to #x20
// #xD Carriage Return
// #xA Line Feed
// #x9 TAB
//
void XMLString::replaceWS(XMLCh* const toConvert)
{
int strLen = XMLString::stringLen(toConvert);
if (strLen == 0)
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
return;
XMLCh* retBuf = new XMLCh[strLen+1];
XMLCh* retPtr = &(retBuf[0]);
XMLCh* startPtr = toConvert;
while ( *startPtr )
{
if ( ( *startPtr == chCR) ||
( *startPtr == chLF) ||
( *startPtr == chHTab))
*retPtr = chSpace;
else
*retPtr = *startPtr;
retPtr++;
startPtr++;
}
retBuf[strLen] = chNull;
XMLString::moveChars(toConvert, retBuf, strLen);
delete[] retBuf;
return;
}
//
// a string is whitespace:collapsed, is whitespace::replaced
// and no
// leading space (#x20)
// trailing space
// no contiguous sequences of spaces
//
bool XMLString::isWSCollapsed(const XMLCh* const toCheck)
{
if (( !toCheck ) || ( !*toCheck ))
return true;
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
// shall be whitespace::replaced first
if ( !isWSReplaced(toCheck) )
return false;
// no leading or trailing space
if ((*toCheck == chSpace) ||
(toCheck[XMLString::stringLen(toCheck)-1] == chSpace))
return false;
const XMLCh* startPtr = toCheck;
XMLCh theChar;
bool inSpace = false;
while ( (theChar = *startPtr) != 0 )
{
if ( theChar == chSpace)
{
if (inSpace)
return false;
else
inSpace = true;
}
else
inSpace = false;
startPtr++;
}
return true;
}
//
// no leading and/or trailing spaces
// no continuous sequences of spaces
//
void XMLString::collapseWS(XMLCh* const toConvert)
{
// If no string, then its a failure
if (( !toConvert ) || ( !*toConvert ))
return;
// replace whitespace first
replaceWS(toConvert);
// remove leading spaces
const XMLCh* startPtr = toConvert;
while ( *startPtr == chSpace )
startPtr++;
if (!*startPtr)
return;
// remove trailing spaces
const XMLCh* endPtr = toConvert + stringLen(toConvert);
while (*(endPtr - 1) == chSpace)
endPtr--;
//
// Work through what remains and chop continuous spaces
//
XMLCh* retBuf = new XMLCh[endPtr - startPtr + 1];
XMLCh* retPtr = &(retBuf[0]);
bool inSpace = false;
while (startPtr < endPtr)
{
if ( *startPtr == chSpace)
{
if (inSpace)
{
//discard it;
}
else
{
inSpace = true;
*retPtr = chSpace; //copy the first chSpace
retPtr++;
}
}
else
{
inSpace = false;
*retPtr = *startPtr;
retPtr++;
}
startPtr++;
}
*retPtr = chNull;
XMLString::moveChars(toConvert, retBuf, stringLen(retBuf)+1); //copy the last chNull as well
delete[] retBuf;
return;
}
/**
* Fixes a platform dependent absolute path filename to standard URI form.
* 1. Windows: fix 'x:' to 'file:///x:' and convert any backslash to forward slash
* 2. UNIX: fix '/blah/blahblah' to 'file:///blah/blahblah'
*/
Tinny Ng
committed
void XMLString::fixURI(const XMLCh* const str, XMLCh* const target)
{
if (!str || !*str)
return;
int colonIdx = XMLString::indexOf(str, chColon);
int slashIdx = XMLString::indexOf(str, chForwardSlash);
// If starts with a '/' we assume
// this is an absolute (UNIX) file path and prefix it with file://
if (colonIdx == -1 && XMLString::indexOf(str, chForwardSlash) == 0) {
Tinny Ng
committed
unsigned index = 0;
target[index++] = chLatin_f;
target[index++] = chLatin_i;
target[index++] = chLatin_l;
target[index++] = chLatin_e;
target[index++] = chColon;
target[index++] = chForwardSlash;
target[index++] = chForwardSlash;
// copy the string
const XMLCh* inPtr = str;
while (*inPtr)
Tinny Ng
committed
target[index++] = *inPtr++;
Tinny Ng
committed
target[index] = chNull;
}
else if (colonIdx == 1 && XMLString::isAlpha(*str)) {
// If starts with a driver letter 'x:' we assume
// this is an absolute (Windows) file path and prefix it with file:///
Tinny Ng
committed
unsigned index = 0;
target[index++] = chLatin_f;
target[index++] = chLatin_i;
target[index++] = chLatin_l;
target[index++] = chLatin_e;
target[index++] = chColon;
target[index++] = chForwardSlash;
target[index++] = chForwardSlash;
target[index++] = chForwardSlash;
// copy the string and fix any backward slash
const XMLCh* inPtr = str;
while (*inPtr) {
if (*inPtr == chYenSign ||
*inPtr == chWonSign ||
*inPtr == chBackSlash)
Tinny Ng
committed
target[index++] = chForwardSlash;
else
Tinny Ng
committed
target[index++] = *inPtr;
inPtr++;
}
// cap it with null
Tinny Ng
committed
target[index] = chNull;
}
else {
// not specific case, so just copy the string over
copyString(target, str);
}
}
// ---------------------------------------------------------------------------
// XMLString: Private static methods
// ---------------------------------------------------------------------------
void XMLString::initString(XMLLCPTranscoder* const defToUse)
{
// Store away the default transcoder that we are to use
gTranscoder = defToUse;
}
void XMLString::termString()
{
// Just clean up our local code page transcoder
delete gTranscoder;
gTranscoder = 0;
}