1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <objc/objc-runtime.h>
22 #import "OOoContentDataParser.h"
24 @implementation OOoContentDataParser
28 if ((self = [super init]) != nil) {
29 shouldReadCharacters = NO;
31 runningTextContent = nil;
39 - (void)parseXML:(NSData*)data intoDictionary:(NSMutableDictionary*)dict
43 //NSLog(@"data: %@ %d", data, [data length]);
45 //init parser settings
46 shouldReadCharacters = NO;
48 NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
51 // class 'OOoContentDataParser' does not implement the 'NSXMLParserDelegate' protocol
52 // So instead of this:
53 // [parser setDelegate:self];
55 objc_msgSend(parser, @selector(setDelegate:), self);
57 [parser setShouldResolveExternalEntities:NO];
65 - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
67 (void) parser; // unused
68 (void) namespaceURI; // FIXME this should not be ignored but should be used
69 // instead of text: prefix in the comparison below!
70 (void) qualifiedName; // unused
71 (void) attributeDict; // unused
72 // all text content is stored inside <text:p> elements
73 if ([elementName isEqualToString:@"text:p"] == YES) {
74 runningTextContent = [NSMutableString new];
75 shouldReadCharacters = YES;
81 //NSLog(@"start element %@", elementName);
84 - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
86 (void) parser; // unused
87 (void) elementName; // unused
88 (void) namespaceURI; // unused
89 (void) qName; // unused
90 if (shouldReadCharacters == TRUE) {
91 if (textContent == nil) {
92 textContent = [NSMutableString new];
93 } else if ([runningTextContent isEqualToString:@""] == NO) {
94 // separate by whitespace
95 [textContent appendString:@" "];
99 [textContent appendString:[NSString stringWithString:runningTextContent]];
100 [runningTextContent release];
102 shouldReadCharacters = NO;
105 - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
107 (void) parser; // unused
108 if (shouldReadCharacters == NO) {
113 [runningTextContent appendString:string];
115 //NSLog(@"read: %@", string);
119 - (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
121 //NSLog(@"parsing finished with error");
122 NSLog(@"An error occurred parsing the document. (Error %li, Description: %@, Line: %li, Column: %li)", (long) [parseError code],
123 [[parser parserError] localizedDescription], (long) [parser lineNumber],
124 (long) [parser columnNumber]);
126 if (runningTextContent != nil) {
127 [runningTextContent release];
129 if (textContent != nil) {
130 [textContent release];
134 - (void)parserDidEndDocument:(NSXMLParser *)parser
136 (void) parser; // unused
137 if (textContent != nil && [textContent length] > 0) {
138 [mdiValues setObject:[NSString stringWithString:textContent] forKey:(NSString*)kMDItemTextContent];
139 [textContent release];
145 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */