Bump version to 4.3-4
[LibreOffice.git] / writerfilter / source / ooxml / OOXMLFactory.cxx
blob1f778c3295a316d4952af497b31a89867b639f88
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <stdio.h>
22 #include <rtl/instance.hxx>
23 #include <osl/mutex.hxx>
24 #include "OOXMLFactory.hxx"
25 #include "OOXMLFastHelper.hxx"
27 namespace writerfilter {
28 namespace ooxml {
30 AttributeInfo::AttributeInfo()
31 :m_nResource(RT_NoResource), m_nRef(0)
35 AttributeInfo::AttributeInfo(ResourceType_t nResource, Id nRef)
36 :m_nResource(nResource), m_nRef(nRef)
40 CreateElement::CreateElement()
41 :m_nResource(RT_NoResource), m_nId(0)
45 CreateElement::CreateElement(ResourceType_t nResource, Id nId)
46 : m_nResource(nResource), m_nId(nId)
50 // class OOXMLFactory_ns
52 OOXMLFactory_ns::~OOXMLFactory_ns()
56 AttributeToResourceMapPointer OOXMLFactory_ns::getAttributeToResourceMap(Id nId)
58 if (m_AttributesMap.find(nId) == m_AttributesMap.end())
59 m_AttributesMap[nId] = createAttributeToResourceMap(nId);
61 return m_AttributesMap[nId];
64 ListValueMapPointer OOXMLFactory_ns::getListValueMap(Id nId)
66 if (m_ListValuesMap.find(nId) == m_ListValuesMap.end())
67 m_ListValuesMap[nId] = createListValueMap(nId);
69 return m_ListValuesMap[nId];
72 CreateElementMapPointer OOXMLFactory_ns::getCreateElementMap(Id nId)
74 if (m_CreateElementsMap.find(nId) == m_CreateElementsMap.end())
75 m_CreateElementsMap[nId] = createCreateElementMap(nId);
77 return m_CreateElementsMap[nId];
80 TokenToIdMapPointer OOXMLFactory_ns::getTokenToIdMap(Id nId)
82 if (m_TokenToIdsMap.find(nId) == m_TokenToIdsMap.end())
83 m_TokenToIdsMap[nId] = createTokenToIdMap(nId);
85 return m_TokenToIdsMap[nId];
88 #ifdef DEBUG_FACTORY
89 string OOXMLFactory_ns::getDefineName(Id /*nId*/) const
91 return "";
93 #endif
95 // class OOXMLFactory
97 typedef rtl::Static< osl::Mutex, OOXMLFactory > OOXMLFactory_Mutex;
99 OOXMLFactory::Pointer_t OOXMLFactory::m_Instance;
101 OOXMLFactory::OOXMLFactory()
103 // multi-thread-safe mutex for all platforms
105 osl::MutexGuard aGuard(OOXMLFactory_Mutex::get());
108 OOXMLFactory::~OOXMLFactory()
112 OOXMLFactory::Pointer_t OOXMLFactory::getInstance()
114 if (m_Instance.get() == NULL)
115 m_Instance.reset(new OOXMLFactory());
117 return m_Instance;
120 void OOXMLFactory::attributes(OOXMLFastContextHandler * pHandler,
121 const uno::Reference< xml::sax::XFastAttributeList > & Attribs)
123 Id nDefine = pHandler->getDefine();
124 OOXMLFactory_ns::Pointer_t pFactory = getFactoryForNamespace(nDefine);
126 if (pFactory.get() != NULL)
128 #ifdef DEBUG_FACTORY
129 debug_logger->startElement("factory.attributes");
130 debug_logger->attribute("define", pFactory->getDefineName(nDefine));
131 char sBuffer[256];
132 snprintf(sBuffer, sizeof(sBuffer), "%08" SAL_PRIxUINT32, nDefine);
133 debug_logger->attribute("define-num", sBuffer);
134 #endif
136 TokenToIdMapPointer pTokenToIdMap = pFactory->getTokenToIdMap(nDefine);
137 AttributeToResourceMapPointer pMap = pFactory->getAttributeToResourceMap(nDefine);
139 AttributeToResourceMap::const_iterator aIt;
140 AttributeToResourceMap::const_iterator aEndIt = pMap->end();
142 for (aIt = pMap->begin(); aIt != aEndIt; ++aIt)
144 Id nId = (*pTokenToIdMap)[aIt->first];
145 #ifdef DEBUG_FACTORY
146 debug_logger->startElement("factory.attribute");
147 debug_logger->attribute("name", fastTokenToId(aIt->first));
148 debug_logger->attribute("tokenid", (*QNameToString::Instance())(nId));
149 snprintf(sBuffer, sizeof(sBuffer), "%08" SAL_PRIxUINT32, nId);
150 debug_logger->attribute("tokenid-num", sBuffer);
151 #endif
152 if (Attribs->hasAttribute(aIt->first))
154 switch (aIt->second.m_nResource)
156 case RT_Boolean:
158 #ifdef DEBUG_FACTORY
159 debug_logger->element("boolean");
160 #endif
161 OUString aValue(Attribs->getValue(aIt->first));
162 OOXMLFastHelper<OOXMLBooleanValue>::newProperty(pHandler, nId, aValue);
164 OOXMLValue::Pointer_t pValue(new OOXMLBooleanValue(aValue));
165 pFactory->attributeAction(pHandler, aIt->first, pValue);
167 break;
168 case RT_String:
170 #ifdef DEBUG_FACTORY
171 debug_logger->element("string");
172 #endif
173 OUString aValue(Attribs->getValue(aIt->first));
174 OOXMLFastHelper<OOXMLStringValue>::newProperty
175 (pHandler, nId, aValue);
177 OOXMLValue::Pointer_t pValue(new OOXMLStringValue(aValue));
178 pFactory->attributeAction(pHandler, aIt->first, pValue);
180 break;
181 case RT_Integer:
183 #ifdef DEBUG_FACTORY
184 debug_logger->element("integer");
185 #endif
186 OUString aValue(Attribs->getValue(aIt->first));
187 OOXMLFastHelper<OOXMLIntegerValue>::newProperty
188 (pHandler, nId, aValue);
190 OOXMLValue::Pointer_t pValue(new OOXMLIntegerValue(aValue));
191 pFactory->attributeAction(pHandler, aIt->first, pValue);
193 break;
194 case RT_Hex:
196 #ifdef DEBUG_FACTORY
197 debug_logger->element("hex");
198 #endif
199 OUString aValue(Attribs->getValue(aIt->first));
200 OOXMLFastHelper<OOXMLHexValue>::newProperty
201 (pHandler, nId, aValue);
203 OOXMLValue::Pointer_t pValue(new OOXMLHexValue(aValue));
204 pFactory->attributeAction(pHandler, aIt->first, pValue);
206 break;
207 case RT_UniversalMeasure:
209 #ifdef DEBUG_FACTORY
210 debug_logger->element("universalMeasure");
211 #endif
212 OUString aValue(Attribs->getValue(aIt->first));
213 OOXMLFastHelper<OOXMLUniversalMeasureValue>::newProperty(pHandler, nId, aValue);
215 OOXMLValue::Pointer_t pValue(new OOXMLUniversalMeasureValue(aValue));
216 pFactory->attributeAction(pHandler, aIt->first, pValue);
218 break;
219 case RT_List:
221 #ifdef DEBUG_FACTORY
222 debug_logger->startElement("list");
223 #endif
224 ListValueMapPointer pListValueMap =
225 pFactory->getListValueMap(aIt->second.m_nRef);
227 if (pListValueMap.get() != NULL)
229 OUString aValue(Attribs->getValue(aIt->first));
230 sal_uInt32 nValue = (*pListValueMap)[aValue];
232 #ifdef DEBUG_FACTORY
233 debug_logger->attribute("value", aValue);
234 debug_logger->attribute("value-num", nValue);
235 #endif
237 OOXMLFastHelper<OOXMLIntegerValue>::newProperty
238 (pHandler, nId, nValue);
240 OOXMLValue::Pointer_t pValue(new OOXMLIntegerValue(nValue));
241 pFactory->attributeAction(pHandler, aIt->first, pValue);
243 #ifdef DEBUG_FACTORY
244 debug_logger->endElement();
245 #endif
247 break;
248 default:
249 #ifdef DEBUG_FACTORY
250 debug_logger->element("unknown-attribute-type");
251 #endif
252 break;
255 #ifdef DEBUG_FACTORY
256 debug_logger->endElement();
257 #endif
260 #ifdef DEBUG_FACTORY
261 debug_logger->endElement();
262 #endif
266 uno::Reference< xml::sax::XFastContextHandler>
267 OOXMLFactory::createFastChildContext(OOXMLFastContextHandler * pHandler,
268 Token_t Element)
270 #ifdef DEBUG_FACTORY
271 debug_logger->startElement("factory.createFastChildContext");
272 debug_logger->attribute("token", fastTokenToId(Element));
273 #endif
275 Id nDefine = pHandler->getDefine();
277 OOXMLFactory_ns::Pointer_t pFactory = getFactoryForNamespace(nDefine);
279 uno::Reference< xml::sax::XFastContextHandler> ret;
281 //Avoid handling unknown tokens and recursing to death
282 if ((Element & 0xffff) < OOXML_FAST_TOKENS_END)
283 ret = createFastChildContextFromFactory(pHandler, pFactory, Element);
285 #ifdef DEBUG_FACTORY
286 debug_logger->endElement();
287 #endif
289 return ret;
292 void OOXMLFactory::characters(OOXMLFastContextHandler * pHandler,
293 const OUString & rString)
295 #ifdef DEBUG_FACTORY
296 debug_logger->startElement("factory.characters");
297 debug_logger->chars(rString);
298 #endif
300 Id nDefine = pHandler->getDefine();
301 OOXMLFactory_ns::Pointer_t pFactory = getFactoryForNamespace(nDefine);
303 if (pFactory.get() != NULL)
305 pFactory->charactersAction(pHandler, rString);
308 #ifdef DEBUG_FACTORY
309 debug_logger->endElement();
310 #endif
313 void OOXMLFactory::startAction(OOXMLFastContextHandler * pHandler, Token_t /*nToken*/)
315 Id nDefine = pHandler->getDefine();
316 OOXMLFactory_ns::Pointer_t pFactory = getFactoryForNamespace(nDefine);
318 if (pFactory.get() != NULL)
320 #ifdef DEBUG_ELEMENT
321 debug_logger->startElement("factory.startAction");
322 #endif
323 pFactory->startAction(pHandler);
324 #ifdef DEBUG_ELEMENT
325 debug_logger->endElement();
326 #endif
330 void OOXMLFactory::endAction(OOXMLFastContextHandler * pHandler, Token_t /*nToken*/)
332 Id nDefine = pHandler->getDefine();
333 OOXMLFactory_ns::Pointer_t pFactory = getFactoryForNamespace(nDefine);
335 if (pFactory.get() != NULL)
337 #ifdef DEBUG_ELEMENT
338 debug_logger->startElement("factory.endAction");
339 #endif
340 pFactory->endAction(pHandler);
341 #ifdef DEBUG_ELEMENT
342 debug_logger->endElement();
343 #endif
347 void OOXMLFactory_ns::startAction(OOXMLFastContextHandler *)
351 void OOXMLFactory_ns::endAction(OOXMLFastContextHandler *)
355 void OOXMLFactory_ns::charactersAction(OOXMLFastContextHandler *, const OUString &)
359 void OOXMLFactory_ns::attributeAction(OOXMLFastContextHandler *, Token_t, OOXMLValue::Pointer_t)
363 #ifdef DEBUG_FACTORY
364 string OOXMLFactory_ns::getName() const
366 return "noname";
368 #endif
373 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */