1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: OOoContentDataParser.m,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 *************************************************************************/
31 #import "OOoContentDataParser.h"
33 @implementation OOoContentDataParser
37 if ((self = [super init]) != nil) {
38 shouldReadCharacters = NO;
40 runningTextContent = nil;
48 - (void)parseXML:(NSData*)data intoDictionary:(NSMutableDictionary*)dict
52 //NSLog(@"data: %@ %d", data, [data length]);
54 //init parser settings
55 shouldReadCharacters = NO;
57 NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
59 [parser setDelegate:self];
60 [parser setShouldResolveExternalEntities:NO];
68 - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
70 // all text content is stored inside <text:p> elements
71 if ([elementName isEqualToString:@"text:p"] == YES) {
72 runningTextContent = [NSMutableString new];
73 shouldReadCharacters = YES;
79 //NSLog(@"start element %@", elementName);
82 - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
84 if (shouldReadCharacters == TRUE) {
85 if (textContent == nil) {
86 textContent = [NSMutableString new];
87 } else if ([runningTextContent isEqualToString:@""] == NO) {
88 // separate by whitespace
89 [textContent appendString:@" "];
93 [textContent appendString:[NSString stringWithString:runningTextContent]];
94 [runningTextContent release];
96 shouldReadCharacters = NO;
99 - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
101 if (shouldReadCharacters == NO) {
106 [runningTextContent appendString:string];
108 //NSLog(@"currentElement: %@", currentElement);
109 //NSLog(@"read: %@", string);
113 - (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
115 //NSLog(@"parsing finished with error");
116 NSLog([NSString stringWithFormat:@"An error occured parsing the document. (Error %i, Description: %@, Line: %i, Column: %i)", [parseError code],
117 [[parser parserError] localizedDescription], [parser lineNumber],
118 [parser columnNumber]]);
120 if (runningTextContent != nil) {
121 [runningTextContent release];
123 if (textContent != nil) {
124 [textContent release];
128 - (void)parserDidEndDocument:(NSXMLParser *)parser
130 if (textContent != nil && [textContent length] > 0) {
131 [mdiValues setObject:[NSString stringWithString:textContent] forKey:(NSString*)kMDItemTextContent];
132 [textContent release];