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 "XMLFootnoteImportContext.hxx"
22 #include <comphelper/diagnose_ex.hxx>
23 #include <rtl/ustring.hxx>
24 #include <sal/log.hxx>
25 #include <xmloff/xmlimp.hxx>
26 #include <xmloff/txtimp.hxx>
27 #include <xmloff/xmlnamespace.hxx>
28 #include <xmloff/xmltoken.hxx>
30 #include "XMLFootnoteBodyImportContext.hxx"
32 #include <com/sun/star/frame/XModel.hpp>
33 #include <com/sun/star/text/XTextContent.hpp>
34 #include <com/sun/star/beans/XPropertySet.hpp>
35 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
36 #include <com/sun/star/text/XFootnote.hpp>
39 using namespace ::com::sun::star::uno
;
40 using namespace ::com::sun::star::text
;
41 using namespace ::com::sun::star::lang
;
42 using namespace ::com::sun::star::beans
;
43 using namespace ::com::sun::star::xml::sax
;
44 using namespace ::xmloff::token
;
46 XMLFootnoteImportContext::XMLFootnoteImportContext(
48 XMLTextImportHelper
& rHlp
)
49 : SvXMLImportContext(rImport
)
50 , mbListContextPushed(false)
55 void XMLFootnoteImportContext::startFastElement(
56 sal_Int32
/*nElement*/,
57 const css::uno::Reference
< css::xml::sax::XFastAttributeList
>& xAttrList
)
60 Reference
<XMultiServiceFactory
> xFactory(GetImport().GetModel(),
65 // create endnote or footnote
66 bool bIsEndnote
= false;
67 for( auto& aIter
: sax_fastparser::castToFastAttributeList(xAttrList
) )
69 if( aIter
.getToken() == XML_ELEMENT(TEXT
, XML_NOTE_CLASS
) )
71 if( IsXMLToken( aIter
, XML_ENDNOTE
) )
77 Reference
<XInterface
> xIfc
= xFactory
->createInstance(
79 u
"com.sun.star.text.Endnote"_ustr
:
80 u
"com.sun.star.text.Footnote"_ustr
);
82 // attach footnote to document
83 Reference
<XTextContent
> xTextContent(xIfc
, UNO_QUERY
);
84 rHelper
.InsertTextContent(xTextContent
);
86 // process id attribute
87 for( auto& aIter
: sax_fastparser::castToFastAttributeList(xAttrList
) )
89 if( aIter
.getToken() == XML_ELEMENT(TEXT
, XML_ID
) )
92 Reference
<XPropertySet
> xPropertySet(xTextContent
, UNO_QUERY
);
93 Any aAny
=xPropertySet
->getPropertyValue(u
"ReferenceId"_ustr
);
97 // ... and insert into map
98 rHelper
.InsertFootnoteID( aIter
.toString(), nID
);
103 // save old cursor and install new one
104 xOldCursor
= rHelper
.GetCursor();
105 Reference
<XText
> xText(xTextContent
, UNO_QUERY
);
108 // May fail e.g. for a nested footnote, which is formally a valid ODF, but is not supported
109 rHelper
.SetCursor(xText
->createTextCursor());
111 catch (css::uno::RuntimeException
&)
113 TOOLS_WARN_EXCEPTION("xmloff.text", "skipping the footnote: caught");
117 // remember old list item and block (#89891#) and reset them
119 rHelper
.PushListContext();
120 mbListContextPushed
= true;
122 // remember footnote (for CreateChildContext)
123 xFootnote
.set(xTextContent
, UNO_QUERY
);
125 // else: ignore footnote! Content will be merged into document.
128 void XMLFootnoteImportContext::endFastElement(sal_Int32
)
130 // get rid of last dummy paragraph
131 rHelper
.DeleteParagraph();
133 // reinstall old cursor
134 rHelper
.SetCursor(xOldCursor
);
136 // reinstall old list item
137 if (mbListContextPushed
) {
138 rHelper
.PopListContext();
142 css::uno::Reference
< css::xml::sax::XFastContextHandler
> XMLFootnoteImportContext::createFastChildContext(
143 sal_Int32 nElement
, const css::uno::Reference
< css::xml::sax::XFastAttributeList
>& xAttrList
)
147 SvXMLImportContextRef xContext
;
151 case XML_ELEMENT(TEXT
, XML_NOTE_CITATION
):
153 // little hack: we only care for one attribute of the citation
154 // element. We handle that here, and then return a
156 for( auto& aIter
: sax_fastparser::castToFastAttributeList(xAttrList
) )
158 if ( aIter
.getToken() == XML_ELEMENT(TEXT
, XML_LABEL
) )
159 xFootnote
->setLabel(aIter
.toString());
162 // ignore content: return default context
166 case XML_ELEMENT(TEXT
, XML_NOTE_BODY
):
167 // return footnote body
168 xContext
= new XMLFootnoteBodyImportContext(GetImport());
172 XMLOFF_WARN_UNKNOWN_ELEMENT("xmloff", nElement
);
178 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */