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];
50 [parser setDelegate:self];
52 [parser setShouldResolveExternalEntities:NO];
60 - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
62 (void) parser; // unused
63 (void) namespaceURI; // FIXME this should not be ignored but should be used
64 // instead of text: prefix in the comparison below!
65 (void) qualifiedName; // unused
66 (void) attributeDict; // unused
67 // all text content is stored inside <text:p> elements
68 if ([elementName isEqualToString:@"text:p"] == YES) {
69 runningTextContent = [NSMutableString new];
70 shouldReadCharacters = YES;
76 //NSLog(@"start element %@", elementName);
79 - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
81 (void) parser; // unused
82 (void) elementName; // unused
83 (void) namespaceURI; // unused
84 (void) qName; // unused
85 if (shouldReadCharacters == TRUE) {
86 if (textContent == nil) {
87 textContent = [NSMutableString new];
88 } else if ([runningTextContent isEqualToString:@""] == NO) {
89 // separate by whitespace
90 [textContent appendString:@" "];
94 [textContent appendString:[NSString stringWithString:runningTextContent]];
95 [runningTextContent release];
96 runningTextContent = nil;
98 shouldReadCharacters = NO;
101 - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
103 (void) parser; // unused
104 if (shouldReadCharacters == NO) {
109 [runningTextContent appendString:string];
111 //NSLog(@"read: %@", string);
115 - (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
117 //NSLog(@"parsing finished with error");
118 NSLog(@"An error occurred parsing the document. (Error %li, Description: %@, Line: %li, Column: %li)", (long) [parseError code],
119 [[parser parserError] localizedDescription], (long) [parser lineNumber],
120 (long) [parser columnNumber]);
122 if (runningTextContent != nil) {
123 [runningTextContent release];
124 runningTextContent = nil;
126 if (textContent != nil) {
127 [textContent release];
132 - (void)parserDidEndDocument:(NSXMLParser *)parser
134 (void) parser; // unused
135 if (textContent != nil && [textContent length] > 0) {
136 [mdiValues setObject:[NSString stringWithString:textContent] forKey:(NSString*)kMDItemTextContent];
137 [textContent release];
144 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */