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