nss: upgrade to release 3.73
[LibreOffice.git] / xmloff / source / text / XMLFootnoteImportContext.cxx
blob645e910c8a36f7b01f86d42b8df55f758327ff3f
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <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>
29 #include "XMLFootnoteBodyImportContext.hxx"
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>
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 namespace {
48 enum XMLFootnoteChildToken {
49 XML_TOK_FTN_NOTE_CITATION,
50 XML_TOK_FTN_NOTE_BODY
55 const SvXMLTokenMapEntry aFootnoteChildTokenMap[] =
57 { XML_NAMESPACE_TEXT, XML_NOTE_CITATION,
58 XML_TOK_FTN_NOTE_CITATION },
59 { XML_NAMESPACE_TEXT, XML_NOTE_BODY, XML_TOK_FTN_NOTE_BODY },
60 XML_TOKEN_MAP_END
64 XMLFootnoteImportContext::XMLFootnoteImportContext(
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)
75 void XMLFootnoteImportContext::StartElement(
76 const Reference<XAttributeList> & xAttrList)
78 // create footnote
79 Reference<XMultiServiceFactory> xFactory(GetImport().GetModel(),
80 UNO_QUERY);
81 if( !xFactory.is() )
82 return;
84 // create endnote or footnote
85 bool bIsEndnote = false;
86 sal_Int16 nLength = xAttrList->getLength();
87 for(sal_Int16 nAttr1 = 0; nAttr1 < nLength; nAttr1++)
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 ) )
96 const OUString& rValue = xAttrList->getValueByIndex( nAttr1 );
97 if( IsXMLToken( rValue, XML_ENDNOTE ) )
98 bIsEndnote = true;
99 break;
103 Reference<XInterface> xIfc = xFactory->createInstance(
104 bIsEndnote ?
105 OUString("com.sun.star.text.Endnote") :
106 OUString("com.sun.star.text.Footnote") );
108 // attach footnote to document
109 Reference<XTextContent> xTextContent(xIfc, UNO_QUERY);
110 rHelper.InsertTextContent(xTextContent);
112 // process id attribute
113 for(sal_Int16 nAttr2 = 0; nAttr2 < nLength; nAttr2++)
115 OUString sLocalName;
116 sal_uInt16 nPrefix = GetImport().GetNamespaceMap().
117 GetKeyByAttrName( xAttrList->getNameByIndex(nAttr2),
118 &sLocalName );
120 if ( (XML_NAMESPACE_TEXT == nPrefix) &&
121 IsXMLToken( sLocalName, XML_ID ) )
123 // get ID ...
124 Reference<XPropertySet> xPropertySet(xTextContent, UNO_QUERY);
125 Any aAny =xPropertySet->getPropertyValue("ReferenceId");
126 sal_Int16 nID = 0;
127 aAny >>= nID;
129 // ... and insert into map
130 rHelper.InsertFootnoteID(
131 xAttrList->getValueByIndex(nAttr2),
132 nID);
136 // save old cursor and install new one
137 xOldCursor = rHelper.GetCursor();
138 Reference<XText> xText(xTextContent, UNO_QUERY);
139 rHelper.SetCursor(xText->createTextCursor());
141 // remember old list item and block (#89891#) and reset them
142 // for the footnote
143 rHelper.PushListContext();
144 mbListContextPushed = true;
146 // remember footnote (for CreateChildContext)
147 Reference<XFootnote> xNote(xTextContent, UNO_QUERY);
148 xFootnote = xNote;
150 // else: ignore footnote! Content will be merged into document.
153 void XMLFootnoteImportContext::endFastElement(sal_Int32 )
155 // get rid of last dummy paragraph
156 rHelper.DeleteParagraph();
158 // reinstall old cursor
159 rHelper.SetCursor(xOldCursor);
161 // reinstall old list item
162 if (mbListContextPushed) {
163 rHelper.PopListContext();
167 SvXMLImportContextRef XMLFootnoteImportContext::CreateChildContext(
168 sal_uInt16 p_nPrefix,
169 const OUString& rLocalName,
170 const Reference<XAttributeList> & xAttrList )
172 SvXMLImportContextRef xContext;
174 static const SvXMLTokenMap aTokenMap(aFootnoteChildTokenMap);
176 switch(aTokenMap.Get(p_nPrefix, rLocalName))
178 case XML_TOK_FTN_NOTE_CITATION:
180 // little hack: we only care for one attribute of the citation
181 // element. We handle that here, and then return a
182 // default context.
183 sal_Int16 nLength = xAttrList->getLength();
184 for(sal_Int16 nAttr = 0; nAttr < nLength; nAttr++)
186 OUString sLocalName;
187 sal_uInt16 nPrefix = GetImport().GetNamespaceMap().
188 GetKeyByAttrName( xAttrList->getNameByIndex(nAttr),
189 &sLocalName );
191 if ( (nPrefix == XML_NAMESPACE_TEXT) &&
192 IsXMLToken( sLocalName, XML_LABEL ) )
194 xFootnote->setLabel(xAttrList->getValueByIndex(nAttr));
198 // ignore content: return default context
199 break;
202 case XML_TOK_FTN_NOTE_BODY:
203 // return footnote body
204 xContext = new XMLFootnoteBodyImportContext(GetImport(),
205 p_nPrefix, rLocalName);
206 break;
209 return xContext;
212 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */