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