Branch data Line data Source code
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 : : #include <iostream>
20 : :
21 : : #include "sal/main.h"
22 : : #include "rtl/process.h"
23 : : #include "rtl/ustrbuf.hxx"
24 : : #include "unodevtools/typemanager.hxx"
25 : : #include "unodevtools/options.hxx"
26 : : #include "ostringostreaminserter.hxx"
27 : : #include "skeletonjava.hxx"
28 : : #include "skeletoncpp.hxx"
29 : :
30 : : #include "com/sun/star/uno/Reference.hxx"
31 : :
32 : : using namespace ::rtl;
33 : : using namespace ::skeletonmaker;
34 : : using namespace ::unodevtools;
35 : : using namespace ::com::sun::star::uno;
36 : :
37 : : namespace {
38 : :
39 : : static const char usageText[] =
40 : : "\n sub-commands:\n"
41 : : " dump dump declarations on stdout (e.g. constructors, methods, type\n"
42 : : " mapping for properties) or complete method bodies with\n"
43 : : " method forwarding.\n"
44 : : " component generates language specific code skeleton files using the\n"
45 : : " implementation name as the file and class name\n"
46 : : " calc-add-in generates a language specific code skeleton for a calc add-in\n"
47 : : " using the implementation name as the file and class name. A \n"
48 : : " service type is necessary, referencing an interface which defines\n"
49 : : " the new add-in functions.\n"
50 : : " add-on generates a language specific code skeleton for an add-on compnent\n"
51 : : " using the implementation name as the file and class name. The protocol\n"
52 : : " name(s) and the corresponding command(s) have to be specified with the\n"
53 : : " '-p' option.\n"
54 : : "\n options:\n"
55 : : " -env:INIFILENAME=<url> url specifies a URL to an UNO ini|rc file of an\n"
56 : : " existing UNO environment (URE, office installation).\n"
57 : : " -env:UNO_TYPES=<url> url specifies a binary type library file. It can be\n"
58 : : " a space separated list of urls.\n"
59 : : " -a, --all list all interface methods, not only the direct\n"
60 : : " ones\n"
61 : : " --(java4|java5|cpp) select the target language\n"
62 : : " --java4 generate output for Java 1.4 or earlier\n"
63 : : " --java5 generate output for Java 1.5 or later (is \n"
64 : : " currently the default)\n"
65 : : " --cpp generate output for C++\n"
66 : : " -sn, --shortnames using namespace abbreviation 'css:': for\n"
67 : : " '::com::sun::star::', only valid for sub-command\n"
68 : : " 'dump' and target language 'cpp'. It is default for the\n"
69 : : " sub-command 'component'.\n"
70 : : " --propertysetmixin the generated skeleton implements the cppu::PropertySetMixin\n"
71 : : " helper if a referenced new style service specifies an\n"
72 : : " interface which provides attributes (directly or inherited).\n"
73 : : " -lh --licenseheader generates a default LibreOffice MPL license\n"
74 : : " header at the beginning of a component source file.\n"
75 : : " This option is taken into account in 'component' mode\n"
76 : : " only and if -o is unequal 'stdout'.\n"
77 : : " -bc specifies that the generated calc add-in is backward\n"
78 : : " --backward-compatible compatible to older office versions and implement the\n"
79 : : " former required add-in interfaces where the implementation\n"
80 : : " is mapped on the new add-in configuration. In this case\n"
81 : : " the config schema needs to be bundled with the extension\n"
82 : : " add-in as well. Default is a minimal add-in component\n"
83 : : " skeleton based on the configuration coming with the\n"
84 : : " office since OO.org 2.0.4.\n"
85 : : " -o <path> path specifies an existing directory where the\n"
86 : : " output files are generated to, only valid for\n"
87 : : " sub-command 'component'. If path=stdout the generated\n"
88 : : " code is generated on standard out instead of a file.\n"
89 : : " -l <file> specifies a binary type library (can be used more\n"
90 : : " than once). The type library is integrated as an\n"
91 : : " additional type provider in the bootstrapped type\n"
92 : : " system.\n"
93 : : " -n <name> specifies an implementation name for the component\n"
94 : : " (used as classname, filename and package|namespace\n"
95 : : " name). In 'dump' mode it is used as classname (e.g.\n"
96 : : " \"MyBase::\", C++ only) to generate method bodies not\n"
97 : : " inline.\n"
98 : : " -d <name> specifies a base classname or a delegator.\n"
99 : : " In 'dump' mode it is used as a delegator to forward\n"
100 : : " methods. It can be used as '<name>::' for base\n"
101 : : " forwarding, or '<name>->|.' for composition.\n"
102 : : " Using \"_\" means that a default bodies with default\n"
103 : : " return values are dumped.\n"
104 : : " -t <name> specifies an UNOIDL type name, e.g.\n"
105 : : " com.sun.star.text.XText (can be used more than once)\n"
106 : : " -p <protocol:cmd(s)> specifies an add-on protocol name and the corresponding\n"
107 : : " command names, where the commands are a ',' separated list\n"
108 : : " of unique commands. This option is only valid for add-ons.\n"
109 : : " -V, --version print version number and exit\n"
110 : : " -h, --help print this help and exit\n\n";
111 : :
112 : 0 : void printUsageAndExit(const char* programname, const char* version)
113 : : {
114 : : std::cerr
115 : 0 : << "\n using: " << programname
116 : 0 : << " (-env:INIFILENAME=<url> | -env:UNO_TYPES=<url>)\n"
117 : 0 : << " dump [<options>] -t <type> ...\n"
118 : 0 : << " " << programname
119 : 0 : << " (-env:INIFILENAME=<url> | -env:UNO_TYPES=<url>)\n"
120 : 0 : << " component [<options>] -n <name> -t <type> ...\n"
121 : 0 : << " " << programname
122 : 0 : << " (-env:INIFILENAME=<url> | -env:UNO_TYPES=<url>)\n"
123 : 0 : << " calc-add-in [<options>] -n <name> -t <add-in_service>\n"
124 : 0 : << " " << programname
125 : 0 : << " (-env:INIFILENAME=<url> | -env:UNO_TYPES=<url>)\n"
126 : 0 : << " add-on [<options>] -n <name> -p <protocol_name:command,...>\n"
127 : 0 : << " " << programname << " -V, --version\n"
128 : 0 : << " " << programname << " -h, --help\n"
129 : 0 : << usageText
130 : 0 : << programname << " Version " << version << "\n\n";
131 : 0 : }
132 : :
133 : : }
134 : :
135 : 0 : SAL_IMPLEMENT_MAIN()
136 : : {
137 : 0 : const char* version = "0.4";
138 : 0 : const char* programname = "uno-skeletonmaker";
139 : :
140 : 0 : sal_uInt32 nCount = rtl_getAppCommandArgCount();
141 : 0 : if ( nCount == 0 ) {
142 : 0 : printUsageAndExit(programname, version);
143 : 0 : exit(EXIT_FAILURE);
144 : : }
145 : :
146 : 0 : ProgramOptions options;
147 : 0 : std::vector< OUString > registries;
148 : 0 : std::vector< OString > types;
149 : 0 : OString delegate;
150 : :
151 : : try {
152 : :
153 : 0 : sal_uInt32 nPos = 0;
154 : 0 : OUString arg, sOption;
155 : 0 : sal_Bool bOption=sal_False;
156 : :
157 : : // check command
158 : 0 : rtl_getAppCommandArg(nPos++, &arg.pData);
159 : 0 : if ( arg == "dump" ) {
160 : 0 : options.dump = true;
161 : 0 : } else if ( arg == "component" ) {
162 : 0 : options.dump = false;
163 : 0 : options.shortnames = true;
164 : 0 : } else if ( arg == "calc-add-in" ) {
165 : 0 : options.dump = false;
166 : 0 : options.shortnames = true;
167 : 0 : options.componenttype = 2;
168 : 0 : } else if ( arg == "add-on" ) {
169 : 0 : options.dump = false;
170 : 0 : options.shortnames = true;
171 : 0 : options.componenttype = 3;
172 : 0 : } else if ( readOption( &bOption, "h", &nPos, arg) ||
173 : 0 : readOption( &bOption, "help", &nPos, arg) ) {
174 : 0 : printUsageAndExit(programname, version);
175 : 0 : exit(EXIT_SUCCESS);
176 : 0 : } else if ( readOption( &bOption, "V", &nPos, arg) ||
177 : 0 : readOption( &bOption, "version", &nPos, arg) ) {
178 : 0 : std::cerr << "\n Sun Microsystems (R) " << programname
179 : 0 : << " Version " << version << "\n\n";
180 : 0 : exit(EXIT_SUCCESS);
181 : : } else {
182 : : std::cerr
183 : 0 : << "ERROR: unexpected command \""
184 : 0 : << OUStringToOString(arg, RTL_TEXTENCODING_UTF8).getStr()
185 : 0 : << "\"!\n";
186 : 0 : printUsageAndExit(programname, version);
187 : 0 : exit(EXIT_FAILURE);
188 : : }
189 : :
190 : : // read up to arguments
191 : 0 : while ( nPos < nCount )
192 : : {
193 : 0 : rtl_getAppCommandArg(nPos, &arg.pData);
194 : :
195 : 0 : if ( readOption( &bOption, "a", &nPos, arg) ||
196 : 0 : readOption( &bOption, "all", &nPos, arg) ) {
197 : 0 : options.all = true;
198 : 0 : continue;
199 : : }
200 : 0 : if ( readOption( &bOption, "java4", &nPos, arg) ) {
201 : 0 : options.java5 = false;
202 : 0 : options.language = 1;
203 : 0 : continue;
204 : : }
205 : 0 : if ( readOption( &bOption, "java5", &nPos, arg) ) {
206 : 0 : options.java5 = true;
207 : 0 : options.language = 1;
208 : 0 : continue;
209 : : }
210 : 0 : if ( readOption( &bOption, "cpp", &nPos, arg) ) {
211 : 0 : options.java5 = false;
212 : 0 : options.language = 2;
213 : 0 : continue;
214 : : }
215 : 0 : if ( readOption( &bOption, "sn", &nPos, arg) ||
216 : 0 : readOption( &bOption, "shortnames", &nPos, arg) ) {
217 : 0 : options.shortnames = true;
218 : 0 : continue;
219 : : }
220 : 0 : if ( readOption( &bOption, "lh", &nPos, arg) ||
221 : 0 : readOption( &bOption, "licenseheader", &nPos, arg) ) {
222 : 0 : options.license = true;
223 : 0 : continue;
224 : : }
225 : 0 : if ( readOption( &bOption, "bc", &nPos, arg) ||
226 : 0 : readOption( &bOption, "backward-compatible", &nPos, arg) ) {
227 : 0 : options.backwardcompatible = true;
228 : 0 : continue;
229 : : }
230 : 0 : if ( readOption( &bOption, "propertysetmixin", &nPos, arg) ) {
231 : 0 : options.supportpropertysetmixin = true;
232 : 0 : continue;
233 : : }
234 : 0 : if ( readOption( &sOption, "d", &nPos, arg) ) {
235 : 0 : delegate = OUStringToOString(sOption, RTL_TEXTENCODING_UTF8);
236 : 0 : continue;
237 : : }
238 : 0 : if ( readOption( &sOption, "n", &nPos, arg) ) {
239 : 0 : options.implname = OUStringToOString(sOption, RTL_TEXTENCODING_UTF8);
240 : 0 : continue;
241 : : }
242 : 0 : if ( readOption( &sOption, "o", &nPos, arg) ) {
243 : 0 : options.outputpath = OUStringToOString(sOption, RTL_TEXTENCODING_UTF8);
244 : 0 : continue;
245 : : }
246 : 0 : if ( readOption( &sOption, "l", &nPos, arg) ) {
247 : 0 : registries.push_back(sOption);
248 : 0 : continue;
249 : : }
250 : 0 : if ( readOption( &sOption, "t", &nPos, arg) ) {
251 : 0 : types.push_back(OUStringToOString(sOption, RTL_TEXTENCODING_UTF8));
252 : 0 : continue;
253 : : }
254 : 0 : if ( readOption( &sOption, "p", &nPos, arg) ) {
255 : 0 : OString sTmp(OUStringToOString(sOption, RTL_TEXTENCODING_UTF8));
256 : 0 : sal_Int32 nIndex= sTmp.indexOf(':');
257 : 0 : OString sPrt = sTmp.copy(0, nIndex+1);
258 : 0 : OString sCmds = sTmp.copy(nIndex+1);
259 : :
260 : 0 : nIndex = 0;
261 : 0 : std::vector< OString > vCmds;
262 : 0 : do {
263 : 0 : OString sCmd = sCmds.getToken( 0, ',', nIndex );
264 : 0 : vCmds.push_back(sCmd);
265 : : } while ( nIndex >= 0 );
266 : :
267 : 0 : options.protocolCmdMap.insert(ProtocolCmdMap::value_type(sPrt, vCmds));
268 : 0 : continue;
269 : : }
270 : :
271 : :
272 : : // else illegal argument
273 : 0 : OUStringBuffer buf( 64 );
274 : 0 : buf.appendAscii(RTL_CONSTASCII_STRINGPARAM("unexpected parameter \""));
275 : 0 : buf.append(arg);
276 : 0 : buf.appendAscii(RTL_CONSTASCII_STRINGPARAM("\"!"));
277 : : throw RuntimeException(buf.makeStringAndClear(),
278 : 0 : Reference< XInterface >());
279 : 0 : }
280 : :
281 : 0 : if ( types.empty() && options.componenttype != 3) {
282 : : std::cerr
283 : 0 : << ("\nError: no type is specified, use the -T option at least once\n");
284 : 0 : printUsageAndExit(programname, version);
285 : 0 : exit(EXIT_FAILURE);
286 : : }
287 : :
288 : 0 : UnoTypeManager manager;
289 : 0 : if ( !manager.init(registries) ) {
290 : : std::cerr
291 : : << ("\nError: Using the binary type libraries failed, check the -L"
292 : 0 : " options\n");
293 : 0 : exit(EXIT_FAILURE);
294 : : }
295 : :
296 : 0 : if ( options.dump ) {
297 : 0 : std::vector< OString >::const_iterator iter = types.begin();
298 : 0 : while (iter != types.end()) {
299 : : std::cout << "\n/***************************************************"
300 : 0 : "*****************************/\n";
301 : 0 : switch (options.language )
302 : : {
303 : : case 1: //Java
304 : : java::generateDocumentation(std::cout, options, manager,
305 : 0 : *iter, delegate);
306 : 0 : break;
307 : : case 2: //C++
308 : : cpp::generateDocumentation(std::cout, options, manager,
309 : 0 : *iter, delegate);
310 : 0 : break;
311 : : default:
312 : : OSL_ASSERT(false);
313 : 0 : break;
314 : : }
315 : 0 : ++iter;
316 : : }
317 : : } else {
318 : 0 : switch ( options.language )
319 : : {
320 : : case 1: //Java
321 : 0 : java::generateSkeleton(options, manager, types);
322 : 0 : break;
323 : : case 2: //C++
324 : 0 : cpp::generateSkeleton(options, manager, types);
325 : 0 : break;
326 : : default:
327 : : OSL_ASSERT(false);
328 : 0 : break;
329 : : }
330 : 0 : }
331 : :
332 : 0 : } catch (const CannotDumpException & e) {
333 : 0 : std::cout.flush();
334 : 0 : std::cerr << "\nError: " << e.m_message << std::endl;
335 : 0 : } catch(const Exception& e) {
336 : 0 : std::cout.flush();
337 : : std::cerr
338 : 0 : << "\nError: "
339 : 0 : << OUStringToOString(e.Message, RTL_TEXTENCODING_UTF8).getStr()
340 : 0 : << std::endl;
341 : : }
342 : :
343 : 0 : return 0;
344 : 0 : }
345 : :
346 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|