Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / extensions / source / macosx / spotlight / OOoContentDataParser.m
blobd394115a301026646ecf1663f27e8765c10cd2bc
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
20 #include <objc/objc-runtime.h>
22 #import "OOoContentDataParser.h"
24 @implementation OOoContentDataParser
26 - (id)init
28     if ((self = [super init]) != nil) {
29         shouldReadCharacters = NO;
30         textContent = nil;
31         runningTextContent = nil;
33         return self;
34     }
36     return nil;
39 - (void)parseXML:(NSData*)data intoDictionary:(NSMutableDictionary*)dict
41     mdiValues = dict;
43     //NSLog(@"data: %@ %d", data, [data length]);
45     //init parser settings
46     shouldReadCharacters = NO;
48     NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
50     // Once again...
51     // class 'OOoContentDataParser' does not implement the 'NSXMLParserDelegate' protocol
52     // So instead of this:
53     // [parser setDelegate:self];
54     // do this:
55     objc_msgSend(parser, @selector(setDelegate:), self);
57     [parser setShouldResolveExternalEntities:NO];
58     [parser parse];
60     [parser release];
62     //NSLog(@"finished");
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;
76         //NSLog(@"start");
77     } else {
78         return;
79     }
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:@" "];
96         }
97         //NSLog(@"end");
99         [textContent appendString:[NSString stringWithString:runningTextContent]];
100         [runningTextContent release];
101     }
102     shouldReadCharacters = NO;
105 - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
107     (void) parser; // unused
108     if (shouldReadCharacters == NO) {
109         return;
110     }
111     //NSLog(string);
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];
128     }
129     if (textContent != nil) {
130         [textContent release];
131     }
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];
140     }
143 @end
145 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */