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