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 <rtl/ustring.hxx>
23 #include <xmloff/xmlimp.hxx>
24 #include <xmloff/txtimp.hxx>
25 #include <xmloff/nmspmap.hxx>
26 #include <xmloff/xmlnmspe.hxx>
27 #include <xmloff/xmltoken.hxx>
29 #include "XMLFootnoteBodyImportContext.hxx"
30 #include "XMLTextListBlockContext.hxx"
31 #include "XMLTextListItemContext.hxx"
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>
41 using namespace ::com::sun::star::uno
;
42 using namespace ::com::sun::star::text
;
43 using namespace ::com::sun::star::lang
;
44 using namespace ::com::sun::star::beans
;
45 using namespace ::com::sun::star::xml::sax
;
46 using namespace ::xmloff::token
;
48 TYPEINIT1(XMLFootnoteImportContext
, SvXMLImportContext
);
50 const sal_Char sAPI_service_footnote
[] = "com.sun.star.text.Footnote";
51 const sal_Char sAPI_service_endnote
[] = "com.sun.star.text.Endnote";
53 enum XMLFootnoteChildToken
{
54 XML_TOK_FTN_NOTE_CITATION
,
58 static const SvXMLTokenMapEntry aFootnoteChildTokenMap
[] =
60 { XML_NAMESPACE_TEXT
, XML_NOTE_CITATION
,
61 XML_TOK_FTN_NOTE_CITATION
},
62 { XML_NAMESPACE_TEXT
, XML_NOTE_BODY
, XML_TOK_FTN_NOTE_BODY
},
67 XMLFootnoteImportContext::XMLFootnoteImportContext(
69 XMLTextImportHelper
& rHlp
,
71 const OUString
& rLocalName
)
72 : SvXMLImportContext(rImport
, nPrfx
, rLocalName
)
73 , sPropertyReferenceId("ReferenceId")
74 , mbListContextPushed(false)
79 void XMLFootnoteImportContext::StartElement(
80 const Reference
<XAttributeList
> & xAttrList
)
83 Reference
<XMultiServiceFactory
> xFactory(GetImport().GetModel(),
87 // create endnote or footnote
88 bool bIsEndnote
= false;
89 sal_Int16 nLength
= xAttrList
->getLength();
90 for(sal_Int16 nAttr1
= 0; nAttr1
< nLength
; nAttr1
++)
93 sal_uInt16 nPrefix
= GetImport().GetNamespaceMap().
94 GetKeyByAttrName( xAttrList
->getNameByIndex(nAttr1
),
96 if( XML_NAMESPACE_TEXT
== nPrefix
&& IsXMLToken( sLocalName
,
99 const OUString
& rValue
= xAttrList
->getValueByIndex( nAttr1
);
100 if( IsXMLToken( rValue
, XML_ENDNOTE
) )
106 Reference
<XInterface
> xIfc
= xFactory
->createInstance(
108 OUString(sAPI_service_endnote
) :
109 OUString(sAPI_service_footnote
) );
111 // attach footnote to document
112 Reference
<XTextContent
> xTextContent(xIfc
, UNO_QUERY
);
113 rHelper
.InsertTextContent(xTextContent
);
115 // process id attribute
116 for(sal_Int16 nAttr2
= 0; nAttr2
< nLength
; nAttr2
++)
119 sal_uInt16 nPrefix
= GetImport().GetNamespaceMap().
120 GetKeyByAttrName( xAttrList
->getNameByIndex(nAttr2
),
123 if ( (XML_NAMESPACE_TEXT
== nPrefix
) &&
124 IsXMLToken( sLocalName
, XML_ID
) )
127 Reference
<XPropertySet
> xPropertySet(xTextContent
, UNO_QUERY
);
128 Any aAny
=xPropertySet
->getPropertyValue(sPropertyReferenceId
);
132 // ... and insert into map
133 rHelper
.InsertFootnoteID(
134 xAttrList
->getValueByIndex(nAttr2
),
139 // save old cursor and install new one
140 xOldCursor
= rHelper
.GetCursor();
141 Reference
<XText
> xText(xTextContent
, UNO_QUERY
);
142 rHelper
.SetCursor(xText
->createTextCursor());
144 // remember old list item and block (#89891#) and reset them
146 rHelper
.PushListContext();
147 mbListContextPushed
= true;
149 // remember footnote (for CreateChildContext)
150 Reference
<XFootnote
> xNote(xTextContent
, UNO_QUERY
);
153 // else: ignore footnote! Content will be merged into document.
156 void XMLFootnoteImportContext::Characters(const OUString
&)
158 // ignore characters! Text must be contained in paragraphs!
159 // rHelper.InsertString(rString);
162 void XMLFootnoteImportContext::EndElement()
164 // get rid of last dummy paragraph
165 rHelper
.DeleteParagraph();
167 // reinstall old cursor
168 rHelper
.SetCursor(xOldCursor
);
170 // reinstall old list item
171 if (mbListContextPushed
) {
172 rHelper
.PopListContext();
177 SvXMLImportContext
*XMLFootnoteImportContext::CreateChildContext(
178 sal_uInt16 p_nPrefix
,
179 const OUString
& rLocalName
,
180 const Reference
<XAttributeList
> & xAttrList
)
182 SvXMLImportContext
* pContext
= NULL
;
184 SvXMLTokenMap
aTokenMap(aFootnoteChildTokenMap
);
186 switch(aTokenMap
.Get(p_nPrefix
, rLocalName
))
188 case XML_TOK_FTN_NOTE_CITATION
:
190 // little hack: we only care for one attribute of the citation
191 // element. We handle that here, and then return a
193 sal_Int16 nLength
= xAttrList
->getLength();
194 for(sal_Int16 nAttr
= 0; nAttr
< nLength
; nAttr
++)
197 sal_uInt16 nPrefix
= GetImport().GetNamespaceMap().
198 GetKeyByAttrName( xAttrList
->getNameByIndex(nAttr
),
201 if ( (nPrefix
== XML_NAMESPACE_TEXT
) &&
202 IsXMLToken( sLocalName
, XML_LABEL
) )
204 xFootnote
->setLabel(xAttrList
->getValueByIndex(nAttr
));
208 // ignore content: return default context
209 pContext
= new SvXMLImportContext(GetImport(),
210 p_nPrefix
, rLocalName
);
214 case XML_TOK_FTN_NOTE_BODY
:
215 // return footnote body
216 pContext
= new XMLFootnoteBodyImportContext(GetImport(),
217 p_nPrefix
, rLocalName
);
221 pContext
= SvXMLImportContext::CreateChildContext(p_nPrefix
,
230 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */