Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / unodevtools / source / skeletonmaker / skeletonmaker.cxx
blobc193df6de267e45a8fc7b93e2ceac91388409e31
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
19 #include <iostream>
21 #include "codemaker/global.hxx"
22 #include "codemaker/typemanager.hxx"
23 #include "sal/main.h"
24 #include "rtl/process.h"
25 #include "unodevtools/options.hxx"
26 #include "unoidl/unoidl.hxx"
28 #include "skeletonjava.hxx"
29 #include "skeletoncpp.hxx"
31 using namespace ::skeletonmaker;
32 using namespace ::unodevtools;
34 namespace {
36 static const char usageText[] =
37 "\n sub-commands:\n"
38 " dump dump declarations on stdout (e.g. constructors, methods, type\n"
39 " mapping for properties) or complete method bodies with\n"
40 " method forwarding.\n"
41 " component generates language specific code skeleton files using the\n"
42 " implementation name as the file and class name\n"
43 " calc-add-in generates a language specific code skeleton for a calc add-in\n"
44 " using the implementation name as the file and class name. A \n"
45 " service type is necessary, referencing an interface which defines\n"
46 " the new add-in functions.\n"
47 " add-on generates a language specific code skeleton for an add-on component\n"
48 " using the implementation name as the file and class name. The protocol\n"
49 " name(s) and the corresponding command(s) have to be specified with the\n"
50 " '-p' option.\n"
51 "\n options:\n"
52 " -a, --all list all interface methods, not only the direct\n"
53 " ones\n"
54 " --(java5|cpp) select the target language\n"
55 " --java5 generate output for Java 1.5 or later (is \n"
56 " currently the default)\n"
57 " --cpp generate output for C++\n"
58 " -sn, --shortnames using namespace abbreviation 'css:': for\n"
59 " '::com::sun::star::', only valid for sub-command\n"
60 " 'dump' and target language 'cpp'. It is default for the\n"
61 " sub-command 'component'.\n"
62 " --propertysetmixin the generated skeleton implements the cppu::PropertySetMixin\n"
63 " helper if a referenced new style service specifies an\n"
64 " interface which provides attributes (directly or inherited).\n"
65 " -lh --licenseheader generates a default LibreOffice MPL license\n"
66 " header at the beginning of a component source file.\n"
67 " This option is taken into account in 'component' mode\n"
68 " only and if -o is unequal 'stdout'.\n"
69 " -bc specifies that the generated calc add-in is backward\n"
70 " --backward-compatible compatible to older office versions and implement the\n"
71 " former required add-in interfaces where the implementation\n"
72 " is mapped on the new add-in configuration. In this case\n"
73 " the config schema needs to be bundled with the extension\n"
74 " add-in as well. Default is a minimal add-in component\n"
75 " skeleton based on the configuration coming with the\n"
76 " office since OO.org 2.0.4.\n"
77 " -o <path> path specifies an existing directory where the\n"
78 " output files are generated to, only valid for\n"
79 " sub-command 'component'. If path=stdout the generated\n"
80 " code is generated on standard out instead of a file.\n"
81 " -l <file> specifies a binary type library (can be used more\n"
82 " than once).\n"
83 " -n <name> specifies an implementation name for the component\n"
84 " (used as classname, filename and package|namespace\n"
85 " name). In 'dump' mode it is used as classname (e.g.\n"
86 " \"MyBase::\", C++ only) to generate method bodies not\n"
87 " inline.\n"
88 " -d <name> specifies a base classname or a delegator.\n"
89 " In 'dump' mode it is used as a delegator to forward\n"
90 " methods. It can be used as '<name>::' for base\n"
91 " forwarding, or '<name>->|.' for composition.\n"
92 " Using \"_\" means that a default bodies with default\n"
93 " return values are dumped.\n"
94 " -t <name> specifies an UNOIDL type name, e.g.\n"
95 " com.sun.star.text.XText (can be used more than once)\n"
96 " -p <protocol:cmd(s)> specifies an add-on protocol name and the corresponding\n"
97 " command names, where the commands are a ',' separated list\n"
98 " of unique commands. This option is only valid for add-ons.\n"
99 " -V, --version print version number and exit\n"
100 " -h, --help print this help and exit\n\n";
102 void printUsageAndExit(const char* programname, const char* version)
104 std::cerr
105 << "\n using: " << programname << "\n"
106 " dump [<options>] -t <type> ...\n"
107 " " << programname << "\n"
108 " component [<options>] -n <name> -t <type> ...\n"
109 " " << programname << "\n"
110 " calc-add-in [<options>] -n <name> -t <add-in_service>\n"
111 " " << programname << "\n"
112 " add-on [<options>] -n <name> -p <protocol_name:command,...>\n"
113 " " << programname << " -V, --version\n"
114 " " << programname << " -h, --help\n"
115 << usageText
116 << programname << " Version " << version << "\n\n";
121 SAL_IMPLEMENT_MAIN()
123 const char* version = "0.4";
124 const char* programname = "uno-skeletonmaker";
126 sal_uInt32 nCount = rtl_getAppCommandArgCount();
127 if ( nCount == 0 ) {
128 printUsageAndExit(programname, version);
129 exit(EXIT_FAILURE);
132 ProgramOptions options;
133 std::vector< OString > registries;
134 std::vector< OString > types;
135 OString delegate;
137 try {
139 sal_uInt32 nPos = 0;
140 OUString arg, sOption;
142 // check command
143 rtl_getAppCommandArg(nPos++, &arg.pData);
144 if ( arg == "dump" ) {
145 options.dump = true;
146 } else if ( arg == "component" ) {
147 options.dump = false;
148 options.shortnames = true;
149 } else if ( arg == "calc-add-in" ) {
150 options.dump = false;
151 options.shortnames = true;
152 options.componenttype = 2;
153 } else if ( arg == "add-on" ) {
154 options.dump = false;
155 options.shortnames = true;
156 options.componenttype = 3;
157 } else if ( readOption( "h", &nPos, arg) ||
158 readOption( "help", &nPos, arg) ) {
159 printUsageAndExit(programname, version);
160 exit(EXIT_SUCCESS);
161 } else if ( readOption( "V", &nPos, arg) ||
162 readOption( "version", &nPos, arg) ) {
163 std::cerr << "\n Sun Microsystems (R) " << programname
164 << " Version " << version << "\n\n";
165 exit(EXIT_SUCCESS);
166 } else {
167 std::cerr
168 << "ERROR: unexpected command \""
169 << OUStringToOString(arg, RTL_TEXTENCODING_UTF8).getStr()
170 << "\"!\n";
171 printUsageAndExit(programname, version);
172 exit(EXIT_FAILURE);
175 // read up to arguments
176 while ( nPos < nCount )
178 rtl_getAppCommandArg(nPos, &arg.pData);
180 if ( readOption( "a", &nPos, arg) ||
181 readOption( "all", &nPos, arg) ) {
182 options.all = true;
183 continue;
185 if ( readOption( "java4", &nPos, arg) ) {
186 std::cerr <<
187 "\nError: Java 1.4 is no longer supported, use --java5 instead\n";
189 if ( readOption( "java5", &nPos, arg) ) {
190 options.language = 1;
191 continue;
193 if ( readOption( "cpp", &nPos, arg) ) {
194 options.language = 2;
195 continue;
197 if ( readOption( "sn", &nPos, arg) ||
198 readOption( "shortnames", &nPos, arg) ) {
199 options.shortnames = true;
200 continue;
202 if ( readOption( "lh", &nPos, arg) ||
203 readOption( "licenseheader", &nPos, arg) ) {
204 options.license = true;
205 continue;
207 if ( readOption( "bc", &nPos, arg) ||
208 readOption( "backward-compatible", &nPos, arg) ) {
209 options.backwardcompatible = true;
210 continue;
212 if ( readOption( "propertysetmixin", &nPos, arg) ) {
213 options.supportpropertysetmixin = true;
214 continue;
216 if ( readOption( &sOption, "d", &nPos, arg) ) {
217 delegate = OUStringToOString(sOption, RTL_TEXTENCODING_UTF8);
218 continue;
220 if ( readOption( &sOption, "n", &nPos, arg) ) {
221 options.implname = OUStringToOString(sOption, RTL_TEXTENCODING_UTF8);
222 continue;
224 if ( readOption( &sOption, "o", &nPos, arg) ) {
225 options.outputpath = OUStringToOString(sOption, RTL_TEXTENCODING_UTF8);
226 continue;
228 if ( readOption( &sOption, "l", &nPos, arg) ) {
229 registries.push_back(OUStringToOString(sOption, RTL_TEXTENCODING_UTF8));
230 continue;
232 if ( readOption( &sOption, "t", &nPos, arg) ) {
233 types.push_back(OUStringToOString(sOption, RTL_TEXTENCODING_UTF8));
234 continue;
236 if ( readOption( &sOption, "p", &nPos, arg) ) {
237 OString sTmp(OUStringToOString(sOption, RTL_TEXTENCODING_UTF8));
238 sal_Int32 nIndex= sTmp.indexOf(':');
239 OString sPrt = sTmp.copy(0, nIndex+1);
240 OString sCmds = sTmp.copy(nIndex+1);
242 nIndex = 0;
243 std::vector< OString > vCmds;
244 do {
245 OString sCmd = sCmds.getToken( 0, ',', nIndex );
246 vCmds.push_back(sCmd);
247 } while ( nIndex >= 0 );
249 options.protocolCmdMap.insert(ProtocolCmdMap::value_type(sPrt, vCmds));
250 continue;
254 // else illegal argument
255 throw CannotDumpException("unexpected parameter \"" + arg + "\"!");
258 if ( types.empty() && options.componenttype != 3) {
259 std::cerr
260 << ("\nError: no type is specified, use the -T option at least once\n");
261 printUsageAndExit(programname, version);
262 exit(EXIT_FAILURE);
265 rtl::Reference< TypeManager > manager(new TypeManager);
266 for (std::vector< OString >::const_iterator i(registries.begin());
267 i != registries.end(); ++i)
269 manager->loadProvider(convertToFileUrl(*i), true);
272 if ( options.dump ) {
273 std::vector< OString >::const_iterator iter = types.begin();
274 while (iter != types.end()) {
275 std::cout << "\n/***************************************************"
276 "*****************************/\n";
277 switch (options.language )
279 case 1: //Java
280 java::generateDocumentation(std::cout, options, manager,
281 *iter, delegate);
282 break;
283 case 2: //C++
284 cpp::generateDocumentation(std::cout, options, manager,
285 *iter, delegate);
286 break;
287 default:
288 OSL_ASSERT(false);
289 break;
291 ++iter;
293 } else {
294 switch ( options.language )
296 case 1: //Java
297 java::generateSkeleton(options, manager, types);
298 break;
299 case 2: //C++
300 cpp::generateSkeleton(options, manager, types);
301 break;
302 default:
303 OSL_ASSERT(false);
304 break;
308 } catch (CannotDumpException & e) {
309 std::cerr << "ERROR: " << e.getMessage() << '\n';
310 return EXIT_FAILURE;
311 } catch (unoidl::NoSuchFileException & e) {
312 std::cerr << "ERROR: No such file <" << e.getUri() << ">\n";
313 return EXIT_FAILURE;
314 } catch (unoidl::FileFormatException & e) {
315 std::cerr
316 << "ERROR: Bad format of <" << e.getUri() << ">, \""
317 << e.getDetail() << "\"\n";
318 return EXIT_FAILURE;
319 } catch (std::exception & e) {
320 std::cerr << "ERROR: " << e.what() << "\n";
321 return EXIT_FAILURE;
324 return 0;
327 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */