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