Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Xerces" and "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact apache\@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation, and was
* originally based on software copyright (c) 1999, International
* Business Machines, Inc., http://www.ibm.com . For more information
* on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
/*
* $Id$
*/
// ---------------------------------------------------------------------------
// Includes
// ---------------------------------------------------------------------------
#include <string.h>
#include <ctype.h>
#include <xercesc/util/ArrayIndexOutOfBoundsException.hpp>
#include <xercesc/util/IllegalArgumentException.hpp>
#include <xercesc/util/NumberFormatException.hpp>
#include <xercesc/util/Janitor.hpp>
#include <xercesc/util/PlatformUtils.hpp>
#include <xercesc/util/RuntimeException.hpp>
#include <xercesc/util/TransService.hpp>
#include <xercesc/util/TranscodingException.hpp>
#include <xercesc/util/ValueArrayOf.hpp>
#include <xercesc/util/XMLString.hpp>
#include <xercesc/util/XMLUniDefs.hpp>
#include <xercesc/util/XMLUni.hpp>
#include <xercesc/internal/XMLReader.hpp>
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// ---------------------------------------------------------------------------
// Local static data
//
// gConverter
// This is initialized when the user calls the platform init method,
// which calls our init method. This is the converter used for default
// conversion to/from the local code page.
// ---------------------------------------------------------------------------
static XMLLCPTranscoder* gTranscoder = 0;
static XMLCh gNullStr[] =
{
chOpenCurly, chLatin_n, chLatin_u, chLatin_l, chLatin_l, chCloseCurly, chNull
};
// ---------------------------------------------------------------------------
// XMLString: Public static methods
// ---------------------------------------------------------------------------
void XMLString::binToText( const unsigned long toFormat
, char* const toFill
, const unsigned int maxChars
, const unsigned int radix)
{
static const char digitList[16] =
{
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
, 'A', 'B', 'C', 'D', 'E', 'F'
};
if (!maxChars)
ThrowXML(IllegalArgumentException, XMLExcepts::Str_ZeroSizedTargetBuf);
// Handle special case
if (!toFormat)
{
toFill[0] = '0';
toFill[1] = 0;
return;
}
// This is used to fill the temp buffer
unsigned int tmpIndex = 0;
// A copy of the conversion value that we can modify
unsigned int tmpVal = toFormat;
//
// Convert into a temp buffer that we know is large enough. This avoids
// having to check for overflow in the inner loops, and we have to flip
// the resulting XMLString anyway.
//
char tmpBuf[128];
//
// For each radix, do the optimal thing. For bin and hex, we can special
// case them and do shift and mask oriented stuff. For oct and decimal
// there isn't much to do but bull through it with divides.
//
if (radix == 2)
{
while (tmpVal)
{
if (tmpVal & 0x1UL)
tmpBuf[tmpIndex++] = '1';
else
tmpBuf[tmpIndex++] = '0';
tmpVal >>= 1;
}
}
else if (radix == 16)
{
while (tmpVal)
{
const unsigned int charInd = (tmpVal & 0xFUL);
tmpBuf[tmpIndex++] = digitList[charInd];
tmpVal >>= 4;
}
}
else if ((radix == 8) || (radix == 10))
{
while (tmpVal)
{
const unsigned int charInd = (tmpVal % radix);
tmpBuf[tmpIndex++] = digitList[charInd];
tmpVal /= radix;
}
}
else
{
ThrowXML(RuntimeException, XMLExcepts::Str_UnknownRadix);
}
// See if have enough room in the caller's buffer
if (tmpIndex > maxChars)
ThrowXML(IllegalArgumentException, XMLExcepts::Str_TargetBufTooSmall);
// Reverse the tmp buffer into the caller's buffer
unsigned int outIndex = 0;
for (; tmpIndex > 0; tmpIndex--)
toFill[outIndex++] = tmpBuf[tmpIndex-1];
// And cap off the caller's buffer
toFill[outIndex] = char(0);
}
void XMLString::binToText( const unsigned int toFormat
, char* const toFill
, const unsigned int maxChars
, const unsigned int radix)
{
// Just call the unsigned long version
binToText((unsigned long)toFormat, toFill, maxChars, radix);
}
void XMLString::binToText( const long toFormat
, char* const toFill
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
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
1168
1169
1170
1171
1172
1173
1174
1175
1176
, const unsigned int maxChars
, const unsigned int radix)
{
//
// If its negative, then put a negative sign into the output and flip
// the sign of the local temp value.
//
unsigned int startInd = 0;
unsigned long actualVal;
if (toFormat < 0)
{
toFill[0] = '-';
startInd++;
actualVal = (unsigned long)(toFormat * -1);
}
else
{
actualVal = (unsigned long)(toFormat);
}
// And now call the unsigned long version
binToText(actualVal, &toFill[startInd], maxChars, radix);
}
void XMLString::binToText( const int toFormat
, char* const toFill
, const unsigned int maxChars
, const unsigned int radix)
{
//
// If its negative, then put a negative sign into the output and flip
// the sign of the local temp value.
//
unsigned int startInd = 0;
unsigned long actualVal;
if (toFormat < 0)
{
toFill[0] = '-';
startInd++;
actualVal = (unsigned long)(toFormat * -1);
}
else
{
actualVal = (unsigned long)(toFormat);
}
// And now call the unsigned long version
binToText(actualVal, &toFill[startInd], maxChars, radix);
}
void XMLString::catString(char* const target, const char* const src)
{
strcat(target, src);
}
int XMLString::compareIString(const char* const str1, const char* const str2)
{
return stricmp(str1, str2);
}
int XMLString::compareNString( const char* const str1
, const char* const str2
, const unsigned int count)
{
// Watch for pathological secenario
if (!count)
return 0;
return strncmp(str1, str2, count);
}
int XMLString::compareNIString( const char* const str1
, const char* const str2
, const unsigned int count)
{
if (!count)
return 0;
return strnicmp(str1, str2, count);
}
int XMLString::compareString( const char* const str1
, const char* const str2)
{
return strcmp(str1, str2);
}
void XMLString::copyString( char* const target
, const char* const src)
{
strcpy(target, src);
}
void XMLString::cut( XMLCh* const toCutFrom
, const unsigned int count)
{
#if defined(XML_DEBUG)
if (count > stringLen(toCutFrom))
{
// <TBD> This is bad of course
}
#endif
// If count is zero, then nothing to do
if (!count)
return;
XMLCh* targetPtr = toCutFrom;
XMLCh* srcPtr = toCutFrom + count;
while (*srcPtr)
*targetPtr++ = *srcPtr++;
// Cap it off at the new end
*targetPtr = 0;
}
unsigned int XMLString::hash( const char* const tohash
, const unsigned int hashModulus)
{
if (!hashModulus)
ThrowXML(IllegalArgumentException, XMLExcepts::Pool_ZeroModulus);
unsigned int hashVal = 0;
if (tohash) {
const char* curCh = tohash;
while (*curCh)
{
unsigned int top = hashVal >> 24;
hashVal += (hashVal * 37) + top + (unsigned int)(*curCh);
curCh++;
}
}
// Divide by modulus
return hashVal % hashModulus;
}
int XMLString::indexOf(const char* const toSearch, const char ch)
{
const unsigned int len = strlen(toSearch);
for (unsigned int i = 0; i < len; i++)
{
if (toSearch[i] == ch)
return i;
}
return -1;
}
int XMLString::indexOf( const char* const toSearch
, const char ch
, const unsigned int fromIndex)
{
const unsigned int len = strlen(toSearch);
// Make sure the start index is within the XMLString bounds
if ((int)fromIndex > len-1)
ThrowXML(ArrayIndexOutOfBoundsException, XMLExcepts::Str_StartIndexPastEnd);
for (unsigned int i = fromIndex; i < len; i++)
{
if (toSearch[i] == ch)
return i;
}
return -1;
}
int XMLString::lastIndexOf(const char* const toSearch, const char ch)
{
const int len = strlen(toSearch);
for (int i = len-1; i >= 0; i--)
{
if (toSearch[i] == ch)
return i;
}
return -1;
}
int XMLString::lastIndexOf( const char* const toSearch
, const char ch
, const unsigned int fromIndex)
{
const int len = strlen(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 >= 0; i--)
{
if (toSearch[i] == ch)
return i;
}
return -1;
}
unsigned int XMLString::replaceTokens( XMLCh* const errText
, const unsigned int maxChars
, const XMLCh* const text1
, const XMLCh* const text2
, const XMLCh* const text3
, const XMLCh* const text4)
{
//
// We have to build the string back into the source string, so allocate
// a temp string and copy the orignal text to it. We'll then treat the
// incoming buffer as a target buffer. Put a janitor on it to make sure
// it gets cleaned up.
//
XMLCh* orgText = replicate(errText);
ArrayJanitor<XMLCh> janText(orgText);
XMLCh* pszSrc = orgText;
unsigned int curOutInd = 0;
while (*pszSrc && (curOutInd < maxChars))
{
//
// Loop until we see a { character. Until we do, just copy chars
// from src to target, being sure not to overrun the output buffer.
//
while ((*pszSrc != chOpenCurly) && (curOutInd < maxChars))
{
if (!*pszSrc)
break;
errText[curOutInd++] = *pszSrc++;
}
// If we did not find a curly, then we are done
if (*pszSrc != chOpenCurly)
break;
//
// Probe this one to see if it matches our pattern of {x}. If not
// then copy over those chars and go back to the first loop.
//
if ((*(pszSrc+1) >= chDigit_0)
&& (*(pszSrc+1) <= chDigit_3)
&& (*(pszSrc+2) == chCloseCurly))
{
//
// Its one of our guys, so move the source pointer up past the
// token we are replacing. First though get out the token number
// character.
//
XMLCh tokCh = *(pszSrc+1);
pszSrc += 3;
// Now copy over the replacement text
const XMLCh* repText = 0;
if (tokCh == chDigit_0)
repText = text1;
else if (tokCh == chDigit_1)
repText = text2;
else if (tokCh == chDigit_2)
repText = text3;
else if (tokCh == chDigit_3)
repText = text4;
// If this one is null, copy over a null string
if (!repText)
repText = gNullStr;
while (*repText && (curOutInd < maxChars))
errText[curOutInd++] = *repText++;
}
else
{
// Escape the curly brace character and continue
errText[curOutInd++] = *pszSrc++;
}
}
// Copy over a null terminator
errText[curOutInd] = 0;
// And return the count of chars we output
return curOutInd;
}
char* XMLString::replicate(const char* const toRep)
{
// If a null string, return a null string
if (!toRep)
return 0;
//
// Get the len of the source and allocate a new buffer. Make sure to
// account for the nul terminator.
//
const unsigned int srcLen = strlen(toRep);
char* ret = new char[srcLen+1];
// Copy over the text, adjusting for the size of a char
memcpy(ret, toRep, (srcLen+1) * sizeof(char));
return ret;
}
bool XMLString::startsWith(const char* const toTest, const char* const prefix)
{
return (strncmp(toTest, prefix, strlen(prefix)) == 0);
}
bool XMLString::startsWithI(const char* const toTest
, const char* const prefix)
{
return (strnicmp(toTest, prefix, strlen(prefix)) == 0);
}
unsigned int XMLString::stringLen(const char* const src)
{
return strlen(src);
}
char* XMLString::transcode(const XMLCh* const toTranscode)
{
return gTranscoder->transcode(toTranscode);
}
bool XMLString::transcode( const XMLCh* const toTranscode
, char* const toFill
, const unsigned int maxChars)
{
if (!gTranscoder->transcode(toTranscode, toFill, maxChars))
return false;
return true;
}
XMLCh* XMLString::transcode(const char* const toTranscode)
{
return gTranscoder->transcode(toTranscode);
}
bool XMLString::transcode( const char* const toTranscode
, XMLCh* const toFill
, const unsigned int maxChars)
{
if (!gTranscoder->transcode(toTranscode, toFill, maxChars))
return false;
return true;
}
void XMLString::trim(char* const toTrim)
{
const unsigned int len = strlen(toTrim);
unsigned int skip, scrape;
for (skip = 0; skip < len; skip++)
{
if (! isspace(toTrim[skip]))
break;
}
for (scrape = len; scrape > skip; scrape--)
{
if (! 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::subString(char* const targetStr, const char* 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 = strlen(srcStr);
const int copySize = endIndex - startIndex;
// Make sure the start index is within the XMLString bounds
if (startIndex > srcLen-1 || endIndex > srcLen )
ThrowXML(ArrayIndexOutOfBoundsException, XMLExcepts::Str_StartIndexPastEnd);
for (int i= startIndex; i < endIndex; i++) {
targetStr[i-startIndex] = srcStr[i];
}
targetStr[copySize] = 0;
}
/**
* isValidNCName
*
* NCName::= (Letter | '_') (NCNameChar)*
* NCNameChar ::= Letter | Digit | '.' | '-' | '_'
* | CombiningChar | Extender
*/
bool XMLString::isValidNCName(const XMLCh* const name) {
if (XMLString::stringLen(name) == 0
|| XMLString::indexOf(name, chColon) != -1) {
return false;
}
const XMLCh* tempName = name;
XMLCh firstChar = *tempName++;
if (!XMLReader::isXMLLetter(firstChar) && firstChar != chUnderscore) {
return false;
}
while(*tempName) {
if (*tempName == chColon || !XMLReader::isNameChar(*tempName++)) {
return false;
}
}
return true;
}
/**
* isValidName
*
* Name::= (Letter | '_' | ':') (NameChar)*
* NameChar ::= Letter | Digit | '.' | '-' | '_' | ':'
* | CombiningChar | Extender
*/
bool XMLString::isValidName(const XMLCh* const name) {
if (!name ||
(XMLString::stringLen(name) == 0))
return false;
const XMLCh* tempName = name;
XMLCh firstChar = *tempName++;
if (!XMLReader::isXMLLetter(firstChar) &&
(firstChar != chUnderscore) &&
(firstChar != chColon) )
return false;
while(*tempName)
if (!XMLReader::isNameChar(*tempName++))
return false;
return true;
}
/**
* isValidEncName
*
* [80] EncName ::= [A-Za-z] ([A-Za-z0-9._] | '-')*
*
*/
bool XMLString::isValidEncName(const XMLCh* const name)
{
if ( ( !name) ||
( XMLString::stringLen(name) == 0 ))
return false;
const XMLCh* tempName = name;
XMLCh firstChar = *tempName++;
if (!isAlpha(firstChar))
return false;
while(*tempName)
{
if (( !isAlpha(*tempName)) &&
( !isDigit(*tempName)) &&
( *tempName != chPeriod) &&
( *tempName != chUnderscore) &&
( *tempName != chDash) )
return false;
tempName++;
}
return true;
}
/**
* isValidQName
*
* [6] QName ::= (Prefix ':')? LocalPart
* [7] Prefix ::= NCName
* [8] LocalPart ::= NCName
*
*/
bool XMLString::isValidQName(const XMLCh* const name)
{
if (!name)
return false;
int strLen = XMLString::stringLen(name);
if (strLen == 0)
return false;
int colonPos = XMLString::indexOf(name, chColon);
if ((colonPos == 0) || // ":abcd"
(colonPos == strLen-1)) // "abcd:"
return false;
//
// prefix
//
if (colonPos != -1)
{
XMLCh *prefix = new XMLCh[colonPos+1];
XMLString::subString(prefix, name, 0, colonPos);
ArrayJanitor<XMLCh> janName(prefix);
if (XMLString::isValidNCName(prefix)==false)
return false;
}
//
// LocalPart
//
return XMLString::isValidNCName(name+colonPos+1);
}
bool XMLString::isAlpha(XMLCh const theChar)
{
if ((( theChar >= chLatin_a ) && ( theChar <= chLatin_z )) ||
(( theChar >= chLatin_A ) && ( theChar <= chLatin_Z )) )
return true;
return false;
}
bool XMLString::isDigit(XMLCh const theChar)
{
if (( theChar >= chDigit_0 ) && ( theChar <= chDigit_9 ))
return true;
return false;
}
bool XMLString::isAlphaNum(XMLCh const theChar)
{
return (isAlpha(theChar) || isDigit(theChar));
}
bool XMLString::isHex(XMLCh const theChar)
{
return (isDigit(theChar) ||
(theChar >= chLatin_a && theChar <= chLatin_f) ||
(theChar >= chLatin_A && theChar <= chLatin_F));
}
bool XMLString::isAllWhiteSpace(const XMLCh* const toCheck)
{
if ( !toCheck )
return true;
const XMLCh* startPtr = toCheck;
while (*startPtr)
{
if (!XMLPlatformUtils::fgTransService->isSpace(*startPtr))
return false;
startPtr++;
}
return true;
}
// ---------------------------------------------------------------------------
// Wide char versions of most of the string methods
// ---------------------------------------------------------------------------
void XMLString::binToText( const unsigned long toFormat
, XMLCh* const toFill
, const unsigned int maxChars
, const unsigned int radix)
{
static const XMLCh digitList[16] =
{
chDigit_0, chDigit_1, chDigit_2, chDigit_3, chDigit_4, chDigit_5
, chDigit_6, chDigit_7, chDigit_8, chDigit_9, chLatin_A, chLatin_B
, chLatin_C, chLatin_D, chLatin_e, chLatin_F
};
if (!maxChars)
ThrowXML(IllegalArgumentException, XMLExcepts::Str_ZeroSizedTargetBuf);
// Handle special case
if (!toFormat)
{
toFill[0] = chDigit_0;
toFill[1] = chNull;
return;
}
// This is used to fill the temp buffer
unsigned int tmpIndex = 0;
// A copy of the conversion value that we can modify
unsigned int tmpVal = toFormat;
//
// Convert into a temp buffer that we know is large enough. This avoids
// having to check for overflow in the inner loops, and we have to flip
// the resulting sring anyway.
//
XMLCh tmpBuf[128];
//
// For each radix, do the optimal thing. For bin and hex, we can special
// case them and do shift and mask oriented stuff. For oct and decimal
// there isn't much to do but bull through it with divides.
//
if (radix == 2)
{
while (tmpVal)
{
if (tmpVal & 0x1UL)
tmpBuf[tmpIndex++] = chDigit_1;
else
tmpBuf[tmpIndex++] = chDigit_0;
tmpVal >>= 1;
}
}
else if (radix == 16)
{
while (tmpVal)
{
const unsigned int charInd = (tmpVal & 0xFUL);
tmpBuf[tmpIndex++] = digitList[charInd];
tmpVal >>= 4;
}
}
else if ((radix == 8) || (radix == 10))
{
while (tmpVal)
{
const unsigned int charInd = (tmpVal % radix);
tmpBuf[tmpIndex++] = digitList[charInd];
tmpVal /= radix;
}
}
else
{
ThrowXML(RuntimeException, XMLExcepts::Str_UnknownRadix);
}
// See if have enough room in the caller's buffer
if (tmpIndex > maxChars)
ThrowXML(IllegalArgumentException, XMLExcepts::Str_TargetBufTooSmall);
// Reverse the tmp buffer into the caller's buffer
unsigned int outIndex = 0;
for (; tmpIndex > 0; tmpIndex--)
toFill[outIndex++] = tmpBuf[tmpIndex-1];
// And cap off the caller's buffer
toFill[outIndex] = chNull;
}
void XMLString::binToText( const unsigned int toFormat
, XMLCh* const toFill
, const unsigned int maxChars
, const unsigned int radix)
{
// Just call the unsigned long version
binToText((unsigned long)toFormat, toFill, maxChars, radix);
}
void XMLString::binToText( const long toFormat
, XMLCh* const toFill
, const unsigned int maxChars
, const unsigned int radix)
{
//
// If its negative, then put a negative sign into the output and flip
// the sign of the local temp value.
//
unsigned int startInd = 0;
unsigned long actualVal;
if (toFormat < 0)
{
toFill[0] = chDash;
startInd++;
actualVal = (unsigned long)(toFormat * -1);
}
else
{
actualVal = (unsigned long)(toFormat);
}
// And now call the unsigned long version
binToText(actualVal, &toFill[startInd], maxChars, radix);
}
void XMLString::binToText( const int toFormat
, XMLCh* const toFill
, const unsigned int maxChars
, const unsigned int radix)
{
//
// If its negative, then put a negative sign into the output and flip
// the sign of the local temp value.
//
unsigned int startInd = 0;
unsigned long actualVal;
if (toFormat < 0)
{
toFill[0] = chDash;
startInd++;
actualVal = (unsigned long)(toFormat * -1);
}
else
{
actualVal = (unsigned long)(toFormat);
}
// And now call the unsigned long version
binToText(actualVal, &toFill[startInd], maxChars, radix);
}
void XMLString::catString(XMLCh* const target, const XMLCh* const src)
{
// Get the starting point for the cat on the target XMLString
unsigned int index = stringLen(target);
// While the source is not zero, add them to target and bump
const XMLCh* pszTmp = src;
while (*pszTmp)
target[index++] = *pszTmp++;
// Cap off the target where we ended
target[index] = chNull;
}
int XMLString::compareIString( const XMLCh* const str1
, const XMLCh* const str2)
{
// Refer this one to the transcoding service
return XMLPlatformUtils::fgTransService->compareIString(str1, str2);
}
int XMLString::compareNString( const XMLCh* const str1
, const XMLCh* const str2
, const unsigned int maxChars)
{
const XMLCh* psz1 = str1;
const XMLCh* psz2 = str2;
unsigned int curCount = 0;
while (true)
{
// If an inequality, then return difference
if (*psz1 != *psz2)
return int(*psz1) - int(*psz2);
// If either ended, then both ended, so equal
if (!*psz1 || !*psz2)
break;
// Move upwards to next chars
psz1++;
psz2++;
//
// Bump the count of chars done. If it equals the max then we are
// equal for the requested count, so break out and return equal.
//
curCount++;
if (curCount == maxChars)
break;
}
return 0;
}
int XMLString::compareNIString( const XMLCh* const str1
, const XMLCh* const str2
, const unsigned int maxChars)
{
// 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
, const XMLCh* const pattern)
{
if (!toSearch || !pattern )
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;
}
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
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
1325
1326
1327
1328
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 unsigned int len = stringLen(toSearch);
for (unsigned int i = 0; i < len; i++)
{
if (toSearch[i] == ch)
return i;
}
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;
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
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);
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
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
1576
1577
1578
1579
1580
}
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 > srcLen-1 || endIndex > srcLen )
ThrowXML(ArrayIndexOutOfBoundsException, XMLExcepts::Str_StartIndexPastEnd);
for (int i= startIndex; i < endIndex; i++) {
targetStr[i-startIndex] = srcStr[i];
}
targetStr[copySize] = 0;
}
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 ))
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
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)
{
// If no string, then its a failure
if (( !toConvert ) || ( !*toConvert ))
return;
int strLen = XMLString::stringLen(toConvert);
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;
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
1734
1735
1736
1737
1738
1739
// 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;
}