1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: XMLIndexTemplateContext.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_xmloff.hxx"
35 #include "XMLIndexTemplateContext.hxx"
36 #include "XMLIndexSimpleEntryContext.hxx"
37 #include "XMLIndexSpanEntryContext.hxx"
38 #include "XMLIndexTabStopEntryContext.hxx"
39 #include "XMLIndexBibliographyEntryContext.hxx"
40 #include "XMLIndexChapterInfoEntryContext.hxx"
41 #include <xmloff/xmlictxt.hxx>
42 #include <xmloff/xmlimp.hxx>
43 #include <xmloff/txtimp.hxx>
44 #include <xmloff/nmspmap.hxx>
45 #include "xmlnmspe.hxx"
46 #include <xmloff/xmltoken.hxx>
47 #include <xmloff/xmluconv.hxx>
48 #include <tools/debug.hxx>
49 #include <rtl/ustring.hxx>
50 #include <rtl/ustrbuf.hxx>
51 #include <com/sun/star/container/XIndexReplace.hpp>
54 using namespace ::std
;
55 //using namespace ::com::sun::star::text;
56 using namespace ::xmloff::token
;
58 using ::rtl::OUString
;
59 using ::rtl::OUStringBuffer
;
60 using ::com::sun::star::beans::XPropertySet
;
61 using ::com::sun::star::beans::PropertyValue
;
62 using ::com::sun::star::beans::PropertyValues
;
63 using ::com::sun::star::uno::Reference
;
64 using ::com::sun::star::uno::Sequence
;
65 using ::com::sun::star::uno::Any
;
66 using ::com::sun::star::xml::sax::XAttributeList
;
67 using ::com::sun::star::container::XIndexReplace
;
69 const sal_Char sAPI_TokenEntryNumber
[] = "TokenEntryNumber";
70 const sal_Char sAPI_TokenEntryText
[] = "TokenEntryText";
71 const sal_Char sAPI_TokenTabStop
[] = "TokenTabStop";
72 const sal_Char sAPI_TokenText
[] = "TokenText";
73 const sal_Char sAPI_TokenPageNumber
[] = "TokenPageNumber";
74 const sal_Char sAPI_TokenChapterInfo
[] = "TokenChapterInfo";
75 const sal_Char sAPI_TokenHyperlinkStart
[] = "TokenHyperlinkStart";
76 const sal_Char sAPI_TokenHyperlinkEnd
[] = "TokenHyperlinkEnd";
77 const sal_Char sAPI_TokenBibliographyDataField
[] =
78 "TokenBibliographyDataField";
81 TYPEINIT1( XMLIndexTemplateContext
, SvXMLImportContext
);
83 XMLIndexTemplateContext::XMLIndexTemplateContext(
85 Reference
<XPropertySet
> & rPropSet
,
87 const OUString
& rLocalName
,
88 const SvXMLEnumMapEntry
* pLevelNameMap
,
89 enum XMLTokenEnum eLevelAttrName
,
90 const sal_Char
** pLevelStylePropMap
,
91 const sal_Bool
* pAllowedTokenTypes
,
93 : SvXMLImportContext(rImport
, nPrfx
, rLocalName
)
94 , pOutlineLevelNameMap(pLevelNameMap
)
95 , eOutlineLevelAttrName(eLevelAttrName
)
96 , pOutlineLevelStylePropMap(pLevelStylePropMap
)
97 , pAllowedTokenTypesMap(pAllowedTokenTypes
)
98 , nOutlineLevel(1) // all indices have level 1 (0 is for header)
99 , bStyleNameOK(sal_False
)
100 , bOutlineLevelOK(sal_False
)
102 , rPropertySet(rPropSet
)
103 , sTokenEntryNumber(RTL_CONSTASCII_USTRINGPARAM(sAPI_TokenEntryNumber
))
104 , sTokenEntryText(RTL_CONSTASCII_USTRINGPARAM(sAPI_TokenEntryText
))
105 , sTokenTabStop(RTL_CONSTASCII_USTRINGPARAM(sAPI_TokenTabStop
))
106 , sTokenText(RTL_CONSTASCII_USTRINGPARAM(sAPI_TokenText
))
107 , sTokenPageNumber(RTL_CONSTASCII_USTRINGPARAM(sAPI_TokenPageNumber
))
108 , sTokenChapterInfo(RTL_CONSTASCII_USTRINGPARAM(sAPI_TokenChapterInfo
))
109 , sTokenHyperlinkStart(RTL_CONSTASCII_USTRINGPARAM(sAPI_TokenHyperlinkStart
))
110 , sTokenHyperlinkEnd(RTL_CONSTASCII_USTRINGPARAM(sAPI_TokenHyperlinkEnd
))
111 , sTokenBibliographyDataField(RTL_CONSTASCII_USTRINGPARAM(sAPI_TokenBibliographyDataField
))
113 , sCharacterStyleName(RTL_CONSTASCII_USTRINGPARAM("CharacterStyleName"))
114 , sTokenType(RTL_CONSTASCII_USTRINGPARAM("TokenType"))
115 , sText(RTL_CONSTASCII_USTRINGPARAM("Text"))
116 , sTabStopRightAligned(RTL_CONSTASCII_USTRINGPARAM("TabStopRightAligned"))
117 , sTabStopPosition(RTL_CONSTASCII_USTRINGPARAM("TabStopPosition"))
118 , sTabStopFillCharacter(RTL_CONSTASCII_USTRINGPARAM("TabStopFillCharacter"))
119 , sBibliographyDataField(RTL_CONSTASCII_USTRINGPARAM("BibliographyDataField"))
120 , sChapterFormat(RTL_CONSTASCII_USTRINGPARAM("ChapterFormat"))
121 , sChapterLevel(RTL_CONSTASCII_USTRINGPARAM("ChapterLevel")) //#i53420
123 , sLevelFormat(RTL_CONSTASCII_USTRINGPARAM("LevelFormat"))
124 , sParaStyleLevel(RTL_CONSTASCII_USTRINGPARAM("ParaStyleLevel"))
126 DBG_ASSERT( ((XML_TOKEN_INVALID
!= eLevelAttrName
) && (NULL
!= pLevelNameMap
))
127 || ((XML_TOKEN_INVALID
== eLevelAttrName
) && (NULL
== pLevelNameMap
)),
128 "need both, attribute name and value map, or neither" );
129 DBG_ASSERT( NULL
!= pOutlineLevelStylePropMap
, "need property name map" );
130 DBG_ASSERT( NULL
!= pAllowedTokenTypes
, "need allowed tokens map" );
132 // no map for outline-level? then use 1
133 if (NULL
== pLevelNameMap
)
136 bOutlineLevelOK
= sal_True
;
140 XMLIndexTemplateContext::~XMLIndexTemplateContext()
145 void XMLIndexTemplateContext::addTemplateEntry(
146 const PropertyValues
& aValues
)
148 aValueVector
.push_back(aValues
);
152 void XMLIndexTemplateContext::StartElement(
153 const Reference
<XAttributeList
> & xAttrList
)
155 // process two attributes: style-name, outline-level
156 sal_Int16 nLength
= xAttrList
->getLength();
157 for(sal_Int16 nAttr
= 0; nAttr
< nLength
; nAttr
++)
160 sal_uInt16 nPrefix
= GetImport().GetNamespaceMap().
161 GetKeyByAttrName( xAttrList
->getNameByIndex(nAttr
),
163 if (XML_NAMESPACE_TEXT
== nPrefix
)
165 if ( IsXMLToken( sLocalName
, XML_STYLE_NAME
) )
168 sStyleName
= xAttrList
->getValueByIndex(nAttr
);
169 bStyleNameOK
= sal_True
;
171 else if (eOutlineLevelAttrName
!= XML_TOKEN_INVALID
)
173 // we have an attr name! Then see if we have the attr, too.
174 if (IsXMLToken(sLocalName
, eOutlineLevelAttrName
))
178 if (SvXMLUnitConverter::convertEnum(
179 nTmp
, xAttrList
->getValueByIndex(nAttr
),
180 pOutlineLevelNameMap
))
182 nOutlineLevel
= nTmp
;
183 bOutlineLevelOK
= sal_True
;
185 // else: illegal value -> ignore
187 // else: unknown attribute -> ignore
189 // else: we don't care about outline-level -> ignore
191 // else: attribute not in text namespace -> ignore
195 void XMLIndexTemplateContext::EndElement()
199 const sal_Int32 nCount
= aValueVector
.size();
200 Sequence
<PropertyValues
> aValueSequence(nCount
);
201 for(sal_Int32 i
= 0; i
<nCount
; i
++)
203 aValueSequence
[i
] = aValueVector
[i
];
206 // get LevelFormat IndexReplace ...
207 Any aAny
= rPropertySet
->getPropertyValue(sLevelFormat
);
208 Reference
<XIndexReplace
> xIndexReplace
;
209 aAny
>>= xIndexReplace
;
212 aAny
<<= aValueSequence
;
213 xIndexReplace
->replaceByIndex(nOutlineLevel
, aAny
);
217 const sal_Char
* pStyleProperty
=
218 pOutlineLevelStylePropMap
[nOutlineLevel
];
220 DBG_ASSERT(NULL
!= pStyleProperty
, "need property name");
221 if (NULL
!= pStyleProperty
)
223 OUString sDisplayStyleName
=
224 GetImport().GetStyleDisplayName(
225 XML_STYLE_FAMILY_TEXT_PARAGRAPH
,
227 // #i50288#: Check if style exists
228 const Reference
< ::com::sun::star::container::XNameContainer
> & rStyles
=
229 GetImport().GetTextImport()->GetParaStyles();
231 rStyles
->hasByName( sDisplayStyleName
) )
233 aAny
<<= sDisplayStyleName
;
234 rPropertySet
->setPropertyValue(
235 OUString::createFromAscii(pStyleProperty
), aAny
);
244 /// template token types; used for aTokenTypeMap parameter
245 enum TemplateTokenType
247 XML_TOK_INDEX_TYPE_ENTRY_TEXT
= 0,
248 XML_TOK_INDEX_TYPE_TAB_STOP
,
249 XML_TOK_INDEX_TYPE_TEXT
,
250 XML_TOK_INDEX_TYPE_PAGE_NUMBER
,
251 XML_TOK_INDEX_TYPE_CHAPTER
,
252 XML_TOK_INDEX_TYPE_LINK_START
,
253 XML_TOK_INDEX_TYPE_LINK_END
,
254 XML_TOK_INDEX_TYPE_BIBLIOGRAPHY
258 SvXMLEnumMapEntry aTemplateTokenTypeMap
[] =
260 { XML_INDEX_ENTRY_TEXT
, XML_TOK_INDEX_TYPE_ENTRY_TEXT
},
261 { XML_INDEX_ENTRY_TAB_STOP
, XML_TOK_INDEX_TYPE_TAB_STOP
},
262 { XML_INDEX_ENTRY_SPAN
, XML_TOK_INDEX_TYPE_TEXT
},
263 { XML_INDEX_ENTRY_PAGE_NUMBER
, XML_TOK_INDEX_TYPE_PAGE_NUMBER
},
264 { XML_INDEX_ENTRY_CHAPTER
, XML_TOK_INDEX_TYPE_CHAPTER
},
265 { XML_INDEX_ENTRY_LINK_START
, XML_TOK_INDEX_TYPE_LINK_START
},
266 { XML_INDEX_ENTRY_LINK_END
, XML_TOK_INDEX_TYPE_LINK_END
},
267 { XML_INDEX_ENTRY_BIBLIOGRAPHY
, XML_TOK_INDEX_TYPE_BIBLIOGRAPHY
},
268 { XML_TOKEN_INVALID
, 0 }
271 SvXMLImportContext
*XMLIndexTemplateContext::CreateChildContext(
273 const OUString
& rLocalName
,
274 const Reference
<XAttributeList
> & xAttrList
)
276 SvXMLImportContext
* pContext
= NULL
;
278 if (XML_NAMESPACE_TEXT
== nPrefix
)
281 if (SvXMLUnitConverter::convertEnum(nToken
, rLocalName
,
282 aTemplateTokenTypeMap
))
284 // can this index accept this kind of token?
285 if (pAllowedTokenTypesMap
[nToken
])
287 switch ((TemplateTokenType
)nToken
)
289 case XML_TOK_INDEX_TYPE_ENTRY_TEXT
:
290 pContext
= new XMLIndexSimpleEntryContext(
291 GetImport(), sTokenEntryText
, *this,
292 nPrefix
, rLocalName
);
295 case XML_TOK_INDEX_TYPE_PAGE_NUMBER
:
296 pContext
= new XMLIndexSimpleEntryContext(
297 GetImport(), sTokenPageNumber
, *this,
298 nPrefix
, rLocalName
);
301 case XML_TOK_INDEX_TYPE_LINK_START
:
302 pContext
= new XMLIndexSimpleEntryContext(
303 GetImport(), sTokenHyperlinkStart
, *this,
304 nPrefix
, rLocalName
);
307 case XML_TOK_INDEX_TYPE_LINK_END
:
308 pContext
= new XMLIndexSimpleEntryContext(
309 GetImport(), sTokenHyperlinkEnd
, *this,
310 nPrefix
, rLocalName
);
313 case XML_TOK_INDEX_TYPE_TEXT
:
314 pContext
= new XMLIndexSpanEntryContext(
315 GetImport(), *this, nPrefix
, rLocalName
);
318 case XML_TOK_INDEX_TYPE_TAB_STOP
:
319 pContext
= new XMLIndexTabStopEntryContext(
320 GetImport(), *this, nPrefix
, rLocalName
);
323 case XML_TOK_INDEX_TYPE_BIBLIOGRAPHY
:
324 pContext
= new XMLIndexBibliographyEntryContext(
325 GetImport(), *this, nPrefix
, rLocalName
);
328 case XML_TOK_INDEX_TYPE_CHAPTER
:
329 pContext
= new XMLIndexChapterInfoEntryContext(
330 GetImport(), *this, nPrefix
, rLocalName
, bTOC
);
342 if (NULL
== pContext
)
344 return SvXMLImportContext::CreateChildContext(nPrefix
, rLocalName
,
354 // maps for the XMLIndexTemplateContext constructor
358 // table of content and user defined index:
360 const SvXMLEnumMapEntry aLevelNameTOCMap
[] =
372 { XML_TOKEN_INVALID
, 0 }
375 const sal_Char
* aLevelStylePropNameTOCMap
[] =
376 { NULL
, "ParaStyleLevel1", "ParaStyleLevel2", "ParaStyleLevel3",
377 "ParaStyleLevel4", "ParaStyleLevel5", "ParaStyleLevel6",
378 "ParaStyleLevel7", "ParaStyleLevel8", "ParaStyleLevel9",
379 "ParaStyleLevel10", NULL
};
381 const sal_Bool aAllowedTokenTypesTOC
[] =
383 sal_True
, // XML_TOK_INDEX_TYPE_ENTRY_TEXT =
384 sal_True
, // XML_TOK_INDEX_TYPE_TAB_STOP,
385 sal_True
, // XML_TOK_INDEX_TYPE_TEXT,
386 sal_True
, // XML_TOK_INDEX_TYPE_PAGE_NUMBER,
387 sal_True
, // XML_TOK_INDEX_TYPE_CHAPTER,
388 sal_True
, // XML_TOK_INDEX_TYPE_LINK_START,
389 sal_True
, // XML_TOK_INDEX_TYPE_LINK_END,
390 sal_False
// XML_TOK_INDEX_TYPE_BIBLIOGRAPHY
393 const sal_Bool aAllowedTokenTypesUser
[] =
395 sal_True
, // XML_TOK_INDEX_TYPE_ENTRY_TEXT =
396 sal_True
, // XML_TOK_INDEX_TYPE_TAB_STOP,
397 sal_True
, // XML_TOK_INDEX_TYPE_TEXT,
398 sal_True
, // XML_TOK_INDEX_TYPE_PAGE_NUMBER,
399 sal_True
, // XML_TOK_INDEX_TYPE_CHAPTER,
400 sal_False
, // XML_TOK_INDEX_TYPE_LINK_START,
401 sal_False
, // XML_TOK_INDEX_TYPE_LINK_END,
402 sal_False
// XML_TOK_INDEX_TYPE_BIBLIOGRAPHY
406 // alphabetical index
408 const SvXMLEnumMapEntry aLevelNameAlphaMap
[] =
410 { XML_SEPARATOR
, 1 },
414 { XML_TOKEN_INVALID
, 0 }
417 const sal_Char
* aLevelStylePropNameAlphaMap
[] =
418 { NULL
, "ParaStyleSeparator", "ParaStyleLevel1", "ParaStyleLevel2",
419 "ParaStyleLevel3", NULL
};
421 const sal_Bool aAllowedTokenTypesAlpha
[] =
423 sal_True
, // XML_TOK_INDEX_TYPE_ENTRY_TEXT =
424 sal_True
, // XML_TOK_INDEX_TYPE_TAB_STOP,
425 sal_True
, // XML_TOK_INDEX_TYPE_TEXT,
426 sal_True
, // XML_TOK_INDEX_TYPE_PAGE_NUMBER,
427 sal_True
, // XML_TOK_INDEX_TYPE_CHAPTER,
428 sal_False
, // XML_TOK_INDEX_TYPE_LINK_START,
429 sal_False
, // XML_TOK_INDEX_TYPE_LINK_END,
430 sal_False
// XML_TOK_INDEX_TYPE_BIBLIOGRAPHY
434 // bibliography index:
436 const SvXMLEnumMapEntry aLevelNameBibliographyMap
[] =
441 { XML_CONFERENCE
, 4 },
449 { XML_INCOLLECTION
, 12 },
450 { XML_INPROCEEDINGS
, 13 },
453 { XML_MASTERSTHESIS
, 16 },
455 { XML_PHDTHESIS
, 18 },
456 { XML_PROCEEDINGS
, 19 },
457 { XML_TECHREPORT
, 20 },
458 { XML_UNPUBLISHED
, 21 },
460 { XML_TOKEN_INVALID
, 0 }
463 // TODO: replace with real property names, when available
464 const sal_Char
* aLevelStylePropNameBibliographyMap
[] =
466 NULL
, "ParaStyleLevel1", "ParaStyleLevel1", "ParaStyleLevel1",
467 "ParaStyleLevel1", "ParaStyleLevel1", "ParaStyleLevel1",
468 "ParaStyleLevel1", "ParaStyleLevel1", "ParaStyleLevel1",
469 "ParaStyleLevel1", "ParaStyleLevel1", "ParaStyleLevel1",
470 "ParaStyleLevel1", "ParaStyleLevel1", "ParaStyleLevel1",
471 "ParaStyleLevel1", "ParaStyleLevel1", "ParaStyleLevel1",
472 "ParaStyleLevel1", "ParaStyleLevel1", "ParaStyleLevel1",
473 "ParaStyleLevel1", NULL
};
475 const sal_Bool aAllowedTokenTypesBibliography
[] =
477 sal_True
, // XML_TOK_INDEX_TYPE_ENTRY_TEXT =
478 sal_True
, // XML_TOK_INDEX_TYPE_TAB_STOP,
479 sal_True
, // XML_TOK_INDEX_TYPE_TEXT,
480 sal_True
, // XML_TOK_INDEX_TYPE_PAGE_NUMBER,
481 sal_False
, // XML_TOK_INDEX_TYPE_CHAPTER,
482 sal_False
, // XML_TOK_INDEX_TYPE_LINK_START,
483 sal_False
, // XML_TOK_INDEX_TYPE_LINK_END,
484 sal_True
// XML_TOK_INDEX_TYPE_BIBLIOGRAPHY
488 // table, illustration and object index
491 const SvXMLEnumMapEntry
* aLevelNameTableMap
= NULL
;
493 const sal_Char
* aLevelStylePropNameTableMap
[] =
494 { NULL
, "ParaStyleLevel1", NULL
};
496 const sal_Bool aAllowedTokenTypesTable
[] =
498 sal_True
, // XML_TOK_INDEX_TYPE_ENTRY_TEXT =
499 sal_True
, // XML_TOK_INDEX_TYPE_TAB_STOP,
500 sal_True
, // XML_TOK_INDEX_TYPE_TEXT,
501 sal_True
, // XML_TOK_INDEX_TYPE_PAGE_NUMBER,
502 sal_True
, // XML_TOK_INDEX_TYPE_CHAPTER,
503 sal_False
, // XML_TOK_INDEX_TYPE_LINK_START,
504 sal_False
, // XML_TOK_INDEX_TYPE_LINK_END,
505 sal_False
// XML_TOK_INDEX_TYPE_BIBLIOGRAPHY