merge the formfield patch from ooo-build
[ooovba.git] / xmloff / source / text / XMLFootnoteImportContext.cxx
blobc0b0e94f77961681f7287e76b7686e8e62dd0d6a
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
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,
73 XML_TOK_FTN_NOTE_BODY
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 },
81 XML_TOKEN_MAP_END
85 XMLFootnoteImportContext::XMLFootnoteImportContext(
86 SvXMLImport& rImport,
87 XMLTextImportHelper& rHlp,
88 sal_uInt16 nPrfx,
89 const OUString& rLocalName )
90 : SvXMLImportContext(rImport, nPrfx, rLocalName)
91 , sPropertyReferenceId(RTL_CONSTASCII_USTRINGPARAM("ReferenceId"))
92 , mbListContextPushed(false)
93 , rHelper(rHlp)
97 void XMLFootnoteImportContext::StartElement(
98 const Reference<XAttributeList> & xAttrList)
100 // create footnote
101 Reference<XMultiServiceFactory> xFactory(GetImport().GetModel(),
102 UNO_QUERY);
103 if( xFactory.is() )
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++)
110 OUString sLocalName;
111 sal_uInt16 nPrefix = GetImport().GetNamespaceMap().
112 GetKeyByAttrName( xAttrList->getNameByIndex(nAttr1),
113 &sLocalName );
114 if( XML_NAMESPACE_TEXT == nPrefix && IsXMLToken( sLocalName,
115 XML_NOTE_CLASS ) )
117 const OUString& rValue = xAttrList->getValueByIndex( nAttr1 );
118 if( IsXMLToken( rValue, XML_ENDNOTE ) )
119 bIsEndnote = sal_True;
120 break;
124 Reference<XInterface> xIfc = xFactory->createInstance(
125 bIsEndnote ?
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++)
136 OUString sLocalName;
137 sal_uInt16 nPrefix = GetImport().GetNamespaceMap().
138 GetKeyByAttrName( xAttrList->getNameByIndex(nAttr2),
139 &sLocalName );
141 if ( (XML_NAMESPACE_TEXT == nPrefix) &&
142 IsXMLToken( sLocalName, XML_ID ) )
144 // get ID ...
145 Reference<XPropertySet> xPropertySet(xTextContent, UNO_QUERY);
146 Any aAny =xPropertySet->getPropertyValue(sPropertyReferenceId);
147 sal_Int16 nID = 0;
148 aAny >>= nID;
150 // ... and insert into map
151 rHelper.InsertFootnoteID(
152 xAttrList->getValueByIndex(nAttr2),
153 nID);
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
163 // for the footnote
164 rHelper.PushListContext();
165 mbListContextPushed = true;
167 // remember footnote (for CreateChildContext)
168 Reference<XFootnote> xNote(xTextContent, UNO_QUERY);
169 xFootnote = xNote;
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
210 // default context.
211 sal_Int16 nLength = xAttrList->getLength();
212 for(sal_Int16 nAttr = 0; nAttr < nLength; nAttr++)
214 OUString sLocalName;
215 sal_uInt16 nPrefix = GetImport().GetNamespaceMap().
216 GetKeyByAttrName( xAttrList->getNameByIndex(nAttr),
217 &sLocalName );
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);
229 break;
232 case XML_TOK_FTN_NOTE_BODY:
233 // return footnote body
234 pContext = new XMLFootnoteBodyImportContext(GetImport(),
235 p_nPrefix, rLocalName);
236 break;
237 default:
238 // default:
239 pContext = SvXMLImportContext::CreateChildContext(p_nPrefix,
240 rLocalName,
241 xAttrList);
242 break;
245 return pContext;