merged tag ooo/OOO330_m14
[LibreOffice.git] / xmlsecurity / tools / demo / util.cxx
bloba124edfd523b45cde45bb70cd592b46cbd3b767a
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_xmlsecurity.hxx"
31 #include "util.hxx"
33 #include <stdio.h>
35 #include <com/sun/star/registry/XImplementationRegistration.hpp>
36 #include <cppuhelper/bootstrap.hxx>
37 #include <comphelper/processfactory.hxx>
38 #include <unotools/streamhelper.hxx>
39 #include <tools/string.hxx>
41 namespace cssu = com::sun::star::uno;
42 namespace cssl = com::sun::star::lang;
43 namespace cssxc = com::sun::star::xml::crypto;
44 namespace cssi = com::sun::star::io;
46 using namespace ::com::sun::star;
48 cssu::Reference< cssl::XMultiServiceFactory > CreateDemoServiceFactory()
50 cssu::Reference< cssl::XMultiServiceFactory > xMSF;
52 try
54 cssu::Reference< cssl::XMultiComponentFactory > xLocalServiceManager = NULL ;
55 cssu::Reference< cssu::XComponentContext > xLocalComponentContext = NULL ;
57 cssu::Reference< ::com::sun::star::registry::XSimpleRegistry > xSimpleRegistry
58 = ::cppu::createSimpleRegistry();
59 OSL_ENSURE( xSimpleRegistry.is(),
60 "serviceManager - "
61 "Cannot create simple registry" ) ;
63 xSimpleRegistry->open(rtl::OUString::createFromAscii( "demo.rdb" ), sal_True, sal_False);
64 OSL_ENSURE( xSimpleRegistry->isValid() ,
65 "serviceManager - "
66 "Cannot open xml security registry rdb" ) ;
68 xLocalComponentContext = ::cppu::bootstrap_InitialComponentContext( xSimpleRegistry ) ;
69 OSL_ENSURE( xLocalComponentContext.is() ,
70 "serviceManager - "
71 "Cannot create intial component context" ) ;
73 xLocalServiceManager = xLocalComponentContext->getServiceManager() ;
74 OSL_ENSURE( xLocalServiceManager.is() ,
75 "serviceManager - "
76 "Cannot create intial service manager" ) ;
78 xMSF = cssu::Reference< cssl::XMultiServiceFactory >(xLocalServiceManager, cssu::UNO_QUERY) ;
80 ::comphelper::setProcessServiceFactory( xMSF );
82 catch( cssu::Exception& e )
84 fprintf( stderr , "Error creating ServiceManager, Exception is %s\n" , rtl::OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US ).getStr() ) ;
85 exit (-1);
88 return xMSF;
91 ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > OpenInputStream( const ::rtl::OUString& rStreamName )
93 SvFileStream* pStream = new SvFileStream( rStreamName, STREAM_READ );
94 pStream->Seek( STREAM_SEEK_TO_END );
95 ULONG nBytes = pStream->Tell();
96 pStream->Seek( STREAM_SEEK_TO_BEGIN );
97 SvLockBytesRef xLockBytes = new SvLockBytes( pStream, TRUE );
98 uno::Reference< io::XInputStream > xInputStream = new utl::OInputStreamHelper( xLockBytes, nBytes );
100 return xInputStream;
104 ::com::sun::star::uno::Reference< ::com::sun::star::io::XOutputStream > OpenOutputStream( const ::rtl::OUString& rStreamName )
106 SvFileStream* pStream = new SvFileStream( rStreamName, STREAM_WRITE );
107 SvLockBytesRef xLockBytes = new SvLockBytes( pStream, TRUE );
108 uno::Reference< io::XOutputStream > xOutputStream = new utl::OOutputStreamHelper( xLockBytes );
110 return xOutputStream;