Bump version to 4.1-6
[LibreOffice.git] / writerfilter / source / ooxml / OOXMLFactory.cxx
blob20fc82c75b6fbe9a0d27ab75235c264abf747990
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 string OOXMLFactory_ns::getDefineName(Id /*nId*/) const
90 return "";
93 // class OOXMLFactory
95 typedef rtl::Static< osl::Mutex, OOXMLFactory > OOXMLFactory_Mutex;
97 OOXMLFactory::Pointer_t OOXMLFactory::m_Instance;
99 OOXMLFactory::OOXMLFactory()
101 // multi-thread-safe mutex for all platforms
103 osl::MutexGuard aGuard(OOXMLFactory_Mutex::get());
106 OOXMLFactory::~OOXMLFactory()
110 OOXMLFactory::Pointer_t OOXMLFactory::getInstance()
112 if (m_Instance.get() == NULL)
113 m_Instance.reset(new OOXMLFactory());
115 return m_Instance;
118 void OOXMLFactory::attributes(OOXMLFastContextHandler * pHandler,
119 const uno::Reference< xml::sax::XFastAttributeList > & Attribs)
121 Id nDefine = pHandler->getDefine();
122 OOXMLFactory_ns::Pointer_t pFactory = getFactoryForNamespace(nDefine);
124 if (pFactory.get() != NULL)
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
134 TokenToIdMapPointer pTokenToIdMap = pFactory->getTokenToIdMap(nDefine);
135 AttributeToResourceMapPointer pMap = pFactory->getAttributeToResourceMap(nDefine);
137 AttributeToResourceMap::const_iterator aIt;
138 AttributeToResourceMap::const_iterator aEndIt = pMap->end();
140 for (aIt = pMap->begin(); aIt != aEndIt; ++aIt)
142 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 if (Attribs->hasAttribute(aIt->first))
152 switch (aIt->second.m_nResource)
154 case RT_Boolean:
156 #ifdef DEBUG_FACTORY
157 debug_logger->element("boolean");
158 #endif
159 OUString aValue(Attribs->getValue(aIt->first));
160 OOXMLFastHelper<OOXMLBooleanValue>::newProperty(pHandler, nId, aValue);
162 OOXMLValue::Pointer_t pValue(new OOXMLBooleanValue(aValue));
163 pFactory->attributeAction(pHandler, aIt->first, pValue);
165 break;
166 case RT_String:
168 #ifdef DEBUG_FACTORY
169 debug_logger->element("string");
170 #endif
171 OUString aValue(Attribs->getValue(aIt->first));
172 OOXMLFastHelper<OOXMLStringValue>::newProperty
173 (pHandler, nId, aValue);
175 OOXMLValue::Pointer_t pValue(new OOXMLStringValue(aValue));
176 pFactory->attributeAction(pHandler, aIt->first, pValue);
178 break;
179 case RT_Integer:
181 #ifdef DEBUG_FACTORY
182 debug_logger->element("integer");
183 #endif
184 OUString aValue(Attribs->getValue(aIt->first));
185 OOXMLFastHelper<OOXMLIntegerValue>::newProperty
186 (pHandler, nId, aValue);
188 OOXMLValue::Pointer_t pValue(new OOXMLIntegerValue(aValue));
189 pFactory->attributeAction(pHandler, aIt->first, pValue);
191 break;
192 case RT_Hex:
194 #ifdef DEBUG_FACTORY
195 debug_logger->element("hex");
196 #endif
197 OUString aValue(Attribs->getValue(aIt->first));
198 OOXMLFastHelper<OOXMLHexValue>::newProperty
199 (pHandler, nId, aValue);
201 OOXMLValue::Pointer_t pValue(new OOXMLHexValue(aValue));
202 pFactory->attributeAction(pHandler, aIt->first, pValue);
204 break;
205 case RT_List:
207 #ifdef DEBUG_FACTORY
208 debug_logger->startElement("list");
209 #endif
210 ListValueMapPointer pListValueMap =
211 pFactory->getListValueMap(aIt->second.m_nRef);
213 if (pListValueMap.get() != NULL)
215 OUString aValue(Attribs->getValue(aIt->first));
216 sal_uInt32 nValue = (*pListValueMap)[aValue];
218 #ifdef DEBUG_FACTORY
219 debug_logger->attribute("value", aValue);
220 debug_logger->attribute("value-num", nValue);
221 #endif
223 OOXMLFastHelper<OOXMLIntegerValue>::newProperty
224 (pHandler, nId, nValue);
226 OOXMLValue::Pointer_t pValue(new OOXMLIntegerValue(nValue));
227 pFactory->attributeAction(pHandler, aIt->first, pValue);
229 #ifdef DEBUG_FACTORY
230 debug_logger->endElement();
231 #endif
233 break;
234 default:
235 #ifdef DEBUG_FACTORY
236 debug_logger->element("unknown-attribute-type");
237 #endif
238 break;
241 #ifdef DEBUG_FACTORY
242 debug_logger->endElement();
243 #endif
246 #ifdef DEBUG_FACTORY
247 debug_logger->endElement();
248 #endif
252 uno::Reference< xml::sax::XFastContextHandler>
253 OOXMLFactory::createFastChildContext(OOXMLFastContextHandler * pHandler,
254 Token_t Element)
256 #ifdef DEBUG_FACTORY
257 debug_logger->startElement("factory.createFastChildContext");
258 debug_logger->attribute("token", fastTokenToId(Element));
259 #endif
261 Id nDefine = pHandler->getDefine();
263 OOXMLFactory_ns::Pointer_t pFactory = getFactoryForNamespace(nDefine);
265 uno::Reference< xml::sax::XFastContextHandler> ret;
267 //Avoid handling unknown tokens and recursing to death
268 if ((Element & 0xffff) < OOXML_FAST_TOKENS_END)
269 ret = createFastChildContextFromFactory(pHandler, pFactory, Element);
271 #ifdef DEBUG_FACTORY
272 debug_logger->endElement("factory.createFastChildContext");
273 #endif
275 return ret;
278 void OOXMLFactory::characters(OOXMLFastContextHandler * pHandler,
279 const OUString & rString)
281 #ifdef DEBUG_FACTORY
282 debug_logger->startElement("factory.characters");
283 debug_logger->chars(rString);
284 #endif
286 Id nDefine = pHandler->getDefine();
287 OOXMLFactory_ns::Pointer_t pFactory = getFactoryForNamespace(nDefine);
289 if (pFactory.get() != NULL)
291 pFactory->charactersAction(pHandler, rString);
294 #ifdef DEBUG_FACTORY
295 debug_logger->endElement("factory.characters");
296 #endif
299 void OOXMLFactory::startAction(OOXMLFastContextHandler * pHandler, Token_t /*nToken*/)
301 Id nDefine = pHandler->getDefine();
302 OOXMLFactory_ns::Pointer_t pFactory = getFactoryForNamespace(nDefine);
304 if (pFactory.get() != NULL)
306 #ifdef DEBUG_ELEMENT
307 debug_logger->startElement("factory.startAction");
308 #endif
309 pFactory->startAction(pHandler);
310 #ifdef DEBUG_ELEMENT
311 debug_logger->endElement();
312 #endif
316 void OOXMLFactory::endAction(OOXMLFastContextHandler * pHandler, Token_t /*nToken*/)
318 Id nDefine = pHandler->getDefine();
319 OOXMLFactory_ns::Pointer_t pFactory = getFactoryForNamespace(nDefine);
321 if (pFactory.get() != NULL)
323 #ifdef DEBUG_ELEMENT
324 debug_logger->startElement("factory.endAction");
325 #endif
326 pFactory->endAction(pHandler);
327 #ifdef DEBUG_ELEMENT
328 debug_logger->endElement();
329 #endif
333 void OOXMLFactory_ns::startAction(OOXMLFastContextHandler *)
337 void OOXMLFactory_ns::endAction(OOXMLFastContextHandler *)
341 void OOXMLFactory_ns::charactersAction(OOXMLFastContextHandler *, const OUString &)
345 void OOXMLFactory_ns::attributeAction(OOXMLFastContextHandler *, Token_t, OOXMLValue::Pointer_t)
349 #ifdef DEBUG_FACTORY
350 string OOXMLFactory_ns::getName() const
352 return "noname";
354 #endif
359 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */