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 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include "XMLIndexTemplateContext.hxx"
21 : #include "XMLIndexSimpleEntryContext.hxx"
22 : #include "XMLIndexSpanEntryContext.hxx"
23 : #include "XMLIndexTabStopEntryContext.hxx"
24 : #include "XMLIndexBibliographyEntryContext.hxx"
25 : #include "XMLIndexChapterInfoEntryContext.hxx"
26 : #include <xmloff/xmlictxt.hxx>
27 : #include <xmloff/xmlimp.hxx>
28 : #include <xmloff/txtimp.hxx>
29 : #include <xmloff/nmspmap.hxx>
30 : #include <xmloff/xmlnmspe.hxx>
31 : #include <xmloff/xmltoken.hxx>
32 : #include <xmloff/xmluconv.hxx>
33 : #include <tools/debug.hxx>
34 : #include <rtl/ustring.hxx>
35 : #include <rtl/ustrbuf.hxx>
36 : #include <com/sun/star/container/XIndexReplace.hpp>
37 :
38 :
39 : using namespace ::std;
40 : using namespace ::xmloff::token;
41 :
42 : using ::com::sun::star::beans::XPropertySet;
43 : using ::com::sun::star::beans::PropertyValue;
44 : using ::com::sun::star::beans::PropertyValues;
45 : using ::com::sun::star::uno::Reference;
46 : using ::com::sun::star::uno::Sequence;
47 : using ::com::sun::star::uno::Any;
48 : using ::com::sun::star::xml::sax::XAttributeList;
49 : using ::com::sun::star::container::XIndexReplace;
50 :
51 : const sal_Char sAPI_TokenEntryNumber[] = "TokenEntryNumber";
52 : const sal_Char sAPI_TokenEntryText[] = "TokenEntryText";
53 : const sal_Char sAPI_TokenTabStop[] = "TokenTabStop";
54 : const sal_Char sAPI_TokenText[] = "TokenText";
55 : const sal_Char sAPI_TokenPageNumber[] = "TokenPageNumber";
56 : const sal_Char sAPI_TokenChapterInfo[] = "TokenChapterInfo";
57 : const sal_Char sAPI_TokenHyperlinkStart[] = "TokenHyperlinkStart";
58 : const sal_Char sAPI_TokenHyperlinkEnd[] = "TokenHyperlinkEnd";
59 : const sal_Char sAPI_TokenBibliographyDataField[] =
60 : "TokenBibliographyDataField";
61 :
62 :
63 0 : TYPEINIT1( XMLIndexTemplateContext, SvXMLImportContext);
64 :
65 278 : XMLIndexTemplateContext::XMLIndexTemplateContext(
66 : SvXMLImport& rImport,
67 : Reference<XPropertySet> & rPropSet,
68 : sal_uInt16 nPrfx,
69 : const OUString& rLocalName,
70 : const SvXMLEnumMapEntry* pLevelNameMap,
71 : enum XMLTokenEnum eLevelAttrName,
72 : const sal_Char** pLevelStylePropMap,
73 : const sal_Bool* pAllowedTokenTypes,
74 : bool bT )
75 : : SvXMLImportContext(rImport, nPrfx, rLocalName)
76 : , pOutlineLevelNameMap(pLevelNameMap)
77 : , eOutlineLevelAttrName(eLevelAttrName)
78 : , pOutlineLevelStylePropMap(pLevelStylePropMap)
79 : , pAllowedTokenTypesMap(pAllowedTokenTypes)
80 : , nOutlineLevel(1) // all indices have level 1 (0 is for header)
81 : , bStyleNameOK(false)
82 : , bOutlineLevelOK(false)
83 : , bTOC( bT )
84 : , rPropertySet(rPropSet)
85 : , sTokenEntryNumber(sAPI_TokenEntryNumber)
86 : , sTokenEntryText(sAPI_TokenEntryText)
87 : , sTokenTabStop(sAPI_TokenTabStop)
88 : , sTokenText(sAPI_TokenText)
89 : , sTokenPageNumber(sAPI_TokenPageNumber)
90 : , sTokenChapterInfo(sAPI_TokenChapterInfo)
91 : , sTokenHyperlinkStart(sAPI_TokenHyperlinkStart)
92 : , sTokenHyperlinkEnd(sAPI_TokenHyperlinkEnd)
93 : , sTokenBibliographyDataField(sAPI_TokenBibliographyDataField)
94 :
95 : , sCharacterStyleName("CharacterStyleName")
96 : , sTokenType("TokenType")
97 : , sText("Text")
98 : , sTabStopRightAligned("TabStopRightAligned")
99 : , sTabStopPosition("TabStopPosition")
100 : , sTabStopFillCharacter("TabStopFillCharacter")
101 : , sBibliographyDataField("BibliographyDataField")
102 : , sChapterFormat("ChapterFormat")
103 : , sChapterLevel("ChapterLevel") //#i53420
104 :
105 : , sLevelFormat("LevelFormat")
106 278 : , sParaStyleLevel("ParaStyleLevel")
107 : {
108 : DBG_ASSERT( ((XML_TOKEN_INVALID != eLevelAttrName) && (NULL != pLevelNameMap))
109 : || ((XML_TOKEN_INVALID == eLevelAttrName) && (NULL == pLevelNameMap)),
110 : "need both, attribute name and value map, or neither" );
111 : DBG_ASSERT( NULL != pOutlineLevelStylePropMap, "need property name map" );
112 : DBG_ASSERT( NULL != pAllowedTokenTypes, "need allowed tokens map" );
113 :
114 : // no map for outline-level? then use 1
115 278 : if (NULL == pLevelNameMap)
116 : {
117 14 : nOutlineLevel = 1;
118 14 : bOutlineLevelOK = true;
119 : }
120 278 : }
121 :
122 556 : XMLIndexTemplateContext::~XMLIndexTemplateContext()
123 : {
124 556 : }
125 :
126 :
127 1426 : void XMLIndexTemplateContext::addTemplateEntry(
128 : const PropertyValues& aValues)
129 : {
130 1426 : aValueVector.push_back(aValues);
131 1426 : }
132 :
133 :
134 278 : void XMLIndexTemplateContext::StartElement(
135 : const Reference<XAttributeList> & xAttrList)
136 : {
137 : // process two attributes: style-name, outline-level
138 278 : sal_Int16 nLength = xAttrList->getLength();
139 820 : for(sal_Int16 nAttr = 0; nAttr < nLength; nAttr++)
140 : {
141 542 : OUString sLocalName;
142 542 : sal_uInt16 nPrefix = GetImport().GetNamespaceMap().
143 542 : GetKeyByAttrName( xAttrList->getNameByIndex(nAttr),
144 1084 : &sLocalName );
145 542 : if (XML_NAMESPACE_TEXT == nPrefix)
146 : {
147 542 : if ( IsXMLToken( sLocalName, XML_STYLE_NAME ) )
148 : {
149 : // style name
150 278 : sStyleName = xAttrList->getValueByIndex(nAttr);
151 278 : bStyleNameOK = true;
152 : }
153 264 : else if (eOutlineLevelAttrName != XML_TOKEN_INVALID)
154 : {
155 : // we have an attr name! Then see if we have the attr, too.
156 264 : if (IsXMLToken(sLocalName, eOutlineLevelAttrName))
157 : {
158 : // outline level
159 : sal_uInt16 nTmp;
160 528 : if (SvXMLUnitConverter::convertEnum(
161 264 : nTmp, xAttrList->getValueByIndex(nAttr),
162 792 : pOutlineLevelNameMap))
163 : {
164 264 : nOutlineLevel = nTmp;
165 264 : bOutlineLevelOK = true;
166 : }
167 : // else: illegal value -> ignore
168 : }
169 : // else: unknown attribute -> ignore
170 : }
171 : // else: we don't care about outline-level -> ignore
172 : }
173 : // else: attribute not in text namespace -> ignore
174 542 : }
175 278 : }
176 :
177 278 : void XMLIndexTemplateContext::EndElement()
178 : {
179 278 : if (bOutlineLevelOK)
180 : {
181 278 : const sal_Int32 nCount = aValueVector.size();
182 278 : Sequence<PropertyValues> aValueSequence(nCount);
183 1704 : for(sal_Int32 i = 0; i<nCount; i++)
184 : {
185 1426 : aValueSequence[i] = aValueVector[i];
186 : }
187 :
188 : // get LevelFormat IndexReplace ...
189 556 : Any aAny = rPropertySet->getPropertyValue(sLevelFormat);
190 556 : Reference<XIndexReplace> xIndexReplace;
191 278 : aAny >>= xIndexReplace;
192 :
193 : // ... and insert
194 278 : aAny <<= aValueSequence;
195 278 : xIndexReplace->replaceByIndex(nOutlineLevel, aAny);
196 :
197 278 : if (bStyleNameOK)
198 : {
199 : const sal_Char* pStyleProperty =
200 278 : pOutlineLevelStylePropMap[nOutlineLevel];
201 :
202 : DBG_ASSERT(NULL != pStyleProperty, "need property name");
203 278 : if (NULL != pStyleProperty)
204 : {
205 : OUString sDisplayStyleName =
206 278 : GetImport().GetStyleDisplayName(
207 : XML_STYLE_FAMILY_TEXT_PARAGRAPH,
208 556 : sStyleName );
209 : // #i50288#: Check if style exists
210 : const Reference < ::com::sun::star::container::XNameContainer > & rStyles =
211 278 : GetImport().GetTextImport()->GetParaStyles();
212 556 : if( rStyles.is() &&
213 278 : rStyles->hasByName( sDisplayStyleName ) )
214 : {
215 22 : aAny <<= sDisplayStyleName;
216 22 : rPropertySet->setPropertyValue(
217 22 : OUString::createFromAscii(pStyleProperty), aAny);
218 278 : }
219 : }
220 278 : }
221 : }
222 278 : }
223 :
224 :
225 :
226 : /// template token types; used for aTokenTypeMap parameter
227 : enum TemplateTokenType
228 : {
229 : XML_TOK_INDEX_TYPE_ENTRY_TEXT = 0,
230 : XML_TOK_INDEX_TYPE_TAB_STOP,
231 : XML_TOK_INDEX_TYPE_TEXT,
232 : XML_TOK_INDEX_TYPE_PAGE_NUMBER,
233 : XML_TOK_INDEX_TYPE_CHAPTER,
234 : XML_TOK_INDEX_TYPE_LINK_START,
235 : XML_TOK_INDEX_TYPE_LINK_END,
236 : XML_TOK_INDEX_TYPE_BIBLIOGRAPHY
237 : };
238 :
239 :
240 : SvXMLEnumMapEntry aTemplateTokenTypeMap[] =
241 : {
242 : { XML_INDEX_ENTRY_TEXT, XML_TOK_INDEX_TYPE_ENTRY_TEXT },
243 : { XML_INDEX_ENTRY_TAB_STOP, XML_TOK_INDEX_TYPE_TAB_STOP },
244 : { XML_INDEX_ENTRY_SPAN, XML_TOK_INDEX_TYPE_TEXT },
245 : { XML_INDEX_ENTRY_PAGE_NUMBER, XML_TOK_INDEX_TYPE_PAGE_NUMBER },
246 : { XML_INDEX_ENTRY_CHAPTER, XML_TOK_INDEX_TYPE_CHAPTER },
247 : { XML_INDEX_ENTRY_LINK_START, XML_TOK_INDEX_TYPE_LINK_START },
248 : { XML_INDEX_ENTRY_LINK_END, XML_TOK_INDEX_TYPE_LINK_END },
249 : { XML_INDEX_ENTRY_BIBLIOGRAPHY, XML_TOK_INDEX_TYPE_BIBLIOGRAPHY },
250 : { XML_TOKEN_INVALID, 0 }
251 : };
252 :
253 1434 : SvXMLImportContext *XMLIndexTemplateContext::CreateChildContext(
254 : sal_uInt16 nPrefix,
255 : const OUString& rLocalName,
256 : const Reference<XAttributeList> & xAttrList )
257 : {
258 1434 : SvXMLImportContext* pContext = NULL;
259 :
260 1434 : if (XML_NAMESPACE_TEXT == nPrefix)
261 : {
262 : sal_uInt16 nToken;
263 1434 : if (SvXMLUnitConverter::convertEnum(nToken, rLocalName,
264 : aTemplateTokenTypeMap))
265 : {
266 : // can this index accept this kind of token?
267 1434 : if (pAllowedTokenTypesMap[nToken])
268 : {
269 1426 : switch ((TemplateTokenType)nToken)
270 : {
271 : case XML_TOK_INDEX_TYPE_ENTRY_TEXT:
272 : pContext = new XMLIndexSimpleEntryContext(
273 190 : GetImport(), sTokenEntryText, *this,
274 190 : nPrefix, rLocalName);
275 190 : break;
276 :
277 : case XML_TOK_INDEX_TYPE_PAGE_NUMBER:
278 : pContext = new XMLIndexSimpleEntryContext(
279 186 : GetImport(), sTokenPageNumber, *this,
280 186 : nPrefix, rLocalName);
281 186 : break;
282 :
283 : case XML_TOK_INDEX_TYPE_LINK_START:
284 : pContext = new XMLIndexSimpleEntryContext(
285 60 : GetImport(), sTokenHyperlinkStart, *this,
286 60 : nPrefix, rLocalName);
287 60 : break;
288 :
289 : case XML_TOK_INDEX_TYPE_LINK_END:
290 : pContext = new XMLIndexSimpleEntryContext(
291 60 : GetImport(), sTokenHyperlinkEnd, *this,
292 60 : nPrefix, rLocalName);
293 60 : break;
294 :
295 : case XML_TOK_INDEX_TYPE_TEXT:
296 : pContext = new XMLIndexSpanEntryContext(
297 268 : GetImport(), *this, nPrefix, rLocalName);
298 268 : break;
299 :
300 : case XML_TOK_INDEX_TYPE_TAB_STOP:
301 : pContext = new XMLIndexTabStopEntryContext(
302 186 : GetImport(), *this, nPrefix, rLocalName);
303 186 : break;
304 :
305 : case XML_TOK_INDEX_TYPE_BIBLIOGRAPHY:
306 : pContext = new XMLIndexBibliographyEntryContext(
307 356 : GetImport(), *this, nPrefix, rLocalName);
308 356 : break;
309 :
310 : case XML_TOK_INDEX_TYPE_CHAPTER:
311 : pContext = new XMLIndexChapterInfoEntryContext(
312 120 : GetImport(), *this, nPrefix, rLocalName, bTOC );
313 120 : break;
314 :
315 : default:
316 : // ignore!
317 0 : break;
318 : }
319 : }
320 : }
321 : }
322 :
323 : // ignore unknown
324 1434 : if (NULL == pContext)
325 : {
326 : return SvXMLImportContext::CreateChildContext(nPrefix, rLocalName,
327 8 : xAttrList);
328 : }
329 :
330 1426 : return pContext;
331 : }
332 :
333 :
334 :
335 :
336 : // maps for the XMLIndexTemplateContext constructor
337 :
338 :
339 :
340 : // table of content and user defined index:
341 :
342 : const SvXMLEnumMapEntry aSvLevelNameTOCMap[] =
343 : {
344 : { XML_1, 1 },
345 : { XML_2, 2 },
346 : { XML_3, 3 },
347 : { XML_4, 4 },
348 : { XML_5, 5 },
349 : { XML_6, 6 },
350 : { XML_7, 7 },
351 : { XML_8, 8 },
352 : { XML_9, 9 },
353 : { XML_10, 10 },
354 : { XML_TOKEN_INVALID, 0 }
355 : };
356 :
357 : const sal_Char* aLevelStylePropNameTOCMap[] =
358 : { NULL, "ParaStyleLevel1", "ParaStyleLevel2", "ParaStyleLevel3",
359 : "ParaStyleLevel4", "ParaStyleLevel5", "ParaStyleLevel6",
360 : "ParaStyleLevel7", "ParaStyleLevel8", "ParaStyleLevel9",
361 : "ParaStyleLevel10", NULL };
362 :
363 : const sal_Bool aAllowedTokenTypesTOC[] =
364 : {
365 : sal_True, // XML_TOK_INDEX_TYPE_ENTRY_TEXT =
366 : sal_True, // XML_TOK_INDEX_TYPE_TAB_STOP,
367 : sal_True, // XML_TOK_INDEX_TYPE_TEXT,
368 : sal_True, // XML_TOK_INDEX_TYPE_PAGE_NUMBER,
369 : sal_True, // XML_TOK_INDEX_TYPE_CHAPTER,
370 : sal_True, // XML_TOK_INDEX_TYPE_LINK_START,
371 : sal_True, // XML_TOK_INDEX_TYPE_LINK_END,
372 : sal_False // XML_TOK_INDEX_TYPE_BIBLIOGRAPHY
373 : };
374 :
375 : const sal_Bool aAllowedTokenTypesUser[] =
376 : {
377 : sal_True, // XML_TOK_INDEX_TYPE_ENTRY_TEXT =
378 : sal_True, // XML_TOK_INDEX_TYPE_TAB_STOP,
379 : sal_True, // XML_TOK_INDEX_TYPE_TEXT,
380 : sal_True, // XML_TOK_INDEX_TYPE_PAGE_NUMBER,
381 : sal_True, // XML_TOK_INDEX_TYPE_CHAPTER,
382 : sal_False, // XML_TOK_INDEX_TYPE_LINK_START,
383 : sal_False, // XML_TOK_INDEX_TYPE_LINK_END,
384 : sal_False // XML_TOK_INDEX_TYPE_BIBLIOGRAPHY
385 : };
386 :
387 :
388 : // alphabetical index
389 :
390 : const SvXMLEnumMapEntry aLevelNameAlphaMap[] =
391 : {
392 : { XML_SEPARATOR, 1 },
393 : { XML_1, 2 },
394 : { XML_2, 3 },
395 : { XML_3, 4 },
396 : { XML_TOKEN_INVALID, 0 }
397 : };
398 :
399 : const sal_Char* aLevelStylePropNameAlphaMap[] =
400 : { NULL, "ParaStyleSeparator", "ParaStyleLevel1", "ParaStyleLevel2",
401 : "ParaStyleLevel3", NULL };
402 :
403 : const sal_Bool aAllowedTokenTypesAlpha[] =
404 : {
405 : sal_True, // XML_TOK_INDEX_TYPE_ENTRY_TEXT =
406 : sal_True, // XML_TOK_INDEX_TYPE_TAB_STOP,
407 : sal_True, // XML_TOK_INDEX_TYPE_TEXT,
408 : sal_True, // XML_TOK_INDEX_TYPE_PAGE_NUMBER,
409 : sal_True, // XML_TOK_INDEX_TYPE_CHAPTER,
410 : sal_False, // XML_TOK_INDEX_TYPE_LINK_START,
411 : sal_False, // XML_TOK_INDEX_TYPE_LINK_END,
412 : sal_False // XML_TOK_INDEX_TYPE_BIBLIOGRAPHY
413 : };
414 :
415 :
416 : // bibliography index:
417 :
418 : const SvXMLEnumMapEntry aLevelNameBibliographyMap[] =
419 : {
420 : { XML_ARTICLE, 1 },
421 : { XML_BOOK, 2 },
422 : { XML_BOOKLET, 3 },
423 : { XML_CONFERENCE, 4 },
424 : { XML_CUSTOM1, 5 },
425 : { XML_CUSTOM2, 6 },
426 : { XML_CUSTOM3, 7 },
427 : { XML_CUSTOM4, 8 },
428 : { XML_CUSTOM5, 9 },
429 : { XML_EMAIL, 10 },
430 : { XML_INBOOK, 11 },
431 : { XML_INCOLLECTION, 12 },
432 : { XML_INPROCEEDINGS, 13 },
433 : { XML_JOURNAL, 14 },
434 : { XML_MANUAL, 15 },
435 : { XML_MASTERSTHESIS, 16 },
436 : { XML_MISC, 17 },
437 : { XML_PHDTHESIS, 18 },
438 : { XML_PROCEEDINGS, 19 },
439 : { XML_TECHREPORT, 20 },
440 : { XML_UNPUBLISHED, 21 },
441 : { XML_WWW, 22 },
442 : { XML_TOKEN_INVALID, 0 }
443 : };
444 :
445 : // TODO: replace with real property names, when available
446 : const sal_Char* aLevelStylePropNameBibliographyMap[] =
447 : {
448 : NULL, "ParaStyleLevel1", "ParaStyleLevel1", "ParaStyleLevel1",
449 : "ParaStyleLevel1", "ParaStyleLevel1", "ParaStyleLevel1",
450 : "ParaStyleLevel1", "ParaStyleLevel1", "ParaStyleLevel1",
451 : "ParaStyleLevel1", "ParaStyleLevel1", "ParaStyleLevel1",
452 : "ParaStyleLevel1", "ParaStyleLevel1", "ParaStyleLevel1",
453 : "ParaStyleLevel1", "ParaStyleLevel1", "ParaStyleLevel1",
454 : "ParaStyleLevel1", "ParaStyleLevel1", "ParaStyleLevel1",
455 : "ParaStyleLevel1", NULL };
456 :
457 : const sal_Bool aAllowedTokenTypesBibliography[] =
458 : {
459 : sal_True, // XML_TOK_INDEX_TYPE_ENTRY_TEXT =
460 : sal_True, // XML_TOK_INDEX_TYPE_TAB_STOP,
461 : sal_True, // XML_TOK_INDEX_TYPE_TEXT,
462 : sal_True, // XML_TOK_INDEX_TYPE_PAGE_NUMBER,
463 : sal_False, // XML_TOK_INDEX_TYPE_CHAPTER,
464 : sal_False, // XML_TOK_INDEX_TYPE_LINK_START,
465 : sal_False, // XML_TOK_INDEX_TYPE_LINK_END,
466 : sal_True // XML_TOK_INDEX_TYPE_BIBLIOGRAPHY
467 : };
468 :
469 :
470 : // table, illustration and object index
471 :
472 : // no name map
473 : const SvXMLEnumMapEntry* aLevelNameTableMap = NULL;
474 :
475 : const sal_Char* aLevelStylePropNameTableMap[] =
476 : { NULL, "ParaStyleLevel1", NULL };
477 :
478 : const sal_Bool aAllowedTokenTypesTable[] =
479 : {
480 : sal_True, // XML_TOK_INDEX_TYPE_ENTRY_TEXT =
481 : sal_True, // XML_TOK_INDEX_TYPE_TAB_STOP,
482 : sal_True, // XML_TOK_INDEX_TYPE_TEXT,
483 : sal_True, // XML_TOK_INDEX_TYPE_PAGE_NUMBER,
484 : sal_True, // XML_TOK_INDEX_TYPE_CHAPTER,
485 : sal_False, // XML_TOK_INDEX_TYPE_LINK_START,
486 : sal_False, // XML_TOK_INDEX_TYPE_LINK_END,
487 : sal_False // XML_TOK_INDEX_TYPE_BIBLIOGRAPHY
488 : };
489 :
490 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|