1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23
24 #include <stdio.h>
25
26 #include <rtl/instance.hxx>
27 #include <osl/mutex.hxx>
28 #include "OOXMLFactory.hxx"
29 #include "OOXMLFastHelper.hxx"
30
31 namespace writerfilter {
32 namespace ooxml {
33
AttributeInfo()34 AttributeInfo::AttributeInfo()
35 :m_nResource(RT_NoResource), m_nRef(0)
36 {
37 }
38
AttributeInfo(ResourceType_t nResource,Id nRef)39 AttributeInfo::AttributeInfo(ResourceType_t nResource, Id nRef)
40 :m_nResource(nResource), m_nRef(nRef)
41 {
42 }
43
CreateElement()44 CreateElement::CreateElement()
45 :m_nResource(RT_NoResource), m_nId(0)
46 {
47 }
48
CreateElement(ResourceType_t nResource,Id nId)49 CreateElement::CreateElement(ResourceType_t nResource, Id nId)
50 : m_nResource(nResource), m_nId(nId)
51 {
52 }
53
54 // class OOXMLFactory_ns
55
~OOXMLFactory_ns()56 OOXMLFactory_ns::~OOXMLFactory_ns()
57 {
58 }
59
getAttributeToResourceMap(Id nId)60 AttributeToResourceMapPointer OOXMLFactory_ns::getAttributeToResourceMap(Id nId)
61 {
62 if (m_AttributesMap.find(nId) == m_AttributesMap.end())
63 m_AttributesMap[nId] = createAttributeToResourceMap(nId);
64
65 return m_AttributesMap[nId];
66 }
67
getListValueMap(Id nId)68 ListValueMapPointer OOXMLFactory_ns::getListValueMap(Id nId)
69 {
70 if (m_ListValuesMap.find(nId) == m_ListValuesMap.end())
71 m_ListValuesMap[nId] = createListValueMap(nId);
72
73 return m_ListValuesMap[nId];
74 }
75
getCreateElementMap(Id nId)76 CreateElementMapPointer OOXMLFactory_ns::getCreateElementMap(Id nId)
77 {
78 if (m_CreateElementsMap.find(nId) == m_CreateElementsMap.end())
79 m_CreateElementsMap[nId] = createCreateElementMap(nId);
80
81 return m_CreateElementsMap[nId];
82 }
83
getTokenToIdMap(Id nId)84 TokenToIdMapPointer OOXMLFactory_ns::getTokenToIdMap(Id nId)
85 {
86 if (m_TokenToIdsMap.find(nId) == m_TokenToIdsMap.end())
87 m_TokenToIdsMap[nId] = createTokenToIdMap(nId);
88
89 return m_TokenToIdsMap[nId];
90 }
91
getDefineName(Id) const92 string OOXMLFactory_ns::getDefineName(Id /*nId*/) const
93 {
94 return "";
95 }
96
97 // class OOXMLFactory
98
99 typedef rtl::Static< osl::Mutex, OOXMLFactory > OOXMLFactory_Mutex;
100
101 OOXMLFactory::Pointer_t OOXMLFactory::m_Instance;
102
OOXMLFactory()103 OOXMLFactory::OOXMLFactory()
104 {
105 // multi-thread-safe mutex for all platforms
106
107 osl::MutexGuard aGuard(OOXMLFactory_Mutex::get());
108 }
109
~OOXMLFactory()110 OOXMLFactory::~OOXMLFactory()
111 {
112 }
113
getInstance()114 OOXMLFactory::Pointer_t OOXMLFactory::getInstance()
115 {
116 if (m_Instance.get() == NULL)
117 m_Instance.reset(new OOXMLFactory());
118
119 return m_Instance;
120 }
121
attributes(OOXMLFastContextHandler * pHandler,const uno::Reference<xml::sax::XFastAttributeList> & Attribs)122 void OOXMLFactory::attributes(OOXMLFastContextHandler * pHandler,
123 const uno::Reference< xml::sax::XFastAttributeList > & Attribs)
124 {
125 Id nDefine = pHandler->getDefine();
126 OOXMLFactory_ns::Pointer_t pFactory = getFactoryForNamespace(nDefine);
127
128 if (pFactory.get() != NULL)
129 {
130 #ifdef DEBUG_FACTORY
131 debug_logger->startElement("factory.attributes");
132 debug_logger->attribute("define", pFactory->getDefineName(nDefine));
133 char sBuffer[256];
134 snprintf(sBuffer, sizeof(sBuffer), "%08" SAL_PRIxUINT32, nDefine);
135 debug_logger->attribute("define-num", sBuffer);
136 #endif
137
138 TokenToIdMapPointer pTokenToIdMap = pFactory->getTokenToIdMap(nDefine);
139 AttributeToResourceMapPointer pMap = pFactory->getAttributeToResourceMap(nDefine);
140
141 AttributeToResourceMap::const_iterator aIt;
142 AttributeToResourceMap::const_iterator aEndIt = pMap->end();
143
144 for (aIt = pMap->begin(); aIt != aEndIt; aIt++)
145 {
146 Id nId = (*pTokenToIdMap)[aIt->first];
147 #ifdef DEBUG_FACTORY
148 debug_logger->startElement("factory.attribute");
149 debug_logger->attribute("name", fastTokenToId(aIt->first));
150 debug_logger->attribute("tokenid", (*QNameToString::Instance())(nId));
151 snprintf(sBuffer, sizeof(sBuffer), "%08" SAL_PRIxUINT32, nId);
152 debug_logger->attribute("tokenid-num", sBuffer);
153 #endif
154 if (Attribs->hasAttribute(aIt->first))
155 {
156 switch (aIt->second.m_nResource)
157 {
158 case RT_Boolean:
159 {
160 #ifdef DEBUG_FACTORY
161 debug_logger->element("boolean");
162 #endif
163 ::rtl::OUString aValue(Attribs->getValue(aIt->first));
164 OOXMLFastHelper<OOXMLBooleanValue>::newProperty(pHandler, nId, aValue);
165
166 OOXMLValue::Pointer_t pValue(new OOXMLBooleanValue(aValue));
167 pFactory->attributeAction(pHandler, aIt->first, pValue);
168 }
169 break;
170 case RT_String:
171 {
172 #ifdef DEBUG_FACTORY
173 debug_logger->element("string");
174 #endif
175 ::rtl::OUString aValue(Attribs->getValue(aIt->first));
176 OOXMLFastHelper<OOXMLStringValue>::newProperty
177 (pHandler, nId, aValue);
178
179 OOXMLValue::Pointer_t pValue(new OOXMLStringValue(aValue));
180 pFactory->attributeAction(pHandler, aIt->first, pValue);
181 }
182 break;
183 case RT_Integer:
184 {
185 #ifdef DEBUG_FACTORY
186 debug_logger->element("integer");
187 #endif
188 ::rtl::OUString aValue(Attribs->getValue(aIt->first));
189 OOXMLFastHelper<OOXMLIntegerValue>::newProperty
190 (pHandler, nId, aValue);
191
192 OOXMLValue::Pointer_t pValue(new OOXMLIntegerValue(aValue));
193 pFactory->attributeAction(pHandler, aIt->first, pValue);
194 }
195 break;
196 case RT_Hex:
197 {
198 #ifdef DEBUG_FACTORY
199 debug_logger->element("hex");
200 #endif
201 ::rtl::OUString aValue(Attribs->getValue(aIt->first));
202 OOXMLFastHelper<OOXMLHexValue>::newProperty
203 (pHandler, nId, aValue);
204
205 OOXMLValue::Pointer_t pValue(new OOXMLHexValue(aValue));
206 pFactory->attributeAction(pHandler, aIt->first, pValue);
207 }
208 break;
209 case RT_List:
210 {
211 #ifdef DEBUG_FACTORY
212 debug_logger->startElement("list");
213 #endif
214 ListValueMapPointer pListValueMap =
215 pFactory->getListValueMap(aIt->second.m_nRef);
216
217 if (pListValueMap.get() != NULL)
218 {
219 ::rtl::OUString aValue(Attribs->getValue(aIt->first));
220 sal_uInt32 nValue = (*pListValueMap)[aValue];
221
222 #ifdef DEBUG_FACTORY
223 debug_logger->attribute("value", aValue);
224 debug_logger->attribute("value-num", nValue);
225 #endif
226
227 OOXMLFastHelper<OOXMLIntegerValue>::newProperty
228 (pHandler, nId, nValue);
229
230 OOXMLValue::Pointer_t pValue(new OOXMLIntegerValue(nValue));
231 pFactory->attributeAction(pHandler, aIt->first, pValue);
232 }
233 #ifdef DEBUG_FACTORY
234 debug_logger->endElement("list");
235 #endif
236 }
237 break;
238 default:
239 #ifdef DEBUG_FACTORY
240 debug_logger->element("unknown-attribute-type");
241 #endif
242 break;
243 }
244 }
245 #ifdef DEBUG_FACTORY
246 debug_logger->endElement("factory.attribute");
247 #endif
248 }
249
250 #ifdef DEBUG_FACTORY
251 debug_logger->endElement("factory.attributes");
252 #endif
253 }
254 }
255
256 uno::Reference< xml::sax::XFastContextHandler>
createFastChildContext(OOXMLFastContextHandler * pHandler,sal_Int32 Element)257 OOXMLFactory::createFastChildContext(OOXMLFastContextHandler * pHandler,
258 sal_Int32 Element)
259 {
260 #ifdef DEBUG_FACTORY
261 debug_logger->startElement("factory.createFastChildContext");
262 debug_logger->attribute("token", fastTokenToId(Element));
263 #endif
264
265 Id nDefine = pHandler->getDefine();
266
267 OOXMLFactory_ns::Pointer_t pFactory = getFactoryForNamespace(nDefine);
268
269 uno::Reference< xml::sax::XFastContextHandler> ret;
270
271 //Avoid handling unknown tokens and recursing to death
272 if ((Element & 0xffff) < OOXML_FAST_TOKENS_END)
273 ret = createFastChildContextFromFactory(pHandler, pFactory, Element);
274
275 #ifdef DEBUG_FACTORY
276 debug_logger->endElement("factory.createFastChildContext");
277 #endif
278
279 return ret;
280 }
281
characters(OOXMLFastContextHandler * pHandler,const::rtl::OUString & rString)282 void OOXMLFactory::characters(OOXMLFastContextHandler * pHandler,
283 const ::rtl::OUString & rString)
284 {
285 #ifdef DEBUG_FACTORY
286 debug_logger->startElement("factory.characters");
287 debug_logger->chars(rString);
288 #endif
289
290 Id nDefine = pHandler->getDefine();
291 OOXMLFactory_ns::Pointer_t pFactory = getFactoryForNamespace(nDefine);
292
293 if (pFactory.get() != NULL)
294 {
295 pFactory->charactersAction(pHandler, rString);
296 }
297
298 #ifdef DEBUG_FACTORY
299 debug_logger->endElement("factory.characters");
300 #endif
301 }
302
startAction(OOXMLFastContextHandler * pHandler,sal_Int32)303 void OOXMLFactory::startAction(OOXMLFastContextHandler * pHandler, sal_Int32 /*nToken*/)
304 {
305 Id nDefine = pHandler->getDefine();
306 OOXMLFactory_ns::Pointer_t pFactory = getFactoryForNamespace(nDefine);
307
308 if (pFactory.get() != NULL)
309 {
310 #ifdef DEBUG_FACTORY
311 debug_logger->startElement("factory.startAction");
312 #endif
313 pFactory->startAction(pHandler);
314 #ifdef DEBUG_FACTORY
315 debug_logger->endElement("factory.startAction");
316 #endif
317 }
318 }
319
endAction(OOXMLFastContextHandler * pHandler,sal_Int32)320 void OOXMLFactory::endAction(OOXMLFastContextHandler * pHandler, sal_Int32 /*nToken*/)
321 {
322 Id nDefine = pHandler->getDefine();
323 OOXMLFactory_ns::Pointer_t pFactory = getFactoryForNamespace(nDefine);
324
325 if (pFactory.get() != NULL)
326 {
327 #ifdef DEBUG_FACTORY
328 debug_logger->startElement("factory.endAction");
329 #endif
330 pFactory->endAction(pHandler);
331 #ifdef DEBUG_FACTORY
332 debug_logger->endElement("factory.endAction");
333 #endif
334 }
335 }
336
startAction(OOXMLFastContextHandler *)337 void OOXMLFactory_ns::startAction(OOXMLFastContextHandler *)
338 {
339 }
340
endAction(OOXMLFastContextHandler *)341 void OOXMLFactory_ns::endAction(OOXMLFastContextHandler *)
342 {
343 }
344
charactersAction(OOXMLFastContextHandler *,const::rtl::OUString &)345 void OOXMLFactory_ns::charactersAction(OOXMLFastContextHandler *, const ::rtl::OUString &)
346 {
347 }
348
attributeAction(OOXMLFastContextHandler *,sal_Int32,OOXMLValue::Pointer_t)349 void OOXMLFactory_ns::attributeAction(OOXMLFastContextHandler *, sal_Int32, OOXMLValue::Pointer_t)
350 {
351 }
352
353 #ifdef DEBUG_FACTORY
getName() const354 string OOXMLFactory_ns::getName() const
355 {
356 return "noname";
357 }
358 #endif
359
360 }
361 }
362
363