Line data Source code
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 <stdio.h>
21 :
22 : #include <rtl/instance.hxx>
23 : #include <osl/mutex.hxx>
24 : #include "OOXMLFactory.hxx"
25 : #include "OOXMLFastHelper.hxx"
26 :
27 : namespace writerfilter {
28 : namespace ooxml {
29 :
30 477 : AttributeInfo::AttributeInfo()
31 477 : :m_nResource(RT_NoResource), m_nRef(0)
32 : {
33 477 : }
34 :
35 482 : AttributeInfo::AttributeInfo(ResourceType_t nResource, Id nRef)
36 482 : :m_nResource(nResource), m_nRef(nRef)
37 : {
38 482 : }
39 :
40 2336 : CreateElement::CreateElement()
41 2336 : :m_nResource(RT_NoResource), m_nId(0)
42 : {
43 2336 : }
44 :
45 1961 : CreateElement::CreateElement(ResourceType_t nResource, Id nId)
46 1961 : : m_nResource(nResource), m_nId(nId)
47 : {
48 1961 : }
49 :
50 : // class OOXMLFactory_ns
51 :
52 48 : OOXMLFactory_ns::~OOXMLFactory_ns()
53 : {
54 48 : }
55 :
56 28273 : AttributeToResourceMapPointer OOXMLFactory_ns::getAttributeToResourceMap(Id nId)
57 : {
58 28273 : if (m_AttributesMap.find(nId) == m_AttributesMap.end())
59 274 : m_AttributesMap[nId] = createAttributeToResourceMap(nId);
60 :
61 28273 : return m_AttributesMap[nId];
62 : }
63 :
64 5453 : ListValueMapPointer OOXMLFactory_ns::getListValueMap(Id nId)
65 : {
66 5453 : if (m_ListValuesMap.find(nId) == m_ListValuesMap.end())
67 69 : m_ListValuesMap[nId] = createListValueMap(nId);
68 :
69 5453 : return m_ListValuesMap[nId];
70 : }
71 :
72 36539 : CreateElementMapPointer OOXMLFactory_ns::getCreateElementMap(Id nId)
73 : {
74 36539 : if (m_CreateElementsMap.find(nId) == m_CreateElementsMap.end())
75 253 : m_CreateElementsMap[nId] = createCreateElementMap(nId);
76 :
77 36539 : return m_CreateElementsMap[nId];
78 : }
79 :
80 64812 : TokenToIdMapPointer OOXMLFactory_ns::getTokenToIdMap(Id nId)
81 : {
82 64812 : if (m_TokenToIdsMap.find(nId) == m_TokenToIdsMap.end())
83 386 : m_TokenToIdsMap[nId] = createTokenToIdMap(nId);
84 :
85 64812 : return m_TokenToIdsMap[nId];
86 : }
87 :
88 0 : string OOXMLFactory_ns::getDefineName(Id /*nId*/) const
89 : {
90 0 : return "";
91 : }
92 :
93 : // class OOXMLFactory
94 :
95 : typedef rtl::Static< osl::Mutex, OOXMLFactory > OOXMLFactory_Mutex;
96 :
97 5 : OOXMLFactory::Pointer_t OOXMLFactory::m_Instance;
98 :
99 2 : OOXMLFactory::OOXMLFactory()
100 : {
101 : // multi-thread-safe mutex for all platforms
102 :
103 2 : osl::MutexGuard aGuard(OOXMLFactory_Mutex::get());
104 2 : }
105 :
106 4 : OOXMLFactory::~OOXMLFactory()
107 : {
108 4 : }
109 :
110 113140 : OOXMLFactory::Pointer_t OOXMLFactory::getInstance()
111 : {
112 113140 : if (m_Instance.get() == NULL)
113 2 : m_Instance.reset(new OOXMLFactory());
114 :
115 113140 : return m_Instance;
116 : }
117 :
118 28273 : void OOXMLFactory::attributes(OOXMLFastContextHandler * pHandler,
119 : const uno::Reference< xml::sax::XFastAttributeList > & Attribs)
120 : {
121 28273 : Id nDefine = pHandler->getDefine();
122 28273 : OOXMLFactory_ns::Pointer_t pFactory = getFactoryForNamespace(nDefine);
123 :
124 28273 : if (pFactory.get() != NULL)
125 : {
126 : #ifdef DEBUG_FACTORY
127 : debug_logger->startElement("factory.attributes");
128 : debug_logger->attribute("define", pFactory->getDefineName(nDefine));
129 : char sBuffer[256];
130 : snprintf(sBuffer, sizeof(sBuffer), "%08" SAL_PRIxUINT32, nDefine);
131 : debug_logger->attribute("define-num", sBuffer);
132 : #endif
133 :
134 28273 : TokenToIdMapPointer pTokenToIdMap = pFactory->getTokenToIdMap(nDefine);
135 28273 : AttributeToResourceMapPointer pMap = pFactory->getAttributeToResourceMap(nDefine);
136 :
137 28273 : AttributeToResourceMap::const_iterator aIt;
138 28273 : AttributeToResourceMap::const_iterator aEndIt = pMap->end();
139 :
140 74968 : for (aIt = pMap->begin(); aIt != aEndIt; ++aIt)
141 : {
142 46695 : Id nId = (*pTokenToIdMap)[aIt->first];
143 : #ifdef DEBUG_FACTORY
144 : debug_logger->startElement("factory.attribute");
145 : debug_logger->attribute("name", fastTokenToId(aIt->first));
146 : debug_logger->attribute("tokenid", (*QNameToString::Instance())(nId));
147 : snprintf(sBuffer, sizeof(sBuffer), "%08" SAL_PRIxUINT32, nId);
148 : debug_logger->attribute("tokenid-num", sBuffer);
149 : #endif
150 46695 : if (Attribs->hasAttribute(aIt->first))
151 : {
152 26712 : switch (aIt->second.m_nResource)
153 : {
154 : case RT_Boolean:
155 : {
156 : #ifdef DEBUG_FACTORY
157 : debug_logger->element("boolean");
158 : #endif
159 637 : OUString aValue(Attribs->getValue(aIt->first));
160 637 : OOXMLFastHelper<OOXMLBooleanValue>::newProperty(pHandler, nId, aValue);
161 :
162 637 : OOXMLValue::Pointer_t pValue(new OOXMLBooleanValue(aValue));
163 637 : pFactory->attributeAction(pHandler, aIt->first, pValue);
164 : }
165 637 : break;
166 : case RT_String:
167 : {
168 : #ifdef DEBUG_FACTORY
169 : debug_logger->element("string");
170 : #endif
171 10578 : OUString aValue(Attribs->getValue(aIt->first));
172 : OOXMLFastHelper<OOXMLStringValue>::newProperty
173 10578 : (pHandler, nId, aValue);
174 :
175 10578 : OOXMLValue::Pointer_t pValue(new OOXMLStringValue(aValue));
176 10578 : pFactory->attributeAction(pHandler, aIt->first, pValue);
177 : }
178 10578 : break;
179 : case RT_Integer:
180 : {
181 : #ifdef DEBUG_FACTORY
182 : debug_logger->element("integer");
183 : #endif
184 5471 : OUString aValue(Attribs->getValue(aIt->first));
185 : OOXMLFastHelper<OOXMLIntegerValue>::newProperty
186 5471 : (pHandler, nId, aValue);
187 :
188 5471 : OOXMLValue::Pointer_t pValue(new OOXMLIntegerValue(aValue));
189 5471 : pFactory->attributeAction(pHandler, aIt->first, pValue);
190 : }
191 5471 : break;
192 : case RT_Hex:
193 : {
194 : #ifdef DEBUG_FACTORY
195 : debug_logger->element("hex");
196 : #endif
197 4573 : OUString aValue(Attribs->getValue(aIt->first));
198 : OOXMLFastHelper<OOXMLHexValue>::newProperty
199 4573 : (pHandler, nId, aValue);
200 :
201 4573 : OOXMLValue::Pointer_t pValue(new OOXMLHexValue(aValue));
202 4573 : pFactory->attributeAction(pHandler, aIt->first, pValue);
203 : }
204 4573 : break;
205 : case RT_List:
206 : {
207 : #ifdef DEBUG_FACTORY
208 : debug_logger->startElement("list");
209 : #endif
210 : ListValueMapPointer pListValueMap =
211 5453 : pFactory->getListValueMap(aIt->second.m_nRef);
212 :
213 5453 : if (pListValueMap.get() != NULL)
214 : {
215 5453 : OUString aValue(Attribs->getValue(aIt->first));
216 5453 : sal_uInt32 nValue = (*pListValueMap)[aValue];
217 :
218 : #ifdef DEBUG_FACTORY
219 : debug_logger->attribute("value", aValue);
220 : debug_logger->attribute("value-num", nValue);
221 : #endif
222 :
223 : OOXMLFastHelper<OOXMLIntegerValue>::newProperty
224 5453 : (pHandler, nId, nValue);
225 :
226 5453 : OOXMLValue::Pointer_t pValue(new OOXMLIntegerValue(nValue));
227 5453 : pFactory->attributeAction(pHandler, aIt->first, pValue);
228 5453 : }
229 : #ifdef DEBUG_FACTORY
230 : debug_logger->endElement();
231 : #endif
232 : }
233 5453 : break;
234 : default:
235 : #ifdef DEBUG_FACTORY
236 : debug_logger->element("unknown-attribute-type");
237 : #endif
238 0 : break;
239 : }
240 : }
241 : #ifdef DEBUG_FACTORY
242 : debug_logger->endElement();
243 : #endif
244 28273 : }
245 :
246 : #ifdef DEBUG_FACTORY
247 : debug_logger->endElement();
248 : #endif
249 28273 : }
250 28273 : }
251 :
252 : uno::Reference< xml::sax::XFastContextHandler>
253 26458 : OOXMLFactory::createFastChildContext(OOXMLFastContextHandler * pHandler,
254 : Token_t Element)
255 : {
256 : #ifdef DEBUG_FACTORY
257 : debug_logger->startElement("factory.createFastChildContext");
258 : debug_logger->attribute("token", fastTokenToId(Element));
259 : #endif
260 :
261 26458 : Id nDefine = pHandler->getDefine();
262 :
263 26458 : OOXMLFactory_ns::Pointer_t pFactory = getFactoryForNamespace(nDefine);
264 :
265 26458 : uno::Reference< xml::sax::XFastContextHandler> ret;
266 :
267 : //Avoid handling unknown tokens and recursing to death
268 26458 : if ((Element & 0xffff) < OOXML_FAST_TOKENS_END)
269 26417 : ret = createFastChildContextFromFactory(pHandler, pFactory, Element);
270 :
271 : #ifdef DEBUG_FACTORY
272 : debug_logger->endElement("factory.createFastChildContext");
273 : #endif
274 :
275 26458 : return ret;
276 : }
277 :
278 6346 : void OOXMLFactory::characters(OOXMLFastContextHandler * pHandler,
279 : const OUString & rString)
280 : {
281 : #ifdef DEBUG_FACTORY
282 : debug_logger->startElement("factory.characters");
283 : debug_logger->chars(rString);
284 : #endif
285 :
286 6346 : Id nDefine = pHandler->getDefine();
287 6346 : OOXMLFactory_ns::Pointer_t pFactory = getFactoryForNamespace(nDefine);
288 :
289 6346 : if (pFactory.get() != NULL)
290 : {
291 6346 : pFactory->charactersAction(pHandler, rString);
292 6346 : }
293 :
294 : #ifdef DEBUG_FACTORY
295 : debug_logger->endElement("factory.characters");
296 : #endif
297 6346 : }
298 :
299 25920 : void OOXMLFactory::startAction(OOXMLFastContextHandler * pHandler, Token_t /*nToken*/)
300 : {
301 25920 : Id nDefine = pHandler->getDefine();
302 25920 : OOXMLFactory_ns::Pointer_t pFactory = getFactoryForNamespace(nDefine);
303 :
304 25920 : if (pFactory.get() != NULL)
305 : {
306 : #ifdef DEBUG_ELEMENT
307 : debug_logger->startElement("factory.startAction");
308 : #endif
309 25920 : pFactory->startAction(pHandler);
310 : #ifdef DEBUG_ELEMENT
311 : debug_logger->endElement();
312 : #endif
313 25920 : }
314 25920 : }
315 :
316 25681 : void OOXMLFactory::endAction(OOXMLFastContextHandler * pHandler, Token_t /*nToken*/)
317 : {
318 25681 : Id nDefine = pHandler->getDefine();
319 25681 : OOXMLFactory_ns::Pointer_t pFactory = getFactoryForNamespace(nDefine);
320 :
321 25681 : if (pFactory.get() != NULL)
322 : {
323 : #ifdef DEBUG_ELEMENT
324 : debug_logger->startElement("factory.endAction");
325 : #endif
326 25681 : pFactory->endAction(pHandler);
327 : #ifdef DEBUG_ELEMENT
328 : debug_logger->endElement();
329 : #endif
330 25681 : }
331 25681 : }
332 :
333 4340 : void OOXMLFactory_ns::startAction(OOXMLFastContextHandler *)
334 : {
335 4340 : }
336 :
337 5863 : void OOXMLFactory_ns::endAction(OOXMLFastContextHandler *)
338 : {
339 5863 : }
340 :
341 0 : void OOXMLFactory_ns::charactersAction(OOXMLFastContextHandler *, const OUString &)
342 : {
343 0 : }
344 :
345 0 : void OOXMLFactory_ns::attributeAction(OOXMLFastContextHandler *, Token_t, OOXMLValue::Pointer_t)
346 : {
347 0 : }
348 :
349 : #ifdef DEBUG_FACTORY
350 : string OOXMLFactory_ns::getName() const
351 : {
352 : return "noname";
353 : }
354 : #endif
355 :
356 : }
357 15 : }
358 :
359 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|