Skip to content
Newer
Older
100644 266 lines (221 sloc) 9.37 KB
9b9dbd4 (split)Initial checkin of TouchXML
jwight authored
1 //
f756040 @schwa (split)Merged with small society
schwa authored
2 // CXMLDocument.m
3 // TouchCode
9b9dbd4 (split)Initial checkin of TouchXML
jwight authored
4 //
5 // Created by Jonathan Wight on 03/07/08.
5eea516 @schwa Fixed license.
schwa authored
6 // Copyright 2011 toxicsoftware.com. All rights reserved.
c2861dd (split)Updated licenses in each source/header. OUCH.
jwight authored
7 //
5eea516 @schwa Fixed license.
schwa authored
8 // Redistribution and use in source and binary forms, with or without modification, are
9 // permitted provided that the following conditions are met:
c2861dd (split)Updated licenses in each source/header. OUCH.
jwight authored
10 //
5eea516 @schwa Fixed license.
schwa authored
11 // 1. Redistributions of source code must retain the above copyright notice, this list of
12 // conditions and the following disclaimer.
c2861dd (split)Updated licenses in each source/header. OUCH.
jwight authored
13 //
5eea516 @schwa Fixed license.
schwa authored
14 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
15 // of conditions and the following disclaimer in the documentation and/or other materials
16 // provided with the distribution.
9b9dbd4 (split)Initial checkin of TouchXML
jwight authored
17 //
5eea516 @schwa Fixed license.
schwa authored
18 // THIS SOFTWARE IS PROVIDED BY TOXICSOFTWARE.COM ``AS IS'' AND ANY EXPRESS OR IMPLIED
19 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 // FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL TOXICSOFTWARE.COM OR
21 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24 // ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 //
28 // The views and conclusions contained in the software and documentation are those of the
29 // authors and should not be interpreted as representing official policies, either expressed
30 // or implied, of toxicsoftware.com.
9b9dbd4 (split)Initial checkin of TouchXML
jwight authored
31
32 #import "CXMLDocument.h"
33
34 #include <libxml/parser.h>
35
36 #import "CXMLNode_PrivateExtensions.h"
37 #import "CXMLElement.h"
38
455f2dd (split)Added libtidy support to TouchXML.
jwight authored
39 #if TOUCHXMLUSETIDY
40 #import "CTidy.h"
41 #endif /* TOUCHXMLUSETIDY */
771e980 (split)Applied patch in issue #12. Added XMLString and HTML support. …
jwight authored
42
79ad50f (split)Fix for #36
jwight authored
43 @implementation CXMLDocument
771e980 (split)Applied patch in issue #12. Added XMLString and HTML support. …
jwight authored
44
9335752 (split)Lots of node navigation methods added (next, prev etc).
jwight authored
45 - (id)initWithXMLString:(NSString *)inString options:(NSUInteger)inOptions error:(NSError **)outError
5005e8e @schwa Style cleanup (WIP)
schwa authored
46 {
47 #pragma unused (inOptions)
48
49 if ((self = [super init]) != NULL)
50 {
51 NSError *theError = NULL;
52
53 #if TOUCHXMLUSETIDY
54 if (inOptions & CXMLDocumentTidyHTML)
55 {
56 inString = [[CTidy tidy] tidyString:inString inputFormat:TidyFormat_HTML outputFormat:TidyFormat_XHTML diagnostics:NULL error:&theError];
57 }
58 else if (inOptions & CXMLDocumentTidyXML)
59 {
60 inString = [[CTidy tidy] tidyString:inString inputFormat:TidyFormat_XML outputFormat:TidyFormat_XML diagnostics:NULL error:&theError];
61 }
62 #endif
63
64 xmlDocPtr theDoc = xmlParseDoc((xmlChar *)[inString UTF8String]);
65 if (theDoc != NULL)
66 {
67 _node = (xmlNodePtr)theDoc;
68 NSAssert(_node->_private == NULL, @"TODO");
69 _node->_private = self; // Note. NOT retained (TODO think more about _private usage)
70 }
71 else
72 {
73 xmlErrorPtr theLastErrorPtr = xmlGetLastError();
74
34d1354 @5minpause Possibile fix for Issue #21 (calling initWithXMLString: options: erro…
5minpause authored
75 NSString* message = [NSString stringWithUTF8String:
76 (theLastErrorPtr ? theLastErrorPtr->message : "Unknown error")];
5005e8e @schwa Style cleanup (WIP)
schwa authored
77
34d1354 @5minpause Possibile fix for Issue #21 (calling initWithXMLString: options: erro…
5minpause authored
78 NSDictionary *theUserInfo = [NSDictionary dictionaryWithObjectsAndKeys:
79 message, NSLocalizedDescriptionKey, NULL];
5005e8e @schwa Style cleanup (WIP)
schwa authored
80 theError = [NSError errorWithDomain:@"CXMLErrorDomain" code:1 userInfo:theUserInfo];
81
82 xmlResetLastError();
83 }
84
85 if (outError)
86 *outError = theError;
87
88 if (theError != NULL)
89 {
90 [self release];
91 self = NULL;
92 }
93 }
94 return(self);
95 }
455f2dd (split)Added libtidy support to TouchXML.
jwight authored
96
97 - (id)initWithData:(NSData *)inData options:(NSUInteger)inOptions error:(NSError **)outError
5005e8e @schwa Style cleanup (WIP)
schwa authored
98 {
42750a6 @schwa (split)Added Jorge Pedroso's enhancements (and unit tests! yay!) for …
schwa authored
99 return [self initWithData:inData encoding:NSUTF8StringEncoding options:inOptions error:outError];
5005e8e @schwa Style cleanup (WIP)
schwa authored
100 }
42750a6 @schwa (split)Added Jorge Pedroso's enhancements (and unit tests! yay!) for …
schwa authored
101
102 - (id)initWithData:(NSData *)inData encoding:(NSStringEncoding)encoding options:(NSUInteger)inOptions error:(NSError **)outError
5005e8e @schwa Style cleanup (WIP)
schwa authored
103 {
104 #pragma unused (inOptions)
105 if ((self = [super init]) != NULL)
106 {
107 NSError *theError = NULL;
108
109 #if TOUCHXMLUSETIDY
110 if (inOptions & CXMLDocumentTidyHTML)
111 {
112 inData = [[CTidy tidy] tidyData:inData inputFormat:TidyFormat_HTML outputFormat:TidyFormat_XHTML diagnostics:NULL error:&theError];
113 }
114 else if (inOptions & CXMLDocumentTidyXML)
115 {
116 inData = [[CTidy tidy] tidyData:inData inputFormat:TidyFormat_XML outputFormat:TidyFormat_XML diagnostics:NULL error:&theError];
117 }
118 #endif
119
120 if (theError == NULL)
121 {
122 xmlDocPtr theDoc = NULL;
123 if (inData && inData.length > 0)
124 {
125 CFStringEncoding cfenc = CFStringConvertNSStringEncodingToEncoding(encoding);
126 CFStringRef cfencstr = CFStringConvertEncodingToIANACharSetName(cfenc);
127 const char *enc = CFStringGetCStringPtr(cfencstr, 0);
128 theDoc = xmlReadMemory([inData bytes], [inData length], NULL, enc, XML_PARSE_RECOVER | XML_PARSE_NOWARNING);
129 }
130
131 if (theDoc != NULL && xmlDocGetRootElement(theDoc) != NULL)
132 {
133 _node = (xmlNodePtr)theDoc;
134 _node->_private = self; // Note. NOT retained (TODO think more about _private usage)
135 }
136 else
137 {
138 xmlErrorPtr theLastErrorPtr = xmlGetLastError();
139 NSString* message = [NSString stringWithUTF8String:
140 (theLastErrorPtr ? theLastErrorPtr->message : "Unknown error")];
141 NSDictionary *theUserInfo = [NSDictionary dictionaryWithObjectsAndKeys:
142 message, NSLocalizedDescriptionKey, NULL];
143 theError = [NSError errorWithDomain:@"CXMLErrorDomain" code:1 userInfo:theUserInfo];
144
145 xmlResetLastError();
146 }
147 }
148
149 if (outError)
150 *outError = theError;
151
152 if (theError != NULL)
153 {
154 [self release];
155 self = NULL;
156 }
157 }
158 return(self);
159 }
9335752 (split)Lots of node navigation methods added (next, prev etc).
jwight authored
160
0f436ad (split)Added a new xpath method that allows user to explicity set nam…
jwight authored
161 - (id)initWithContentsOfURL:(NSURL *)inURL options:(NSUInteger)inOptions error:(NSError **)outError
5005e8e @schwa Style cleanup (WIP)
schwa authored
162 {
68d7d21 @schwa (split)More encoding improvments from Patrick Burleson.
schwa authored
163 return [self initWithContentsOfURL:inURL encoding:NSUTF8StringEncoding options:inOptions error:outError];
5005e8e @schwa Style cleanup (WIP)
schwa authored
164 }
68d7d21 @schwa (split)More encoding improvments from Patrick Burleson.
schwa authored
165
166 - (id)initWithContentsOfURL:(NSURL *)inURL encoding:(NSStringEncoding)encoding options:(NSUInteger)inOptions error:(NSError **)outError
5005e8e @schwa Style cleanup (WIP)
schwa authored
167 {
168 if (outError)
169 *outError = NULL;
170
171 NSData *theData = [NSData dataWithContentsOfURL:inURL options:NSUncachedRead error:outError];
172 if (theData)
173 {
174 self = [self initWithData:theData encoding:encoding options:inOptions error:outError];
175 }
176 else
177 {
178 self = NULL;
179 }
180
181 return(self);
182 }
9b9dbd4 (split)Initial checkin of TouchXML
jwight authored
183
e1c31e3 (split)Added better memory management via the node pool (see wiki for…
jwight authored
184 - (void)dealloc
5005e8e @schwa Style cleanup (WIP)
schwa authored
185 {
186 // Fix for #35 http://code.google.com/p/touchcode/issues/detail?id=35 -- clear up the node objects first (inside a pool so I _know_ they're cleared) and then freeing the document
e199f63 (split)Fix for #35. Now clearing up nodes before document.
jwight authored
187
5005e8e @schwa Style cleanup (WIP)
schwa authored
188 NSAutoreleasePool *thePool = [[NSAutoreleasePool alloc] init];
e1c31e3 (split)Added better memory management via the node pool (see wiki for…
jwight authored
189
540e643 @schwa Possible fix for the #11
schwa authored
190 for (CXMLNode *theNode in nodePool)
191 {
192 [theNode invalidate];
193 }
194
5005e8e @schwa Style cleanup (WIP)
schwa authored
195 [nodePool release];
196 nodePool = NULL;
e199f63 (split)Fix for #35. Now clearing up nodes before document.
jwight authored
197
5005e8e @schwa Style cleanup (WIP)
schwa authored
198 [thePool release];
199 //
ccc75e0 @schwa Refs #11 - unlink nodes before freeing.
schwa authored
200 xmlUnlinkNode(_node);
5005e8e @schwa Style cleanup (WIP)
schwa authored
201 xmlFreeDoc((xmlDocPtr)_node);
202 _node = NULL;
49d391f @jcmontiel missing [super dealloc]
jcmontiel authored
203 [super dealloc];
ccc75e0 @schwa Refs #11 - unlink nodes before freeing.
schwa authored
204 }
e1c31e3 (split)Added better memory management via the node pool (see wiki for…
jwight authored
205
9335752 (split)Lots of node navigation methods added (next, prev etc).
jwight authored
206 //- (NSString *)characterEncoding;
207 //- (NSString *)version;
208 //- (BOOL)isStandalone;
209 //- (CXMLDocumentContentKind)documentContentKind;
210 //- (NSString *)MIMEType;
211 //- (CXMLDTD *)DTD;
212
9b9dbd4 (split)Initial checkin of TouchXML
jwight authored
213 - (CXMLElement *)rootElement
5005e8e @schwa Style cleanup (WIP)
schwa authored
214 {
215 xmlNodePtr theLibXMLNode = xmlDocGetRootElement((xmlDocPtr)_node);
216 return([CXMLNode nodeWithLibXMLNode:theLibXMLNode freeOnDealloc:NO]);
217 }
9b9dbd4 (split)Initial checkin of TouchXML
jwight authored
218
bad573b (split)Turning on warnings but keeping recover
jwight authored
219 - (NSData *)XMLData
5005e8e @schwa Style cleanup (WIP)
schwa authored
220 {
221 return([self XMLDataWithOptions:0]);
222 }
bad573b (split)Turning on warnings but keeping recover
jwight authored
223
224 - (NSData *)XMLDataWithOptions:(NSUInteger)options
5005e8e @schwa Style cleanup (WIP)
schwa authored
225 {
226 #pragma unused (options)
227 xmlChar *theBuffer = NULL;
228 int theBufferSize = 0;
229 xmlDocDumpMemory((xmlDocPtr)self->_node, &theBuffer, &theBufferSize);
bad573b (split)Turning on warnings but keeping recover
jwight authored
230
5005e8e @schwa Style cleanup (WIP)
schwa authored
231 NSData *theData = [NSData dataWithBytes:theBuffer length:theBufferSize];
bad573b (split)Turning on warnings but keeping recover
jwight authored
232
5005e8e @schwa Style cleanup (WIP)
schwa authored
233 xmlFree(theBuffer);
bad573b (split)Turning on warnings but keeping recover
jwight authored
234
5005e8e @schwa Style cleanup (WIP)
schwa authored
235 return(theData);
236 }
9335752 (split)Lots of node navigation methods added (next, prev etc).
jwight authored
237
238 //- (id)objectByApplyingXSLT:(NSData *)xslt arguments:(NSDictionary *)arguments error:(NSError **)error;
239 //- (id)objectByApplyingXSLTString:(NSString *)xslt arguments:(NSDictionary *)arguments error:(NSError **)error;
240 //- (id)objectByApplyingXSLTAtURL:(NSURL *)xsltURL arguments:(NSDictionary *)argument error:(NSError **)error;
771e980 (split)Applied patch in issue #12. Added XMLString and HTML support. …
jwight authored
241 - (id)XMLStringWithOptions:(NSUInteger)options
5005e8e @schwa Style cleanup (WIP)
schwa authored
242 {
243 CXMLElement *theRoot = [self rootElement];
244 NSMutableString *xmlString = [NSMutableString string];
245 [xmlString appendString:[theRoot XMLStringWithOptions:options]];
246 return xmlString;
247 }
9335752 (split)Lots of node navigation methods added (next, prev etc).
jwight authored
248
4042456 (split)Applied patch for issue #27. Better descriptions for debugging.
jwight authored
249 - (NSString *)description
5005e8e @schwa Style cleanup (WIP)
schwa authored
250 {
251 NSAssert(_node != NULL, @"TODO");
252
253 NSMutableString *result = [NSMutableString stringWithFormat:@"<%@ %p [%p]> ", NSStringFromClass([self class]), self, self->_node];
254 xmlChar *xmlbuff;
255 int buffersize;
256
257 xmlDocDumpFormatMemory((xmlDocPtr)(self->_node), &xmlbuff, &buffersize, 1);
258 NSString *dump = [[[NSString alloc] initWithBytes:xmlbuff length:buffersize encoding:NSUTF8StringEncoding] autorelease];
259 xmlFree(xmlbuff);
260
261 [result appendString:dump];
262 return result;
263 }
4042456 (split)Applied patch for issue #27. Better descriptions for debugging.
jwight authored
264
9b9dbd4 (split)Initial checkin of TouchXML
jwight authored
265 @end
Something went wrong with that request. Please try again.