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