1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
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>
39 using namespace ::std
;
40 using namespace ::xmloff::token
;
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
;
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";
63 TYPEINIT1( XMLIndexTemplateContext
, SvXMLImportContext
);
65 XMLIndexTemplateContext::XMLIndexTemplateContext(
67 Reference
<XPropertySet
> & rPropSet
,
69 const OUString
& rLocalName
,
70 const SvXMLEnumMapEntry
* pLevelNameMap
,
71 enum XMLTokenEnum eLevelAttrName
,
72 const sal_Char
** pLevelStylePropMap
,
73 const sal_Bool
* pAllowedTokenTypes
,
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)
82 , bOutlineLevelOK(false)
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
)
95 , sCharacterStyleName("CharacterStyleName")
96 , sTokenType("TokenType")
98 , sTabStopRightAligned("TabStopRightAligned")
99 , sTabStopPosition("TabStopPosition")
100 , sTabStopFillCharacter("TabStopFillCharacter")
101 , sBibliographyDataField("BibliographyDataField")
102 , sChapterFormat("ChapterFormat")
103 , sChapterLevel("ChapterLevel") //#i53420
105 , sLevelFormat("LevelFormat")
106 , sParaStyleLevel("ParaStyleLevel")
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" );
114 // no map for outline-level? then use 1
115 if (NULL
== pLevelNameMap
)
118 bOutlineLevelOK
= true;
122 XMLIndexTemplateContext::~XMLIndexTemplateContext()
127 void XMLIndexTemplateContext::addTemplateEntry(
128 const PropertyValues
& aValues
)
130 aValueVector
.push_back(aValues
);
134 void XMLIndexTemplateContext::StartElement(
135 const Reference
<XAttributeList
> & xAttrList
)
137 // process two attributes: style-name, outline-level
138 sal_Int16 nLength
= xAttrList
->getLength();
139 for(sal_Int16 nAttr
= 0; nAttr
< nLength
; nAttr
++)
142 sal_uInt16 nPrefix
= GetImport().GetNamespaceMap().
143 GetKeyByAttrName( xAttrList
->getNameByIndex(nAttr
),
145 if (XML_NAMESPACE_TEXT
== nPrefix
)
147 if ( IsXMLToken( sLocalName
, XML_STYLE_NAME
) )
150 sStyleName
= xAttrList
->getValueByIndex(nAttr
);
153 else if (eOutlineLevelAttrName
!= XML_TOKEN_INVALID
)
155 // we have an attr name! Then see if we have the attr, too.
156 if (IsXMLToken(sLocalName
, eOutlineLevelAttrName
))
160 if (SvXMLUnitConverter::convertEnum(
161 nTmp
, xAttrList
->getValueByIndex(nAttr
),
162 pOutlineLevelNameMap
))
164 nOutlineLevel
= nTmp
;
165 bOutlineLevelOK
= true;
167 // else: illegal value -> ignore
169 // else: unknown attribute -> ignore
171 // else: we don't care about outline-level -> ignore
173 // else: attribute not in text namespace -> ignore
177 void XMLIndexTemplateContext::EndElement()
181 const sal_Int32 nCount
= aValueVector
.size();
182 Sequence
<PropertyValues
> aValueSequence(nCount
);
183 for(sal_Int32 i
= 0; i
<nCount
; i
++)
185 aValueSequence
[i
] = aValueVector
[i
];
188 // get LevelFormat IndexReplace ...
189 Any aAny
= rPropertySet
->getPropertyValue(sLevelFormat
);
190 Reference
<XIndexReplace
> xIndexReplace
;
191 aAny
>>= xIndexReplace
;
194 aAny
<<= aValueSequence
;
195 xIndexReplace
->replaceByIndex(nOutlineLevel
, aAny
);
199 const sal_Char
* pStyleProperty
=
200 pOutlineLevelStylePropMap
[nOutlineLevel
];
202 DBG_ASSERT(NULL
!= pStyleProperty
, "need property name");
203 if (NULL
!= pStyleProperty
)
205 OUString sDisplayStyleName
=
206 GetImport().GetStyleDisplayName(
207 XML_STYLE_FAMILY_TEXT_PARAGRAPH
,
209 // #i50288#: Check if style exists
210 const Reference
< ::com::sun::star::container::XNameContainer
> & rStyles
=
211 GetImport().GetTextImport()->GetParaStyles();
213 rStyles
->hasByName( sDisplayStyleName
) )
215 aAny
<<= sDisplayStyleName
;
216 rPropertySet
->setPropertyValue(
217 OUString::createFromAscii(pStyleProperty
), aAny
);
226 /// template token types; used for aTokenTypeMap parameter
227 enum TemplateTokenType
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
240 SvXMLEnumMapEntry aTemplateTokenTypeMap
[] =
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 }
253 SvXMLImportContext
*XMLIndexTemplateContext::CreateChildContext(
255 const OUString
& rLocalName
,
256 const Reference
<XAttributeList
> & xAttrList
)
258 SvXMLImportContext
* pContext
= NULL
;
260 if (XML_NAMESPACE_TEXT
== nPrefix
)
263 if (SvXMLUnitConverter::convertEnum(nToken
, rLocalName
,
264 aTemplateTokenTypeMap
))
266 // can this index accept this kind of token?
267 if (pAllowedTokenTypesMap
[nToken
])
269 switch ((TemplateTokenType
)nToken
)
271 case XML_TOK_INDEX_TYPE_ENTRY_TEXT
:
272 pContext
= new XMLIndexSimpleEntryContext(
273 GetImport(), sTokenEntryText
, *this,
274 nPrefix
, rLocalName
);
277 case XML_TOK_INDEX_TYPE_PAGE_NUMBER
:
278 pContext
= new XMLIndexSimpleEntryContext(
279 GetImport(), sTokenPageNumber
, *this,
280 nPrefix
, rLocalName
);
283 case XML_TOK_INDEX_TYPE_LINK_START
:
284 pContext
= new XMLIndexSimpleEntryContext(
285 GetImport(), sTokenHyperlinkStart
, *this,
286 nPrefix
, rLocalName
);
289 case XML_TOK_INDEX_TYPE_LINK_END
:
290 pContext
= new XMLIndexSimpleEntryContext(
291 GetImport(), sTokenHyperlinkEnd
, *this,
292 nPrefix
, rLocalName
);
295 case XML_TOK_INDEX_TYPE_TEXT
:
296 pContext
= new XMLIndexSpanEntryContext(
297 GetImport(), *this, nPrefix
, rLocalName
);
300 case XML_TOK_INDEX_TYPE_TAB_STOP
:
301 pContext
= new XMLIndexTabStopEntryContext(
302 GetImport(), *this, nPrefix
, rLocalName
);
305 case XML_TOK_INDEX_TYPE_BIBLIOGRAPHY
:
306 pContext
= new XMLIndexBibliographyEntryContext(
307 GetImport(), *this, nPrefix
, rLocalName
);
310 case XML_TOK_INDEX_TYPE_CHAPTER
:
311 pContext
= new XMLIndexChapterInfoEntryContext(
312 GetImport(), *this, nPrefix
, rLocalName
, bTOC
);
324 if (NULL
== pContext
)
326 return SvXMLImportContext::CreateChildContext(nPrefix
, rLocalName
,
336 // maps for the XMLIndexTemplateContext constructor
340 // table of content and user defined index:
342 const SvXMLEnumMapEntry aSvLevelNameTOCMap
[] =
354 { XML_TOKEN_INVALID
, 0 }
357 const sal_Char
* aLevelStylePropNameTOCMap
[] =
358 { NULL
, "ParaStyleLevel1", "ParaStyleLevel2", "ParaStyleLevel3",
359 "ParaStyleLevel4", "ParaStyleLevel5", "ParaStyleLevel6",
360 "ParaStyleLevel7", "ParaStyleLevel8", "ParaStyleLevel9",
361 "ParaStyleLevel10", NULL
};
363 const sal_Bool aAllowedTokenTypesTOC
[] =
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
375 const sal_Bool aAllowedTokenTypesUser
[] =
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
388 // alphabetical index
390 const SvXMLEnumMapEntry aLevelNameAlphaMap
[] =
392 { XML_SEPARATOR
, 1 },
396 { XML_TOKEN_INVALID
, 0 }
399 const sal_Char
* aLevelStylePropNameAlphaMap
[] =
400 { NULL
, "ParaStyleSeparator", "ParaStyleLevel1", "ParaStyleLevel2",
401 "ParaStyleLevel3", NULL
};
403 const sal_Bool aAllowedTokenTypesAlpha
[] =
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
416 // bibliography index:
418 const SvXMLEnumMapEntry aLevelNameBibliographyMap
[] =
423 { XML_CONFERENCE
, 4 },
431 { XML_INCOLLECTION
, 12 },
432 { XML_INPROCEEDINGS
, 13 },
435 { XML_MASTERSTHESIS
, 16 },
437 { XML_PHDTHESIS
, 18 },
438 { XML_PROCEEDINGS
, 19 },
439 { XML_TECHREPORT
, 20 },
440 { XML_UNPUBLISHED
, 21 },
442 { XML_TOKEN_INVALID
, 0 }
445 // TODO: replace with real property names, when available
446 const sal_Char
* aLevelStylePropNameBibliographyMap
[] =
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
};
457 const sal_Bool aAllowedTokenTypesBibliography
[] =
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
470 // table, illustration and object index
473 const SvXMLEnumMapEntry
* aLevelNameTableMap
= NULL
;
475 const sal_Char
* aLevelStylePropNameTableMap
[] =
476 { NULL
, "ParaStyleLevel1", NULL
};
478 const sal_Bool aAllowedTokenTypesTable
[] =
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
490 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */