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