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