1/************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24#import "OOoContentDataParser.h" 25 26@implementation OOoContentDataParser 27 28- (id)init 29{ 30 if ((self = [super init]) != nil) { 31 shouldReadCharacters = NO; 32 textContent = nil; 33 runningTextContent = nil; 34 35 return self; 36 } 37 38 return nil; 39} 40 41- (void)parseXML:(NSData*)data intoDictionary:(NSMutableDictionary*)dict 42{ 43 mdiValues = dict; 44 45 //NSLog(@"data: %@ %d", data, [data length]); 46 47 //init parser settings 48 shouldReadCharacters = NO; 49 50 NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data]; 51 52 [parser setDelegate:self]; 53 [parser setShouldResolveExternalEntities:NO]; 54 [parser parse]; 55 56 [parser release]; 57 58 //NSLog(@"finished"); 59} 60 61- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict 62{ 63 // all text content is stored inside <text:p> elements 64 if ([elementName isEqualToString:@"text:p"] == YES) { 65 runningTextContent = [NSMutableString new]; 66 shouldReadCharacters = YES; 67 //NSLog(@"start"); 68 } else { 69 return; 70 } 71 72 //NSLog(@"start element %@", elementName); 73} 74 75- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName 76{ 77 if (shouldReadCharacters == TRUE) { 78 if (textContent == nil) { 79 textContent = [NSMutableString new]; 80 } else if ([runningTextContent isEqualToString:@""] == NO) { 81 // separate by whitespace 82 [textContent appendString:@" "]; 83 } 84 //NSLog(@"end"); 85 86 [textContent appendString:[NSString stringWithString:runningTextContent]]; 87 [runningTextContent release]; 88 } 89 shouldReadCharacters = NO; 90} 91 92- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string 93{ 94 if (shouldReadCharacters == NO) { 95 return; 96 } 97 //NSLog(string); 98 99 [runningTextContent appendString:string]; 100 101 //NSLog(@"currentElement: %@", currentElement); 102 //NSLog(@"read: %@", string); 103 104} 105 106- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError 107{ 108 //NSLog(@"parsing finished with error"); 109 NSLog([NSString stringWithFormat:@"An error occured parsing the document. (Error %i, Description: %@, Line: %i, Column: %i)", [parseError code], 110 [[parser parserError] localizedDescription], [parser lineNumber], 111 [parser columnNumber]]); 112 113 if (runningTextContent != nil) { 114 [runningTextContent release]; 115 } 116 if (textContent != nil) { 117 [textContent release]; 118 } 119} 120 121- (void)parserDidEndDocument:(NSXMLParser *)parser 122{ 123 if (textContent != nil && [textContent length] > 0) { 124 [mdiValues setObject:[NSString stringWithString:textContent] forKey:(NSString*)kMDItemTextContent]; 125 [textContent release]; 126 } 127} 128 129@end 130