Differences From Artifact [f6cfcd55b0]:
- File src/OFXMLParser.m — part of check-in [469a496cd2] at 2017-05-08 21:37:11 on branch trunk — Add OF_DESIGNATED_INITIALIZER & OF_METHOD_FAMILY (user: js, size: 24012) [annotate] [blame] [check-ins using]
To Artifact [9b8cdb5bf4]:
- File
src/OFXMLParser.m
— part of check-in
[6b77a5dd8b]
at
2017-05-21 21:28:57
on branch trunk
— Prefix private methods with of_ instead of OF_
This matches Apple's style. (user: js, size: 24012) [annotate] [blame] [check-ins using]
| ︙ | ︙ | |||
143 144 145 146 147 148 149 |
@implementation OFXMLParser
@synthesize delegate = _delegate, depthLimit = _depthLimit;
+ (void)initialize
{
const SEL selectors_[OF_XMLPARSER_NUM_STATES] = {
| | | | | | | | | | | | | | | | | | | | | | 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 |
@implementation OFXMLParser
@synthesize delegate = _delegate, depthLimit = _depthLimit;
+ (void)initialize
{
const SEL selectors_[OF_XMLPARSER_NUM_STATES] = {
@selector(of_inByteOrderMarkState),
@selector(of_outsideTagState),
@selector(of_tagOpenedState),
@selector(of_inProcessingInstructionsState),
@selector(of_inTagNameState),
@selector(of_inCloseTagNameState),
@selector(of_inTagState),
@selector(of_inAttributeNameState),
@selector(of_expectAttributeEqualSignState),
@selector(of_expectAttributeDelimiterState),
@selector(of_inAttributeValueState),
@selector(of_expectTagCloseState),
@selector(of_expectSpaceOrTagCloseState),
@selector(of_inExclamationMarkState),
@selector(of_inCDATAOpeningState),
@selector(of_inCDATAState),
@selector(of_inCommentOpeningState),
@selector(of_inCommentState1),
@selector(of_inCommentState2),
@selector(of_inDOCTYPEState)
};
memcpy(selectors, selectors_, sizeof(selectors_));
for (size_t i = 0; i < OF_XMLPARSER_NUM_STATES; i++) {
if (![self instancesRespondToSelector: selectors[i]])
@throw [OFInitializationFailedException
exceptionWithClass: self];
|
| ︙ | ︙ | |||
298 299 300 301 302 303 304 | /* * The following methods handle the different states of the parser. They are * looked up in +[initialize] and put in a lookup table to speed things up. * One dispatch for every character would be way too slow! */ | | | | 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 |
/*
* The following methods handle the different states of the parser. They are
* looked up in +[initialize] and put in a lookup table to speed things up.
* One dispatch for every character would be way too slow!
*/
- (void)of_inByteOrderMarkState
{
if (_data[_i] != "\xEF\xBB\xBF"[_level]) {
if (_level == 0) {
_state = OF_XMLPARSER_OUTSIDE_TAG;
_i--;
return;
}
@throw [OFMalformedXMLException exceptionWithParser: self];
}
if (_level++ == 2)
_state = OF_XMLPARSER_OUTSIDE_TAG;
_last = _i + 1;
}
/* Not in a tag */
- (void)of_outsideTagState
{
size_t length;
if ((_finishedParsing || [_previous count] < 1) && _data[_i] != ' ' &&
_data[_i] != '\t' && _data[_i] != '\n' && _data[_i] != '\r' &&
_data[_i] != '<')
@throw [OFMalformedXMLException exceptionWithParser: self];
|
| ︙ | ︙ | |||
351 352 353 354 355 356 357 | [_buffer removeAllItems]; _last = _i + 1; _state = OF_XMLPARSER_TAG_OPENED; } /* Tag was just opened */ | | | 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 |
[_buffer removeAllItems];
_last = _i + 1;
_state = OF_XMLPARSER_TAG_OPENED;
}
/* Tag was just opened */
- (void)of_tagOpenedState
{
if (_finishedParsing && _data[_i] != '!' && _data[_i] != '?')
@throw [OFMalformedXMLException exceptionWithParser: self];
switch (_data[_i]) {
case '?':
_last = _i + 1;
|
| ︙ | ︙ | |||
385 386 387 388 389 390 391 | _acceptProlog = false; _i--; break; } } /* <?xml […]?> */ | | | 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 |
_acceptProlog = false;
_i--;
break;
}
}
/* <?xml […]?> */
- (bool)of_parseXMLProcessingInstructions: (OFString *)pi
{
const char *cString;
size_t length, last;
int PIState = 0;
OFString *attribute = nil;
OFMutableString *value = nil;
char piDelimiter = 0;
|
| ︙ | ︙ | |||
473 474 475 476 477 478 479 | if (PIState != 0 || !hasVersion) return false; return true; } /* Inside processing instructions */ | | | | | 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 |
if (PIState != 0 || !hasVersion)
return false;
return true;
}
/* Inside processing instructions */
- (void)of_inProcessingInstructionsState
{
if (_data[_i] == '?')
_level = 1;
else if (_level == 1 && _data[_i] == '>') {
void *pool = objc_autoreleasePoolPush();
OFString *PI;
appendToBuffer(_buffer, _data + _last, _encoding, _i - _last);
PI = transformString(self, _buffer, 1, false);
if ([PI isEqual: @"xml"] || [PI hasPrefix: @"xml "] ||
[PI hasPrefix: @"xml\t"] || [PI hasPrefix: @"xml\r"] ||
[PI hasPrefix: @"xml\n"])
if (![self of_parseXMLProcessingInstructions: PI])
@throw [OFMalformedXMLException
exceptionWithParser: self];
if ([_delegate respondsToSelector:
@selector(parser:foundProcessingInstructions:)])
[_delegate parser: self
foundProcessingInstructions: PI];
objc_autoreleasePoolPop(pool);
[_buffer removeAllItems];
_last = _i + 1;
_state = OF_XMLPARSER_OUTSIDE_TAG;
} else
_level = 0;
}
/* Inside a tag, no name yet */
- (void)of_inTagNameState
{
void *pool;
const char *bufferCString, *tmp;
size_t length, bufferLength;
OFString *bufferString;
if (_data[_i] != ' ' && _data[_i] != '\t' && _data[_i] != '\n' &&
|
| ︙ | ︙ | |||
592 593 594 595 596 597 598 | objc_autoreleasePoolPop(pool); [_buffer removeAllItems]; _last = _i + 1; } /* Inside a close tag, no name yet */ | | | 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 |
objc_autoreleasePoolPop(pool);
[_buffer removeAllItems];
_last = _i + 1;
}
/* Inside a close tag, no name yet */
- (void)of_inCloseTagNameState
{
void *pool;
const char *bufferCString, *tmp;
size_t length, bufferLength;
OFString *bufferString, *namespace;
if (_data[_i] != ' ' && _data[_i] != '\t' && _data[_i] != '\n' &&
|
| ︙ | ︙ | |||
662 663 664 665 666 667 668 | : OF_XMLPARSER_EXPECT_SPACE_OR_TAG_CLOSE); if ([_previous count] == 0) _finishedParsing = true; } /* Inside a tag, name found */ | | | 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 |
: OF_XMLPARSER_EXPECT_SPACE_OR_TAG_CLOSE);
if ([_previous count] == 0)
_finishedParsing = true;
}
/* Inside a tag, name found */
- (void)of_inTagState
{
void *pool;
OFString *namespace;
OFXMLAttribute *const *attributesObjects;
size_t attributesCount;
if (_data[_i] != '>' && _data[_i] != '/') {
|
| ︙ | ︙ | |||
736 737 738 739 740 741 742 | _last = _i + 1; _state = (_data[_i] == '/' ? OF_XMLPARSER_EXPECT_TAG_CLOSE : OF_XMLPARSER_OUTSIDE_TAG); } /* Looking for attribute name */ | | | 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 |
_last = _i + 1;
_state = (_data[_i] == '/'
? OF_XMLPARSER_EXPECT_TAG_CLOSE
: OF_XMLPARSER_OUTSIDE_TAG);
}
/* Looking for attribute name */
- (void)of_inAttributeNameState
{
void *pool;
OFString *bufferString;
const char *bufferCString, *tmp;
size_t length, bufferLength;
if (_data[_i] != '=' && _data[_i] != ' ' && _data[_i] != '\t' &&
|
| ︙ | ︙ | |||
782 783 784 785 786 787 788 | _last = _i + 1; _state = (_data[_i] == '=' ? OF_XMLPARSER_EXPECT_ATTRIBUTE_DELIMITER : OF_XMLPARSER_EXPECT_ATTRIBUTE_EQUAL_SIGN); } /* Expecting equal sign of an attribute */ | | | | | 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 |
_last = _i + 1;
_state = (_data[_i] == '='
? OF_XMLPARSER_EXPECT_ATTRIBUTE_DELIMITER
: OF_XMLPARSER_EXPECT_ATTRIBUTE_EQUAL_SIGN);
}
/* Expecting equal sign of an attribute */
- (void)of_expectAttributeEqualSignState
{
if (_data[_i] == '=') {
_last = _i + 1;
_state = OF_XMLPARSER_EXPECT_ATTRIBUTE_DELIMITER;
return;
}
if (_data[_i] != ' ' && _data[_i] != '\t' && _data[_i] != '\n' &&
_data[_i] != '\r')
@throw [OFMalformedXMLException exceptionWithParser: self];
}
/* Expecting name/value delimiter of an attribute */
- (void)of_expectAttributeDelimiterState
{
_last = _i + 1;
if (_data[_i] == ' ' || _data[_i] == '\t' || _data[_i] == '\n' ||
_data[_i] == '\r')
return;
if (_data[_i] != '\'' && _data[_i] != '"')
@throw [OFMalformedXMLException exceptionWithParser: self];
_delimiter = _data[_i];
_state = OF_XMLPARSER_IN_ATTRIBUTE_VALUE;
}
/* Looking for attribute value */
- (void)of_inAttributeValueState
{
void *pool;
OFString *attributeValue;
size_t length;
if (_data[_i] != _delimiter)
return;
|
| ︙ | ︙ | |||
851 852 853 854 855 856 857 | _attributeName = _attributePrefix = nil; _last = _i + 1; _state = OF_XMLPARSER_IN_TAG; } /* Expecting closing '>' */ | | | | | | | 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 |
_attributeName = _attributePrefix = nil;
_last = _i + 1;
_state = OF_XMLPARSER_IN_TAG;
}
/* Expecting closing '>' */
- (void)of_expectTagCloseState
{
if (_data[_i] == '>') {
_last = _i + 1;
_state = OF_XMLPARSER_OUTSIDE_TAG;
} else
@throw [OFMalformedXMLException exceptionWithParser: self];
}
/* Expecting closing '>' or space */
- (void)of_expectSpaceOrTagCloseState
{
if (_data[_i] == '>') {
_last = _i + 1;
_state = OF_XMLPARSER_OUTSIDE_TAG;
} else if (_data[_i] != ' ' && _data[_i] != '\t' &&
_data[_i] != '\n' && _data[_i] != '\r')
@throw [OFMalformedXMLException exceptionWithParser: self];
}
/* In <! */
- (void)of_inExclamationMarkState
{
if (_finishedParsing && _data[_i] != '-')
@throw [OFMalformedXMLException exceptionWithParser: self];
if (_data[_i] == '-')
_state = OF_XMLPARSER_IN_COMMENT_OPENING;
else if (_data[_i] == '[') {
_state = OF_XMLPARSER_IN_CDATA_OPENING;
_level = 0;
} else if (_data[_i] == 'D') {
_state = OF_XMLPARSER_IN_DOCTYPE;
_level = 0;
} else
@throw [OFMalformedXMLException exceptionWithParser: self];
_last = _i + 1;
}
/* CDATA */
- (void)of_inCDATAOpeningState
{
if (_data[_i] != "CDATA["[_level])
@throw [OFMalformedXMLException exceptionWithParser: self];
if (++_level == 6) {
_state = OF_XMLPARSER_IN_CDATA;
_level = 0;
}
_last = _i + 1;
}
- (void)of_inCDATAState
{
if (_data[_i] == ']')
_level++;
else if (_data[_i] == '>' && _level >= 2) {
void *pool = objc_autoreleasePoolPush();
OFString *CDATA;
|
| ︙ | ︙ | |||
933 934 935 936 937 938 939 | _last = _i + 1; _state = OF_XMLPARSER_OUTSIDE_TAG; } else _level = 0; } /* Comment */ | | | | | 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 |
_last = _i + 1;
_state = OF_XMLPARSER_OUTSIDE_TAG;
} else
_level = 0;
}
/* Comment */
- (void)of_inCommentOpeningState
{
if (_data[_i] != '-')
@throw [OFMalformedXMLException exceptionWithParser: self];
_last = _i + 1;
_state = OF_XMLPARSER_IN_COMMENT_1;
_level = 0;
}
- (void)of_inCommentState1
{
if (_data[_i] == '-')
_level++;
else
_level = 0;
if (_level == 2)
_state = OF_XMLPARSER_IN_COMMENT_2;
}
- (void)of_inCommentState2
{
void *pool;
OFString *comment;
if (_data[_i] != '>')
@throw [OFMalformedXMLException exceptionWithParser: self];
|
| ︙ | ︙ | |||
980 981 982 983 984 985 986 | [_buffer removeAllItems]; _last = _i + 1; _state = OF_XMLPARSER_OUTSIDE_TAG; } /* In <!DOCTYPE ...> */ | | | 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 |
[_buffer removeAllItems];
_last = _i + 1;
_state = OF_XMLPARSER_OUTSIDE_TAG;
}
/* In <!DOCTYPE ...> */
- (void)of_inDOCTYPEState
{
if ((_level < 6 && _data[_i] != "OCTYPE"[_level]) ||
(_level == 6 && _data[_i] != ' ' && _data[_i] != '\t' &&
_data[_i] != '\n' && _data[_i] != '\r'))
@throw [OFMalformedXMLException exceptionWithParser: self];
_level++;
|
| ︙ | ︙ |