LibreOffice Module extensions (master) 1
OOoContentDataParser.m
Go to the documentation of this file.
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 */
19
20#include <objc/objc-runtime.h>
21
23
24@implementation OOoContentDataParser
25
26- (id)init
27{
28 if ((self = [super init]) != nil) {
30 textContent = nil;
32
33 return self;
34 }
35
36 return nil;
37}
38
39- (void)parseXML:(NSData*)data intoDictionary:(NSMutableDictionary*)dict
40{
41 mdiValues = dict;
42
43 //NSLog(@"data: %@ %d", data, [data length]);
44
45 //init parser settings
47
48 NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
49
50 [parser setDelegate:self];
51
52 [parser setShouldResolveExternalEntities:NO];
53 [parser parse];
54
55 [parser release];
56
57 //NSLog(@"finished");
58}
59
60- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
61{
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];
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 (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:@" "];
91 }
92 //NSLog(@"end");
93
94 [textContent appendString:[NSString stringWithString:runningTextContent]];
95 [runningTextContent release];
97 }
99}
100
101- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
102{
103 (void) parser; // unused
104 if (shouldReadCharacters == NO) {
105 return;
106 }
107 //NSLog(string);
108
109 [runningTextContent appendString:string];
110
111 //NSLog(@"read: %@", string);
112
113}
114
115- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
116{
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]);
121
122 if (runningTextContent != nil) {
123 [runningTextContent release];
124 runningTextContent = nil;
125 }
126 if (textContent != nil) {
127 [textContent release];
128 textContent = nil;
129 }
130}
131
132- (void)parserDidEndDocument:(NSXMLParser *)parser
133{
134 (void) parser; // unused
135 if (textContent != nil && [textContent length] > 0) {
136 [mdiValues setObject:[NSString stringWithString:textContent] forKey:(NSString*)kMDItemTextContent];
137 [textContent release];
138 textContent = nil;
139 }
140}
141
142@end
143
144/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
FILE * init(int, char **)
NSMutableString * runningTextContent
NSMutableString * textContent
NSMutableDictionary * mdiValues
parser