1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_xmloff.hxx"
30 #include "XMLAutoTextEventExport.hxx"
31 #include <com/sun/star/frame/XModel.hpp>
32 #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
33 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
34 #include <com/sun/star/document/XEventsSupplier.hpp>
35 #include <com/sun/star/container/XNameReplace.hpp>
36 #include <com/sun/star/container/XNameAccess.hpp>
37 #include <com/sun/star/uno/Reference.hxx>
38 #include <com/sun/star/uno/Sequence.hxx>
39 
40 #ifndef _COM_SUN_STAR_UNO_EXCEPTION_HPP
41 #include <com/sun/star/uno/Exception.hpp>
42 #endif
43 
44 // #110680#
45 //#ifndef _COMPHELPER_PROCESSFACTORY_HXX_
46 //#include <comphelper/processfactory.hxx>
47 //#endif
48 #include <rtl/ustrbuf.hxx>
49 #include "xmloff/xmlnmspe.hxx"
50 #include <xmloff/nmspmap.hxx>
51 #include <xmloff/xmltoken.hxx>
52 #include <xmloff/XMLEventExport.hxx>
53 #include <tools/debug.hxx>
54 
55 
56 using namespace ::com::sun::star;
57 using namespace ::xmloff::token;
58 
59 // #110680#
60 // using ::comphelper::getProcessServiceFactory;
61 using ::rtl::OUString;
62 using ::rtl::OUStringBuffer;
63 using ::std::set;
64 using ::com::sun::star::beans::XPropertySet;
65 using ::com::sun::star::beans::PropertyValue;
66 using ::com::sun::star::container::XNameAccess;
67 using ::com::sun::star::container::XNameReplace;
68 using ::com::sun::star::document::XEventsSupplier;
69 using ::com::sun::star::frame::XModel;
70 using ::com::sun::star::lang::XMultiServiceFactory;
71 using ::com::sun::star::uno::Any;
72 using ::com::sun::star::uno::Exception;
73 using ::com::sun::star::uno::Reference;
74 using ::com::sun::star::uno::Sequence;
75 using ::com::sun::star::uno::XInterface;
76 using ::com::sun::star::uno::UNO_QUERY;
77 using ::com::sun::star::xml::sax::XDocumentHandler;
78 
79 
80 const sal_Char sAPI_AutoText[] = "com.sun.star.text.AutoTextContainer";
81 
82 
83 // #110680#
84 XMLAutoTextEventExport::XMLAutoTextEventExport(
85 	const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xServiceFactory,
86 		sal_uInt16 nFlags
87 	)
88 :		SvXMLExport( xServiceFactory, MAP_INCH, XML_AUTO_TEXT, nFlags ),
89 		sEventType(RTL_CONSTASCII_USTRINGPARAM("EventType")),
90 		sNone(RTL_CONSTASCII_USTRINGPARAM("None"))
91 {
92 }
93 
94 // #110680#
95 XMLAutoTextEventExport::XMLAutoTextEventExport(
96 	const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xServiceFactory,
97 	const OUString& rFileName,
98 	const Reference<XDocumentHandler> & rHandler,
99 	const Reference<XModel> & rModel,
100 	const Reference<XNameAccess> & rEvents,
101 	sal_uInt16 /*nFlags*/ )
102 :	SvXMLExport( xServiceFactory, rFileName, rHandler, rModel, MAP_INCH ),
103 	xEvents(rEvents),
104 	sEventType(RTL_CONSTASCII_USTRINGPARAM("EventType")),
105 	sNone(RTL_CONSTASCII_USTRINGPARAM("None"))
106 {
107 }
108 
109 XMLAutoTextEventExport::~XMLAutoTextEventExport()
110 {
111 }
112 
113 
114 void XMLAutoTextEventExport::initialize(
115 	const Sequence<Any> & rArguments )
116 		throw(uno::Exception, uno::RuntimeException)
117 {
118 	if (rArguments.getLength() > 1)
119 	{
120 		Reference<XEventsSupplier> xSupplier;
121 		rArguments[1] >>= xSupplier;
122 		if (xSupplier.is())
123 		{
124 			Reference<XNameAccess> xAccess(xSupplier->getEvents(), UNO_QUERY);
125 			xEvents = xAccess;
126 		}
127 		else
128 		{
129 			Reference<XNameReplace> xReplace;
130 			rArguments[1] >>= xReplace;
131 			if (xReplace.is())
132 			{
133 				Reference<XNameAccess> xAccess(xReplace, UNO_QUERY);
134 				xEvents = xAccess;
135 			}
136 			else
137 			{
138 				rArguments[1] >>= xEvents;
139 			}
140 		}
141 	}
142 
143 	// call super class (for XHandler)
144 	SvXMLExport::initialize(rArguments);
145 }
146 
147 
148 sal_uInt32 XMLAutoTextEventExport::exportDoc( enum XMLTokenEnum )
149 {
150 	if( (getExportFlags() & EXPORT_OASIS) == 0 )
151 	{
152 		Reference< lang::XMultiServiceFactory > xFactory = getServiceFactory();
153 		if( xFactory.is() )
154 		{
155 			try
156 			{
157 
158 				Sequence<Any> aArgs( 1 );
159 				aArgs[0] <<= GetDocHandler();
160 
161 				// get filter component
162 				Reference< xml::sax::XDocumentHandler > xTmpDocHandler(
163 					xFactory->createInstanceWithArguments(
164 					OUString::createFromAscii("com.sun.star.comp.Oasis2OOoTransformer"),
165 								aArgs), UNO_QUERY);
166 				OSL_ENSURE( xTmpDocHandler.is(),
167 					"can't instantiate OASIS transformer component" );
168 				if( xTmpDocHandler.is() )
169 				{
170 					SetDocHandler( xTmpDocHandler );
171 				}
172 			}
173 			catch( com::sun::star::uno::Exception& )
174 			{
175 			}
176 		}
177 	}
178 	if (hasEvents())
179 	{
180 		GetDocHandler()->startDocument();
181 
182 		addNamespaces();
183 
184 		{
185 			// container element
186 			SvXMLElementExport aContainerElement(
187 				*this, XML_NAMESPACE_OOO, XML_AUTO_TEXT_EVENTS,
188 				sal_True, sal_True);
189 
190 			exportEvents();
191 		}
192 
193 		// and close document again
194 		GetDocHandler()->endDocument();
195 	}
196 
197 	return 0;
198 }
199 
200 sal_Bool XMLAutoTextEventExport::hasEvents()
201 {
202 	// TODO: provide full implementation that check for presence of events
203 	return xEvents.is();
204 }
205 
206 void XMLAutoTextEventExport::addNamespaces()
207 {
208 	// namespaces for office:, text: and script:
209 	GetAttrList().AddAttribute(
210 		GetNamespaceMap().GetAttrNameByIndex( XML_NAMESPACE_OFFICE ),
211 		GetNamespaceMap().GetNameByIndex( XML_NAMESPACE_OFFICE ) );
212 	GetAttrList().AddAttribute(
213 		GetNamespaceMap().GetAttrNameByIndex( XML_NAMESPACE_TEXT ),
214 		GetNamespaceMap().GetNameByIndex( XML_NAMESPACE_TEXT ) );
215 	GetAttrList().AddAttribute(
216 		GetNamespaceMap().GetAttrNameByIndex( XML_NAMESPACE_SCRIPT ),
217 		GetNamespaceMap().GetNameByIndex( XML_NAMESPACE_SCRIPT ) );
218 	GetAttrList().AddAttribute(
219 		GetNamespaceMap().GetAttrNameByIndex( XML_NAMESPACE_DOM ),
220 		GetNamespaceMap().GetNameByIndex( XML_NAMESPACE_DOM ) );
221 	GetAttrList().AddAttribute(
222 		GetNamespaceMap().GetAttrNameByIndex( XML_NAMESPACE_OOO ),
223 		GetNamespaceMap().GetNameByIndex( XML_NAMESPACE_OOO ) );
224 	GetAttrList().AddAttribute(
225 		GetNamespaceMap().GetAttrNameByIndex( XML_NAMESPACE_XLINK ),
226 		GetNamespaceMap().GetNameByIndex( XML_NAMESPACE_XLINK ) );
227 }
228 
229 void XMLAutoTextEventExport::exportEvents()
230 {
231 	DBG_ASSERT(hasEvents(), "no events to export!");
232 
233 	GetEventExport().Export(xEvents, sal_True);
234 }
235 
236 
237 
238 // methods without content:
239 
240 void XMLAutoTextEventExport::_ExportMeta() {}
241 void XMLAutoTextEventExport::_ExportScripts() {}
242 void XMLAutoTextEventExport::_ExportFontDecls() {}
243 void XMLAutoTextEventExport::_ExportStyles( sal_Bool ) {}
244 void XMLAutoTextEventExport::_ExportAutoStyles() {}
245 void XMLAutoTextEventExport::_ExportMasterStyles() {}
246 void XMLAutoTextEventExport::_ExportChangeTracking() {}
247 void XMLAutoTextEventExport::_ExportContent() {}
248 
249 
250 
251 // methods to support the component registration
252 
253 Sequence< OUString > SAL_CALL XMLAutoTextEventExport_getSupportedServiceNames()
254 	throw()
255 {
256 	Sequence< OUString > aSeq( 1 );
257 	aSeq[0] = XMLAutoTextEventExport_getImplementationName();
258 	return aSeq;
259 }
260 
261 OUString SAL_CALL XMLAutoTextEventExport_getImplementationName() throw()
262 {
263 	return OUString( RTL_CONSTASCII_USTRINGPARAM(
264 		"com.sun.star.comp.Writer.XMLOasisAutotextEventsExporter" ) );
265 }
266 
267 Reference< XInterface > SAL_CALL XMLAutoTextEventExport_createInstance(
268 		const Reference< XMultiServiceFactory > & rSMgr)
269 	throw( Exception )
270 {
271 	// #110680#
272 	// return (cppu::OWeakObject*)new XMLAutoTextEventExport;
273 	return (cppu::OWeakObject*)new XMLAutoTextEventExport(rSMgr, EXPORT_ALL|EXPORT_OASIS);
274 }
275 
276 // methods to support the component registration
277 
278 Sequence< OUString > SAL_CALL XMLAutoTextEventExportOOO_getSupportedServiceNames()
279 	throw()
280 {
281 	Sequence< OUString > aSeq( 1 );
282 	aSeq[0] = XMLAutoTextEventExportOOO_getImplementationName();
283 	return aSeq;
284 }
285 
286 OUString SAL_CALL XMLAutoTextEventExportOOO_getImplementationName() throw()
287 {
288 	return OUString( RTL_CONSTASCII_USTRINGPARAM(
289 		"com.sun.star.comp.Writer.XMLAutotextEventsExporter" ) );
290 }
291 
292 Reference< XInterface > SAL_CALL XMLAutoTextEventExportOOO_createInstance(
293 		const Reference< XMultiServiceFactory > & rSMgr)
294 	throw( Exception )
295 {
296 	// #110680#
297 	// return (cppu::OWeakObject*)new XMLAutoTextEventExport;
298 	return (cppu::OWeakObject*)new XMLAutoTextEventExport(rSMgr,EXPORT_ALL);
299 }
300 
301