Version 3.6.0.4, tag libreoffice-3.6.0.4
[LibreOffice.git] / unodevtools / source / skeletonmaker / skeletonmaker.cxx
blob3609798cb54336268e957b962d2a2f2197abd33d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
28 #include <iostream>
30 #include "sal/main.h"
31 #include "rtl/process.h"
32 #include "rtl/ustrbuf.hxx"
33 #include "unodevtools/typemanager.hxx"
34 #include "unodevtools/options.hxx"
35 #include "ostringostreaminserter.hxx"
36 #include "skeletonjava.hxx"
37 #include "skeletoncpp.hxx"
39 #include "com/sun/star/uno/Reference.hxx"
41 using namespace ::rtl;
42 using namespace ::skeletonmaker;
43 using namespace ::unodevtools;
44 using namespace ::com::sun::star::uno;
46 namespace {
48 static const char usageText[] =
49 "\n sub-commands:\n"
50 " dump dump declarations on stdout (e.g. constructors, methods, type\n"
51 " mapping for properties) or complete method bodies with\n"
52 " method forwarding.\n"
53 " component generates language specific code skeleton files using the\n"
54 " implementation name as the file and class name\n"
55 " calc-add-in generates a language specific code skeleton for a calc add-in\n"
56 " using the implementation name as the file and class name. A \n"
57 " service type is necessary, referencing an interface which defines\n"
58 " the new add-in functions.\n"
59 " add-on generates a language specific code skeleton for an add-on compnent\n"
60 " using the implementation name as the file and class name. The protocol\n"
61 " name(s) and the corresponding command(s) have to be specified with the\n"
62 " '-p' option.\n"
63 "\n options:\n"
64 " -env:INIFILENAME=<url> url specifies a URL to an UNO ini|rc file of an\n"
65 " existing UNO environment (URE, office installation).\n"
66 " -env:UNO_TYPES=<url> url specifies a binary type library file. It can be\n"
67 " a space separated list of urls.\n"
68 " -a, --all list all interface methods, not only the direct\n"
69 " ones\n"
70 " --(java4|java5|cpp) select the target language\n"
71 " --java4 generate output for Java 1.4 or earlier\n"
72 " --java5 generate output for Java 1.5 or later (is \n"
73 " currently the default)\n"
74 " --cpp generate output for C++\n"
75 " -sn, --shortnames using namespace abbreviation 'css:': for\n"
76 " '::com::sun::star::', only valid for sub-command\n"
77 " 'dump' and target language 'cpp'. It is default for the\n"
78 " sub-command 'component'.\n"
79 " --propertysetmixin the generated skeleton implements the cppu::PropertySetMixin\n"
80 " helper if a referenced new style service specifies an\n"
81 " interface which provides attributes (directly or inherited).\n"
82 " -lh --licenseheader generates a default OpenOffice.org LGPL license\n"
83 " header at the beginning of a component source file.\n"
84 " This option is taken into account in 'component' mode\n"
85 " only and if -o is unequal 'stdout'.\n"
86 " -bc specifies that the generated calc add-in is backward\n"
87 " --backward-compatible compatible to older office versions and implement the\n"
88 " former required add-in interfaces where the implementation\n"
89 " is mapped on the new add-in configuration. In this case\n"
90 " the config schema needs to be bundled with the extension\n"
91 " add-in as well. Default is a minimal add-in component\n"
92 " skeleton based on the configuration coming with the\n"
93 " office since OO.org 2.0.4.\n"
94 " -o <path> path specifies an existing directory where the\n"
95 " output files are generated to, only valid for\n"
96 " sub-command 'component'. If path=stdout the generated\n"
97 " code is generated on standard out instead of a file.\n"
98 " -l <file> specifies a binary type library (can be used more\n"
99 " than once). The type library is integrated as an\n"
100 " additional type provider in the bootstrapped type\n"
101 " system.\n"
102 " -n <name> specifies an implementation name for the component\n"
103 " (used as classname, filename and package|namespace\n"
104 " name). In 'dump' mode it is used as classname (e.g.\n"
105 " \"MyBase::\", C++ only) to generate method bodies not\n"
106 " inline.\n"
107 " -d <name> specifies a base classname or a delegator.\n"
108 " In 'dump' mode it is used as a delegator to forward\n"
109 " methods. It can be used as '<name>::' for base\n"
110 " forwarding, or '<name>->|.' for composition.\n"
111 " Using \"_\" means that a default bodies with default\n"
112 " return values are dumped.\n"
113 " -t <name> specifies an UNOIDL type name, e.g.\n"
114 " com.sun.star.text.XText (can be used more than once)\n"
115 " -p <protocol:cmd(s)> specifies an add-on protocol name and the corresponding\n"
116 " command names, where the commands are a ',' separated list\n"
117 " of unique commands. This option is only valid for add-ons.\n"
118 " -V, --version print version number and exit\n"
119 " -h, --help print this help and exit\n\n";
121 void printUsageAndExit(const char* programname, const char* version)
123 std::cerr
124 << "\n using: " << programname
125 << " (-env:INIFILENAME=<url> | -env:UNO_TYPES=<url>)\n"
126 << " dump [<options>] -t <type> ...\n"
127 << " " << programname
128 << " (-env:INIFILENAME=<url> | -env:UNO_TYPES=<url>)\n"
129 << " component [<options>] -n <name> -t <type> ...\n"
130 << " " << programname
131 << " (-env:INIFILENAME=<url> | -env:UNO_TYPES=<url>)\n"
132 << " calc-add-in [<options>] -n <name> -t <add-in_service>\n"
133 << " " << programname
134 << " (-env:INIFILENAME=<url> | -env:UNO_TYPES=<url>)\n"
135 << " add-on [<options>] -n <name> -p <protocol_name:command,...>\n"
136 << " " << programname << " -V, --version\n"
137 << " " << programname << " -h, --help\n"
138 << usageText
139 << programname << " Version " << version << "\n\n";
144 SAL_IMPLEMENT_MAIN()
146 const char* version = "0.4";
147 const char* programname = "uno-skeletonmaker";
149 sal_uInt32 nCount = rtl_getAppCommandArgCount();
150 if ( nCount == 0 ) {
151 printUsageAndExit(programname, version);
152 exit(EXIT_FAILURE);
155 ProgramOptions options;
156 std::vector< OUString > registries;
157 std::vector< OString > types;
158 OString delegate;
160 try {
162 sal_uInt32 nPos = 0;
163 OUString arg, sOption;
164 sal_Bool bOption=sal_False;
166 // check command
167 rtl_getAppCommandArg(nPos++, &arg.pData);
168 if ( arg == "dump" ) {
169 options.dump = true;
170 } else if ( arg == "component" ) {
171 options.dump = false;
172 options.shortnames = true;
173 } else if ( arg == "calc-add-in" ) {
174 options.dump = false;
175 options.shortnames = true;
176 options.componenttype = 2;
177 } else if ( arg == "add-on" ) {
178 options.dump = false;
179 options.shortnames = true;
180 options.componenttype = 3;
181 } else if ( readOption( &bOption, "h", &nPos, arg) ||
182 readOption( &bOption, "help", &nPos, arg) ) {
183 printUsageAndExit(programname, version);
184 exit(EXIT_SUCCESS);
185 } else if ( readOption( &bOption, "V", &nPos, arg) ||
186 readOption( &bOption, "version", &nPos, arg) ) {
187 std::cerr << "\n Sun Microsystems (R) " << programname
188 << " Version " << version << "\n\n";
189 exit(EXIT_SUCCESS);
190 } else {
191 std::cerr
192 << "ERROR: unexpected command \""
193 << OUStringToOString(arg, RTL_TEXTENCODING_UTF8).getStr()
194 << "\"!\n";
195 printUsageAndExit(programname, version);
196 exit(EXIT_FAILURE);
199 // read up to arguments
200 while ( nPos < nCount )
202 rtl_getAppCommandArg(nPos, &arg.pData);
204 if ( readOption( &bOption, "a", &nPos, arg) ||
205 readOption( &bOption, "all", &nPos, arg) ) {
206 options.all = true;
207 continue;
209 if ( readOption( &bOption, "java4", &nPos, arg) ) {
210 options.java5 = false;
211 options.language = 1;
212 continue;
214 if ( readOption( &bOption, "java5", &nPos, arg) ) {
215 options.java5 = true;
216 options.language = 1;
217 continue;
219 if ( readOption( &bOption, "cpp", &nPos, arg) ) {
220 options.java5 = false;
221 options.language = 2;
222 continue;
224 if ( readOption( &bOption, "sn", &nPos, arg) ||
225 readOption( &bOption, "shortnames", &nPos, arg) ) {
226 options.shortnames = true;
227 continue;
229 if ( readOption( &bOption, "lh", &nPos, arg) ||
230 readOption( &bOption, "licenseheader", &nPos, arg) ) {
231 options.license = true;
232 continue;
234 if ( readOption( &bOption, "bc", &nPos, arg) ||
235 readOption( &bOption, "backward-compatible", &nPos, arg) ) {
236 options.backwardcompatible = true;
237 continue;
239 if ( readOption( &bOption, "propertysetmixin", &nPos, arg) ) {
240 options.supportpropertysetmixin = true;
241 continue;
243 if ( readOption( &sOption, "d", &nPos, arg) ) {
244 delegate = OUStringToOString(sOption, RTL_TEXTENCODING_UTF8);
245 continue;
247 if ( readOption( &sOption, "n", &nPos, arg) ) {
248 options.implname = OUStringToOString(sOption, RTL_TEXTENCODING_UTF8);
249 continue;
251 if ( readOption( &sOption, "o", &nPos, arg) ) {
252 options.outputpath = OUStringToOString(sOption, RTL_TEXTENCODING_UTF8);
253 continue;
255 if ( readOption( &sOption, "l", &nPos, arg) ) {
256 registries.push_back(sOption);
257 continue;
259 if ( readOption( &sOption, "t", &nPos, arg) ) {
260 types.push_back(OUStringToOString(sOption, RTL_TEXTENCODING_UTF8));
261 continue;
263 if ( readOption( &sOption, "p", &nPos, arg) ) {
264 OString sTmp(OUStringToOString(sOption, RTL_TEXTENCODING_UTF8));
265 sal_Int32 nIndex= sTmp.indexOf(':');
266 OString sPrt = sTmp.copy(0, nIndex+1);
267 OString sCmds = sTmp.copy(nIndex+1);
269 nIndex = 0;
270 std::vector< OString > vCmds;
271 do {
272 OString sCmd = sCmds.getToken( 0, ',', nIndex );
273 vCmds.push_back(sCmd);
274 } while ( nIndex >= 0 );
276 options.protocolCmdMap.insert(ProtocolCmdMap::value_type(sPrt, vCmds));
277 continue;
281 // else illegal argument
282 OUStringBuffer buf( 64 );
283 buf.appendAscii(RTL_CONSTASCII_STRINGPARAM("unexpected parameter \""));
284 buf.append(arg);
285 buf.appendAscii(RTL_CONSTASCII_STRINGPARAM("\"!"));
286 throw RuntimeException(buf.makeStringAndClear(),
287 Reference< XInterface >());
290 if ( types.empty() && options.componenttype != 3) {
291 std::cerr
292 << ("\nError: no type is specified, use the -T option at least once\n");
293 printUsageAndExit(programname, version);
294 exit(EXIT_FAILURE);
297 UnoTypeManager manager;
298 if ( !manager.init(registries) ) {
299 std::cerr
300 << ("\nError: Using the binary type libraries failed, check the -L"
301 " options\n");
302 exit(EXIT_FAILURE);
305 if ( options.dump ) {
306 std::vector< OString >::const_iterator iter = types.begin();
307 while (iter != types.end()) {
308 std::cout << "\n/***************************************************"
309 "*****************************/\n";
310 switch (options.language )
312 case 1: //Java
313 java::generateDocumentation(std::cout, options, manager,
314 *iter, delegate);
315 break;
316 case 2: //C++
317 cpp::generateDocumentation(std::cout, options, manager,
318 *iter, delegate);
319 break;
320 default:
321 OSL_ASSERT(false);
322 break;
324 ++iter;
326 } else {
327 switch ( options.language )
329 case 1: //Java
330 java::generateSkeleton(options, manager, types);
331 break;
332 case 2: //C++
333 cpp::generateSkeleton(options, manager, types);
334 break;
335 default:
336 OSL_ASSERT(false);
337 break;
341 } catch (const CannotDumpException & e) {
342 std::cout.flush();
343 std::cerr << "\nError: " << e.m_message << std::endl;
344 } catch(const Exception& e) {
345 std::cout.flush();
346 std::cerr
347 << "\nError: "
348 << OUStringToOString(e.Message, RTL_TEXTENCODING_UTF8).getStr()
349 << std::endl;
352 return 0;
355 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */