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 #ifndef INCLUDED_FAST_HELPER_HXX
24 #define INCLUDED_FAST_HELPER_HXX
25
26 #include <iostream>
27 #include <resourcemodel/QNameToString.hxx>
28 #include "OOXMLFastContextHandler.hxx"
29 #include "ooxmlLoggers.hxx"
30 namespace writerfilter {
31
32 namespace ooxml
33 {
34 using namespace ::std;
35 using namespace ::com::sun::star;
36 using namespace ::com::sun::star::xml::sax;
37
38 template <class T>
39 class OOXMLFastHelper
40 {
41 public:
42 static uno::Reference<XFastContextHandler> createAndSetParent
43 (OOXMLFastContextHandler * pHandler, sal_uInt32 nToken, Id nId);
44
45 static uno::Reference<XFastContextHandler> createAndSetParentAndDefine
46 (OOXMLFastContextHandler * pHandler, sal_uInt32 nToken, Id nId, Id nDefine);
47
48 static uno::Reference<XFastContextHandler> createAndSetParentRef
49 (OOXMLFastContextHandler * pHandler, sal_uInt32 nToken,
50 const uno::Reference < xml::sax::XFastAttributeList > & Attribs);
51
52 static void newProperty(OOXMLFastContextHandler * pHandler,
53 Id nId,
54 const ::rtl::OUString & rValue);
55
56 static void newProperty(OOXMLFastContextHandler * pHandler,
57 Id nId, sal_Int32 nValue);
58
59 static void mark(OOXMLFastContextHandler * pHandler,
60 Id nId,
61 const ::rtl::OUString & rValue);
62
63 static void attributes
64 (OOXMLFastContextHandler * pContext,
65 const uno::Reference < xml::sax::XFastAttributeList > & Attribs);
66 };
67
68 template <class T>
69 uno::Reference<XFastContextHandler>
createAndSetParent(OOXMLFastContextHandler * pHandler,sal_uInt32 nToken,Id nId)70 OOXMLFastHelper<T>::createAndSetParent
71 (OOXMLFastContextHandler * pHandler, sal_uInt32 nToken, Id nId)
72 {
73 #ifdef DEBUG_HELPER
74 debug_logger->startElement("helper.createAndSetParent");
75 debug_logger->attribute("context", pHandler->getType());
76 debug_logger->attribute("id", (*QNameToString::Instance())(nId));
77 #endif
78
79 OOXMLFastContextHandler * pTmp = new T(pHandler);
80
81 pTmp->setToken(nToken);
82 pTmp->setId(nId);
83
84 #ifdef DEBUG_CREATE
85 debug_logger->startElement("createAndSetParent");
86 debug_logger->attribute("context", pHandler->getType());
87 debug_logger->attribute("token", fastTokenToId(pTmp->getToken()));
88 debug_logger->attribute("id", (*QNameToString::Instance())(nId));
89
90 debug_logger->startElement("created");
91 debug_logger->addTag(pTmp->toTag());
92 debug_logger->endElement("created");
93 debug_logger->endElement("helper.createAndSetParent");
94 #endif
95
96 uno::Reference<XFastContextHandler> aResult(pTmp);
97
98 return aResult;
99 }
100
101 template <class T>
102 uno::Reference<XFastContextHandler>
createAndSetParentAndDefine(OOXMLFastContextHandler * pHandler,sal_uInt32 nToken,Id nId,Id nDefine)103 OOXMLFastHelper<T>::createAndSetParentAndDefine
104 (OOXMLFastContextHandler * pHandler, sal_uInt32 nToken, Id nId, Id nDefine)
105 {
106 #ifdef DEBUG_HELPER
107 debug_logger->startElement("helper.createAndSetParentAndDefine");
108 debug_logger->attribute("context", pHandler->getType());
109 debug_logger->attribute("id", (*QNameToString::Instance())(nId));
110
111 static char buffer[16];
112 snprintf(buffer, sizeof(buffer), "0x%08" SAL_PRIxUINT32, nId);
113
114 debug_logger->attribute("idnum", buffer);
115 #endif
116
117 OOXMLFastContextHandler * pTmp = new T(pHandler);
118
119 pTmp->setToken(nToken);
120 pTmp->setId(nId);
121 pTmp->setDefine(nDefine);
122
123
124 #ifdef DEBUG_HELPER
125 debug_logger->startElement("created");
126 debug_logger->addTag(pTmp->toTag());
127 debug_logger->endElement("created");
128 debug_logger->endElement("helper.createAndSetParentAndDefine");
129 #endif
130
131 uno::Reference<XFastContextHandler> aResult(pTmp);
132
133 return aResult;
134 }
135
136 template <class T>
137 uno::Reference<XFastContextHandler>
createAndSetParentRef(OOXMLFastContextHandler * pHandler,sal_uInt32 nToken,const uno::Reference<xml::sax::XFastAttributeList> & Attribs)138 OOXMLFastHelper<T>::createAndSetParentRef
139 (OOXMLFastContextHandler * pHandler, sal_uInt32 nToken,
140 const uno::Reference < xml::sax::XFastAttributeList > & Attribs)
141 {
142 #ifdef DEBUG_HELPER
143 debug_logger->startElement("helper.createAndSetParentRef");
144 debug_logger->attribute("context", pHandler->getType());
145 debug_logger->attribute("type", fastTokenToId(nToken));
146 #endif
147
148 boost::shared_ptr<OOXMLFastContextHandler> pTmp(new T(pHandler));
149
150 uno::Reference<XFastContextHandler> xChild =
151 pTmp->createFastChildContext(nToken, Attribs);
152
153 OOXMLFastContextHandler * pResult = NULL;
154 if (xChild.is())
155 {
156 pResult = dynamic_cast<OOXMLFastContextHandler *>(xChild.get());
157 pResult->setToken(nToken);
158 pResult->setParent(pHandler);
159 }
160
161
162 #ifdef DEBUG_HELPER
163 debug_logger->startElement("created");
164 debug_logger->addTag(pTmp->toTag());
165 debug_logger->endElement("created");
166 debug_logger->endElement("helper.createAndSetParentRef");
167 #endif
168
169 return xChild;
170 }
171
172 template <class T>
newProperty(OOXMLFastContextHandler * pHandler,Id nId,const::rtl::OUString & rValue)173 void OOXMLFastHelper<T>::newProperty(OOXMLFastContextHandler * pHandler,
174 Id nId,
175 const ::rtl::OUString & rValue)
176 {
177 OOXMLValue::Pointer_t pVal(new T(rValue));
178
179 #ifdef DEBUG_HELPER
180 string aStr = (*QNameToString::Instance())(nId);
181
182 debug_logger->startElement("newProperty-from-string");
183 debug_logger->attribute("name", aStr);
184 debug_logger->attribute
185 ("value",
186 ::rtl::OUStringToOString
187 (rValue, RTL_TEXTENCODING_ASCII_US).getStr());
188
189 if (aStr.size() == 0)
190 debug_logger->addTag(XMLTag::Pointer_t(new XMLTag("unknown-qname")));
191 #endif
192
193 pHandler->newProperty(nId, pVal);
194
195 #ifdef DEBUG_HELPER
196 debug_logger->endElement("newProperty-from-string");
197 #endif
198
199 }
200
201 template <class T>
newProperty(OOXMLFastContextHandler * pHandler,Id nId,sal_Int32 nVal)202 void OOXMLFastHelper<T>::newProperty(OOXMLFastContextHandler * pHandler,
203 Id nId,
204 sal_Int32 nVal)
205 {
206 OOXMLValue::Pointer_t pVal(new T(nVal));
207
208 #ifdef DEBUG_HELPER
209 string aStr = (*QNameToString::Instance())(nId);
210
211 debug_logger->startElement("helper.newProperty-from-int");
212 debug_logger->attribute("name", aStr);
213 debug_logger->attribute("value", pVal->toString());
214
215 if (aStr.size() == 0)
216 debug_logger->addTag(XMLTag::Pointer_t(new XMLTag("unknown-qname")));
217
218 debug_logger->endElement("helper.newProperty-from-int");
219 #endif
220
221 pHandler->newProperty(nId, pVal);
222 }
223
224 template <class T>
mark(OOXMLFastContextHandler * pHandler,Id nId,const::rtl::OUString & rValue)225 void OOXMLFastHelper<T>::mark(OOXMLFastContextHandler * pHandler,
226 Id nId,
227 const ::rtl::OUString & rValue)
228 {
229 OOXMLValue::Pointer_t pVal(new T(rValue));
230
231 string aStr = (*QNameToString::Instance())(nId);
232
233 #ifdef DEBUG_HELPER
234 debug_logger->startElement("helper.mark");
235 debug_logger->attribute("name", aStr);
236 debug_logger->attribute
237 ("value",
238 ::rtl::OUStringToOString
239 (rValue, RTL_TEXTENCODING_ASCII_US).getStr());
240
241 if (aStr.size() == 0)
242 debug_logger->addTag(XMLTag::Pointer_t(new XMLTag("unknown-qname")));
243
244 debug_logger->endElement("helper.mark");
245 #endif
246
247 pHandler->mark(nId, pVal);
248 }
249 }}
250 #endif // INCLUDED_FAST_HELPER_HXX
251