r80838 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r80837‎ | r80838 | r80839 >
Date:23:03, 23 January 2011
Author:hartman
Status:deferred
Tags:
Comment:
Add XML to NSDictionary parser.

Licensed MIT. Made by Troy Brant and adapted by Benoit Caccinolo

http://troybrant.net/blog/2010/09/simple-xml-to-nsdictionary-converter/
https://github.com/bcaccinolo/XML-to-NSDictionary/tree/
Modified paths:
  • /trunk/tools/WikiSnaps/Classes/XMLtoDict (added) (history)
  • /trunk/tools/WikiSnaps/Classes/XMLtoDict/LICENSE (added) (history)
  • /trunk/tools/WikiSnaps/Classes/XMLtoDict/XMLReader.h (added) (history)
  • /trunk/tools/WikiSnaps/Classes/XMLtoDict/XMLReader.m (added) (history)

Diff [purge]

Index: trunk/tools/WikiSnaps/Classes/XMLtoDict/LICENSE
@@ -0,0 +1,17 @@
 2+Permission is hereby granted, free of charge, to any person obtaining a copy
 3+of this software and associated documentation files (the "Software"), to deal
 4+in the Software without restriction, including without limitation the rights
 5+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 6+copies of the Software, and to permit persons to whom the Software is
 7+furnished to do so, subject to the following conditions:
 8+
 9+The above copyright notice and this permission notice shall be included in
 10+all copies or substantial portions of the Software.
 11+
 12+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 13+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 14+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 15+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 16+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 17+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 18+THE SOFTWARE.
\ No newline at end of file
Index: trunk/tools/WikiSnaps/Classes/XMLtoDict/XMLReader.h
@@ -0,0 +1,19 @@
 2+//
 3+// XMLReader.h
 4+//
 5+//
 6+
 7+#import <Foundation/Foundation.h>
 8+
 9+
 10+@interface XMLReader : NSObject
 11+{
 12+ NSMutableArray *dictionaryStack;
 13+ NSMutableString *textInProgress;
 14+ NSError **errorPointer;
 15+}
 16+
 17++ (NSDictionary *)dictionaryForXMLData:(NSData *)data error:(NSError **)errorPointer;
 18++ (NSDictionary *)dictionaryForXMLString:(NSString *)string error:(NSError **)errorPointer;
 19+
 20+@end
Property changes on: trunk/tools/WikiSnaps/Classes/XMLtoDict/XMLReader.h
___________________________________________________________________
Added: svn:eol-style
121 + native
Index: trunk/tools/WikiSnaps/Classes/XMLtoDict/XMLReader.m
@@ -0,0 +1,158 @@
 2+//
 3+// XMLReader.m
 4+//
 5+
 6+#import "XMLReader.h"
 7+
 8+NSString *const kXMLReaderTextNodeKey = @"text";
 9+
 10+@interface XMLReader (Internal)
 11+
 12+- (id)initWithError:(NSError **)error;
 13+- (NSDictionary *)objectWithData:(NSData *)data;
 14+
 15+@end
 16+
 17+
 18+@implementation XMLReader
 19+
 20+#pragma mark -
 21+#pragma mark Public methods
 22+
 23++ (NSDictionary *)dictionaryForXMLData:(NSData *)data error:(NSError **)error
 24+{
 25+ XMLReader *reader = [[XMLReader alloc] initWithError:error];
 26+ NSDictionary *rootDictionary = [reader objectWithData:data];
 27+ [reader release];
 28+ return rootDictionary;
 29+}
 30+
 31++ (NSDictionary *)dictionaryForXMLString:(NSString *)string error:(NSError **)error
 32+{
 33+ NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
 34+ return [XMLReader dictionaryForXMLData:data error:error];
 35+}
 36+
 37+#pragma mark -
 38+#pragma mark Parsing
 39+
 40+- (id)initWithError:(NSError **)error
 41+{
 42+ if (self = [super init])
 43+ {
 44+ errorPointer = error;
 45+ }
 46+ return self;
 47+}
 48+
 49+- (void)dealloc
 50+{
 51+ [dictionaryStack release];
 52+ [textInProgress release];
 53+ [super dealloc];
 54+}
 55+
 56+- (NSDictionary *)objectWithData:(NSData *)data
 57+{
 58+ // Clear out any old data
 59+ [dictionaryStack release];
 60+ [textInProgress release];
 61+
 62+ dictionaryStack = [[NSMutableArray alloc] init];
 63+ textInProgress = [[NSMutableString alloc] init];
 64+
 65+ // Initialize the stack with a fresh dictionary
 66+ [dictionaryStack addObject:[NSMutableDictionary dictionary]];
 67+
 68+ // Parse the XML
 69+ NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
 70+ parser.delegate = self;
 71+ BOOL success = [parser parse];
 72+
 73+ // Return the stack's root dictionary on success
 74+ if (success)
 75+ {
 76+ NSDictionary *resultDict = [dictionaryStack objectAtIndex:0];
 77+ return resultDict;
 78+ }
 79+
 80+ return nil;
 81+}
 82+
 83+#pragma mark -
 84+#pragma mark NSXMLParserDelegate methods
 85+
 86+- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
 87+{
 88+ // Get the dictionary for the current level in the stack
 89+ NSMutableDictionary *parentDict = [dictionaryStack lastObject];
 90+
 91+ // Create the child dictionary for the new element, and initilaize it with the attributes
 92+ NSMutableDictionary *childDict = [NSMutableDictionary dictionary];
 93+ [childDict addEntriesFromDictionary:attributeDict];
 94+
 95+ // If there's already an item for this key, it means we need to create an array
 96+ id existingValue = [parentDict objectForKey:elementName];
 97+ if (existingValue)
 98+ {
 99+ NSMutableArray *array = nil;
 100+ if ([existingValue isKindOfClass:[NSMutableArray class]])
 101+ {
 102+ // The array exists, so use it
 103+ array = (NSMutableArray *) existingValue;
 104+ }
 105+ else
 106+ {
 107+ // Create an array if it doesn't exist
 108+ array = [NSMutableArray array];
 109+ [array addObject:existingValue];
 110+
 111+ // Replace the child dictionary with an array of children dictionaries
 112+ [parentDict setObject:array forKey:elementName];
 113+ }
 114+
 115+ // Add the new child dictionary to the array
 116+ [array addObject:childDict];
 117+ }
 118+ else
 119+ {
 120+ // No existing value, so update the dictionary
 121+ [parentDict setObject:childDict forKey:elementName];
 122+ }
 123+
 124+ // Update the stack
 125+ [dictionaryStack addObject:childDict];
 126+}
 127+
 128+- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
 129+{
 130+ // Update the parent dict with text info
 131+ NSMutableDictionary *dictInProgress = [dictionaryStack lastObject];
 132+
 133+ // Set the text property
 134+ if ([textInProgress length] > 0)
 135+ {
 136+ [dictInProgress setObject:textInProgress forKey:kXMLReaderTextNodeKey];
 137+
 138+ // Reset the text
 139+ [textInProgress release];
 140+ textInProgress = [[NSMutableString alloc] init];
 141+ }
 142+
 143+ // Pop the current dict
 144+ [dictionaryStack removeLastObject];
 145+}
 146+
 147+- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
 148+{
 149+ // Build the text value
 150+ [textInProgress appendString:string];
 151+}
 152+
 153+- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
 154+{
 155+ // Set the error pointer to the parser's error object
 156+ *errorPointer = parseError;
 157+}
 158+
 159+@end

Status & tagging log