1/*************************************************************************
2*
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
6 *
7 * OpenOffice.org - a multi-platform office productivity suite
8 *
9 * This file is part of OpenOffice.org.
10 *
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
14 *
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
20 *
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org.  If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
25 *
26*************************************************************************/
27
28#import "OOoContentDataParser.h"
29
30@implementation OOoContentDataParser
31
32- (id)init
33{
34    if ((self = [super init]) != nil) {
35        shouldReadCharacters = NO;
36        textContent = nil;
37        runningTextContent = nil;
38
39        return self;
40    }
41
42    return nil;
43}
44
45- (void)parseXML:(NSData*)data intoDictionary:(NSMutableDictionary*)dict
46{
47    mdiValues = dict;
48
49    //NSLog(@"data: %@ %d", data, [data length]);
50
51    //init parser settings
52    shouldReadCharacters = NO;
53
54    NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
55
56    [parser setDelegate:self];
57    [parser setShouldResolveExternalEntities:NO];
58    [parser parse];
59
60    [parser release];
61
62    //NSLog(@"finished");
63}
64
65- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
66{
67    // all text content is stored inside <text:p> elements
68    if ([elementName isEqualToString:@"text:p"] == YES) {
69        runningTextContent = [NSMutableString new];
70        shouldReadCharacters = YES;
71        //NSLog(@"start");
72    } else {
73        return;
74    }
75
76    //NSLog(@"start element %@", elementName);
77}
78
79- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
80{
81    if (shouldReadCharacters == TRUE) {
82        if (textContent == nil) {
83            textContent = [NSMutableString new];
84        } else if ([runningTextContent isEqualToString:@""] == NO) {
85            // separate by whitespace
86            [textContent appendString:@" "];
87        }
88        //NSLog(@"end");
89
90        [textContent appendString:[NSString stringWithString:runningTextContent]];
91        [runningTextContent release];
92    }
93    shouldReadCharacters = NO;
94}
95
96- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
97{
98    if (shouldReadCharacters == NO) {
99        return;
100    }
101    //NSLog(string);
102
103    [runningTextContent appendString:string];
104
105    //NSLog(@"currentElement: %@", currentElement);
106    //NSLog(@"read: %@", string);
107
108}
109
110- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
111{
112    //NSLog(@"parsing finished with error");
113    NSLog([NSString stringWithFormat:@"An error occured parsing the document. (Error %i, Description: %@, Line: %i, Column: %i)", [parseError code],
114        [[parser parserError] localizedDescription], [parser lineNumber],
115        [parser columnNumber]]);
116
117    if (runningTextContent != nil) {
118        [runningTextContent release];
119    }
120    if (textContent != nil) {
121        [textContent release];
122    }
123}
124
125- (void)parserDidEndDocument:(NSXMLParser *)parser
126{
127    if (textContent != nil && [textContent length] > 0) {
128        [mdiValues setObject:[NSString stringWithString:textContent] forKey:(NSString*)kMDItemTextContent];
129        [textContent release];
130    }
131}
132
133@end
134