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