xref: /aoo42x/main/xmlsecurity/tools/demo/util.cxx (revision cdf0e10c)
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_xmlsecurity.hxx"
30 
31 #include "util.hxx"
32 
33 #include <stdio.h>
34 
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>
40 
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;
45 
46 using namespace ::com::sun::star;
47 
48 cssu::Reference< cssl::XMultiServiceFactory > CreateDemoServiceFactory()
49 {
50 	cssu::Reference< cssl::XMultiServiceFactory > xMSF;
51 
52 	try
53 	{
54 		cssu::Reference< cssl::XMultiComponentFactory > xLocalServiceManager = NULL ;
55 		cssu::Reference< cssu::XComponentContext > xLocalComponentContext = NULL ;
56 
57 		cssu::Reference< ::com::sun::star::registry::XSimpleRegistry > xSimpleRegistry
58 			= ::cppu::createSimpleRegistry();
59 		OSL_ENSURE( xSimpleRegistry.is(),
60 			"serviceManager - "
61 			"Cannot create simple registry" ) ;
62 
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" ) ;
67 
68 		xLocalComponentContext = ::cppu::bootstrap_InitialComponentContext( xSimpleRegistry ) ;
69 		OSL_ENSURE( xLocalComponentContext.is() ,
70 			"serviceManager - "
71 			"Cannot create intial component context" ) ;
72 
73 		xLocalServiceManager = xLocalComponentContext->getServiceManager() ;
74 		OSL_ENSURE( xLocalServiceManager.is() ,
75 			"serviceManager - "
76 			"Cannot create intial service manager" ) ;
77 
78 		xMSF = cssu::Reference< cssl::XMultiServiceFactory >(xLocalServiceManager, cssu::UNO_QUERY) ;
79 
80 		::comphelper::setProcessServiceFactory( xMSF );
81 	}
82 	catch( cssu::Exception& e )
83 	{
84 		fprintf( stderr , "Error creating ServiceManager, Exception is %s\n" , rtl::OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US ).getStr() ) ;
85 		exit (-1);
86 	}
87 
88 	return xMSF;
89 }
90 
91 ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > OpenInputStream( const ::rtl::OUString& rStreamName )
92 {
93 	SvFileStream* pStream = new SvFileStream( rStreamName, STREAM_READ );
94 	pStream->Seek( STREAM_SEEK_TO_END );
95 	sal_uLong nBytes = pStream->Tell();
96 	pStream->Seek( STREAM_SEEK_TO_BEGIN );
97 	SvLockBytesRef xLockBytes = new SvLockBytes( pStream, sal_True );
98 	uno::Reference< io::XInputStream > xInputStream = new utl::OInputStreamHelper( xLockBytes, nBytes );
99 
100 	return xInputStream;
101 
102 }
103 
104 ::com::sun::star::uno::Reference< ::com::sun::star::io::XOutputStream > OpenOutputStream( const ::rtl::OUString& rStreamName )
105 {
106 	SvFileStream* pStream = new SvFileStream( rStreamName, STREAM_WRITE );
107 	SvLockBytesRef xLockBytes = new SvLockBytes( pStream, sal_True );
108 	uno::Reference< io::XOutputStream > xOutputStream = new utl::OOutputStreamHelper( xLockBytes );
109 
110 	return xOutputStream;
111 }
112