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/lang/XMultiServiceFactory.hpp>
22 #include <com/sun/star/lang/IllegalArgumentException.hpp>
23 #include <com/sun/star/uno/XInterface.hpp>
24 #include <com/sun/star/text/XTextContent.hpp>
25 #include <com/sun/star/text/XTextSection.hpp>
26 #include <com/sun/star/text/XRelativeTextContentInsert.hpp>
27 #include <com/sun/star/beans/XPropertySet.hpp>
28 #include <sax/tools/converter.hxx>
29 #include "XMLIndexTOCSourceContext.hxx"
30 #include "XMLIndexObjectSourceContext.hxx"
31 #include "XMLIndexAlphabeticalSourceContext.hxx"
32 #include "XMLIndexUserSourceContext.hxx"
33 #include "XMLIndexBibliographySourceContext.hxx"
34 #include "XMLIndexTableSourceContext.hxx"
35 #include "XMLIndexIllustrationSourceContext.hxx"
36 #include "XMLIndexBodyContext.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/prstylei.hxx>
44 #include "xmloff/xmlerror.hxx"
45 #include <xmloff/xmluconv.hxx>
46 #include <rtl/ustring.hxx>
49 using namespace ::com::sun::star::uno
;
50 using namespace ::com::sun::star::text
;
51 using namespace ::xmloff::token
;
53 using ::com::sun::star::beans::XPropertySet
;
54 using ::com::sun::star::uno::Reference
;
55 using ::com::sun::star::xml::sax::XAttributeList
;
56 using ::com::sun::star::lang::XMultiServiceFactory
;
57 using ::com::sun::star::lang::IllegalArgumentException
;
60 TYPEINIT1(XMLIndexTOCContext
, SvXMLImportContext
);
62 static const sal_Char
* aIndexServiceMap
[] =
64 "com.sun.star.text.ContentIndex",
65 "com.sun.star.text.DocumentIndex",
66 "com.sun.star.text.TableIndex",
67 "com.sun.star.text.ObjectIndex",
68 "com.sun.star.text.Bibliography",
69 "com.sun.star.text.UserIndex",
70 "com.sun.star.text.IllustrationsIndex"
73 static const XMLTokenEnum aIndexSourceElementMap
[] =
75 XML_TABLE_OF_CONTENT_SOURCE
,
76 XML_ALPHABETICAL_INDEX_SOURCE
,
77 XML_TABLE_INDEX_SOURCE
,
78 XML_OBJECT_INDEX_SOURCE
,
79 XML_BIBLIOGRAPHY_SOURCE
,
80 XML_USER_INDEX_SOURCE
,
81 XML_ILLUSTRATION_INDEX_SOURCE
84 SvXMLEnumMapEntry
const aIndexTypeMap
[] =
86 { XML_TABLE_OF_CONTENT
, TEXT_INDEX_TOC
},
87 { XML_ALPHABETICAL_INDEX
, TEXT_INDEX_ALPHABETICAL
},
88 { XML_TABLE_INDEX
, TEXT_INDEX_TABLE
},
89 { XML_OBJECT_INDEX
, TEXT_INDEX_OBJECT
},
90 { XML_BIBLIOGRAPHY
, TEXT_INDEX_BIBLIOGRAPHY
},
91 { XML_USER_INDEX
, TEXT_INDEX_USER
},
92 { XML_ILLUSTRATION_INDEX
, TEXT_INDEX_ILLUSTRATION
},
93 { XML_TOKEN_INVALID
, 0 }
97 XMLIndexTOCContext::XMLIndexTOCContext(
100 const OUString
& rLocalName
)
101 : SvXMLImportContext(rImport
, nPrfx
, rLocalName
)
103 , sIsProtected("IsProtected")
107 if (XML_NAMESPACE_TEXT
== nPrfx
)
110 if (SvXMLUnitConverter::convertEnum(nTmp
, rLocalName
, aIndexTypeMap
))
112 // check for array index:
113 OSL_ENSURE(nTmp
< (SAL_N_ELEMENTS(aIndexServiceMap
)), "index out of range");
114 OSL_ENSURE(SAL_N_ELEMENTS(aIndexServiceMap
) ==
115 SAL_N_ELEMENTS(aIndexSourceElementMap
),
116 "service and source element maps must be same size");
118 eIndexType
= static_cast<IndexTypeEnum
>(nTmp
);
124 XMLIndexTOCContext::~XMLIndexTOCContext()
128 void XMLIndexTOCContext::StartElement(
129 const Reference
<XAttributeList
> & xAttrList
)
133 // find text:style-name attribute and set section style
134 // find text:protected and set value
135 // find text:name and set value (if not empty)
136 sal_Int16 nCount
= xAttrList
->getLength();
137 sal_Bool bProtected
= sal_False
;
140 XMLPropStyleContext
* pStyle(NULL
);
141 for(sal_Int16 nAttr
= 0; nAttr
< nCount
; nAttr
++)
144 sal_uInt16 nPrefix
= GetImport().GetNamespaceMap().
145 GetKeyByAttrName( xAttrList
->getNameByIndex(nAttr
),
147 if ( XML_NAMESPACE_TEXT
== nPrefix
)
149 if ( IsXMLToken( sLocalName
, XML_STYLE_NAME
) )
151 pStyle
= GetImport().GetTextImport()->FindSectionStyle(
152 xAttrList
->getValueByIndex(nAttr
));
154 else if ( IsXMLToken( sLocalName
, XML_PROTECTED
) )
157 if (::sax::Converter::convertBool(
158 bTmp
, xAttrList
->getValueByIndex(nAttr
)))
163 else if ( IsXMLToken( sLocalName
, XML_NAME
) )
165 sIndexName
= xAttrList
->getValueByIndex(nAttr
);
168 else if ( XML_NAMESPACE_XML
== nPrefix
)
170 if ( IsXMLToken( sLocalName
, XML_ID
) )
172 sXmlId
= xAttrList
->getValueByIndex(nAttr
);
177 // create table of content (via MultiServiceFactory)
178 Reference
<XMultiServiceFactory
> xFactory(GetImport().GetModel(),
182 Reference
<XInterface
> xIfc
=
183 xFactory
->createInstance(
184 OUString::createFromAscii(aIndexServiceMap
[eIndexType
]));
188 Reference
<XPropertySet
> xPropSet(xIfc
, UNO_QUERY
);
189 xTOCPropertySet
= xPropSet
;
193 // The inserted index consists of an empty paragraph
194 // only, as well as an empty paragraph *after* the index
195 // b) insert marker after index, and put Cursor inside of the
200 OUString
sMarker(" ");
202 OUString
sMarker("Y");
204 UniReference
<XMLTextImportHelper
> rImport
=
205 GetImport().GetTextImport();
208 Reference
<XTextContent
> xTextContent(xIfc
, UNO_QUERY
);
211 GetImport().GetTextImport()->InsertTextContent(
214 catch(const IllegalArgumentException
& e
)
216 // illegal argument? Then we can't accept indices here!
217 Sequence
<OUString
> aSeq(1);
218 aSeq
[0] = GetLocalName();
219 GetImport().SetError(
220 XMLERROR_FLAG_ERROR
| XMLERROR_NO_INDEX_ALLOWED_HERE
,
221 aSeq
, e
.Message
, NULL
);
223 // set bValid to false, and return prematurely
228 // xml:id for RDF metadata
229 GetImport().SetXmlId(xIfc
, sXmlId
);
231 // b) insert marker and move cursor
232 rImport
->InsertString(sMarker
);
233 rImport
->GetCursor()->goLeft(2, sal_False
);
237 // finally, check for redlines that should start at
238 // the section start node
240 GetImport().GetTextImport()->
241 RedlineAdjustStartNodeCursor(sal_True
);
245 pStyle
->FillPropertySet( xTOCPropertySet
);
249 aAny
.setValue( &bProtected
, ::getBooleanCppuType() );
250 xTOCPropertySet
->setPropertyValue( sIsProtected
, aAny
);
252 if (!sIndexName
.isEmpty())
255 xTOCPropertySet
->setPropertyValue( sName
, aAny
);
260 void XMLIndexTOCContext::EndElement()
262 // complete import of index by removing the markers (if the index
263 // was actually inserted, that is)
268 UniReference
<XMLTextImportHelper
> rHelper
= GetImport().GetTextImport();
270 // get rid of last paragraph (unless it's the only paragraph)
271 rHelper
->GetCursor()->goRight(1, sal_False
);
272 if( xBodyContextRef
.Is() &&
273 ((XMLIndexBodyContext
*)&xBodyContextRef
)->HasContent() )
275 rHelper
->GetCursor()->goLeft(1, sal_True
);
276 rHelper
->GetText()->insertString(rHelper
->GetCursorAsRange(),
280 // and delete second marker
281 rHelper
->GetCursor()->goRight(1, sal_True
);
282 rHelper
->GetText()->insertString(rHelper
->GetCursorAsRange(),
285 // check for Redlines on our end node
286 GetImport().GetTextImport()->RedlineAdjustStartNodeCursor(sal_False
);
290 SvXMLImportContext
* XMLIndexTOCContext::CreateChildContext(
292 const OUString
& rLocalName
,
293 const Reference
<XAttributeList
> & xAttrList
)
295 SvXMLImportContext
* pContext
= NULL
;
299 if (XML_NAMESPACE_TEXT
== nPrefix
)
301 if ( IsXMLToken( rLocalName
, XML_INDEX_BODY
) )
303 pContext
= new XMLIndexBodyContext(GetImport(), nPrefix
,
305 if ( !xBodyContextRef
.Is() ||
306 !((XMLIndexBodyContext
*)&xBodyContextRef
)->HasContent() )
308 xBodyContextRef
= pContext
;
311 else if (IsXMLToken(rLocalName
, aIndexSourceElementMap
[eIndexType
]))
313 // instantiate source context for the appropriate index type
317 pContext
= new XMLIndexTOCSourceContext(
318 GetImport(), nPrefix
, rLocalName
, xTOCPropertySet
);
321 case TEXT_INDEX_OBJECT
:
322 pContext
= new XMLIndexObjectSourceContext(
323 GetImport(), nPrefix
, rLocalName
, xTOCPropertySet
);
326 case TEXT_INDEX_ALPHABETICAL
:
327 pContext
= new XMLIndexAlphabeticalSourceContext(
328 GetImport(), nPrefix
, rLocalName
, xTOCPropertySet
);
331 case TEXT_INDEX_USER
:
332 pContext
= new XMLIndexUserSourceContext(
333 GetImport(), nPrefix
, rLocalName
, xTOCPropertySet
);
336 case TEXT_INDEX_BIBLIOGRAPHY
:
337 pContext
= new XMLIndexBibliographySourceContext(
338 GetImport(), nPrefix
, rLocalName
, xTOCPropertySet
);
341 case TEXT_INDEX_TABLE
:
342 pContext
= new XMLIndexTableSourceContext(
343 GetImport(), nPrefix
, rLocalName
, xTOCPropertySet
);
346 case TEXT_INDEX_ILLUSTRATION
:
347 pContext
= new XMLIndexIllustrationSourceContext(
348 GetImport(), nPrefix
, rLocalName
, xTOCPropertySet
);
352 OSL_FAIL("index type not implemented");
358 // else: no text: namespace -> ignore
360 // else: not valid -> ignore
363 if (pContext
== NULL
)
365 pContext
= SvXMLImportContext::CreateChildContext(nPrefix
, rLocalName
,
372 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */