update dev300-m58
[ooovba.git] / configmgr / source / xml / writersvc.cxx
blobc68334c2e12ea38c2778f3ff9346504273007cee
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: writersvc.cxx,v $
10 * $Revision: 1.11 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_configmgr.hxx"
34 #include "writersvc.hxx"
36 #ifndef CONFIGMGR_API_FACTORY_HXX_
37 #include "confapifactory.hxx"
38 #endif
39 #include <com/sun/star/configuration/backend/XLayerHandler.hpp>
40 #include <com/sun/star/lang/WrappedTargetException.hpp>
41 #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
42 #include <com/sun/star/lang/IllegalArgumentException.hpp>
44 // -----------------------------------------------------------------------------
46 namespace configmgr
48 // -----------------------------------------------------------------------------
49 namespace xml
51 // -----------------------------------------------------------------------------
52 namespace uno = ::com::sun::star::uno;
53 namespace lang = ::com::sun::star::lang;
54 namespace io = ::com::sun::star::io;
55 namespace sax = ::com::sun::star::xml::sax;
56 namespace backenduno = ::com::sun::star::configuration::backend;
57 // -----------------------------------------------------------------------------
59 template <class BackendInterface>
60 struct WriterServiceTraits;
61 // -----------------------------------------------------------------------------
62 static inline void clear(rtl::OUString & _rs) { _rs = rtl::OUString(); }
64 // -----------------------------------------------------------------------------
65 template <class BackendInterface>
66 WriterService<BackendInterface>::WriterService(uno::Reference< uno::XComponentContext > const & _xContext)
67 : m_xServiceFactory(_xContext->getServiceManager(), uno::UNO_QUERY)
68 , m_xWriter()
70 if (!m_xServiceFactory.is())
72 rtl::OUString sMessage( RTL_CONSTASCII_USTRINGPARAM("Configuration XML Writer: Context has no service manager"));
73 throw uno::RuntimeException(sMessage,NULL);
76 // -----------------------------------------------------------------------------
78 // XInitialization
79 template <class BackendInterface>
80 void SAL_CALL
81 WriterService<BackendInterface>::initialize( const uno::Sequence< uno::Any >& aArguments )
82 throw (uno::Exception, uno::RuntimeException)
84 switch(aArguments.getLength())
86 case 0:
88 break;
91 case 1:
93 if (aArguments[0] >>= m_xWriter)
94 break;
96 uno::Reference< io::XOutputStream > xStream;
98 if (aArguments[0] >>= xStream)
100 this->setOutputStream(xStream);
101 break;
104 rtl::OUString sMessage( RTL_CONSTASCII_USTRINGPARAM("Cannot use argument to initialize a Configuration XML Writer"
105 "- SAX XDocumentHandler or XOutputStream expected"));
106 throw lang::IllegalArgumentException(sMessage,*this,1);
108 default:
110 rtl::OUString sMessage( RTL_CONSTASCII_USTRINGPARAM("Too many arguments to initialize a Configuration Parser"));
111 throw lang::IllegalArgumentException(sMessage,*this,0);
115 // -----------------------------------------------------------------------------
117 template <class BackendInterface>
118 inline
119 ServiceInfoHelper WriterService<BackendInterface>::getServiceInfo()
121 return WriterServiceTraits<BackendInterface>::getServiceInfo();
123 // -----------------------------------------------------------------------------
125 // XServiceInfo
126 template <class BackendInterface>
127 ::rtl::OUString SAL_CALL
128 WriterService<BackendInterface>::getImplementationName( )
129 throw (uno::RuntimeException)
131 return getServiceInfo().getImplementationName( );
133 // -----------------------------------------------------------------------------
135 template <class BackendInterface>
136 sal_Bool SAL_CALL
137 WriterService<BackendInterface>::supportsService( const ::rtl::OUString& ServiceName )
138 throw (uno::RuntimeException)
140 return getServiceInfo().supportsService( ServiceName );
142 // -----------------------------------------------------------------------------
144 template <class BackendInterface>
145 uno::Sequence< ::rtl::OUString > SAL_CALL
146 WriterService<BackendInterface>::getSupportedServiceNames( )
147 throw (uno::RuntimeException)
149 return getServiceInfo().getSupportedServiceNames( );
151 // -----------------------------------------------------------------------------
153 template <class BackendInterface>
154 void SAL_CALL
155 WriterService<BackendInterface>::setOutputStream( const uno::Reference< io::XOutputStream >& aStream )
156 throw (uno::RuntimeException)
158 uno::Reference< io::XActiveDataSource > xDS( m_xWriter, uno::UNO_QUERY );
160 if (xDS.is())
162 xDS->setOutputStream(aStream);
164 else
166 uno::Reference< sax::XDocumentHandler > xNewHandler = this->createHandler();
168 xDS.set( xNewHandler, uno::UNO_QUERY );
169 if (!xDS.is())
171 rtl::OUString sMessage( RTL_CONSTASCII_USTRINGPARAM("Configuration XML Writer: Cannot set output stream to sax.Writer - missing interface XActiveDataSource."));
172 throw uno::RuntimeException(sMessage,*this);
174 xDS->setOutputStream(aStream);
176 m_xWriter = xNewHandler;
179 // -----------------------------------------------------------------------------
181 template <class BackendInterface>
182 uno::Reference< io::XOutputStream > SAL_CALL
183 WriterService<BackendInterface>::getOutputStream( )
184 throw (uno::RuntimeException)
186 uno::Reference< io::XActiveDataSource > xDS( m_xWriter, uno::UNO_QUERY );
188 return xDS.is()? xDS->getOutputStream() : uno::Reference< io::XOutputStream >();
190 // -----------------------------------------------------------------------------
192 template <class BackendInterface>
193 uno::Reference< sax::XDocumentHandler > WriterService<BackendInterface>::getWriteHandler()
194 throw (uno::RuntimeException)
196 if (!m_xWriter.is())
197 m_xWriter = this->createHandler();
199 return m_xWriter;
202 // -----------------------------------------------------------------------------
204 template <class BackendInterface>
205 uno::Reference< sax::XDocumentHandler > WriterService<BackendInterface>::createHandler() const
206 throw (uno::RuntimeException)
210 static rtl::OUString const k_sSaxWriterSvc( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.xml.sax.Writer") );
212 return uno::Reference< sax::XDocumentHandler >::query( getServiceFactory()->createInstance(k_sSaxWriterSvc) );
214 catch (uno::RuntimeException& ) { throw; }
215 catch (uno::Exception& e)
217 lang::XInitialization * const pThis = const_cast<WriterService *>(this);
218 throw lang::WrappedTargetRuntimeException(e.Message, pThis, uno::makeAny(e));
222 // -----------------------------------------------------------------------------
223 // -----------------------------------------------------------------------------
224 sal_Char const * const aLayerWriterServices[] =
226 "com.sun.star.configuration.backend.xml.LayerWriter",
229 extern // needed by SunCC 5.2, if used from template
230 const ServiceImplementationInfo aLayerWriterSI =
232 "com.sun.star.comp.configuration.backend.xml.LayerWriter",
233 aLayerWriterServices,
236 // -----------------------------------------------------------------------------
237 // -----------------------------------------------------------------------------
238 template <>
239 struct WriterServiceTraits< backenduno::XLayerHandler >
241 static ServiceImplementationInfo const * getServiceInfo()
242 { return & aLayerWriterSI; }
244 // -----------------------------------------------------------------------------
246 const ServiceRegistrationInfo* getLayerWriterServiceInfo()
247 { return getRegistrationInfo(& aLayerWriterSI); }
248 // -----------------------------------------------------------------------------
249 // -----------------------------------------------------------------------------
251 // instantiate here !
252 template class WriterService< backenduno::XLayerHandler >;
254 // -----------------------------------------------------------------------------
255 // -----------------------------------------------------------------------------
256 // -----------------------------------------------------------------------------
257 } // namespace
259 // -----------------------------------------------------------------------------
260 } // namespace