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