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