LibreOffice Module dtrans (master)  1
testcnttype.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements. See the NOTICE file distributed
13  * with this work for additional information regarding copyright
14  * ownership. The ASF licenses this file to you under the Apache
15  * License, Version 2.0 (the "License"); you may not use this file
16  * except in compliance with the License. You may obtain a copy of
17  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #include <cppuhelper/servicefactory.hxx>
21 #include <com/sun/star/lang/XTypeProvider.hpp>
22 #include <com/sun/star/lang/IllegalArgumentException.hpp>
23 #include <com/sun/star/container/NoSuchElementException.hpp>
24 #include <com/sun/star/datatransfer/XMimeContentType.hpp>
25 #include <com/sun/star/datatransfer/XMimeContentTypeFactory.hpp>
26 #include <com/sun/star/lang/XComponent.hpp>
27 #include <rtl/ustring.hxx>
28 #include <sal/types.h>
29 #include <osl/diagnose.h>
30 
31 #include <stdio.h>
32 
33 #include <vector>
34 
35 // my defines
36 
37 #define TEST_CLIPBOARD
38 #define RDB_SYSPATH "d:\\projects\\src621\\dtrans\\wntmsci7\\bin\\applicat.rdb"
39 
40 // namespaces
41 
42 using namespace ::std;
43 using namespace ::cppu;
44 using namespace ::com::sun::star::datatransfer;
45 using namespace ::com::sun::star::uno;
46 using namespace ::com::sun::star::lang;
47 using namespace ::com::sun::star::container;
48 
49 void ShutdownServiceMgr( Reference< XMultiServiceFactory >& SrvMgr )
50 {
51  // Cast factory to XComponent
52  Reference< XComponent > xComponent( SrvMgr, UNO_QUERY );
53 
54  if ( !xComponent.is() )
55  OSL_FAIL("Error shutting down");
56 
57  // Dispose and clear factory
58  xComponent->dispose();
59  SrvMgr.clear();
60 }
61 
62 sal_Bool readCntTypesFromFileIntoVector( char* fname, vector< string >& vecData )
63 {
64  FILE* fstream;
65 
66  fstream = fopen( fname, "r+" );
67  if ( !fstream )
68  return sal_False;
69 
70  // set pointer to file start
71  fseek( fstream, 0, SEEK_SET );
72 
73  char line[1024];
74  while ( fscanf( fstream, "%1023[^\n]s", line ) != EOF )
75  {
76  vecData.push_back( line );
77  fgetc( fstream );
78  }
79 
80  fclose( fstream );
81 
82  return sal_True;
83 }
84 
85 sal_Bool processCntTypesAndWriteResultIntoFile( char* fname, vector< string >& vecData, Reference< XMimeContentTypeFactory > cnttypeFactory )
86 {
87  FILE* fstream;
88 
89  fstream = fopen( fname, "w" );
90  if ( !fstream )
91  return sal_False;
92 
93  // set pointer to file start
94  fseek( fstream, 0, SEEK_SET );
95 
96  for ( const auto& rData : vecData )
97  {
98  try
99  {
100  fprintf( fstream, "Read: %s\n", rData.c_str( ) );
101 
102  Reference< XMimeContentType > xMCntTyp = cnttypeFactory->createMimeContentType( OUString::createFromAscii( rData.c_str( ) ) );
103 
104  fwprintf( fstream, OUString("Type: %s\n"), xMCntTyp->getMediaType( ).getStr( ) );
105  fwprintf( fstream, OUString("Subtype: %s\n"), xMCntTyp->getMediaSubtype( ).getStr( ) );
106 
107  Sequence< OUString > seqParam = xMCntTyp->getParameters( );
108  sal_Int32 nParams = seqParam.getLength( );
109 
110  for ( sal_Int32 i = 0; i < nParams; i++ )
111  {
112  fwprintf( fstream, OUString("PName: %s\n"), seqParam[i].getStr( ) );
113  fwprintf( fstream, OUString("PValue: %s\n"), xMCntTyp->getParameterValue( seqParam[i] ).getStr( ) );
114  }
115  }
116  catch( IllegalArgumentException& ex )
117  {
118  fwprintf( fstream, OUString("Read incorrect content type!\n\n") );
119  }
120  catch( NoSuchElementException& )
121  {
122  fwprintf( fstream, OUString("Value of parameter not available\n") );
123  }
124  catch( ... )
125  {
126  fwprintf( fstream, OUString("Unknown error!\n\n") );
127  }
128 
129  fwprintf( fstream, OUString("\n#############################################\n\n") );
130  }
131 
132  fclose( fstream );
133 
134  return sal_True;
135 }
136 
137 // main
138 
139 int SAL_CALL main( int nArgc, char* argv[] )
140 {
141  if ( nArgc != 3 )
142  printf( "Start with: testcnttype input-file output-file\n" );
143 
144  // get the global service-manager
145 
146  Reference< XMultiServiceFactory > g_xFactory( createRegistryServiceFactory( RDB_SYSPATH ) );
147 
148  // Print a message if an error occurred.
149  if ( !g_xFactory.is( ) )
150  {
151  OSL_FAIL("Can't create RegistryServiceFactory");
152  return(-1);
153  }
154 
155  vector< string > vecCntTypes;
156 
157  // open input-file and read the data
158  if ( !readCntTypesFromFileIntoVector( argv[1], vecCntTypes ) )
159  {
160  printf( "Can't open input file" );
161  ShutdownServiceMgr( g_xFactory );
162  }
163 
164  Reference< XMimeContentTypeFactory >
165  xMCntTypeFactory( g_xFactory->createInstance("com.sun.star.datatransfer.MimeContentTypeFactory"), UNO_QUERY );
166 
167  if ( !xMCntTypeFactory.is( ) )
168  {
169  OSL_FAIL( "Error creating MimeContentTypeFactory Service" );
170  return(-1);
171  }
172 
173  if ( !processCntTypesAndWriteResultIntoFile( argv[2], vecCntTypes, xMCntTypeFactory ) )
174  {
175  printf( "Can't open output file" );
176  ShutdownServiceMgr( g_xFactory );
177  }
178 
179  // shutdown the service manager
180 
181  ShutdownServiceMgr( g_xFactory );
182 
183  return 0;
184 }
185 
186 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
tuple line
int SAL_CALL main(int nArgc, char *argv[])
#define RDB_SYSPATH
Definition: testcnttype.cxx:38
const ContentProperties & rData
SAL_DLLPUBLIC_EXPORT css::uno::Reference< css::lang::XMultiServiceFactory > SAL_CALL createRegistryServiceFactory(OUString const &, OUString const &, sal_Bool, OUString const &)
int i
#define sal_True
unsigned char sal_Bool
Reference< XMultiServiceFactory > g_xFactory
sal_Bool readCntTypesFromFileIntoVector(char *fname, vector< string > &vecData)
Definition: testcnttype.cxx:62
void ShutdownServiceMgr(Reference< XMultiServiceFactory > &SrvMgr)
Definition: testcnttype.cxx:49
#define sal_False
sal_Bool processCntTypesAndWriteResultIntoFile(char *fname, vector< string > &vecData, Reference< XMimeContentTypeFactory > cnttypeFactory)
Definition: testcnttype.cxx:85