Bump for 3.6-28
[LibreOffice.git] / xmloff / source / text / XMLIndexTemplateContext.cxx
blobcea0931a79b776aa27e3a6a581a0f21f74d2cac1
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
31 #include "XMLIndexTemplateContext.hxx"
32 #include "XMLIndexSimpleEntryContext.hxx"
33 #include "XMLIndexSpanEntryContext.hxx"
34 #include "XMLIndexTabStopEntryContext.hxx"
35 #include "XMLIndexBibliographyEntryContext.hxx"
36 #include "XMLIndexChapterInfoEntryContext.hxx"
37 #include <xmloff/xmlictxt.hxx>
38 #include <xmloff/xmlimp.hxx>
39 #include <xmloff/txtimp.hxx>
40 #include <xmloff/nmspmap.hxx>
41 #include "xmloff/xmlnmspe.hxx"
42 #include <xmloff/xmltoken.hxx>
43 #include <xmloff/xmluconv.hxx>
44 #include <tools/debug.hxx>
45 #include <rtl/ustring.hxx>
46 #include <rtl/ustrbuf.hxx>
47 #include <com/sun/star/container/XIndexReplace.hpp>
50 using namespace ::std;
51 using namespace ::xmloff::token;
53 using ::rtl::OUString;
54 using ::rtl::OUStringBuffer;
55 using ::com::sun::star::beans::XPropertySet;
56 using ::com::sun::star::beans::PropertyValue;
57 using ::com::sun::star::beans::PropertyValues;
58 using ::com::sun::star::uno::Reference;
59 using ::com::sun::star::uno::Sequence;
60 using ::com::sun::star::uno::Any;
61 using ::com::sun::star::xml::sax::XAttributeList;
62 using ::com::sun::star::container::XIndexReplace;
64 const sal_Char sAPI_TokenEntryNumber[] = "TokenEntryNumber";
65 const sal_Char sAPI_TokenEntryText[] = "TokenEntryText";
66 const sal_Char sAPI_TokenTabStop[] = "TokenTabStop";
67 const sal_Char sAPI_TokenText[] = "TokenText";
68 const sal_Char sAPI_TokenPageNumber[] = "TokenPageNumber";
69 const sal_Char sAPI_TokenChapterInfo[] = "TokenChapterInfo";
70 const sal_Char sAPI_TokenHyperlinkStart[] = "TokenHyperlinkStart";
71 const sal_Char sAPI_TokenHyperlinkEnd[] = "TokenHyperlinkEnd";
72 const sal_Char sAPI_TokenBibliographyDataField[] =
73 "TokenBibliographyDataField";
76 TYPEINIT1( XMLIndexTemplateContext, SvXMLImportContext);
78 XMLIndexTemplateContext::XMLIndexTemplateContext(
79 SvXMLImport& rImport,
80 Reference<XPropertySet> & rPropSet,
81 sal_uInt16 nPrfx,
82 const OUString& rLocalName,
83 const SvXMLEnumMapEntry* pLevelNameMap,
84 enum XMLTokenEnum eLevelAttrName,
85 const sal_Char** pLevelStylePropMap,
86 const sal_Bool* pAllowedTokenTypes,
87 sal_Bool bT )
88 : SvXMLImportContext(rImport, nPrfx, rLocalName)
89 , pOutlineLevelNameMap(pLevelNameMap)
90 , eOutlineLevelAttrName(eLevelAttrName)
91 , pOutlineLevelStylePropMap(pLevelStylePropMap)
92 , pAllowedTokenTypesMap(pAllowedTokenTypes)
93 , nOutlineLevel(1) // all indices have level 1 (0 is for header)
94 , bStyleNameOK(sal_False)
95 , bOutlineLevelOK(sal_False)
96 , bTOC( bT )
97 , rPropertySet(rPropSet)
98 , sTokenEntryNumber(RTL_CONSTASCII_USTRINGPARAM(sAPI_TokenEntryNumber))
99 , sTokenEntryText(RTL_CONSTASCII_USTRINGPARAM(sAPI_TokenEntryText))
100 , sTokenTabStop(RTL_CONSTASCII_USTRINGPARAM(sAPI_TokenTabStop))
101 , sTokenText(RTL_CONSTASCII_USTRINGPARAM(sAPI_TokenText))
102 , sTokenPageNumber(RTL_CONSTASCII_USTRINGPARAM(sAPI_TokenPageNumber))
103 , sTokenChapterInfo(RTL_CONSTASCII_USTRINGPARAM(sAPI_TokenChapterInfo))
104 , sTokenHyperlinkStart(RTL_CONSTASCII_USTRINGPARAM(sAPI_TokenHyperlinkStart))
105 , sTokenHyperlinkEnd(RTL_CONSTASCII_USTRINGPARAM(sAPI_TokenHyperlinkEnd))
106 , sTokenBibliographyDataField(RTL_CONSTASCII_USTRINGPARAM(sAPI_TokenBibliographyDataField))
108 , sCharacterStyleName(RTL_CONSTASCII_USTRINGPARAM("CharacterStyleName"))
109 , sTokenType(RTL_CONSTASCII_USTRINGPARAM("TokenType"))
110 , sText(RTL_CONSTASCII_USTRINGPARAM("Text"))
111 , sTabStopRightAligned(RTL_CONSTASCII_USTRINGPARAM("TabStopRightAligned"))
112 , sTabStopPosition(RTL_CONSTASCII_USTRINGPARAM("TabStopPosition"))
113 , sTabStopFillCharacter(RTL_CONSTASCII_USTRINGPARAM("TabStopFillCharacter"))
114 , sBibliographyDataField(RTL_CONSTASCII_USTRINGPARAM("BibliographyDataField"))
115 , sChapterFormat(RTL_CONSTASCII_USTRINGPARAM("ChapterFormat"))
116 , sChapterLevel(RTL_CONSTASCII_USTRINGPARAM("ChapterLevel")) //#i53420
118 , sLevelFormat(RTL_CONSTASCII_USTRINGPARAM("LevelFormat"))
119 , sParaStyleLevel(RTL_CONSTASCII_USTRINGPARAM("ParaStyleLevel"))
121 DBG_ASSERT( ((XML_TOKEN_INVALID != eLevelAttrName) && (NULL != pLevelNameMap))
122 || ((XML_TOKEN_INVALID == eLevelAttrName) && (NULL == pLevelNameMap)),
123 "need both, attribute name and value map, or neither" );
124 DBG_ASSERT( NULL != pOutlineLevelStylePropMap, "need property name map" );
125 DBG_ASSERT( NULL != pAllowedTokenTypes, "need allowed tokens map" );
127 // no map for outline-level? then use 1
128 if (NULL == pLevelNameMap)
130 nOutlineLevel = 1;
131 bOutlineLevelOK = sal_True;
135 XMLIndexTemplateContext::~XMLIndexTemplateContext()
140 void XMLIndexTemplateContext::addTemplateEntry(
141 const PropertyValues& aValues)
143 aValueVector.push_back(aValues);
147 void XMLIndexTemplateContext::StartElement(
148 const Reference<XAttributeList> & xAttrList)
150 // process two attributes: style-name, outline-level
151 sal_Int16 nLength = xAttrList->getLength();
152 for(sal_Int16 nAttr = 0; nAttr < nLength; nAttr++)
154 OUString sLocalName;
155 sal_uInt16 nPrefix = GetImport().GetNamespaceMap().
156 GetKeyByAttrName( xAttrList->getNameByIndex(nAttr),
157 &sLocalName );
158 if (XML_NAMESPACE_TEXT == nPrefix)
160 if ( IsXMLToken( sLocalName, XML_STYLE_NAME ) )
162 // style name
163 sStyleName = xAttrList->getValueByIndex(nAttr);
164 bStyleNameOK = sal_True;
166 else if (eOutlineLevelAttrName != XML_TOKEN_INVALID)
168 // we have an attr name! Then see if we have the attr, too.
169 if (IsXMLToken(sLocalName, eOutlineLevelAttrName))
171 // outline level
172 sal_uInt16 nTmp;
173 if (SvXMLUnitConverter::convertEnum(
174 nTmp, xAttrList->getValueByIndex(nAttr),
175 pOutlineLevelNameMap))
177 nOutlineLevel = nTmp;
178 bOutlineLevelOK = sal_True;
180 // else: illegal value -> ignore
182 // else: unknown attribute -> ignore
184 // else: we don't care about outline-level -> ignore
186 // else: attribute not in text namespace -> ignore
190 void XMLIndexTemplateContext::EndElement()
192 if (bOutlineLevelOK)
194 const sal_Int32 nCount = aValueVector.size();
195 Sequence<PropertyValues> aValueSequence(nCount);
196 for(sal_Int32 i = 0; i<nCount; i++)
198 aValueSequence[i] = aValueVector[i];
201 // get LevelFormat IndexReplace ...
202 Any aAny = rPropertySet->getPropertyValue(sLevelFormat);
203 Reference<XIndexReplace> xIndexReplace;
204 aAny >>= xIndexReplace;
206 // ... and insert
207 aAny <<= aValueSequence;
208 xIndexReplace->replaceByIndex(nOutlineLevel, aAny);
210 if (bStyleNameOK)
212 const sal_Char* pStyleProperty =
213 pOutlineLevelStylePropMap[nOutlineLevel];
215 DBG_ASSERT(NULL != pStyleProperty, "need property name");
216 if (NULL != pStyleProperty)
218 OUString sDisplayStyleName =
219 GetImport().GetStyleDisplayName(
220 XML_STYLE_FAMILY_TEXT_PARAGRAPH,
221 sStyleName );
222 // #i50288#: Check if style exists
223 const Reference < ::com::sun::star::container::XNameContainer > & rStyles =
224 GetImport().GetTextImport()->GetParaStyles();
225 if( rStyles.is() &&
226 rStyles->hasByName( sDisplayStyleName ) )
228 aAny <<= sDisplayStyleName;
229 rPropertySet->setPropertyValue(
230 OUString::createFromAscii(pStyleProperty), aAny);
239 /// template token types; used for aTokenTypeMap parameter
240 enum TemplateTokenType
242 XML_TOK_INDEX_TYPE_ENTRY_TEXT = 0,
243 XML_TOK_INDEX_TYPE_TAB_STOP,
244 XML_TOK_INDEX_TYPE_TEXT,
245 XML_TOK_INDEX_TYPE_PAGE_NUMBER,
246 XML_TOK_INDEX_TYPE_CHAPTER,
247 XML_TOK_INDEX_TYPE_LINK_START,
248 XML_TOK_INDEX_TYPE_LINK_END,
249 XML_TOK_INDEX_TYPE_BIBLIOGRAPHY
253 SvXMLEnumMapEntry aTemplateTokenTypeMap[] =
255 { XML_INDEX_ENTRY_TEXT, XML_TOK_INDEX_TYPE_ENTRY_TEXT },
256 { XML_INDEX_ENTRY_TAB_STOP, XML_TOK_INDEX_TYPE_TAB_STOP },
257 { XML_INDEX_ENTRY_SPAN, XML_TOK_INDEX_TYPE_TEXT },
258 { XML_INDEX_ENTRY_PAGE_NUMBER, XML_TOK_INDEX_TYPE_PAGE_NUMBER },
259 { XML_INDEX_ENTRY_CHAPTER, XML_TOK_INDEX_TYPE_CHAPTER },
260 { XML_INDEX_ENTRY_LINK_START, XML_TOK_INDEX_TYPE_LINK_START },
261 { XML_INDEX_ENTRY_LINK_END, XML_TOK_INDEX_TYPE_LINK_END },
262 { XML_INDEX_ENTRY_BIBLIOGRAPHY, XML_TOK_INDEX_TYPE_BIBLIOGRAPHY },
263 { XML_TOKEN_INVALID, 0 }
266 SvXMLImportContext *XMLIndexTemplateContext::CreateChildContext(
267 sal_uInt16 nPrefix,
268 const OUString& rLocalName,
269 const Reference<XAttributeList> & xAttrList )
271 SvXMLImportContext* pContext = NULL;
273 if (XML_NAMESPACE_TEXT == nPrefix)
275 sal_uInt16 nToken;
276 if (SvXMLUnitConverter::convertEnum(nToken, rLocalName,
277 aTemplateTokenTypeMap))
279 // can this index accept this kind of token?
280 if (pAllowedTokenTypesMap[nToken])
282 switch ((TemplateTokenType)nToken)
284 case XML_TOK_INDEX_TYPE_ENTRY_TEXT:
285 pContext = new XMLIndexSimpleEntryContext(
286 GetImport(), sTokenEntryText, *this,
287 nPrefix, rLocalName);
288 break;
290 case XML_TOK_INDEX_TYPE_PAGE_NUMBER:
291 pContext = new XMLIndexSimpleEntryContext(
292 GetImport(), sTokenPageNumber, *this,
293 nPrefix, rLocalName);
294 break;
296 case XML_TOK_INDEX_TYPE_LINK_START:
297 pContext = new XMLIndexSimpleEntryContext(
298 GetImport(), sTokenHyperlinkStart, *this,
299 nPrefix, rLocalName);
300 break;
302 case XML_TOK_INDEX_TYPE_LINK_END:
303 pContext = new XMLIndexSimpleEntryContext(
304 GetImport(), sTokenHyperlinkEnd, *this,
305 nPrefix, rLocalName);
306 break;
308 case XML_TOK_INDEX_TYPE_TEXT:
309 pContext = new XMLIndexSpanEntryContext(
310 GetImport(), *this, nPrefix, rLocalName);
311 break;
313 case XML_TOK_INDEX_TYPE_TAB_STOP:
314 pContext = new XMLIndexTabStopEntryContext(
315 GetImport(), *this, nPrefix, rLocalName);
316 break;
318 case XML_TOK_INDEX_TYPE_BIBLIOGRAPHY:
319 pContext = new XMLIndexBibliographyEntryContext(
320 GetImport(), *this, nPrefix, rLocalName);
321 break;
323 case XML_TOK_INDEX_TYPE_CHAPTER:
324 pContext = new XMLIndexChapterInfoEntryContext(
325 GetImport(), *this, nPrefix, rLocalName, bTOC );
326 break;
328 default:
329 // ignore!
330 break;
336 // ignore unknown
337 if (NULL == pContext)
339 return SvXMLImportContext::CreateChildContext(nPrefix, rLocalName,
340 xAttrList);
343 return pContext;
349 // maps for the XMLIndexTemplateContext constructor
353 // table of content and user defined index:
355 const SvXMLEnumMapEntry aLevelNameTOCMap[] =
357 { XML_1, 1 },
358 { XML_2, 2 },
359 { XML_3, 3 },
360 { XML_4, 4 },
361 { XML_5, 5 },
362 { XML_6, 6 },
363 { XML_7, 7 },
364 { XML_8, 8 },
365 { XML_9, 9 },
366 { XML_10, 10 },
367 { XML_TOKEN_INVALID, 0 }
370 const sal_Char* aLevelStylePropNameTOCMap[] =
371 { NULL, "ParaStyleLevel1", "ParaStyleLevel2", "ParaStyleLevel3",
372 "ParaStyleLevel4", "ParaStyleLevel5", "ParaStyleLevel6",
373 "ParaStyleLevel7", "ParaStyleLevel8", "ParaStyleLevel9",
374 "ParaStyleLevel10", NULL };
376 const sal_Bool aAllowedTokenTypesTOC[] =
378 sal_True, // XML_TOK_INDEX_TYPE_ENTRY_TEXT =
379 sal_True, // XML_TOK_INDEX_TYPE_TAB_STOP,
380 sal_True, // XML_TOK_INDEX_TYPE_TEXT,
381 sal_True, // XML_TOK_INDEX_TYPE_PAGE_NUMBER,
382 sal_True, // XML_TOK_INDEX_TYPE_CHAPTER,
383 sal_True, // XML_TOK_INDEX_TYPE_LINK_START,
384 sal_True, // XML_TOK_INDEX_TYPE_LINK_END,
385 sal_False // XML_TOK_INDEX_TYPE_BIBLIOGRAPHY
388 const sal_Bool aAllowedTokenTypesUser[] =
390 sal_True, // XML_TOK_INDEX_TYPE_ENTRY_TEXT =
391 sal_True, // XML_TOK_INDEX_TYPE_TAB_STOP,
392 sal_True, // XML_TOK_INDEX_TYPE_TEXT,
393 sal_True, // XML_TOK_INDEX_TYPE_PAGE_NUMBER,
394 sal_True, // XML_TOK_INDEX_TYPE_CHAPTER,
395 sal_False, // XML_TOK_INDEX_TYPE_LINK_START,
396 sal_False, // XML_TOK_INDEX_TYPE_LINK_END,
397 sal_False // XML_TOK_INDEX_TYPE_BIBLIOGRAPHY
401 // alphabetical index
403 const SvXMLEnumMapEntry aLevelNameAlphaMap[] =
405 { XML_SEPARATOR, 1 },
406 { XML_1, 2 },
407 { XML_2, 3 },
408 { XML_3, 4 },
409 { XML_TOKEN_INVALID, 0 }
412 const sal_Char* aLevelStylePropNameAlphaMap[] =
413 { NULL, "ParaStyleSeparator", "ParaStyleLevel1", "ParaStyleLevel2",
414 "ParaStyleLevel3", NULL };
416 const sal_Bool aAllowedTokenTypesAlpha[] =
418 sal_True, // XML_TOK_INDEX_TYPE_ENTRY_TEXT =
419 sal_True, // XML_TOK_INDEX_TYPE_TAB_STOP,
420 sal_True, // XML_TOK_INDEX_TYPE_TEXT,
421 sal_True, // XML_TOK_INDEX_TYPE_PAGE_NUMBER,
422 sal_True, // XML_TOK_INDEX_TYPE_CHAPTER,
423 sal_False, // XML_TOK_INDEX_TYPE_LINK_START,
424 sal_False, // XML_TOK_INDEX_TYPE_LINK_END,
425 sal_False // XML_TOK_INDEX_TYPE_BIBLIOGRAPHY
429 // bibliography index:
431 const SvXMLEnumMapEntry aLevelNameBibliographyMap[] =
433 { XML_ARTICLE, 1 },
434 { XML_BOOK, 2 },
435 { XML_BOOKLET, 3 },
436 { XML_CONFERENCE, 4 },
437 { XML_CUSTOM1, 5 },
438 { XML_CUSTOM2, 6 },
439 { XML_CUSTOM3, 7 },
440 { XML_CUSTOM4, 8 },
441 { XML_CUSTOM5, 9 },
442 { XML_EMAIL, 10 },
443 { XML_INBOOK, 11 },
444 { XML_INCOLLECTION, 12 },
445 { XML_INPROCEEDINGS, 13 },
446 { XML_JOURNAL, 14 },
447 { XML_MANUAL, 15 },
448 { XML_MASTERSTHESIS, 16 },
449 { XML_MISC, 17 },
450 { XML_PHDTHESIS, 18 },
451 { XML_PROCEEDINGS, 19 },
452 { XML_TECHREPORT, 20 },
453 { XML_UNPUBLISHED, 21 },
454 { XML_WWW, 22 },
455 { XML_TOKEN_INVALID, 0 }
458 // TODO: replace with real property names, when available
459 const sal_Char* aLevelStylePropNameBibliographyMap[] =
461 NULL, "ParaStyleLevel1", "ParaStyleLevel1", "ParaStyleLevel1",
462 "ParaStyleLevel1", "ParaStyleLevel1", "ParaStyleLevel1",
463 "ParaStyleLevel1", "ParaStyleLevel1", "ParaStyleLevel1",
464 "ParaStyleLevel1", "ParaStyleLevel1", "ParaStyleLevel1",
465 "ParaStyleLevel1", "ParaStyleLevel1", "ParaStyleLevel1",
466 "ParaStyleLevel1", "ParaStyleLevel1", "ParaStyleLevel1",
467 "ParaStyleLevel1", "ParaStyleLevel1", "ParaStyleLevel1",
468 "ParaStyleLevel1", NULL };
470 const sal_Bool aAllowedTokenTypesBibliography[] =
472 sal_True, // XML_TOK_INDEX_TYPE_ENTRY_TEXT =
473 sal_True, // XML_TOK_INDEX_TYPE_TAB_STOP,
474 sal_True, // XML_TOK_INDEX_TYPE_TEXT,
475 sal_True, // XML_TOK_INDEX_TYPE_PAGE_NUMBER,
476 sal_False, // XML_TOK_INDEX_TYPE_CHAPTER,
477 sal_False, // XML_TOK_INDEX_TYPE_LINK_START,
478 sal_False, // XML_TOK_INDEX_TYPE_LINK_END,
479 sal_True // XML_TOK_INDEX_TYPE_BIBLIOGRAPHY
483 // table, illustration and object index
485 // no name map
486 const SvXMLEnumMapEntry* aLevelNameTableMap = NULL;
488 const sal_Char* aLevelStylePropNameTableMap[] =
489 { NULL, "ParaStyleLevel1", NULL };
491 const sal_Bool aAllowedTokenTypesTable[] =
493 sal_True, // XML_TOK_INDEX_TYPE_ENTRY_TEXT =
494 sal_True, // XML_TOK_INDEX_TYPE_TAB_STOP,
495 sal_True, // XML_TOK_INDEX_TYPE_TEXT,
496 sal_True, // XML_TOK_INDEX_TYPE_PAGE_NUMBER,
497 sal_True, // XML_TOK_INDEX_TYPE_CHAPTER,
498 sal_False, // XML_TOK_INDEX_TYPE_LINK_START,
499 sal_False, // XML_TOK_INDEX_TYPE_LINK_END,
500 sal_False // XML_TOK_INDEX_TYPE_BIBLIOGRAPHY
503 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */