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 "MetaExportComponent.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/uno/Sequence.hxx>
35 #include <com/sun/star/uno/Reference.hxx>
36 
37 #ifndef _COM_SUN_STAR_UNO_EXCEPTION_HPP
38 #include <com/sun/star/uno/Exception.hpp>
39 #endif
40 #include <com/sun/star/beans/PropertyAttribute.hpp>
41 
42 // #110680#
43 //#ifndef _COMPHELPER_PROCESSFACTORY_HXX_
44 //#include <comphelper/processfactory.hxx>
45 //#endif
46 #include <comphelper/genericpropertyset.hxx>
47 #include <rtl/ustrbuf.hxx>
48 #include "xmloff/xmlnmspe.hxx"
49 #include <xmloff/nmspmap.hxx>
50 #include <xmloff/xmltoken.hxx>
51 #include <xmloff/xmlmetae.hxx>
52 #include "PropertySetMerger.hxx"
53 #include <tools/debug.hxx>
54 
55 #include <unotools/docinfohelper.hxx>
56 
57 
58 using namespace ::com::sun::star;
59 using namespace ::xmloff::token;
60 
61 // #110680#
62 XMLMetaExportComponent::XMLMetaExportComponent(
63 	const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& xServiceFactory,
64 		sal_uInt16 nFlags )
65 :	SvXMLExport( xServiceFactory, MAP_INCH, XML_TEXT, nFlags )
66 {
67 }
68 
69 XMLMetaExportComponent::~XMLMetaExportComponent()
70 {
71 }
72 
73 void SAL_CALL XMLMetaExportComponent::setSourceDocument( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent >& xDoc ) throw(::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::uno::RuntimeException)
74 {
75 	try
76 	{
77 		SvXMLExport::setSourceDocument( xDoc );
78 	}
79 	catch( lang::IllegalArgumentException& )
80 	{
81 		// allow to use document properties service without model access
82 		// this is required for document properties exporter
83 		mxDocProps =
84             uno::Reference< document::XDocumentProperties >::query( xDoc );
85 		if( !mxDocProps.is() )
86 			throw lang::IllegalArgumentException();
87 	}
88 }
89 
90 sal_uInt32 XMLMetaExportComponent::exportDoc( enum XMLTokenEnum )
91 {
92 	uno::Reference< xml::sax::XDocumentHandler > xDocHandler = GetDocHandler();
93 
94 	if( (getExportFlags() & EXPORT_OASIS) == 0 )
95 	{
96 		uno::Reference< lang::XMultiServiceFactory > xFactory = getServiceFactory();
97 		if( xFactory.is() )
98 		{
99 			try
100 			{
101 				::comphelper::PropertyMapEntry aInfoMap[] =
102 				{
103 					{ "Class", sizeof("Class")-1, 0,
104 						&::getCppuType((::rtl::OUString*)0),
105 						beans::PropertyAttribute::MAYBEVOID, 0},
106 					{ NULL, 0, 0, NULL, 0, 0 }
107 				};
108 				uno::Reference< beans::XPropertySet > xConvPropSet(
109 					::comphelper::GenericPropertySet_CreateInstance(
110 							new ::comphelper::PropertySetInfo( aInfoMap ) ) );
111 
112 				uno::Any aAny;
113 				aAny <<= GetXMLToken( XML_TEXT );
114 				xConvPropSet->setPropertyValue(
115 						::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Class")), aAny );
116 
117 				uno::Reference< beans::XPropertySet > xPropSet =
118 					getExportInfo().is()
119 						?  PropertySetMerger_CreateInstance( getExportInfo(),
120 														  xConvPropSet )
121 						: getExportInfo();
122 
123 				uno::Sequence< uno::Any > aArgs( 3 );
124 				aArgs[0] <<= xDocHandler;
125 				aArgs[1] <<= xPropSet;
126 				aArgs[2] <<= GetModel();
127 
128 				// get filter component
129 				xDocHandler = uno::Reference< xml::sax::XDocumentHandler >(
130 					xFactory->createInstanceWithArguments(
131 						::rtl::OUString::createFromAscii("com.sun.star.comp.Oasis2OOoTransformer"),
132 						aArgs),
133 					uno::UNO_QUERY_THROW );
134 
135 				SetDocHandler( xDocHandler );
136 			}
137 			catch( com::sun::star::uno::Exception& )
138 			{
139 				OSL_ENSURE( sal_False, "Cannot instantiate com.sun.star.comp.Oasis2OOoTransformer!\n");
140 			}
141 		}
142 	}
143 
144 
145 	xDocHandler->startDocument();
146 	{
147 #if 0
148         GetAttrList().AddAttribute(
149                 GetNamespaceMap().GetAttrNameByKey( XML_NAMESPACE_DC ),
150                 GetNamespaceMap().GetNameByKey( XML_NAMESPACE_DC ) );
151         GetAttrList().AddAttribute(
152                 GetNamespaceMap().GetAttrNameByKey( XML_NAMESPACE_META ),
153                 GetNamespaceMap().GetNameByKey( XML_NAMESPACE_META ) );
154         GetAttrList().AddAttribute(
155                 GetNamespaceMap().GetAttrNameByKey( XML_NAMESPACE_OFFICE ),
156                 GetNamespaceMap().GetNameByKey( XML_NAMESPACE_OFFICE ) );
157 #else
158         const SvXMLNamespaceMap& rMap = GetNamespaceMap();
159 		sal_uInt16 nPos = rMap.GetFirstKey();
160 		while( USHRT_MAX != nPos )
161 		{
162 			GetAttrList().AddAttribute( rMap.GetAttrNameByKey( nPos ), rMap.GetNameByKey( nPos ) );
163 			nPos = GetNamespaceMap().GetNextKey( nPos );
164 		}
165 #endif
166 
167         const sal_Char* pVersion = 0;
168         switch( getDefaultVersion() )
169         {
170         case SvtSaveOptions::ODFVER_LATEST: pVersion = "1.2"; break;
171         case SvtSaveOptions::ODFVER_012: pVersion = "1.2"; break;
172         case SvtSaveOptions::ODFVER_011: pVersion = "1.1"; break;
173         case SvtSaveOptions::ODFVER_010: break;
174 
175         default:
176             DBG_ERROR("xmloff::XMLMetaExportComponent::exportDoc(), unexpected odf default version!");
177         }
178 
179         if( pVersion )
180 		    AddAttribute( XML_NAMESPACE_OFFICE, XML_VERSION,
181                             ::rtl::OUString::createFromAscii(pVersion) );
182 
183 			SvXMLElementExport aDocElem( *this, XML_NAMESPACE_OFFICE, XML_DOCUMENT_META,
184 						sal_True, sal_True );
185 
186         // NB: office:meta is now written by _ExportMeta
187         _ExportMeta();
188 	}
189 	xDocHandler->endDocument();
190 	return 0;
191 }
192 
193 void XMLMetaExportComponent::_ExportMeta()
194 {
195     if (mxDocProps.is()) {
196         ::rtl::OUString generator( ::utl::DocInfoHelper::GetGeneratorString() );
197         // update generator here
198         mxDocProps->setGenerator(generator);
199         SvXMLMetaExport * pMeta = new SvXMLMetaExport(*this, mxDocProps);
200         uno::Reference<xml::sax::XDocumentHandler> xMeta(pMeta);
201         pMeta->Export();
202     } else {
203         SvXMLExport::_ExportMeta();
204     }
205 }
206 
207 // methods without content:
208 void XMLMetaExportComponent::_ExportAutoStyles() {}
209 void XMLMetaExportComponent::_ExportMasterStyles() {}
210 void XMLMetaExportComponent::_ExportContent() {}
211 
212 
213 uno::Sequence< rtl::OUString > SAL_CALL XMLMetaExportComponent_getSupportedServiceNames()
214 	throw()
215 {
216 	const rtl::OUString aServiceName(
217 		RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.XMLOasisMetaExporter" ) );
218 	const uno::Sequence< rtl::OUString > aSeq( &aServiceName, 1 );
219 	return aSeq;
220 }
221 
222 rtl::OUString SAL_CALL XMLMetaExportComponent_getImplementationName() throw()
223 {
224 	return rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "XMLMetaExportComponent" ) );
225 }
226 
227 uno::Reference< uno::XInterface > SAL_CALL XMLMetaExportComponent_createInstance(
228 		const uno::Reference< lang::XMultiServiceFactory > & rSMgr)
229 	throw( uno::Exception )
230 {
231 	// #110680#
232 	// return (cppu::OWeakObject*)new XMLMetaExportComponent;
233 	return (cppu::OWeakObject*)new XMLMetaExportComponent(rSMgr, EXPORT_META|EXPORT_OASIS);
234 }
235 
236 uno::Sequence< rtl::OUString > SAL_CALL XMLMetaExportOOO_getSupportedServiceNames()
237 	throw()
238 {
239 	const rtl::OUString aServiceName(
240 		RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.XMLMetaExporter" ) );
241 	const uno::Sequence< rtl::OUString > aSeq( &aServiceName, 1 );
242 	return aSeq;
243 }
244 
245 rtl::OUString SAL_CALL XMLMetaExportOOO_getImplementationName() throw()
246 {
247 	return rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "XMLMetaExportOOo" ) );
248 }
249 
250 uno::Reference< uno::XInterface > SAL_CALL XMLMetaExportOOO_createInstance(
251 		const uno::Reference< lang::XMultiServiceFactory > & rSMgr)
252 	throw( uno::Exception )
253 {
254 	// #110680#
255 	// return (cppu::OWeakObject*)new XMLMetaExportComponent;
256 	return (cppu::OWeakObject*)new XMLMetaExportComponent(rSMgr, EXPORT_META);
257 }
258 
259