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
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;
}
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
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);
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
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
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
}
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;
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
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;
Gareth Reakes
committed
BaseRefVectorOf<XMLCh>* XMLString::tokenizeString(const XMLCh* const tokenizeSrc)
{
XMLCh* orgText = replicate(tokenizeSrc);
ArrayJanitor<XMLCh> janText(orgText);
XMLCh* tokenizeStr = orgText;
Gareth Reakes
committed
RefArrayVectorOf<XMLCh>* tokenStack = new RefArrayVectorOf<XMLCh>(16, true);
1396
1397
1398
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
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
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
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 ))
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
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)
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
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;
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
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
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
// 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;
}
//
// remove whitespace
//
void XMLString::removeWS(XMLCh* const toConvert)
{
// If no string, then its a failure
if (( !toConvert ) || ( !*toConvert ))
return;
XMLCh* retBuf = new XMLCh[ XMLString::stringLen(toConvert) + 1];
XMLCh* retPtr = &(retBuf[0]);
XMLCh* startPtr = toConvert;
while (*startPtr)
{
if ( ( *startPtr != chCR) &&
( *startPtr != chLF) &&
( *startPtr != chHTab) &&
( *startPtr != chSpace) )
*retPtr++ = *startPtr;
}
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);
}
}
void XMLString::release(char** buf)
{
delete [] *buf;
*buf = 0;
}
void XMLString::release(XMLCh** buf)
{
delete [] *buf;
*buf = 0;
}
void XMLString::release(XMLByte** buf)
{
delete [] *buf;
*buf = 0;
}
// ---------------------------------------------------------------------------
// 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;
}