forked from itod/parsekit
/
TDXmlToken.m
209 lines (179 loc) · 6.47 KB
/
TDXmlToken.m
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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
201
202
203
204
205
206
207
208
209
// Copyright 2010 Todd Ditchendorf
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#import "TDXmlToken.h"
@interface TDXmlTokenEOF : TDXmlToken {}
@end
@implementation TDXmlTokenEOF
- (NSString *)description {
return [NSString stringWithFormat:@"<TDXmlTokenEOF %p>", self];
}
@end
@interface TDXmlToken ()
@property (nonatomic, readwrite, getter=isNone) BOOL none;
@property (nonatomic, readwrite, getter=isStartTag) BOOL startTag;
@property (nonatomic, readwrite, getter=isAttribute) BOOL attribute;
@property (nonatomic, readwrite, getter=isText) BOOL text;
@property (nonatomic, readwrite, getter=isCdata) BOOL cdata;
@property (nonatomic, readwrite, getter=isEntityRef) BOOL entityRef;
@property (nonatomic, readwrite, getter=isEntity) BOOL entity;
@property (nonatomic, readwrite, getter=isProcessingInstruction) BOOL processingInstruction;
@property (nonatomic, readwrite, getter=isComment) BOOL comment;
@property (nonatomic, readwrite, getter=isDocument) BOOL document;
@property (nonatomic, readwrite, getter=isDoctype) BOOL doctype;
@property (nonatomic, readwrite, getter=isFragment) BOOL fragment;
@property (nonatomic, readwrite, getter=isNotation) BOOL notation;
@property (nonatomic, readwrite, getter=isWhitespace) BOOL whitespace;
@property (nonatomic, readwrite, getter=isSignificantWhitespace) BOOL significantWhitespace;
@property (nonatomic, readwrite, getter=isEndTag) BOOL endTag;
@property (nonatomic, readwrite, getter=isEndEntity) BOOL endEntity;
@property (nonatomic, readwrite, getter=isXmlDecl) BOOL xmlDecl;
@property (nonatomic, readwrite, copy) NSString *stringValue;
@property (nonatomic, readwrite) TDXmlTokenType tokenType;
@property (nonatomic, readwrite, copy) id value;
@end
@implementation TDXmlToken
+ (TDXmlToken *)EOFToken {
static TDXmlToken *EOFToken = nil;
@synchronized (self) {
if (!EOFToken) {
EOFToken = [[TDXmlTokenEOF alloc] initWithTokenType:TDTT_XML_EOF stringValue:nil];
}
}
return EOFToken;
}
+ (id)tokenWithTokenType:(TDXmlTokenType)t stringValue:(NSString *)s {
return [[[self alloc] initWithTokenType:t stringValue:s] autorelease];
}
#pragma mark -
// designated initializer
- (id)initWithTokenType:(TDXmlTokenType)t stringValue:(NSString *)s {
if (self = [super init]) {
self.tokenType = t;
self.stringValue = s;
self.none = (TDTT_XML_NONE == t);
self.startTag = (TDTT_XML_START_TAG == t);
self.attribute = (TDTT_XML_ATTRIBUTE == t);
self.text = (TDTT_XML_TEXT == t);
self.cdata = (TDTT_XML_CDATA == t);
self.entityRef = (TDTT_XML_ENTITY_REF == t);
self.entity = (TDTT_XML_ENTITY == t);
self.processingInstruction = (TDTT_XML_PROCESSING_INSTRUCTION == t);
self.comment = (TDTT_XML_COMMENT == t);
self.document = (TDTT_XML_DOCUMENT == t);
self.doctype = (TDTT_XML_DOCTYPE == t);
self.fragment = (TDTT_XML_FRAGMENT == t);
self.notation = (TDTT_XML_NOTATION == t);
self.whitespace = (TDTT_XML_WHITESPACE == t);
self.significantWhitespace = (TDTT_XML_SIGNIFICANT_WHITESPACE == t);
self.endTag = (TDTT_XML_END_TAG == t);
self.endEntity = (TDTT_XML_END_ENTITY == t);
self.xmlDecl = (TDTT_XML_XML_DECL == t);
self.value = stringValue;
}
return self;
}
- (void)dealloc {
self.stringValue = nil;
self.value = nil;
[super dealloc];
}
- (NSUInteger)hash {
return [stringValue hash];
}
- (BOOL)isEqual:(id)rhv {
if (![rhv isMemberOfClass:[TDXmlToken class]]) {
return NO;
}
TDXmlToken *that = (TDXmlToken *)rhv;
if (tokenType != that.tokenType) {
return NO;
}
return [stringValue isEqualToString:that.stringValue];
}
- (BOOL)isEqualIgnoringCase:(id)rhv {
if (![rhv isMemberOfClass:[TDXmlToken class]]) {
return NO;
}
TDXmlToken *that = (TDXmlToken *)rhv;
if (tokenType != that.tokenType) {
return NO;
}
return [stringValue.lowercaseString isEqualToString:that.stringValue.lowercaseString];
}
- (NSString *)debugDescription {
NSString *typeString = nil;
if (self.isNone) {
typeString = @"None";
} else if (self.isStartTag) {
typeString = @"Start Tag";
} else if (self.isAttribute) {
typeString = @"Attribute";
} else if (self.isText) {
typeString = @"Text";
} else if (self.isCdata) {
typeString = @"CData";
} else if (self.isEntityRef) {
typeString = @"Entity Reference";
} else if (self.isEntity) {
typeString = @"Entity";
} else if (self.isProcessingInstruction) {
typeString = @"Processing Instruction";
} else if (self.isComment) {
typeString = @"Comment";
} else if (self.isDocument) {
typeString = @"Document";
} else if (self.isDoctype) {
typeString = @"Doctype";
} else if (self.isFragment) {
typeString = @"Fragment";
} else if (self.isNotation) {
typeString = @"Notation";
} else if (self.isWhitespace) {
typeString = @"Whitespace";
} else if (self.isSignificantWhitespace) {
typeString = @"Significant Whitespace";
} else if (self.isEndTag) {
typeString = @"End Tag";
} else if (self.isEndEntity) {
typeString = @"End Entity";
} else if (self.isXmlDecl) {
typeString = @"XML Declaration";
}
return [NSString stringWithFormat:@"<%@ %C%@%C>", typeString, 0x00ab, self.value, 0x00bb];
}
- (NSString *)description {
return [self debugDescription];
}
@synthesize none;
@synthesize startTag;
@synthesize attribute;
@synthesize text;
@synthesize cdata;
@synthesize entityRef;
@synthesize entity;
@synthesize processingInstruction;
@synthesize comment;
@synthesize document;
@synthesize doctype;
@synthesize fragment;
@synthesize notation;
@synthesize whitespace;
@synthesize significantWhitespace;
@synthesize endTag;
@synthesize endEntity;
@synthesize xmlDecl;
@synthesize stringValue;
@synthesize tokenType;
@synthesize value;
@end