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 "XMLIndexTOCContext.hxx"
21 #include <com/sun/star/frame/XModel.hpp>
22 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
23 #include <com/sun/star/lang/IllegalArgumentException.hpp>
24 #include <com/sun/star/uno/XInterface.hpp>
25 #include <com/sun/star/text/XTextContent.hpp>
26 #include <com/sun/star/beans/XPropertySet.hpp>
27 #include <sax/tools/converter.hxx>
28 #include "XMLIndexTOCSourceContext.hxx"
29 #include "XMLIndexObjectSourceContext.hxx"
30 #include "XMLIndexAlphabeticalSourceContext.hxx"
31 #include "XMLIndexUserSourceContext.hxx"
32 #include "XMLIndexBibliographySourceContext.hxx"
33 #include "XMLIndexTableSourceContext.hxx"
34 #include "XMLIndexIllustrationSourceContext.hxx"
35 #include "XMLIndexBodyContext.hxx"
36 #include <xmloff/xmlictxt.hxx>
37 #include <xmloff/xmlimp.hxx>
38 #include <xmloff/txtimp.hxx>
39 #include <xmloff/namespacemap.hxx>
40 #include <xmloff/xmlnamespace.hxx>
41 #include <xmloff/xmltoken.hxx>
42 #include <xmloff/prstylei.hxx>
43 #include <xmloff/xmlerror.hxx>
44 #include <xmloff/xmluconv.hxx>
45 #include <xmloff/xmlement.hxx>
46 #include <rtl/ustring.hxx>
47 #include <osl/diagnose.h>
50 using namespace ::com::sun::star::uno
;
51 using namespace ::com::sun::star::text
;
52 using namespace ::xmloff::token
;
54 using ::com::sun::star::beans::XPropertySet
;
55 using ::com::sun::star::uno::Reference
;
56 using ::com::sun::star::xml::sax::XAttributeList
;
57 using ::com::sun::star::lang::XMultiServiceFactory
;
58 using ::com::sun::star::lang::IllegalArgumentException
;
61 static const char* aIndexServiceMap
[] =
63 "com.sun.star.text.ContentIndex",
64 "com.sun.star.text.DocumentIndex",
65 "com.sun.star.text.TableIndex",
66 "com.sun.star.text.ObjectIndex",
67 "com.sun.star.text.Bibliography",
68 "com.sun.star.text.UserIndex",
69 "com.sun.star.text.IllustrationsIndex"
72 const XMLTokenEnum aIndexSourceElementMap
[] =
74 XML_TABLE_OF_CONTENT_SOURCE
,
75 XML_ALPHABETICAL_INDEX_SOURCE
,
76 XML_TABLE_INDEX_SOURCE
,
77 XML_OBJECT_INDEX_SOURCE
,
78 XML_BIBLIOGRAPHY_SOURCE
,
79 XML_USER_INDEX_SOURCE
,
80 XML_ILLUSTRATION_INDEX_SOURCE
83 SvXMLEnumMapEntry
<IndexTypeEnum
> const aIndexTypeMap
[] =
85 { XML_TABLE_OF_CONTENT
, TEXT_INDEX_TOC
},
86 { XML_ALPHABETICAL_INDEX
, TEXT_INDEX_ALPHABETICAL
},
87 { XML_TABLE_INDEX
, TEXT_INDEX_TABLE
},
88 { XML_OBJECT_INDEX
, TEXT_INDEX_OBJECT
},
89 { XML_BIBLIOGRAPHY
, TEXT_INDEX_BIBLIOGRAPHY
},
90 { XML_USER_INDEX
, TEXT_INDEX_USER
},
91 { XML_ILLUSTRATION_INDEX
, TEXT_INDEX_ILLUSTRATION
},
92 { XML_TOKEN_INVALID
, IndexTypeEnum(0) }
96 XMLIndexTOCContext::XMLIndexTOCContext(SvXMLImport
& rImport
,
97 sal_uInt16 nPrfx
, const OUString
& rLocalName
)
98 : SvXMLImportContext(rImport
, nPrfx
, rLocalName
)
99 , eIndexType(TEXT_INDEX_UNKNOWN
)
102 if (XML_NAMESPACE_TEXT
== nPrfx
)
104 if (SvXMLUnitConverter::convertEnum(eIndexType
, rLocalName
, aIndexTypeMap
))
106 // check for array index:
107 OSL_ENSURE(unsigned(eIndexType
) < (SAL_N_ELEMENTS(aIndexServiceMap
)), "index out of range");
108 OSL_ENSURE(SAL_N_ELEMENTS(aIndexServiceMap
) ==
109 SAL_N_ELEMENTS(aIndexSourceElementMap
),
110 "service and source element maps must be same size");
116 XMLIndexTOCContext::~XMLIndexTOCContext()
120 void XMLIndexTOCContext::StartElement(
121 const Reference
<XAttributeList
> & xAttrList
)
126 // find text:style-name attribute and set section style
127 // find text:protected and set value
128 // find text:name and set value (if not empty)
129 sal_Int16 nCount
= xAttrList
->getLength();
130 bool bProtected
= false;
133 XMLPropStyleContext
* pStyle(nullptr);
134 for(sal_Int16 nAttr
= 0; nAttr
< nCount
; nAttr
++)
137 sal_uInt16 nPrefix
= GetImport().GetNamespaceMap().
138 GetKeyByAttrName( xAttrList
->getNameByIndex(nAttr
),
140 if ( XML_NAMESPACE_TEXT
== nPrefix
)
142 if ( IsXMLToken( sLocalName
, XML_STYLE_NAME
) )
144 pStyle
= GetImport().GetTextImport()->FindSectionStyle(
145 xAttrList
->getValueByIndex(nAttr
));
147 else if ( IsXMLToken( sLocalName
, XML_PROTECTED
) )
150 if (::sax::Converter::convertBool(
151 bTmp
, xAttrList
->getValueByIndex(nAttr
)))
156 else if ( IsXMLToken( sLocalName
, XML_NAME
) )
158 sIndexName
= xAttrList
->getValueByIndex(nAttr
);
161 else if ( XML_NAMESPACE_XML
== nPrefix
)
163 if ( IsXMLToken( sLocalName
, XML_ID
) )
165 sXmlId
= xAttrList
->getValueByIndex(nAttr
);
170 // create table of content (via MultiServiceFactory)
171 Reference
<XMultiServiceFactory
> xFactory(GetImport().GetModel(),
175 Reference
<XInterface
> xIfc
=
176 xFactory
->createInstance(
177 OUString::createFromAscii(aIndexServiceMap
[eIndexType
]));
181 Reference
<XPropertySet
> xPropSet(xIfc
, UNO_QUERY
);
182 xTOCPropertySet
= xPropSet
;
186 // The inserted index consists of an empty paragraph
187 // only, as well as an empty paragraph *after* the index
188 // b) insert marker after index, and put Cursor inside of the
193 OUString
const sMarker(" ");
195 OUString
const sMarker("Y");
197 rtl::Reference
<XMLTextImportHelper
> rImport
=
198 GetImport().GetTextImport();
201 Reference
<XTextContent
> xTextContent(xIfc
, UNO_QUERY
);
204 GetImport().GetTextImport()->InsertTextContent(
207 catch(const IllegalArgumentException
& e
)
209 // illegal argument? Then we can't accept indices here!
210 Sequence
<OUString
> aSeq
{ GetLocalName() };
211 GetImport().SetError(
212 XMLERROR_FLAG_ERROR
| XMLERROR_NO_INDEX_ALLOWED_HERE
,
213 aSeq
, e
.Message
, nullptr );
215 // set bValid to false, and return prematurely
220 // xml:id for RDF metadata
221 GetImport().SetXmlId(xIfc
, sXmlId
);
223 // b) insert marker and move cursor
224 rImport
->InsertString(sMarker
);
225 rImport
->GetCursor()->goLeft(2, false);
229 // finally, check for redlines that should start at
230 // the section start node
232 GetImport().GetTextImport()->RedlineAdjustStartNodeCursor();
234 if (pStyle
!= nullptr)
236 pStyle
->FillPropertySet( xTOCPropertySet
);
239 xTOCPropertySet
->setPropertyValue( "IsProtected", Any(bProtected
) );
241 if (!sIndexName
.isEmpty())
243 xTOCPropertySet
->setPropertyValue( "Name", Any(sIndexName
) );
248 void XMLIndexTOCContext::endFastElement(sal_Int32
)
250 // complete import of index by removing the markers (if the index
251 // was actually inserted, that is)
255 rtl::Reference
<XMLTextImportHelper
> rHelper
= GetImport().GetTextImport();
257 // get rid of last paragraph (unless it's the only paragraph)
258 rHelper
->GetCursor()->goRight(1, false);
259 if( xBodyContextRef
.is() && xBodyContextRef
->HasContent() )
261 rHelper
->GetCursor()->goLeft(1, true);
262 rHelper
->GetText()->insertString(rHelper
->GetCursorAsRange(),
266 // and delete second marker
267 rHelper
->GetCursor()->goRight(1, true);
268 rHelper
->GetText()->insertString(rHelper
->GetCursorAsRange(),
271 // check for Redlines on our end node
272 GetImport().GetTextImport()->RedlineAdjustStartNodeCursor();
276 SvXMLImportContextRef
XMLIndexTOCContext::CreateChildContext(
278 const OUString
& rLocalName
,
279 const Reference
<XAttributeList
> & /*xAttrList*/ )
281 SvXMLImportContextRef xContext
;
285 if (XML_NAMESPACE_TEXT
== nPrefix
)
287 if ( IsXMLToken( rLocalName
, XML_INDEX_BODY
) )
289 rtl::Reference
<XMLIndexBodyContext
> xNewBodyContext
= new XMLIndexBodyContext(GetImport(), nPrefix
,
291 xContext
= xNewBodyContext
;
292 if ( !xBodyContextRef
.is() || !xBodyContextRef
->HasContent() )
294 xBodyContextRef
= xNewBodyContext
;
297 else if (IsXMLToken(rLocalName
, aIndexSourceElementMap
[eIndexType
]))
299 // instantiate source context for the appropriate index type
303 xContext
= new XMLIndexTOCSourceContext(
304 GetImport(), nPrefix
, rLocalName
, xTOCPropertySet
);
307 case TEXT_INDEX_OBJECT
:
308 xContext
= new XMLIndexObjectSourceContext(
309 GetImport(), nPrefix
, rLocalName
, xTOCPropertySet
);
312 case TEXT_INDEX_ALPHABETICAL
:
313 xContext
= new XMLIndexAlphabeticalSourceContext(
314 GetImport(), nPrefix
, rLocalName
, xTOCPropertySet
);
317 case TEXT_INDEX_USER
:
318 xContext
= new XMLIndexUserSourceContext(
319 GetImport(), nPrefix
, rLocalName
, xTOCPropertySet
);
322 case TEXT_INDEX_BIBLIOGRAPHY
:
323 xContext
= new XMLIndexBibliographySourceContext(
324 GetImport(), nPrefix
, rLocalName
, xTOCPropertySet
);
327 case TEXT_INDEX_TABLE
:
328 xContext
= new XMLIndexTableSourceContext(
329 GetImport(), nPrefix
, rLocalName
, xTOCPropertySet
);
332 case TEXT_INDEX_ILLUSTRATION
:
333 xContext
= new XMLIndexIllustrationSourceContext(
334 GetImport(), nPrefix
, rLocalName
, xTOCPropertySet
);
338 OSL_FAIL("index type not implemented");
344 // else: no text: namespace -> ignore
346 // else: not valid -> ignore
351 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */