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 <xmloff/xmlimp.hxx>
24 #include <xmloff/txtimp.hxx>
25 #include <xmloff/namespacemap.hxx>
26 #include <xmloff/xmlnamespace.hxx>
27 #include <xmloff/xmltoken.hxx>
28 
30 
31 #include <com/sun/star/frame/XModel.hpp>
32 #include <com/sun/star/xml/sax/XAttributeList.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>
37 
38 
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;
45 
46 namespace {
47 
49  XML_TOK_FTN_NOTE_CITATION,
50  XML_TOK_FTN_NOTE_BODY
51 };
52 
53 }
54 
56 {
58  XML_TOK_FTN_NOTE_CITATION },
59  { XML_NAMESPACE_TEXT, XML_NOTE_BODY, XML_TOK_FTN_NOTE_BODY },
61 };
62 
63 
65  SvXMLImport& rImport,
66  XMLTextImportHelper& rHlp,
67  sal_uInt16 nPrfx,
68  const OUString& rLocalName )
69 : SvXMLImportContext(rImport, nPrfx, rLocalName)
70 , mbListContextPushed(false)
71 , rHelper(rHlp)
72 {
73 }
74 
76  const Reference<XAttributeList> & xAttrList)
77 {
78  // create footnote
79  Reference<XMultiServiceFactory> xFactory(GetImport().GetModel(),
80  UNO_QUERY);
81  if( !xFactory.is() )
82  return;
83 
84  // create endnote or footnote
85  bool bIsEndnote = false;
86  sal_Int16 nLength = xAttrList->getLength();
87  for(sal_Int16 nAttr1 = 0; nAttr1 < nLength; nAttr1++)
88  {
89  OUString sLocalName;
90  sal_uInt16 nPrefix = GetImport().GetNamespaceMap().
91  GetKeyByAttrName( xAttrList->getNameByIndex(nAttr1),
92  &sLocalName );
93  if( XML_NAMESPACE_TEXT == nPrefix && IsXMLToken( sLocalName,
94  XML_NOTE_CLASS ) )
95  {
96  const OUString& rValue = xAttrList->getValueByIndex( nAttr1 );
97  if( IsXMLToken( rValue, XML_ENDNOTE ) )
98  bIsEndnote = true;
99  break;
100  }
101  }
102 
103  Reference<XInterface> xIfc = xFactory->createInstance(
104  bIsEndnote ?
105  OUString("com.sun.star.text.Endnote") :
106  OUString("com.sun.star.text.Footnote") );
107 
108  // attach footnote to document
109  Reference<XTextContent> xTextContent(xIfc, UNO_QUERY);
110  rHelper.InsertTextContent(xTextContent);
111 
112  // process id attribute
113  for(sal_Int16 nAttr2 = 0; nAttr2 < nLength; nAttr2++)
114  {
115  OUString sLocalName;
116  sal_uInt16 nPrefix = GetImport().GetNamespaceMap().
117  GetKeyByAttrName( xAttrList->getNameByIndex(nAttr2),
118  &sLocalName );
119 
120  if ( (XML_NAMESPACE_TEXT == nPrefix) &&
121  IsXMLToken( sLocalName, XML_ID ) )
122  {
123  // get ID ...
124  Reference<XPropertySet> xPropertySet(xTextContent, UNO_QUERY);
125  Any aAny =xPropertySet->getPropertyValue("ReferenceId");
126  sal_Int16 nID = 0;
127  aAny >>= nID;
128 
129  // ... and insert into map
131  xAttrList->getValueByIndex(nAttr2),
132  nID);
133  }
134  }
135 
136  // save old cursor and install new one
138  Reference<XText> xText(xTextContent, UNO_QUERY);
139  rHelper.SetCursor(xText->createTextCursor());
140 
141  // remember old list item and block (#89891#) and reset them
142  // for the footnote
144  mbListContextPushed = true;
145 
146  // remember footnote (for CreateChildContext)
147  Reference<XFootnote> xNote(xTextContent, UNO_QUERY);
148  xFootnote = xNote;
149 
150  // else: ignore footnote! Content will be merged into document.
151 }
152 
154 {
155  // ignore characters! Text must be contained in paragraphs!
156  // rHelper.InsertString(rString);
157 }
158 
160 {
161  // get rid of last dummy paragraph
163 
164  // reinstall old cursor
166 
167  // reinstall old list item
168  if (mbListContextPushed) {
170  }
171 }
172 
174  sal_uInt16 p_nPrefix,
175  const OUString& rLocalName,
176  const Reference<XAttributeList> & xAttrList )
177 {
178  SvXMLImportContextRef xContext;
179 
180  static const SvXMLTokenMap aTokenMap(aFootnoteChildTokenMap);
181 
182  switch(aTokenMap.Get(p_nPrefix, rLocalName))
183  {
184  case XML_TOK_FTN_NOTE_CITATION:
185  {
186  // little hack: we only care for one attribute of the citation
187  // element. We handle that here, and then return a
188  // default context.
189  sal_Int16 nLength = xAttrList->getLength();
190  for(sal_Int16 nAttr = 0; nAttr < nLength; nAttr++)
191  {
192  OUString sLocalName;
193  sal_uInt16 nPrefix = GetImport().GetNamespaceMap().
194  GetKeyByAttrName( xAttrList->getNameByIndex(nAttr),
195  &sLocalName );
196 
197  if ( (nPrefix == XML_NAMESPACE_TEXT) &&
198  IsXMLToken( sLocalName, XML_LABEL ) )
199  {
200  xFootnote->setLabel(xAttrList->getValueByIndex(nAttr));
201  }
202  }
203 
204  // ignore content: return default context
205  break;
206  }
207 
208  case XML_TOK_FTN_NOTE_BODY:
209  // return footnote body
210  xContext = new XMLFootnoteBodyImportContext(GetImport(),
211  p_nPrefix, rLocalName);
212  break;
213  }
214 
215  return xContext;
216 }
217 
218 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const SvXMLTokenMapEntry aFootnoteChildTokenMap[]
bool IsXMLToken(const OUString &rString, enum XMLTokenEnum eToken)
compare eToken to the string
Definition: xmltoken.cxx:3434
virtual void EndElement() override
EndElement is called before a context will be destructed, but after an elements context has been pars...
XMLTokenEnum const aTokenMap[]
Definition: Oasis2OOo.cxx:1145
css::uno::Reference< css::text::XTextCursor > & GetCursor()
Definition: txtimp.cxx:625
void DeleteParagraph()
Definition: txtimp.cxx:1248
SvXMLImport & GetImport()
Definition: xmlictxt.hxx:62
void PopListContext()
pop the list context stack
Definition: txtimp.cxx:2544
SvXMLNamespaceMap & GetNamespaceMap()
Definition: xmlimp.hxx:402
bool mbListContextPushed
old list item and block (#89891#)
#define XML_TOKEN_MAP_END
Definition: xmltkmap.hxx:33
css::uno::Any const & rValue
Definition: ImageStyle.hxx:38
virtual void Characters(const OUString &rChars) override
This method is called for all characters that are contained in the current element.
XMLTextImportHelper & rHelper
text import helper; holds current XTextCursor (and XText)
constexpr sal_uInt16 XML_NAMESPACE_TEXT
void PushListContext()
push a list context on the list context stack
Definition: txtimp.cxx:2539
virtual SvXMLImportContextRef CreateChildContext(sal_uInt16 nPrefix, const OUString &rLocalName, const css::uno::Reference< css::xml::sax::XAttributeList > &xAttrList) override
Create a children element context.
This class deliberately does not support XWeak, to improve performance when loading large documents...
Definition: xmlictxt.hxx:44
void InsertTextContent(css::uno::Reference< css::text::XTextContent > const &xContent)
Definition: txtimp.cxx:1236
Handling of tokens in XML:
XMLFootnoteImportContext(SvXMLImport &rImport, XMLTextImportHelper &rHlp, sal_uInt16 nPrfx, const OUString &rLocalName)
void SetCursor(const css::uno::Reference< css::text::XTextCursor > &rCursor)
Definition: txtimp.cxx:1094
css::uno::Reference< css::text::XFootnote > xFootnote
the footnote
import foot- and endnote body elements ()
Reference< XSingleServiceFactory > xFactory
sal_Int32 nLength
Definition: xmltoken.cxx:36
void InsertFootnoteID(const OUString &sXMLId, sal_Int16 nAPIId)
insert new footnote ID.
virtual void StartElement(const css::uno::Reference< css::xml::sax::XAttributeList > &xAttrList) override
StartElement is called after a context has been constructed and before an elements context is parsed...
css::uno::Reference< css::text::XTextCursor > xOldCursor
old document cursor