LibreOffice Module xmloff (master) 1
XMLFootnoteImportContext.cxx
Go to the documentation of this file.
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
21
22#include <rtl/ustring.hxx>
23#include <sal/log.hxx>
24#include <xmloff/xmlimp.hxx>
25#include <xmloff/txtimp.hxx>
28#include <xmloff/xmltoken.hxx>
29
31
32#include <com/sun/star/frame/XModel.hpp>
33#include <com/sun/star/xml/sax/XAttributeList.hpp>
34#include <com/sun/star/text/XTextContent.hpp>
35#include <com/sun/star/beans/XPropertySet.hpp>
36#include <com/sun/star/lang/XMultiServiceFactory.hpp>
37#include <com/sun/star/text/XFootnote.hpp>
38
39
40using namespace ::com::sun::star::uno;
41using namespace ::com::sun::star::text;
42using namespace ::com::sun::star::lang;
43using namespace ::com::sun::star::beans;
44using namespace ::com::sun::star::xml::sax;
45using namespace ::xmloff::token;
46
48 SvXMLImport& rImport,
50: SvXMLImportContext(rImport)
51, mbListContextPushed(false)
52, rHelper(rHlp)
53{
54}
55
57 sal_Int32 /*nElement*/,
58 const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
59{
60 // create footnote
61 Reference<XMultiServiceFactory> xFactory(GetImport().GetModel(),
62 UNO_QUERY);
63 if( !xFactory.is() )
64 return;
65
66 // create endnote or footnote
67 bool bIsEndnote = false;
68 for( auto& aIter : sax_fastparser::castToFastAttributeList(xAttrList) )
69 {
70 if( aIter.getToken() == XML_ELEMENT(TEXT, XML_NOTE_CLASS) )
71 {
72 if( IsXMLToken( aIter, XML_ENDNOTE ) )
73 bIsEndnote = true;
74 break;
75 }
76 }
77
78 Reference<XInterface> xIfc = xFactory->createInstance(
79 bIsEndnote ?
80 OUString("com.sun.star.text.Endnote") :
81 OUString("com.sun.star.text.Footnote") );
82
83 // attach footnote to document
84 Reference<XTextContent> xTextContent(xIfc, UNO_QUERY);
85 rHelper.InsertTextContent(xTextContent);
86
87 // process id attribute
88 for( auto& aIter : sax_fastparser::castToFastAttributeList(xAttrList) )
89 {
90 if( aIter.getToken() == XML_ELEMENT(TEXT, XML_ID) )
91 {
92 // get ID ...
93 Reference<XPropertySet> xPropertySet(xTextContent, UNO_QUERY);
94 Any aAny =xPropertySet->getPropertyValue("ReferenceId");
95 sal_Int16 nID = 0;
96 aAny >>= nID;
97
98 // ... and insert into map
99 rHelper.InsertFootnoteID( aIter.toString(), nID);
100 break;
101 }
102 }
103
104 // save old cursor and install new one
106 Reference<XText> xText(xTextContent, UNO_QUERY);
107 rHelper.SetCursor(xText->createTextCursor());
108
109 // remember old list item and block (#89891#) and reset them
110 // for the footnote
112 mbListContextPushed = true;
113
114 // remember footnote (for CreateChildContext)
115 Reference<XFootnote> xNote(xTextContent, UNO_QUERY);
116 xFootnote = xNote;
117
118 // else: ignore footnote! Content will be merged into document.
119}
120
122{
123 // get rid of last dummy paragraph
125
126 // reinstall old cursor
128
129 // reinstall old list item
132 }
133}
134
135css::uno::Reference< css::xml::sax::XFastContextHandler > XMLFootnoteImportContext::createFastChildContext(
136 sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
137{
138 SvXMLImportContextRef xContext;
139
140 switch(nElement)
141 {
143 {
144 // little hack: we only care for one attribute of the citation
145 // element. We handle that here, and then return a
146 // default context.
147 for( auto& aIter : sax_fastparser::castToFastAttributeList(xAttrList) )
148 {
149 if ( aIter.getToken() == XML_ELEMENT(TEXT, XML_LABEL) )
150 xFootnote->setLabel(aIter.toString());
151 }
152
153 // ignore content: return default context
154 break;
155 }
156
158 // return footnote body
159 xContext = new XMLFootnoteBodyImportContext(GetImport());
160 break;
161
162 default:
163 XMLOFF_WARN_UNKNOWN_ELEMENT("xmloff", nElement);
164 }
165
166 return xContext;
167}
168
169/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
This class deliberately does not support XWeak, to improve performance when loading large documents.
Definition: xmlictxt.hxx:48
SvXMLImport & GetImport()
Definition: xmlictxt.hxx:60
import foot- and endnote body elements (<text:footnote-body>)
virtual void SAL_CALL startFastElement(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList) override
css::uno::Reference< css::text::XTextCursor > xOldCursor
old document cursor
XMLFootnoteImportContext(SvXMLImport &rImport, XMLTextImportHelper &rHlp)
virtual void SAL_CALL endFastElement(sal_Int32 nElement) override
endFastElement is called before a context will be destructed, but after an elements context has been ...
virtual css::uno::Reference< css::xml::sax::XFastContextHandler > SAL_CALL createFastChildContext(sal_Int32 nElement, const css::uno::Reference< css::xml::sax::XFastAttributeList > &AttrList) override
XMLTextImportHelper & rHelper
text import helper; holds current XTextCursor (and XText)
bool mbListContextPushed
old list item and block (#89891#)
css::uno::Reference< css::text::XFootnote > xFootnote
the footnote
void InsertFootnoteID(const OUString &sXMLId, sal_Int16 nAPIId)
insert new footnote ID.
void InsertTextContent(css::uno::Reference< css::text::XTextContent > const &xContent)
Definition: txtimp.cxx:757
css::uno::Reference< css::text::XTextCursor > & GetCursor()
Definition: txtimp.cxx:216
void PopListContext()
pop the list context stack
Definition: txtimp.cxx:2067
void PushListContext()
push a list context on the list context stack
Definition: txtimp.cxx:2062
void SetCursor(const css::uno::Reference< css::text::XTextCursor > &rCursor)
Definition: txtimp.cxx:615
void DeleteParagraph()
Definition: txtimp.cxx:769
Reference< XSingleServiceFactory > xFactory
FastAttributeList & castToFastAttributeList(const css::uno::Reference< css::xml::sax::XFastAttributeList > &xAttrList)
Handling of tokens in XML:
bool IsXMLToken(std::u16string_view rString, enum XMLTokenEnum eToken)
compare eToken to the string
Definition: xmltoken.cxx:3597
TEXT
#define XMLOFF_WARN_UNKNOWN_ELEMENT(area, token)
Definition: xmlictxt.hxx:120
#define XML_ELEMENT(prefix, name)
Definition: xmlimp.hxx:97