xref: /AOO41X/main/xmloff/source/text/XMLFootnoteImportContext.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_xmloff.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir #include "XMLFootnoteImportContext.hxx"
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir #include <rtl/ustring.hxx>
35*cdf0e10cSrcweir #include <tools/debug.hxx>
36*cdf0e10cSrcweir #include <xmloff/xmlimp.hxx>
37*cdf0e10cSrcweir #include <xmloff/txtimp.hxx>
38*cdf0e10cSrcweir #include <xmloff/nmspmap.hxx>
39*cdf0e10cSrcweir #include "xmloff/xmlnmspe.hxx"
40*cdf0e10cSrcweir #include <xmloff/xmltoken.hxx>
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir #include "XMLFootnoteBodyImportContext.hxx"
43*cdf0e10cSrcweir #include "XMLTextListBlockContext.hxx"
44*cdf0e10cSrcweir #include "XMLTextListItemContext.hxx"
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir #include <com/sun/star/xml/sax/XAttributeList.hpp>
47*cdf0e10cSrcweir #include <com/sun/star/text/XTextContent.hpp>
48*cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
49*cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
50*cdf0e10cSrcweir #include <com/sun/star/text/XFootnote.hpp>
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir using ::rtl::OUString;
54*cdf0e10cSrcweir using ::rtl::OUStringBuffer;
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
57*cdf0e10cSrcweir using namespace ::com::sun::star::text;
58*cdf0e10cSrcweir using namespace ::com::sun::star::lang;
59*cdf0e10cSrcweir using namespace ::com::sun::star::beans;
60*cdf0e10cSrcweir using namespace ::com::sun::star::xml::sax;
61*cdf0e10cSrcweir using namespace ::xmloff::token;
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir TYPEINIT1(XMLFootnoteImportContext, SvXMLImportContext);
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir const sal_Char sAPI_service_footnote[] = "com.sun.star.text.Footnote";
66*cdf0e10cSrcweir const sal_Char sAPI_service_endnote[] = "com.sun.star.text.Endnote";
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir enum XMLFootnoteChildToken {
69*cdf0e10cSrcweir 	XML_TOK_FTN_NOTE_CITATION,
70*cdf0e10cSrcweir 	XML_TOK_FTN_NOTE_BODY
71*cdf0e10cSrcweir };
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir static __FAR_DATA SvXMLTokenMapEntry aFootnoteChildTokenMap[] =
74*cdf0e10cSrcweir {
75*cdf0e10cSrcweir 	{ XML_NAMESPACE_TEXT, XML_NOTE_CITATION,
76*cdf0e10cSrcweir 	  XML_TOK_FTN_NOTE_CITATION },
77*cdf0e10cSrcweir 	{ XML_NAMESPACE_TEXT, XML_NOTE_BODY, XML_TOK_FTN_NOTE_BODY },
78*cdf0e10cSrcweir 	XML_TOKEN_MAP_END
79*cdf0e10cSrcweir };
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir XMLFootnoteImportContext::XMLFootnoteImportContext(
83*cdf0e10cSrcweir 	SvXMLImport& rImport,
84*cdf0e10cSrcweir 	XMLTextImportHelper& rHlp,
85*cdf0e10cSrcweir     sal_uInt16 nPrfx,
86*cdf0e10cSrcweir 	const OUString& rLocalName )
87*cdf0e10cSrcweir :	SvXMLImportContext(rImport, nPrfx, rLocalName)
88*cdf0e10cSrcweir ,	sPropertyReferenceId(RTL_CONSTASCII_USTRINGPARAM("ReferenceId"))
89*cdf0e10cSrcweir ,   mbListContextPushed(false)
90*cdf0e10cSrcweir ,	rHelper(rHlp)
91*cdf0e10cSrcweir {
92*cdf0e10cSrcweir }
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir void XMLFootnoteImportContext::StartElement(
95*cdf0e10cSrcweir 	const Reference<XAttributeList> & xAttrList)
96*cdf0e10cSrcweir {
97*cdf0e10cSrcweir 	// create footnote
98*cdf0e10cSrcweir 	Reference<XMultiServiceFactory> xFactory(GetImport().GetModel(),
99*cdf0e10cSrcweir 											 UNO_QUERY);
100*cdf0e10cSrcweir 	if( xFactory.is() )
101*cdf0e10cSrcweir 	{
102*cdf0e10cSrcweir 		// create endnote or footnote
103*cdf0e10cSrcweir 		sal_Bool bIsEndnote = sal_False;
104*cdf0e10cSrcweir 		sal_Int16 nLength = xAttrList->getLength();
105*cdf0e10cSrcweir 		for(sal_Int16 nAttr1 = 0; nAttr1 < nLength; nAttr1++)
106*cdf0e10cSrcweir 		{
107*cdf0e10cSrcweir 			OUString sLocalName;
108*cdf0e10cSrcweir 			sal_uInt16 nPrefix = GetImport().GetNamespaceMap().
109*cdf0e10cSrcweir 				GetKeyByAttrName( xAttrList->getNameByIndex(nAttr1),
110*cdf0e10cSrcweir 								  &sLocalName );
111*cdf0e10cSrcweir 			if( XML_NAMESPACE_TEXT == nPrefix && IsXMLToken( sLocalName,
112*cdf0e10cSrcweir 															XML_NOTE_CLASS ) )
113*cdf0e10cSrcweir 			{
114*cdf0e10cSrcweir 				const OUString& rValue = xAttrList->getValueByIndex( nAttr1 );
115*cdf0e10cSrcweir 				if( IsXMLToken( rValue, XML_ENDNOTE ) )
116*cdf0e10cSrcweir 					bIsEndnote = sal_True;
117*cdf0e10cSrcweir 				break;
118*cdf0e10cSrcweir 			}
119*cdf0e10cSrcweir 		}
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir 		Reference<XInterface> xIfc = xFactory->createInstance(
122*cdf0e10cSrcweir 			bIsEndnote ?
123*cdf0e10cSrcweir 			OUString(RTL_CONSTASCII_USTRINGPARAM(sAPI_service_endnote)) :
124*cdf0e10cSrcweir 			OUString(RTL_CONSTASCII_USTRINGPARAM(sAPI_service_footnote)) );
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir 		// attach footnote to document
127*cdf0e10cSrcweir 		Reference<XTextContent> xTextContent(xIfc, UNO_QUERY);
128*cdf0e10cSrcweir 		rHelper.InsertTextContent(xTextContent);
129*cdf0e10cSrcweir 
130*cdf0e10cSrcweir 		// process id attribute
131*cdf0e10cSrcweir 		for(sal_Int16 nAttr2 = 0; nAttr2 < nLength; nAttr2++)
132*cdf0e10cSrcweir 		{
133*cdf0e10cSrcweir 			OUString sLocalName;
134*cdf0e10cSrcweir 			sal_uInt16 nPrefix = GetImport().GetNamespaceMap().
135*cdf0e10cSrcweir 				GetKeyByAttrName( xAttrList->getNameByIndex(nAttr2),
136*cdf0e10cSrcweir 								  &sLocalName );
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir 			if ( (XML_NAMESPACE_TEXT == nPrefix) &&
139*cdf0e10cSrcweir 				 IsXMLToken( sLocalName, XML_ID )   )
140*cdf0e10cSrcweir 			{
141*cdf0e10cSrcweir 				// get ID ...
142*cdf0e10cSrcweir 				Reference<XPropertySet> xPropertySet(xTextContent, UNO_QUERY);
143*cdf0e10cSrcweir 				Any aAny =xPropertySet->getPropertyValue(sPropertyReferenceId);
144*cdf0e10cSrcweir 				sal_Int16 nID = 0;
145*cdf0e10cSrcweir 				aAny >>= nID;
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir 				// ... and insert into map
148*cdf0e10cSrcweir 				rHelper.InsertFootnoteID(
149*cdf0e10cSrcweir 					xAttrList->getValueByIndex(nAttr2),
150*cdf0e10cSrcweir 					nID);
151*cdf0e10cSrcweir 			}
152*cdf0e10cSrcweir 		}
153*cdf0e10cSrcweir 
154*cdf0e10cSrcweir 		// save old cursor and install new one
155*cdf0e10cSrcweir 		xOldCursor = rHelper.GetCursor();
156*cdf0e10cSrcweir 		Reference<XText> xText(xTextContent, UNO_QUERY);
157*cdf0e10cSrcweir 		rHelper.SetCursor(xText->createTextCursor());
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir         // remember old list item and block (#89891#) and reset them
160*cdf0e10cSrcweir         // for the footnote
161*cdf0e10cSrcweir         rHelper.PushListContext();
162*cdf0e10cSrcweir         mbListContextPushed = true;
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir 		// remember footnote (for CreateChildContext)
165*cdf0e10cSrcweir 		Reference<XFootnote> xNote(xTextContent, UNO_QUERY);
166*cdf0e10cSrcweir 		xFootnote = xNote;
167*cdf0e10cSrcweir 	}
168*cdf0e10cSrcweir 	// else: ignore footnote! Content will be merged into document.
169*cdf0e10cSrcweir }
170*cdf0e10cSrcweir 
171*cdf0e10cSrcweir void XMLFootnoteImportContext::Characters(const OUString&)
172*cdf0e10cSrcweir {
173*cdf0e10cSrcweir 	// ignore characters! Text must be contained in paragraphs!
174*cdf0e10cSrcweir 	// rHelper.InsertString(rString);
175*cdf0e10cSrcweir }
176*cdf0e10cSrcweir 
177*cdf0e10cSrcweir void XMLFootnoteImportContext::EndElement()
178*cdf0e10cSrcweir {
179*cdf0e10cSrcweir 	// get rid of last dummy paragraph
180*cdf0e10cSrcweir 	rHelper.DeleteParagraph();
181*cdf0e10cSrcweir 
182*cdf0e10cSrcweir 	// reinstall old cursor
183*cdf0e10cSrcweir 	rHelper.SetCursor(xOldCursor);
184*cdf0e10cSrcweir 
185*cdf0e10cSrcweir     // reinstall old list item
186*cdf0e10cSrcweir     if (mbListContextPushed) {
187*cdf0e10cSrcweir         rHelper.PopListContext();
188*cdf0e10cSrcweir     }
189*cdf0e10cSrcweir }
190*cdf0e10cSrcweir 
191*cdf0e10cSrcweir 
192*cdf0e10cSrcweir SvXMLImportContext *XMLFootnoteImportContext::CreateChildContext(
193*cdf0e10cSrcweir 	sal_uInt16 p_nPrefix,
194*cdf0e10cSrcweir 	const OUString& rLocalName,
195*cdf0e10cSrcweir 	const Reference<XAttributeList> & xAttrList )
196*cdf0e10cSrcweir {
197*cdf0e10cSrcweir 	SvXMLImportContext* pContext = NULL;
198*cdf0e10cSrcweir 
199*cdf0e10cSrcweir 	SvXMLTokenMap aTokenMap(aFootnoteChildTokenMap);
200*cdf0e10cSrcweir 
201*cdf0e10cSrcweir 	switch(aTokenMap.Get(p_nPrefix, rLocalName))
202*cdf0e10cSrcweir 	{
203*cdf0e10cSrcweir 		case XML_TOK_FTN_NOTE_CITATION:
204*cdf0e10cSrcweir 		{
205*cdf0e10cSrcweir 			// little hack: we only care for one attribute of the citation
206*cdf0e10cSrcweir 			//			  	element. We handle that here, and then return a
207*cdf0e10cSrcweir 			// 				default context.
208*cdf0e10cSrcweir 			sal_Int16 nLength = xAttrList->getLength();
209*cdf0e10cSrcweir 			for(sal_Int16 nAttr = 0; nAttr < nLength; nAttr++)
210*cdf0e10cSrcweir 			{
211*cdf0e10cSrcweir 				OUString sLocalName;
212*cdf0e10cSrcweir 				sal_uInt16 nPrefix = GetImport().GetNamespaceMap().
213*cdf0e10cSrcweir 					GetKeyByAttrName( xAttrList->getNameByIndex(nAttr),
214*cdf0e10cSrcweir 									  &sLocalName );
215*cdf0e10cSrcweir 
216*cdf0e10cSrcweir 				if ( (nPrefix == XML_NAMESPACE_TEXT) &&
217*cdf0e10cSrcweir 					 IsXMLToken( sLocalName, XML_LABEL ) )
218*cdf0e10cSrcweir 				{
219*cdf0e10cSrcweir 					xFootnote->setLabel(xAttrList->getValueByIndex(nAttr));
220*cdf0e10cSrcweir 				}
221*cdf0e10cSrcweir 			}
222*cdf0e10cSrcweir 
223*cdf0e10cSrcweir 			// ignore content: return default context
224*cdf0e10cSrcweir 			pContext = new SvXMLImportContext(GetImport(),
225*cdf0e10cSrcweir 											  p_nPrefix, rLocalName);
226*cdf0e10cSrcweir 			break;
227*cdf0e10cSrcweir 		}
228*cdf0e10cSrcweir 
229*cdf0e10cSrcweir 		case XML_TOK_FTN_NOTE_BODY:
230*cdf0e10cSrcweir 			// return footnote body
231*cdf0e10cSrcweir 			pContext = new XMLFootnoteBodyImportContext(GetImport(),
232*cdf0e10cSrcweir 														p_nPrefix, rLocalName);
233*cdf0e10cSrcweir 			break;
234*cdf0e10cSrcweir 		default:
235*cdf0e10cSrcweir 			// default:
236*cdf0e10cSrcweir 			pContext = SvXMLImportContext::CreateChildContext(p_nPrefix,
237*cdf0e10cSrcweir 															  rLocalName,
238*cdf0e10cSrcweir 															  xAttrList);
239*cdf0e10cSrcweir 			break;
240*cdf0e10cSrcweir 	}
241*cdf0e10cSrcweir 
242*cdf0e10cSrcweir 	return pContext;
243*cdf0e10cSrcweir }
244