My favorites
▼
|
Sign in
touchcode
Repository of iPhone and iPod Touch source code.
Project Home
Export to GitHub
READ-ONLY: This project has been
archived
. For more information see
this post
.
Search
Search within:
All issues
Open issues
New issues
Issues to verify
for
Advanced search
Search tips
Subscriptions
Issue
12
attachment: XMLString.patch
(7.9 KB)
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
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
Index: CXMLNode.h
===================================================================
--- CXMLNode.h (revision 78)
+++ CXMLNode.h (working copy)
@@ -12,6 +12,7 @@
typedef enum {
CXMLInvalidKind = 0,
+ CXMLElementKind = XML_ELEMENT_NODE,
CXMLAttributeKind = XML_ATTRIBUTE_NODE,
CXMLTextKind = XML_TEXT_NODE,
CXMLProcessingInstructionKind = XML_PI_NODE,
@@ -53,9 +54,10 @@
//+ (NSString *)prefixForName:(NSString *)name;
//+ (CXMLNode *)predefinedNamespaceForPrefix:(NSString *)name;
- (NSString *)description;
-//- (NSString *)XMLString;
-//- (NSString *)XMLStringWithOptions:(NSUInteger)options;
+- (NSString *)XMLString;
+- (NSString *)XMLStringWithOptions:(NSUInteger)options;
//- (NSString *)canonicalXMLStringPreservingComments:(BOOL)comments;
- (NSArray *)nodesForXPath:(NSString *)xpath error:(NSError **)error;
+- (NSString*)_XMLStringWithOptions:(NSUInteger)options appendingToString:(NSMutableString*)str;
@end
Index: CXMLElement.h
===================================================================
--- CXMLElement.h (revision 78)
+++ CXMLElement.h (working copy)
@@ -25,4 +25,5 @@
//- (CXMLNode *)resolveNamespaceForName:(NSString *)name;
//- (NSString *)resolvePrefixForNamespaceURI:(NSString *)namespaceURI;
+- (NSString*)_XMLStringWithOptions:(NSUInteger)options appendingToString:(NSMutableString*)str;
@end
Index: CXMLNode.m
===================================================================
--- CXMLNode.m (revision 78)
+++ CXMLNode.m (working copy)
@@ -48,14 +48,18 @@
- (NSString *)stringValue
{
NSAssert(_node != NULL, @"TODO");
+xmlChar *theXMLString;
+if ( _node->type == CXMLTextKind )
+ theXMLString = _node->content;
+else
+ theXMLString = xmlNodeListGetString(_node->doc, _node->children, YES);
-xmlChar *theXMLString = xmlNodeListGetString(_node->doc, _node->children, YES);
-
NSString *theStringValue = NULL;
if (theXMLString != NULL)
{
theStringValue = [NSString stringWithUTF8String:(const char *)theXMLString];
- xmlFree(theXMLString);
+ if ( _node->type != CXMLTextKind )
+ xmlFree(theXMLString);
}
return(theStringValue);
@@ -176,8 +180,39 @@
return([NSString stringWithFormat:@"<%@ %p %@ %@>", NSStringFromClass([self class]), self, [self name], [self stringValue]]);
}
-//- (NSString *)XMLString;
-//- (NSString *)XMLStringWithOptions:(NSUInteger)options;
+- (NSString *)XMLString
+{
+return [self XMLStringWithOptions:0];
+}
+
+
+- (NSString*)_XMLStringWithOptions:(NSUInteger)options appendingToString:(NSMutableString*)str
+{
+id value;
+switch([self kind])
+ {
+ case CXMLAttributeKind:
+ value = [NSMutableString stringWithString:[self stringValue]];
+ [value replaceOccurrencesOfString:@"\"" withString:@""" options:0 range:NSMakeRange(0, [value length])];
+ [str appendString:[NSString stringWithFormat:@" %@=\"%@\"", [self name], value]];
+ break;
+ case CXMLTextKind:
+ [str appendString:[self stringValue]];
+ break;
+ case XML_COMMENT_NODE:
+ case XML_CDATA_SECTION_NODE:
+ // TODO: NSXML does not have XML_CDATA_SECTION_NODE correspondent.
+ break;
+ default:
+ NSAssert1(NO, @"TODO not implemented type (%d).", [self kind]);
+ }
+return str;
+}
+
+- (NSString *)XMLStringWithOptions:(NSUInteger)options
+{
+return [self _XMLStringWithOptions:options appendingToString:@""];
+}
//- (NSString *)canonicalXMLStringPreservingComments:(BOOL)comments;
- (NSArray *)nodesForXPath:(NSString *)xpath error:(NSError **)error
Index: CXMLDocument.h
===================================================================
--- CXMLDocument.h (revision 78)
+++ CXMLDocument.h (working copy)
@@ -8,6 +8,10 @@
#import "CXMLNode.h"
+enum {
+ CXMLDocumentTidyHTML = 1 << 9
+};
+
@class CXMLElement;
@interface CXMLDocument : CXMLNode {
@@ -34,4 +38,6 @@
//- (id)objectByApplyingXSLTString:(NSString *)xslt arguments:(NSDictionary *)arguments error:(NSError **)error;
//- (id)objectByApplyingXSLTAtURL:(NSURL *)xsltURL arguments:(NSDictionary *)argument error:(NSError **)error;
+//- (id)XMLStringWithOptions:(NSUInteger)options;
+
@end
Index: CXMLElement.m
===================================================================
--- CXMLElement.m (revision 78)
+++ CXMLElement.m (working copy)
@@ -72,5 +72,45 @@
//- (CXMLNode *)resolveNamespaceForName:(NSString *)name;
//- (NSString *)resolvePrefixForNamespaceURI:(NSString *)namespaceURI;
+- (NSString*)_XMLStringWithOptions:(NSUInteger)options appendingToString:(NSMutableString*)str
+{
+NSString* name = [self name];
+[str appendString:[NSString stringWithFormat:@"<%@", name]];
+for (id attribute in [self attributes] )
+ {
+ [attribute _XMLStringWithOptions:options appendingToString:str];
+ }
+
+if ( ! _node->children )
+ {
+ bool isEmpty = NO;
+ NSArray *emptyTags = [NSArray arrayWithObjects: @"br", @"area", @"link", @"img", @"param", @"hr", @"input", @"col", @"base", @"meta", nil ];
+ for (id s in inNamespaceMappings)
+ {
+ if ( [s isEqualToString:@"base"] )
+ {
+ isEmpty = YES;
+ NSLog(@"%@", s);
+ break;
+ }
+ }
+ if ( isEmpty )
+ {
+ [str appendString:@"/>"];
+ return str;
+ }
+ }
+
+[str appendString:@">"];
+
+if ( _node->children )
+ {
+ for (id child in [self children])
+ [child _XMLStringWithOptions:options appendingToString:str];
+ }
+[str appendString:[NSString stringWithFormat:@"</%@>", name]];
+return str;
+}
+
@end
Index: CXMLDocument.m
===================================================================
--- CXMLDocument.m (revision 78)
+++ CXMLDocument.m (working copy)
@@ -9,18 +9,60 @@
#import "CXMLDocument.h"
#include <libxml/parser.h>
+#include <libxml/HTMLparser.h>
+#include <libxml/HTMLtree.h>
#import "CXMLNode_PrivateExtensions.h"
#import "CXMLElement.h"
@implementation CXMLDocument
+static void htmlparser_error(void *ctx, const char *msg, ...)
+{
+ va_list args;
+ va_start(args, msg);
+ va_end(args);
+}
+
+static void htmlparser_warning(void *ctx, const char *msg, ...)
+{
+ va_list args;
+ va_start(args, msg);
+ va_end(args);
+}
+
- (id)initWithXMLString:(NSString *)inString options:(NSUInteger)inOptions error:(NSError **)outError
{
if ((self = [super init]) != NULL)
{
- xmlDocPtr theDoc = xmlParseDoc((xmlChar *)[inString UTF8String]);
-
+ xmlDocPtr theDoc;
+ if ( inOptions & CXMLDocumentTidyHTML )
+ {
+ const char *htmlString = (const char*) [inString UTF8String];
+ int length = xmlStrlen( (const xmlChar*)htmlString );
+ htmlParserCtxtPtr ctx = htmlCreateMemoryParserCtxt(htmlString, length);
+
+ if (! ctx ) {
+ return 0;
+ }
+
+ ctx->vctxt.error = htmlparser_error;
+ ctx->vctxt.warning = htmlparser_warning;
+ if (ctx->sax != NULL)
+ {
+ ctx->sax->error = htmlparser_error;
+ ctx->sax->warning = htmlparser_warning;
+ }
+ htmlParseDocument(ctx);
+ theDoc = ctx->myDoc;
+ htmlFreeParserCtxt(ctx);
+ }
+ else
+ {
+ theDoc = xmlParseDoc((xmlChar *)[inString UTF8String]);
+ }
+
+
if (theDoc != NULL)
{
_node = (xmlNodePtr)theDoc;
@@ -58,6 +100,7 @@
if ((self = [super init]) != NULL)
{
xmlDocPtr theDoc = NULL;
+
if (inData && inData.length > 0)
{
theDoc = xmlParseMemory([inData bytes], [inData length]);
@@ -110,5 +153,12 @@
//- (id)objectByApplyingXSLT:(NSData *)xslt arguments:(NSDictionary *)arguments error:(NSError **)error;
//- (id)objectByApplyingXSLTString:(NSString *)xslt arguments:(NSDictionary *)arguments error:(NSError **)error;
//- (id)objectByApplyingXSLTAtURL:(NSURL *)xsltURL arguments:(NSDictionary *)argument error:(NSError **)error;
+- (id)XMLStringWithOptions:(NSUInteger)options
+{
+id root = [self rootElement];
+NSMutableString* xmlString = [NSMutableString stringWithString:@""];
+[root _XMLStringWithOptions:options appendingToString:xmlString];
+return xmlString;
+}
@end
Index: CXMLNode_PrivateExtensions.m
===================================================================
--- CXMLNode_PrivateExtensions.m (revision 78)
+++ CXMLNode_PrivateExtensions.m (working copy)
@@ -37,6 +37,7 @@
case XML_ATTRIBUTE_NODE:
case XML_TEXT_NODE:
case XML_CDATA_SECTION_NODE:
+ case XML_COMMENT_NODE:
break;
default:
NSAssert1(NO, @"TODO Unhandled type (%d).", inLibXMLNode->type);
Powered by
Google Project Hosting