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