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 #include "XMLAutoTextEventExport.hxx"
31*cdf0e10cSrcweir #include <com/sun/star/frame/XModel.hpp>
32*cdf0e10cSrcweir #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
33*cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
34*cdf0e10cSrcweir #include <com/sun/star/document/XEventsSupplier.hpp>
35*cdf0e10cSrcweir #include <com/sun/star/container/XNameReplace.hpp>
36*cdf0e10cSrcweir #include <com/sun/star/container/XNameAccess.hpp>
37*cdf0e10cSrcweir #include <com/sun/star/uno/Reference.hxx>
38*cdf0e10cSrcweir #include <com/sun/star/uno/Sequence.hxx>
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir #ifndef _COM_SUN_STAR_UNO_EXCEPTION_HPP
41*cdf0e10cSrcweir #include <com/sun/star/uno/Exception.hpp>
42*cdf0e10cSrcweir #endif
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir // #110680#
45*cdf0e10cSrcweir //#ifndef _COMPHELPER_PROCESSFACTORY_HXX_
46*cdf0e10cSrcweir //#include <comphelper/processfactory.hxx>
47*cdf0e10cSrcweir //#endif
48*cdf0e10cSrcweir #include <rtl/ustrbuf.hxx>
49*cdf0e10cSrcweir #include "xmloff/xmlnmspe.hxx"
50*cdf0e10cSrcweir #include <xmloff/nmspmap.hxx>
51*cdf0e10cSrcweir #include <xmloff/xmltoken.hxx>
52*cdf0e10cSrcweir #include <xmloff/XMLEventExport.hxx>
53*cdf0e10cSrcweir #include <tools/debug.hxx>
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir using namespace ::com::sun::star;
57*cdf0e10cSrcweir using namespace ::xmloff::token;
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir // #110680#
60*cdf0e10cSrcweir // using ::comphelper::getProcessServiceFactory;
61*cdf0e10cSrcweir using ::rtl::OUString;
62*cdf0e10cSrcweir using ::rtl::OUStringBuffer;
63*cdf0e10cSrcweir using ::std::set;
64*cdf0e10cSrcweir using ::com::sun::star::beans::XPropertySet;
65*cdf0e10cSrcweir using ::com::sun::star::beans::PropertyValue;
66*cdf0e10cSrcweir using ::com::sun::star::container::XNameAccess;
67*cdf0e10cSrcweir using ::com::sun::star::container::XNameReplace;
68*cdf0e10cSrcweir using ::com::sun::star::document::XEventsSupplier;
69*cdf0e10cSrcweir using ::com::sun::star::frame::XModel;
70*cdf0e10cSrcweir using ::com::sun::star::lang::XMultiServiceFactory;
71*cdf0e10cSrcweir using ::com::sun::star::uno::Any;
72*cdf0e10cSrcweir using ::com::sun::star::uno::Exception;
73*cdf0e10cSrcweir using ::com::sun::star::uno::Reference;
74*cdf0e10cSrcweir using ::com::sun::star::uno::Sequence;
75*cdf0e10cSrcweir using ::com::sun::star::uno::XInterface;
76*cdf0e10cSrcweir using ::com::sun::star::uno::UNO_QUERY;
77*cdf0e10cSrcweir using ::com::sun::star::xml::sax::XDocumentHandler;
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir const sal_Char sAPI_AutoText[] = "com.sun.star.text.AutoTextContainer";
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir // #110680#
84*cdf0e10cSrcweir XMLAutoTextEventExport::XMLAutoTextEventExport(
85*cdf0e10cSrcweir 	const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xServiceFactory,
86*cdf0e10cSrcweir 		sal_uInt16 nFlags
87*cdf0e10cSrcweir 	)
88*cdf0e10cSrcweir :		SvXMLExport( xServiceFactory, MAP_INCH, XML_AUTO_TEXT, nFlags ),
89*cdf0e10cSrcweir 		sEventType(RTL_CONSTASCII_USTRINGPARAM("EventType")),
90*cdf0e10cSrcweir 		sNone(RTL_CONSTASCII_USTRINGPARAM("None"))
91*cdf0e10cSrcweir {
92*cdf0e10cSrcweir }
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir // #110680#
95*cdf0e10cSrcweir XMLAutoTextEventExport::XMLAutoTextEventExport(
96*cdf0e10cSrcweir 	const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xServiceFactory,
97*cdf0e10cSrcweir 	const OUString& rFileName,
98*cdf0e10cSrcweir 	const Reference<XDocumentHandler> & rHandler,
99*cdf0e10cSrcweir 	const Reference<XModel> & rModel,
100*cdf0e10cSrcweir 	const Reference<XNameAccess> & rEvents,
101*cdf0e10cSrcweir 	sal_uInt16 /*nFlags*/ )
102*cdf0e10cSrcweir :	SvXMLExport( xServiceFactory, rFileName, rHandler, rModel, MAP_INCH ),
103*cdf0e10cSrcweir 	xEvents(rEvents),
104*cdf0e10cSrcweir 	sEventType(RTL_CONSTASCII_USTRINGPARAM("EventType")),
105*cdf0e10cSrcweir 	sNone(RTL_CONSTASCII_USTRINGPARAM("None"))
106*cdf0e10cSrcweir {
107*cdf0e10cSrcweir }
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir XMLAutoTextEventExport::~XMLAutoTextEventExport()
110*cdf0e10cSrcweir {
111*cdf0e10cSrcweir }
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir void XMLAutoTextEventExport::initialize(
115*cdf0e10cSrcweir 	const Sequence<Any> & rArguments )
116*cdf0e10cSrcweir 		throw(uno::Exception, uno::RuntimeException)
117*cdf0e10cSrcweir {
118*cdf0e10cSrcweir 	if (rArguments.getLength() > 1)
119*cdf0e10cSrcweir 	{
120*cdf0e10cSrcweir 		Reference<XEventsSupplier> xSupplier;
121*cdf0e10cSrcweir 		rArguments[1] >>= xSupplier;
122*cdf0e10cSrcweir 		if (xSupplier.is())
123*cdf0e10cSrcweir 		{
124*cdf0e10cSrcweir 			Reference<XNameAccess> xAccess(xSupplier->getEvents(), UNO_QUERY);
125*cdf0e10cSrcweir 			xEvents = xAccess;
126*cdf0e10cSrcweir 		}
127*cdf0e10cSrcweir 		else
128*cdf0e10cSrcweir 		{
129*cdf0e10cSrcweir 			Reference<XNameReplace> xReplace;
130*cdf0e10cSrcweir 			rArguments[1] >>= xReplace;
131*cdf0e10cSrcweir 			if (xReplace.is())
132*cdf0e10cSrcweir 			{
133*cdf0e10cSrcweir 				Reference<XNameAccess> xAccess(xReplace, UNO_QUERY);
134*cdf0e10cSrcweir 				xEvents = xAccess;
135*cdf0e10cSrcweir 			}
136*cdf0e10cSrcweir 			else
137*cdf0e10cSrcweir 			{
138*cdf0e10cSrcweir 				rArguments[1] >>= xEvents;
139*cdf0e10cSrcweir 			}
140*cdf0e10cSrcweir 		}
141*cdf0e10cSrcweir 	}
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir 	// call super class (for XHandler)
144*cdf0e10cSrcweir 	SvXMLExport::initialize(rArguments);
145*cdf0e10cSrcweir }
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir sal_uInt32 XMLAutoTextEventExport::exportDoc( enum XMLTokenEnum )
149*cdf0e10cSrcweir {
150*cdf0e10cSrcweir 	if( (getExportFlags() & EXPORT_OASIS) == 0 )
151*cdf0e10cSrcweir 	{
152*cdf0e10cSrcweir 		Reference< lang::XMultiServiceFactory > xFactory = getServiceFactory();
153*cdf0e10cSrcweir 		if( xFactory.is() )
154*cdf0e10cSrcweir 		{
155*cdf0e10cSrcweir 			try
156*cdf0e10cSrcweir 			{
157*cdf0e10cSrcweir 
158*cdf0e10cSrcweir 				Sequence<Any> aArgs( 1 );
159*cdf0e10cSrcweir 				aArgs[0] <<= GetDocHandler();
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir 				// get filter component
162*cdf0e10cSrcweir 				Reference< xml::sax::XDocumentHandler > xTmpDocHandler(
163*cdf0e10cSrcweir 					xFactory->createInstanceWithArguments(
164*cdf0e10cSrcweir 					OUString::createFromAscii("com.sun.star.comp.Oasis2OOoTransformer"),
165*cdf0e10cSrcweir 								aArgs), UNO_QUERY);
166*cdf0e10cSrcweir 				OSL_ENSURE( xTmpDocHandler.is(),
167*cdf0e10cSrcweir 					"can't instantiate OASIS transformer component" );
168*cdf0e10cSrcweir 				if( xTmpDocHandler.is() )
169*cdf0e10cSrcweir 				{
170*cdf0e10cSrcweir 					SetDocHandler( xTmpDocHandler );
171*cdf0e10cSrcweir 				}
172*cdf0e10cSrcweir 			}
173*cdf0e10cSrcweir 			catch( com::sun::star::uno::Exception& )
174*cdf0e10cSrcweir 			{
175*cdf0e10cSrcweir 			}
176*cdf0e10cSrcweir 		}
177*cdf0e10cSrcweir 	}
178*cdf0e10cSrcweir 	if (hasEvents())
179*cdf0e10cSrcweir 	{
180*cdf0e10cSrcweir 		GetDocHandler()->startDocument();
181*cdf0e10cSrcweir 
182*cdf0e10cSrcweir 		addNamespaces();
183*cdf0e10cSrcweir 
184*cdf0e10cSrcweir 		{
185*cdf0e10cSrcweir 			// container element
186*cdf0e10cSrcweir 			SvXMLElementExport aContainerElement(
187*cdf0e10cSrcweir 				*this, XML_NAMESPACE_OOO, XML_AUTO_TEXT_EVENTS,
188*cdf0e10cSrcweir 				sal_True, sal_True);
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir 			exportEvents();
191*cdf0e10cSrcweir 		}
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir 		// and close document again
194*cdf0e10cSrcweir 		GetDocHandler()->endDocument();
195*cdf0e10cSrcweir 	}
196*cdf0e10cSrcweir 
197*cdf0e10cSrcweir 	return 0;
198*cdf0e10cSrcweir }
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir sal_Bool XMLAutoTextEventExport::hasEvents()
201*cdf0e10cSrcweir {
202*cdf0e10cSrcweir 	// TODO: provide full implementation that check for presence of events
203*cdf0e10cSrcweir 	return xEvents.is();
204*cdf0e10cSrcweir }
205*cdf0e10cSrcweir 
206*cdf0e10cSrcweir void XMLAutoTextEventExport::addNamespaces()
207*cdf0e10cSrcweir {
208*cdf0e10cSrcweir 	// namespaces for office:, text: and script:
209*cdf0e10cSrcweir 	GetAttrList().AddAttribute(
210*cdf0e10cSrcweir 		GetNamespaceMap().GetAttrNameByIndex( XML_NAMESPACE_OFFICE ),
211*cdf0e10cSrcweir 		GetNamespaceMap().GetNameByIndex( XML_NAMESPACE_OFFICE ) );
212*cdf0e10cSrcweir 	GetAttrList().AddAttribute(
213*cdf0e10cSrcweir 		GetNamespaceMap().GetAttrNameByIndex( XML_NAMESPACE_TEXT ),
214*cdf0e10cSrcweir 		GetNamespaceMap().GetNameByIndex( XML_NAMESPACE_TEXT ) );
215*cdf0e10cSrcweir 	GetAttrList().AddAttribute(
216*cdf0e10cSrcweir 		GetNamespaceMap().GetAttrNameByIndex( XML_NAMESPACE_SCRIPT ),
217*cdf0e10cSrcweir 		GetNamespaceMap().GetNameByIndex( XML_NAMESPACE_SCRIPT ) );
218*cdf0e10cSrcweir 	GetAttrList().AddAttribute(
219*cdf0e10cSrcweir 		GetNamespaceMap().GetAttrNameByIndex( XML_NAMESPACE_DOM ),
220*cdf0e10cSrcweir 		GetNamespaceMap().GetNameByIndex( XML_NAMESPACE_DOM ) );
221*cdf0e10cSrcweir 	GetAttrList().AddAttribute(
222*cdf0e10cSrcweir 		GetNamespaceMap().GetAttrNameByIndex( XML_NAMESPACE_OOO ),
223*cdf0e10cSrcweir 		GetNamespaceMap().GetNameByIndex( XML_NAMESPACE_OOO ) );
224*cdf0e10cSrcweir 	GetAttrList().AddAttribute(
225*cdf0e10cSrcweir 		GetNamespaceMap().GetAttrNameByIndex( XML_NAMESPACE_XLINK ),
226*cdf0e10cSrcweir 		GetNamespaceMap().GetNameByIndex( XML_NAMESPACE_XLINK ) );
227*cdf0e10cSrcweir }
228*cdf0e10cSrcweir 
229*cdf0e10cSrcweir void XMLAutoTextEventExport::exportEvents()
230*cdf0e10cSrcweir {
231*cdf0e10cSrcweir 	DBG_ASSERT(hasEvents(), "no events to export!");
232*cdf0e10cSrcweir 
233*cdf0e10cSrcweir 	GetEventExport().Export(xEvents, sal_True);
234*cdf0e10cSrcweir }
235*cdf0e10cSrcweir 
236*cdf0e10cSrcweir 
237*cdf0e10cSrcweir 
238*cdf0e10cSrcweir // methods without content:
239*cdf0e10cSrcweir 
240*cdf0e10cSrcweir void XMLAutoTextEventExport::_ExportMeta() {}
241*cdf0e10cSrcweir void XMLAutoTextEventExport::_ExportScripts() {}
242*cdf0e10cSrcweir void XMLAutoTextEventExport::_ExportFontDecls() {}
243*cdf0e10cSrcweir void XMLAutoTextEventExport::_ExportStyles( sal_Bool ) {}
244*cdf0e10cSrcweir void XMLAutoTextEventExport::_ExportAutoStyles() {}
245*cdf0e10cSrcweir void XMLAutoTextEventExport::_ExportMasterStyles() {}
246*cdf0e10cSrcweir void XMLAutoTextEventExport::_ExportChangeTracking() {}
247*cdf0e10cSrcweir void XMLAutoTextEventExport::_ExportContent() {}
248*cdf0e10cSrcweir 
249*cdf0e10cSrcweir 
250*cdf0e10cSrcweir 
251*cdf0e10cSrcweir // methods to support the component registration
252*cdf0e10cSrcweir 
253*cdf0e10cSrcweir Sequence< OUString > SAL_CALL XMLAutoTextEventExport_getSupportedServiceNames()
254*cdf0e10cSrcweir 	throw()
255*cdf0e10cSrcweir {
256*cdf0e10cSrcweir 	Sequence< OUString > aSeq( 1 );
257*cdf0e10cSrcweir 	aSeq[0] = XMLAutoTextEventExport_getImplementationName();
258*cdf0e10cSrcweir 	return aSeq;
259*cdf0e10cSrcweir }
260*cdf0e10cSrcweir 
261*cdf0e10cSrcweir OUString SAL_CALL XMLAutoTextEventExport_getImplementationName() throw()
262*cdf0e10cSrcweir {
263*cdf0e10cSrcweir 	return OUString( RTL_CONSTASCII_USTRINGPARAM(
264*cdf0e10cSrcweir 		"com.sun.star.comp.Writer.XMLOasisAutotextEventsExporter" ) );
265*cdf0e10cSrcweir }
266*cdf0e10cSrcweir 
267*cdf0e10cSrcweir Reference< XInterface > SAL_CALL XMLAutoTextEventExport_createInstance(
268*cdf0e10cSrcweir 		const Reference< XMultiServiceFactory > & rSMgr)
269*cdf0e10cSrcweir 	throw( Exception )
270*cdf0e10cSrcweir {
271*cdf0e10cSrcweir 	// #110680#
272*cdf0e10cSrcweir 	// return (cppu::OWeakObject*)new XMLAutoTextEventExport;
273*cdf0e10cSrcweir 	return (cppu::OWeakObject*)new XMLAutoTextEventExport(rSMgr, EXPORT_ALL|EXPORT_OASIS);
274*cdf0e10cSrcweir }
275*cdf0e10cSrcweir 
276*cdf0e10cSrcweir // methods to support the component registration
277*cdf0e10cSrcweir 
278*cdf0e10cSrcweir Sequence< OUString > SAL_CALL XMLAutoTextEventExportOOO_getSupportedServiceNames()
279*cdf0e10cSrcweir 	throw()
280*cdf0e10cSrcweir {
281*cdf0e10cSrcweir 	Sequence< OUString > aSeq( 1 );
282*cdf0e10cSrcweir 	aSeq[0] = XMLAutoTextEventExportOOO_getImplementationName();
283*cdf0e10cSrcweir 	return aSeq;
284*cdf0e10cSrcweir }
285*cdf0e10cSrcweir 
286*cdf0e10cSrcweir OUString SAL_CALL XMLAutoTextEventExportOOO_getImplementationName() throw()
287*cdf0e10cSrcweir {
288*cdf0e10cSrcweir 	return OUString( RTL_CONSTASCII_USTRINGPARAM(
289*cdf0e10cSrcweir 		"com.sun.star.comp.Writer.XMLAutotextEventsExporter" ) );
290*cdf0e10cSrcweir }
291*cdf0e10cSrcweir 
292*cdf0e10cSrcweir Reference< XInterface > SAL_CALL XMLAutoTextEventExportOOO_createInstance(
293*cdf0e10cSrcweir 		const Reference< XMultiServiceFactory > & rSMgr)
294*cdf0e10cSrcweir 	throw( Exception )
295*cdf0e10cSrcweir {
296*cdf0e10cSrcweir 	// #110680#
297*cdf0e10cSrcweir 	// return (cppu::OWeakObject*)new XMLAutoTextEventExport;
298*cdf0e10cSrcweir 	return (cppu::OWeakObject*)new XMLAutoTextEventExport(rSMgr,EXPORT_ALL);
299*cdf0e10cSrcweir }
300*cdf0e10cSrcweir 
301