Skip to content

Commit

Permalink
Initial commit (copied from active project)
Browse files Browse the repository at this point in the history
  • Loading branch information
myobie committed Mar 3, 2009
0 parents commit 49ee094
Show file tree
Hide file tree
Showing 7 changed files with 554 additions and 0 deletions.
Empty file added README
Empty file.
20 changes: 20 additions & 0 deletions SAXMachine.h
@@ -0,0 +1,20 @@
/*
* SAXMachine.h
* TableTest
*
* Created by Nathan Herald on 3/2/09.
* Copyright 2009 The Myobie Corporation. All rights reserved.
*
*/

// This is the protocol to impliment for reacting to xml events in real time as they happen. xmlib will stream the xml and parse it in chunks, so this is your gateway to interact with those chunks.
// All of this crap happens on a secondary thread, so be sure not to do any un-threadsafe stuff.

@protocol SAXMachine <NSObject>

@optional

- (void)startElementNamed:(NSString *)name withAtrributes:(NSDictionary *)attributes;
- (void)endElementNamed:(NSString *)name;

@end
83 changes: 83 additions & 0 deletions XMLParser.h
@@ -0,0 +1,83 @@
//
// XMLParser.h
// TableTest
//
// Created by Nathan Herald on 2/26/09.
// Copyright 2009 The Myobie Corporation. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "XMLParserObject.h"
#import "XMLParserDelegate.h"
#import "SAXMachine.h"
#import <libxml/tree.h>

@interface XMLParser : NSObject <SAXMachine> {
@private
id <XMLParserDelegate> delegate;
NSMutableArray *parsedObjects;

NSArray *hierarchy;
NSString *hierarchyString;

// for XML parsing

xmlParserCtxtPtr context; // Reference to the libxml parser context
NSURLConnection *remoteConnection;

BOOL done; // Overall state of the parser, used to exit the run loop.

// State variable used to determine whether or not to ignore a given XML element
BOOL parsingAnObject;

// The following state variables deal with getting character data from XML elements. This is a potentially expensive
// operation. The character data in a given element may be delivered over the course of multiple callbacks, so that
// data must be appended to a buffer. The optimal way of doing this is to use a C string buffer that grows exponentially.
// When all the characters have been delivered, an NSString is constructed and the buffer is reset.
BOOL storingCharacters;
NSMutableData *characterBuffer;

XMLParserObject *currentObject; // A reference to the current object the parser is working with.

// The number of parsed objects is tracked so that the autorelease pool for the parsing thread can be periodically
// emptied to keep the memory footprint under control.
NSUInteger countOfParsedObjects;
NSAutoreleasePool *downloadAndParsePool;
}

@property (nonatomic, assign) id <XMLParserDelegate> delegate;

@property (nonatomic, retain) XMLParserObject *currentObject;
@property BOOL parsingAnObject;

@property (nonatomic, retain) NSArray *hierarchy;
@property (nonatomic, retain) NSString *hierarchyString;

- (void)parseFromRemoteUrl:(NSString *)urlString;

// This will be invoked on a secondary thread to keep the application responsive.
// Although NSURLConnection is inherently asynchronous, the parsing can be quite CPU intensive on the device, so
// the user interface can be kept responsive by moving that work off the main thread. This does create additional
// complexity, as any code which interacts with the UI must then do so in a thread-safe manner.
- (void)downloadAndParse:(NSURL *)url;

// Each of these methods must be invoked on the main thread.
- (void)downloadStarted;
- (void)downloadEnded;
- (void)parseEnded;
- (void)parsedObject:(XMLParserObject *)object;
- (void)finishedCurrentObject;
- (void)parseError:(NSError *)error;

- (NSString *)currentString;

- (void)startStoringCharacters;
- (void)stopStoringCharacters;

- (void)pushOnHierarchy:(NSString *)name;
- (void)popHierarchy;
- (void)parseHierarchyForString;

- (NSArray *)recordCharactersIfNameOrPathIsIn;

@end

0 comments on commit 49ee094

Please sign in to comment.