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 :
10 : #include "celltextparacontext.hxx"
11 : #include "xmlimprt.hxx"
12 : #include "xmlcelli.hxx"
13 :
14 : #include "xmloff/nmspmap.hxx"
15 : #include "comphelper/string.hxx"
16 :
17 : #include <com/sun/star/xml/sax/XAttributeList.hpp>
18 :
19 : using namespace com::sun::star;
20 :
21 0 : ScXMLCellTextParaContext::ScXMLCellTextParaContext(
22 : ScXMLImport& rImport, sal_uInt16 nPrefix, const OUString& rLName, ScXMLTableRowCellContext& rParent) :
23 : ScXMLImportContext(rImport, nPrefix, rLName),
24 0 : mrParentCxt(rParent)
25 : {
26 0 : }
27 :
28 0 : void ScXMLCellTextParaContext::StartElement(const uno::Reference<xml::sax::XAttributeList>& /*xAttrList*/)
29 : {
30 0 : }
31 :
32 0 : void ScXMLCellTextParaContext::EndElement()
33 : {
34 0 : if (!maContent.isEmpty())
35 0 : mrParentCxt.PushParagraphSpan(maContent, OUString());
36 :
37 0 : mrParentCxt.PushParagraphEnd();
38 0 : }
39 :
40 0 : void ScXMLCellTextParaContext::Characters(const OUString& rChars)
41 : {
42 0 : maContent += rChars;
43 0 : }
44 :
45 0 : SvXMLImportContext* ScXMLCellTextParaContext::CreateChildContext(
46 : sal_uInt16 nPrefix, const OUString& rLocalName, const uno::Reference<xml::sax::XAttributeList>& /*xAttrList*/)
47 : {
48 0 : if (!maContent.isEmpty())
49 : {
50 0 : mrParentCxt.PushParagraphSpan(maContent, OUString());
51 0 : maContent = OUString();
52 : }
53 :
54 0 : const SvXMLTokenMap& rTokenMap = GetScImport().GetCellTextParaElemTokenMap();
55 0 : switch (rTokenMap.Get(nPrefix, rLocalName))
56 : {
57 : case XML_TOK_CELL_TEXT_S:
58 0 : return new ScXMLCellFieldSContext(GetScImport(), nPrefix, rLocalName, *this);
59 : case XML_TOK_CELL_TEXT_SPAN:
60 0 : return new ScXMLCellTextSpanContext(GetScImport(), nPrefix, rLocalName, *this);
61 : case XML_TOK_CELL_TEXT_SHEET_NAME:
62 0 : return new ScXMLCellFieldSheetNameContext(GetScImport(), nPrefix, rLocalName, *this);
63 : case XML_TOK_CELL_TEXT_DATE:
64 0 : return new ScXMLCellFieldDateContext(GetScImport(), nPrefix, rLocalName, *this);
65 : case XML_TOK_CELL_TEXT_TITLE:
66 0 : return new ScXMLCellFieldTitleContext(GetScImport(), nPrefix, rLocalName, *this);
67 : case XML_TOK_CELL_TEXT_URL:
68 0 : return new ScXMLCellFieldURLContext(GetScImport(), nPrefix, rLocalName, *this);
69 : default:
70 : ;
71 : }
72 :
73 0 : return new SvXMLImportContext(GetImport(), nPrefix, rLocalName);
74 : }
75 :
76 0 : void ScXMLCellTextParaContext::PushSpan(const OUString& rSpan, const OUString& rStyleName)
77 : {
78 0 : mrParentCxt.PushParagraphSpan(rSpan, rStyleName);
79 0 : }
80 :
81 0 : void ScXMLCellTextParaContext::PushFieldSheetName(const OUString& rStyleName)
82 : {
83 0 : mrParentCxt.PushParagraphFieldSheetName(rStyleName);
84 0 : }
85 :
86 0 : void ScXMLCellTextParaContext::PushFieldDate(const OUString& rStyleName)
87 : {
88 0 : mrParentCxt.PushParagraphFieldDate(rStyleName);
89 0 : }
90 :
91 0 : void ScXMLCellTextParaContext::PushFieldTitle(const OUString& rStyleName)
92 : {
93 0 : mrParentCxt.PushParagraphFieldDocTitle(rStyleName);
94 0 : }
95 :
96 0 : void ScXMLCellTextParaContext::PushFieldURL(
97 : const OUString& rURL, const OUString& rRep, const OUString& rStyleName)
98 : {
99 0 : mrParentCxt.PushParagraphFieldURL(rURL, rRep, rStyleName);
100 0 : }
101 :
102 0 : ScXMLCellTextSpanContext::ScXMLCellTextSpanContext(
103 : ScXMLImport& rImport, sal_uInt16 nPrefix, const OUString& rLName, ScXMLCellTextParaContext& rParent) :
104 : ScXMLImportContext(rImport, nPrefix, rLName),
105 0 : mrParentCxt(rParent)
106 : {
107 0 : }
108 :
109 0 : void ScXMLCellTextSpanContext::StartElement(const uno::Reference<xml::sax::XAttributeList>& xAttrList)
110 : {
111 0 : if (!xAttrList.is())
112 0 : return;
113 :
114 0 : OUString aLocalName;
115 0 : sal_Int16 nAttrCount = xAttrList->getLength();
116 :
117 0 : const SvXMLTokenMap& rTokenMap = GetScImport().GetCellTextSpanAttrTokenMap();
118 0 : for (sal_Int16 i = 0; i < nAttrCount; ++i)
119 : {
120 0 : sal_uInt16 nAttrPrefix = GetImport().GetNamespaceMap().GetKeyByAttrName(
121 0 : xAttrList->getNameByIndex(i), &aLocalName);
122 :
123 0 : const OUString& rAttrValue = xAttrList->getValueByIndex(i);
124 0 : sal_uInt16 nToken = rTokenMap.Get(nAttrPrefix, aLocalName);
125 0 : switch (nToken)
126 : {
127 : case XML_TOK_CELL_TEXT_SPAN_ATTR_STYLE_NAME:
128 0 : maStyleName = rAttrValue;
129 0 : break;
130 : default:
131 : ;
132 : }
133 0 : }
134 : }
135 :
136 0 : void ScXMLCellTextSpanContext::EndElement()
137 : {
138 0 : if (!maContent.isEmpty())
139 : {
140 0 : mrParentCxt.PushSpan(maContent, maStyleName);
141 : }
142 0 : }
143 :
144 0 : void ScXMLCellTextSpanContext::Characters(const OUString& rChars)
145 : {
146 0 : maContent += rChars;
147 0 : }
148 :
149 0 : SvXMLImportContext* ScXMLCellTextSpanContext::CreateChildContext(
150 : sal_uInt16 nPrefix, const OUString& rLocalName, const uno::Reference<xml::sax::XAttributeList>& /*xAttrList*/)
151 : {
152 0 : if (!maContent.isEmpty())
153 : {
154 0 : mrParentCxt.PushSpan(maContent, maStyleName);
155 0 : maContent = OUString();
156 : }
157 :
158 0 : const SvXMLTokenMap& rTokenMap = GetScImport().GetCellTextSpanElemTokenMap();
159 0 : switch (rTokenMap.Get(nPrefix, rLocalName))
160 : {
161 : case XML_TOK_CELL_TEXT_SPAN_ELEM_SHEET_NAME:
162 : {
163 0 : ScXMLCellFieldSheetNameContext* p = new ScXMLCellFieldSheetNameContext(GetScImport(), nPrefix, rLocalName, mrParentCxt);
164 0 : p->SetStyleName(maStyleName);
165 0 : return p;
166 : }
167 : case XML_TOK_CELL_TEXT_SPAN_ELEM_DATE:
168 : {
169 0 : ScXMLCellFieldDateContext* p = new ScXMLCellFieldDateContext(GetScImport(), nPrefix, rLocalName, mrParentCxt);
170 0 : p->SetStyleName(maStyleName);
171 0 : return p;
172 : }
173 : case XML_TOK_CELL_TEXT_SPAN_ELEM_TITLE:
174 : {
175 0 : ScXMLCellFieldTitleContext* p = new ScXMLCellFieldTitleContext(GetScImport(), nPrefix, rLocalName, mrParentCxt);
176 0 : p->SetStyleName(maStyleName);
177 0 : return p;
178 : }
179 : case XML_TOK_CELL_TEXT_SPAN_ELEM_URL:
180 : {
181 0 : ScXMLCellFieldURLContext* p = new ScXMLCellFieldURLContext(GetScImport(), nPrefix, rLocalName, mrParentCxt);
182 0 : p->SetStyleName(maStyleName);
183 0 : return p;
184 : }
185 : case XML_TOK_CELL_TEXT_SPAN_ELEM_S:
186 : {
187 0 : ScXMLCellFieldSContext* p = new ScXMLCellFieldSContext(GetScImport(), nPrefix, rLocalName, mrParentCxt);
188 0 : p->SetStyleName(maStyleName);
189 0 : return p;
190 : }
191 : default:
192 : ;
193 : }
194 :
195 0 : return new SvXMLImportContext(GetImport(), nPrefix, rLocalName);
196 : }
197 :
198 0 : ScXMLCellFieldSheetNameContext::ScXMLCellFieldSheetNameContext(
199 : ScXMLImport& rImport, sal_uInt16 nPrefix, const OUString& rLName, ScXMLCellTextParaContext& rParent) :
200 : ScXMLImportContext(rImport, nPrefix, rLName),
201 0 : mrParentCxt(rParent)
202 : {
203 0 : }
204 :
205 0 : void ScXMLCellFieldSheetNameContext::SetStyleName(const OUString& rStyleName)
206 : {
207 0 : maStyleName = rStyleName;
208 0 : }
209 :
210 0 : void ScXMLCellFieldSheetNameContext::StartElement(const uno::Reference<xml::sax::XAttributeList>& /*xAttrList*/)
211 : {
212 : // <text:sheet-name> has no attributes (that I'm aware of).
213 0 : }
214 :
215 0 : void ScXMLCellFieldSheetNameContext::EndElement()
216 : {
217 0 : mrParentCxt.PushFieldSheetName(maStyleName);
218 0 : }
219 :
220 0 : void ScXMLCellFieldSheetNameContext::Characters(const OUString& /*rChars*/)
221 : {
222 0 : }
223 :
224 0 : SvXMLImportContext* ScXMLCellFieldSheetNameContext::CreateChildContext(
225 : sal_uInt16 nPrefix, const OUString& rLocalName, const uno::Reference<xml::sax::XAttributeList>& /*xAttrList*/)
226 : {
227 0 : return new SvXMLImportContext(GetImport(), nPrefix, rLocalName);
228 : }
229 :
230 0 : ScXMLCellFieldDateContext::ScXMLCellFieldDateContext(
231 : ScXMLImport& rImport, sal_uInt16 nPrefix, const OUString& rLName, ScXMLCellTextParaContext& rParent) :
232 : ScXMLImportContext(rImport, nPrefix, rLName),
233 0 : mrParentCxt(rParent)
234 : {
235 0 : }
236 :
237 0 : void ScXMLCellFieldDateContext::SetStyleName(const OUString& rStyleName)
238 : {
239 0 : maStyleName = rStyleName;
240 0 : }
241 :
242 0 : void ScXMLCellFieldDateContext::StartElement(const uno::Reference<xml::sax::XAttributeList>& /*xAttrList*/)
243 : {
244 0 : }
245 :
246 0 : void ScXMLCellFieldDateContext::EndElement()
247 : {
248 0 : mrParentCxt.PushFieldDate(maStyleName);
249 0 : }
250 :
251 0 : void ScXMLCellFieldDateContext::Characters(const OUString& /*rChars*/)
252 : {
253 0 : }
254 :
255 0 : SvXMLImportContext* ScXMLCellFieldDateContext::CreateChildContext(
256 : sal_uInt16 nPrefix, const OUString& rLocalName, const uno::Reference<xml::sax::XAttributeList>& /*xAttrList*/)
257 : {
258 0 : return new SvXMLImportContext(GetImport(), nPrefix, rLocalName);
259 : }
260 :
261 0 : ScXMLCellFieldTitleContext::ScXMLCellFieldTitleContext(
262 : ScXMLImport& rImport, sal_uInt16 nPrefix, const OUString& rLName, ScXMLCellTextParaContext& rParent) :
263 : ScXMLImportContext(rImport, nPrefix, rLName),
264 0 : mrParentCxt(rParent)
265 : {
266 0 : }
267 :
268 0 : void ScXMLCellFieldTitleContext::SetStyleName(const OUString& rStyleName)
269 : {
270 0 : maStyleName = rStyleName;
271 0 : }
272 :
273 0 : void ScXMLCellFieldTitleContext::StartElement(const uno::Reference<xml::sax::XAttributeList>& /*xAttrList*/)
274 : {
275 0 : }
276 :
277 0 : void ScXMLCellFieldTitleContext::EndElement()
278 : {
279 0 : mrParentCxt.PushFieldTitle(maStyleName);
280 0 : }
281 :
282 0 : void ScXMLCellFieldTitleContext::Characters(const OUString& /*rChars*/)
283 : {
284 0 : }
285 :
286 0 : SvXMLImportContext* ScXMLCellFieldTitleContext::CreateChildContext(
287 : sal_uInt16 nPrefix, const OUString& rLocalName, const uno::Reference<xml::sax::XAttributeList>& /*xAttrList*/)
288 : {
289 0 : return new SvXMLImportContext(GetImport(), nPrefix, rLocalName);
290 : }
291 :
292 0 : ScXMLCellFieldURLContext::ScXMLCellFieldURLContext(
293 : ScXMLImport& rImport, sal_uInt16 nPrefix, const OUString& rLName, ScXMLCellTextParaContext& rParent) :
294 : ScXMLImportContext(rImport, nPrefix, rLName),
295 0 : mrParentCxt(rParent)
296 : {
297 0 : }
298 :
299 0 : void ScXMLCellFieldURLContext::SetStyleName(const OUString& rStyleName)
300 : {
301 0 : maStyleName = rStyleName;
302 0 : }
303 :
304 0 : void ScXMLCellFieldURLContext::StartElement(const uno::Reference<xml::sax::XAttributeList>& xAttrList)
305 : {
306 0 : if (!xAttrList.is())
307 0 : return;
308 :
309 0 : OUString aLocalName;
310 0 : sal_Int16 nAttrCount = xAttrList->getLength();
311 :
312 0 : const SvXMLTokenMap& rTokenMap = GetScImport().GetCellTextURLAttrTokenMap();
313 0 : for (sal_Int16 i = 0; i < nAttrCount; ++i)
314 : {
315 0 : sal_uInt16 nAttrPrefix = GetImport().GetNamespaceMap().GetKeyByAttrName(
316 0 : xAttrList->getNameByIndex(i), &aLocalName);
317 :
318 0 : const OUString& rAttrValue = xAttrList->getValueByIndex(i);
319 0 : sal_uInt16 nToken = rTokenMap.Get(nAttrPrefix, aLocalName);
320 0 : switch (nToken)
321 : {
322 : case XML_TOK_CELL_TEXT_URL_ATTR_UREF:
323 0 : maURL = rAttrValue;
324 0 : break;
325 : case XML_TOK_CELL_TEXT_URL_ATTR_TYPE:
326 : // Ignored for now.
327 0 : break;
328 : default:
329 : ;
330 : }
331 0 : }
332 : }
333 :
334 0 : void ScXMLCellFieldURLContext::EndElement()
335 : {
336 0 : mrParentCxt.PushFieldURL(maURL, maRep, maStyleName);
337 0 : }
338 :
339 0 : void ScXMLCellFieldURLContext::Characters(const OUString& rChars)
340 : {
341 0 : maRep += rChars;
342 0 : }
343 :
344 0 : SvXMLImportContext* ScXMLCellFieldURLContext::CreateChildContext(
345 : sal_uInt16 nPrefix, const OUString& rLocalName, const uno::Reference<xml::sax::XAttributeList>& /*xAttrList*/)
346 : {
347 0 : return new SvXMLImportContext(GetImport(), nPrefix, rLocalName);
348 : }
349 :
350 0 : ScXMLCellFieldSContext::ScXMLCellFieldSContext(
351 : ScXMLImport& rImport, sal_uInt16 nPrefix, const OUString& rLName, ScXMLCellTextParaContext& rParent) :
352 : ScXMLImportContext(rImport, nPrefix, rLName),
353 : mrParentCxt(rParent),
354 0 : mnCount(1)
355 : {
356 0 : }
357 :
358 0 : void ScXMLCellFieldSContext::SetStyleName(const OUString& rStyleName)
359 : {
360 0 : maStyleName = rStyleName;
361 0 : }
362 :
363 0 : void ScXMLCellFieldSContext::StartElement(const uno::Reference<xml::sax::XAttributeList>& xAttrList)
364 : {
365 0 : if (!xAttrList.is())
366 0 : return;
367 :
368 0 : OUString aLocalName;
369 0 : sal_Int16 nAttrCount = xAttrList->getLength();
370 :
371 0 : const SvXMLTokenMap& rTokenMap = GetScImport().GetCellTextSAttrTokenMap();
372 0 : for (sal_Int16 i = 0; i < nAttrCount; ++i)
373 : {
374 0 : sal_uInt16 nAttrPrefix = GetImport().GetNamespaceMap().GetKeyByAttrName(
375 0 : xAttrList->getNameByIndex(i), &aLocalName);
376 :
377 0 : const OUString& rAttrValue = xAttrList->getValueByIndex(i);
378 0 : sal_uInt16 nToken = rTokenMap.Get(nAttrPrefix, aLocalName);
379 0 : switch (nToken)
380 : {
381 : case XML_TOK_CELL_TEXT_S_ATTR_C:
382 0 : mnCount = rAttrValue.toInt32();
383 0 : if (mnCount <= 0)
384 0 : mnCount = 1; // worth a warning?
385 0 : break;
386 : default:
387 : ;
388 : }
389 0 : }
390 : }
391 :
392 0 : void ScXMLCellFieldSContext::EndElement()
393 : {
394 0 : if (mnCount)
395 0 : PushSpaces();
396 0 : }
397 :
398 0 : SvXMLImportContext* ScXMLCellFieldSContext::CreateChildContext(
399 : sal_uInt16 nPrefix, const OUString& rLocalName, const uno::Reference<xml::sax::XAttributeList>& /*xAttrList*/)
400 : {
401 : // <text:s> does not have child elements, but ...
402 0 : if (mnCount)
403 : {
404 0 : PushSpaces();
405 0 : mnCount = 0;
406 : }
407 :
408 0 : return new SvXMLImportContext(GetImport(), nPrefix, rLocalName);
409 : }
410 :
411 0 : void ScXMLCellFieldSContext::PushSpaces()
412 : {
413 0 : if (mnCount > 0)
414 : {
415 0 : if (mnCount == 1)
416 0 : mrParentCxt.PushSpan(" ", maStyleName);
417 : else
418 : {
419 0 : OUStringBuffer aBuf( mnCount);
420 0 : comphelper::string::padToLength( aBuf, mnCount, ' ');
421 0 : mrParentCxt.PushSpan( aBuf.makeStringAndClear(), maStyleName);
422 : }
423 : }
424 0 : }
425 :
426 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|