1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: XMLFootnoteImportContext.cxx,v $
10 * $Revision: 1.15.66.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_xmloff.hxx"
35 #include "XMLFootnoteImportContext.hxx"
37 #include <rtl/ustring.hxx>
38 #include <tools/debug.hxx>
39 #include <xmloff/xmlimp.hxx>
40 #include <xmloff/txtimp.hxx>
41 #include <xmloff/nmspmap.hxx>
42 #include "xmlnmspe.hxx"
43 #include <xmloff/xmltoken.hxx>
45 #include "XMLFootnoteBodyImportContext.hxx"
46 #include "XMLTextListBlockContext.hxx"
47 #include "XMLTextListItemContext.hxx"
49 #include <com/sun/star/xml/sax/XAttributeList.hpp>
50 #include <com/sun/star/text/XTextContent.hpp>
51 #include <com/sun/star/beans/XPropertySet.hpp>
52 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
53 #include <com/sun/star/text/XFootnote.hpp>
56 using ::rtl::OUString
;
57 using ::rtl::OUStringBuffer
;
59 using namespace ::com::sun::star::uno
;
60 using namespace ::com::sun::star::text
;
61 using namespace ::com::sun::star::lang
;
62 using namespace ::com::sun::star::beans
;
63 using namespace ::com::sun::star::xml::sax
;
64 using namespace ::xmloff::token
;
66 TYPEINIT1(XMLFootnoteImportContext
, SvXMLImportContext
);
68 const sal_Char sAPI_service_footnote
[] = "com.sun.star.text.Footnote";
69 const sal_Char sAPI_service_endnote
[] = "com.sun.star.text.Endnote";
71 enum XMLFootnoteChildToken
{
72 XML_TOK_FTN_NOTE_CITATION
,
76 static __FAR_DATA SvXMLTokenMapEntry aFootnoteChildTokenMap
[] =
78 { XML_NAMESPACE_TEXT
, XML_NOTE_CITATION
,
79 XML_TOK_FTN_NOTE_CITATION
},
80 { XML_NAMESPACE_TEXT
, XML_NOTE_BODY
, XML_TOK_FTN_NOTE_BODY
},
85 XMLFootnoteImportContext::XMLFootnoteImportContext(
87 XMLTextImportHelper
& rHlp
,
89 const OUString
& rLocalName
)
90 : SvXMLImportContext(rImport
, nPrfx
, rLocalName
)
91 , sPropertyReferenceId(RTL_CONSTASCII_USTRINGPARAM("ReferenceId"))
92 , mbListContextPushed(false)
97 void XMLFootnoteImportContext::StartElement(
98 const Reference
<XAttributeList
> & xAttrList
)
101 Reference
<XMultiServiceFactory
> xFactory(GetImport().GetModel(),
105 // create endnote or footnote
106 sal_Bool bIsEndnote
= sal_False
;
107 sal_Int16 nLength
= xAttrList
->getLength();
108 for(sal_Int16 nAttr1
= 0; nAttr1
< nLength
; nAttr1
++)
111 sal_uInt16 nPrefix
= GetImport().GetNamespaceMap().
112 GetKeyByAttrName( xAttrList
->getNameByIndex(nAttr1
),
114 if( XML_NAMESPACE_TEXT
== nPrefix
&& IsXMLToken( sLocalName
,
117 const OUString
& rValue
= xAttrList
->getValueByIndex( nAttr1
);
118 if( IsXMLToken( rValue
, XML_ENDNOTE
) )
119 bIsEndnote
= sal_True
;
124 Reference
<XInterface
> xIfc
= xFactory
->createInstance(
126 OUString(RTL_CONSTASCII_USTRINGPARAM(sAPI_service_endnote
)) :
127 OUString(RTL_CONSTASCII_USTRINGPARAM(sAPI_service_footnote
)) );
129 // attach footnote to document
130 Reference
<XTextContent
> xTextContent(xIfc
, UNO_QUERY
);
131 rHelper
.InsertTextContent(xTextContent
);
133 // process id attribute
134 for(sal_Int16 nAttr2
= 0; nAttr2
< nLength
; nAttr2
++)
137 sal_uInt16 nPrefix
= GetImport().GetNamespaceMap().
138 GetKeyByAttrName( xAttrList
->getNameByIndex(nAttr2
),
141 if ( (XML_NAMESPACE_TEXT
== nPrefix
) &&
142 IsXMLToken( sLocalName
, XML_ID
) )
145 Reference
<XPropertySet
> xPropertySet(xTextContent
, UNO_QUERY
);
146 Any aAny
=xPropertySet
->getPropertyValue(sPropertyReferenceId
);
150 // ... and insert into map
151 rHelper
.InsertFootnoteID(
152 xAttrList
->getValueByIndex(nAttr2
),
157 // save old cursor and install new one
158 xOldCursor
= rHelper
.GetCursor();
159 Reference
<XText
> xText(xTextContent
, UNO_QUERY
);
160 rHelper
.SetCursor(xText
->createTextCursor());
162 // remember old list item and block (#89891#) and reset them
164 rHelper
.PushListContext();
165 mbListContextPushed
= true;
167 // remember footnote (for CreateChildContext)
168 Reference
<XFootnote
> xNote(xTextContent
, UNO_QUERY
);
171 // else: ignore footnote! Content will be merged into document.
174 void XMLFootnoteImportContext::Characters(const OUString
&)
176 // ignore characters! Text must be contained in paragraphs!
177 // rHelper.InsertString(rString);
180 void XMLFootnoteImportContext::EndElement()
182 // get rid of last dummy paragraph
183 rHelper
.DeleteParagraph();
185 // reinstall old cursor
186 rHelper
.SetCursor(xOldCursor
);
188 // reinstall old list item
189 if (mbListContextPushed
) {
190 rHelper
.PopListContext();
195 SvXMLImportContext
*XMLFootnoteImportContext::CreateChildContext(
196 sal_uInt16 p_nPrefix
,
197 const OUString
& rLocalName
,
198 const Reference
<XAttributeList
> & xAttrList
)
200 SvXMLImportContext
* pContext
= NULL
;
202 SvXMLTokenMap
aTokenMap(aFootnoteChildTokenMap
);
204 switch(aTokenMap
.Get(p_nPrefix
, rLocalName
))
206 case XML_TOK_FTN_NOTE_CITATION
:
208 // little hack: we only care for one attribute of the citation
209 // element. We handle that here, and then return a
211 sal_Int16 nLength
= xAttrList
->getLength();
212 for(sal_Int16 nAttr
= 0; nAttr
< nLength
; nAttr
++)
215 sal_uInt16 nPrefix
= GetImport().GetNamespaceMap().
216 GetKeyByAttrName( xAttrList
->getNameByIndex(nAttr
),
219 if ( (nPrefix
== XML_NAMESPACE_TEXT
) &&
220 IsXMLToken( sLocalName
, XML_LABEL
) )
222 xFootnote
->setLabel(xAttrList
->getValueByIndex(nAttr
));
226 // ignore content: return default context
227 pContext
= new SvXMLImportContext(GetImport(),
228 p_nPrefix
, rLocalName
);
232 case XML_TOK_FTN_NOTE_BODY
:
233 // return footnote body
234 pContext
= new XMLFootnoteBodyImportContext(GetImport(),
235 p_nPrefix
, rLocalName
);
239 pContext
= SvXMLImportContext::CreateChildContext(p_nPrefix
,