1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_codemaker.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <stdio.h>
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir #include "sal/main.h"
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir #include "codemaker/typemanager.hxx"
36*cdf0e10cSrcweir #include "codemaker/generatedtypeset.hxx"
37*cdf0e10cSrcweir #include "javaoptions.hxx"
38*cdf0e10cSrcweir #include "javatype.hxx"
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir using namespace rtl;
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir sal_Bool produceAllTypes(RegistryKey& rTypeKey, sal_Bool bIsExtraType,
43*cdf0e10cSrcweir 						 TypeManager const & typeMgr,
44*cdf0e10cSrcweir                          codemaker::GeneratedTypeSet & generated,
45*cdf0e10cSrcweir 						 JavaOptions* pOptions,
46*cdf0e10cSrcweir 						 sal_Bool bFullScope)
47*cdf0e10cSrcweir 	throw( CannotDumpException )
48*cdf0e10cSrcweir {
49*cdf0e10cSrcweir     OString typeName = typeMgr.getTypeName(rTypeKey);
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir 	if (!produceType(rTypeKey, bIsExtraType, typeMgr, generated, pOptions))
52*cdf0e10cSrcweir 	{
53*cdf0e10cSrcweir 		fprintf(stderr, "%s ERROR: %s\n",
54*cdf0e10cSrcweir 				pOptions->getProgramName().getStr(),
55*cdf0e10cSrcweir 				OString("cannot dump Type '" + typeName + "'").getStr());
56*cdf0e10cSrcweir 		exit(99);
57*cdf0e10cSrcweir 	}
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir     RegistryKeyList typeKeys = typeMgr.getTypeKeys(typeName);
60*cdf0e10cSrcweir 	RegistryKeyList::const_iterator iter = typeKeys.begin();
61*cdf0e10cSrcweir     RegistryKey key, subKey;
62*cdf0e10cSrcweir     RegistryKeyArray subKeys;
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir 	while (iter != typeKeys.end())
65*cdf0e10cSrcweir 	{
66*cdf0e10cSrcweir         key = (*iter).first;
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir         if (!(*iter).second  && !key.openSubKeys(OUString(), subKeys))
69*cdf0e10cSrcweir         {
70*cdf0e10cSrcweir             for (sal_uInt32 i = 0; i < subKeys.getLength(); i++)
71*cdf0e10cSrcweir             {
72*cdf0e10cSrcweir                 subKey = subKeys.getElement(i);
73*cdf0e10cSrcweir                 if (bFullScope)
74*cdf0e10cSrcweir                 {
75*cdf0e10cSrcweir                     if (!produceAllTypes(
76*cdf0e10cSrcweir                             subKey, (*iter).second,
77*cdf0e10cSrcweir                             typeMgr, generated, pOptions, sal_True))
78*cdf0e10cSrcweir                         return sal_False;
79*cdf0e10cSrcweir                 } else
80*cdf0e10cSrcweir                 {
81*cdf0e10cSrcweir                     if (!produceType(subKey, (*iter).second,
82*cdf0e10cSrcweir                                      typeMgr, generated, pOptions))
83*cdf0e10cSrcweir                         return sal_False;
84*cdf0e10cSrcweir                 }
85*cdf0e10cSrcweir             }
86*cdf0e10cSrcweir         }
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir         ++iter;
89*cdf0e10cSrcweir 	}
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir 	return sal_True;
92*cdf0e10cSrcweir }
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir sal_Bool produceAllTypes(const OString& typeName,
95*cdf0e10cSrcweir 						 TypeManager const & typeMgr,
96*cdf0e10cSrcweir                          codemaker::GeneratedTypeSet & generated,
97*cdf0e10cSrcweir 						 JavaOptions* pOptions,
98*cdf0e10cSrcweir 						 sal_Bool bFullScope)
99*cdf0e10cSrcweir 	throw( CannotDumpException )
100*cdf0e10cSrcweir {
101*cdf0e10cSrcweir 	if (!produceType(typeName, typeMgr,	generated, pOptions))
102*cdf0e10cSrcweir 	{
103*cdf0e10cSrcweir 		fprintf(stderr, "%s ERROR: %s\n",
104*cdf0e10cSrcweir 				pOptions->getProgramName().getStr(),
105*cdf0e10cSrcweir 				OString("cannot dump Type '" + typeName + "'").getStr());
106*cdf0e10cSrcweir 		exit(99);
107*cdf0e10cSrcweir 	}
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir     RegistryKeyList typeKeys = typeMgr.getTypeKeys(typeName);
110*cdf0e10cSrcweir 	RegistryKeyList::const_iterator iter = typeKeys.begin();
111*cdf0e10cSrcweir     RegistryKey key, subKey;
112*cdf0e10cSrcweir     RegistryKeyArray subKeys;
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir 	while (iter != typeKeys.end())
115*cdf0e10cSrcweir 	{
116*cdf0e10cSrcweir         key = (*iter).first;
117*cdf0e10cSrcweir         if (!(*iter).second  && !key.openSubKeys(OUString(), subKeys))
118*cdf0e10cSrcweir         {
119*cdf0e10cSrcweir             for (sal_uInt32 i = 0; i < subKeys.getLength(); i++)
120*cdf0e10cSrcweir             {
121*cdf0e10cSrcweir                 subKey = subKeys.getElement(i);
122*cdf0e10cSrcweir                 if (bFullScope)
123*cdf0e10cSrcweir                 {
124*cdf0e10cSrcweir                     if (!produceAllTypes(
125*cdf0e10cSrcweir                             subKey, (*iter).second,
126*cdf0e10cSrcweir                             typeMgr, generated, pOptions, sal_True))
127*cdf0e10cSrcweir                         return sal_False;
128*cdf0e10cSrcweir                 } else
129*cdf0e10cSrcweir                 {
130*cdf0e10cSrcweir                     if (!produceType(subKey, (*iter).second,
131*cdf0e10cSrcweir                                      typeMgr, generated, pOptions))
132*cdf0e10cSrcweir                         return sal_False;
133*cdf0e10cSrcweir                 }
134*cdf0e10cSrcweir             }
135*cdf0e10cSrcweir         }
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir         ++iter;
138*cdf0e10cSrcweir 	}
139*cdf0e10cSrcweir 
140*cdf0e10cSrcweir 	return sal_True;
141*cdf0e10cSrcweir }
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir SAL_IMPLEMENT_MAIN_WITH_ARGS(argc, argv)
144*cdf0e10cSrcweir {
145*cdf0e10cSrcweir 	JavaOptions options;
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir 	try
148*cdf0e10cSrcweir 	{
149*cdf0e10cSrcweir 		if (!options.initOptions(argc, argv))
150*cdf0e10cSrcweir 		{
151*cdf0e10cSrcweir 			exit(1);
152*cdf0e10cSrcweir 		}
153*cdf0e10cSrcweir 	}
154*cdf0e10cSrcweir 	catch( IllegalArgument& e)
155*cdf0e10cSrcweir 	{
156*cdf0e10cSrcweir 		fprintf(stderr, "Illegal option: %s\n", e.m_message.getStr());
157*cdf0e10cSrcweir 		exit(99);
158*cdf0e10cSrcweir 	}
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir 	RegistryTypeManager typeMgr;
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir 	if (!typeMgr.init(options.getInputFiles(), options.getExtraInputFiles()))
163*cdf0e10cSrcweir 	{
164*cdf0e10cSrcweir 		fprintf(stderr, "%s : init registries failed, check your registry files.\n", options.getProgramName().getStr());
165*cdf0e10cSrcweir 		exit(99);
166*cdf0e10cSrcweir 	}
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir 	if (options.isValid("-B"))
169*cdf0e10cSrcweir 	{
170*cdf0e10cSrcweir 		typeMgr.setBase(options.getOption("-B"));
171*cdf0e10cSrcweir 	}
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir 	try
174*cdf0e10cSrcweir 	{
175*cdf0e10cSrcweir 		if (options.isValid("-T"))
176*cdf0e10cSrcweir 		{
177*cdf0e10cSrcweir 			OString tOption(options.getOption("-T"));
178*cdf0e10cSrcweir             sal_Int32 nIndex = 0;
179*cdf0e10cSrcweir 
180*cdf0e10cSrcweir             codemaker::GeneratedTypeSet generated;
181*cdf0e10cSrcweir 			OString typeName, tmpName;
182*cdf0e10cSrcweir 			sal_Bool ret = sal_False;
183*cdf0e10cSrcweir             do
184*cdf0e10cSrcweir 			{
185*cdf0e10cSrcweir 				typeName = tOption.getToken(0, ';', nIndex);
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir                 sal_Int32 nPos = typeName.lastIndexOf( '.' );
188*cdf0e10cSrcweir 				tmpName = typeName.copy( nPos != -1 ? nPos+1 : 0 );
189*cdf0e10cSrcweir 				if (tmpName == "*")
190*cdf0e10cSrcweir 				{
191*cdf0e10cSrcweir 					// produce this type and his scope.
192*cdf0e10cSrcweir 					if (typeName.equals("*"))
193*cdf0e10cSrcweir 					{
194*cdf0e10cSrcweir 						tmpName = "/";
195*cdf0e10cSrcweir 					} else
196*cdf0e10cSrcweir 					{
197*cdf0e10cSrcweir 						tmpName = typeName.copy(0, typeName.lastIndexOf('.')).replace('.', '/');
198*cdf0e10cSrcweir 						if (tmpName.getLength() == 0)
199*cdf0e10cSrcweir 							tmpName = "/";
200*cdf0e10cSrcweir 						else
201*cdf0e10cSrcweir 							tmpName.replace('.', '/');
202*cdf0e10cSrcweir 					}
203*cdf0e10cSrcweir                     // related to task #116780# the scope is recursively
204*cdf0e10cSrcweir                     // generated.  bFullScope = true
205*cdf0e10cSrcweir 					ret = produceAllTypes(
206*cdf0e10cSrcweir                         tmpName, typeMgr, generated, &options, sal_True);
207*cdf0e10cSrcweir 				} else
208*cdf0e10cSrcweir 				{
209*cdf0e10cSrcweir 					// produce only this type
210*cdf0e10cSrcweir 					ret = produceType(
211*cdf0e10cSrcweir                         typeName.replace('.', '/'), typeMgr, generated,
212*cdf0e10cSrcweir                         &options);
213*cdf0e10cSrcweir 				}
214*cdf0e10cSrcweir 
215*cdf0e10cSrcweir 				if (!ret)
216*cdf0e10cSrcweir 				{
217*cdf0e10cSrcweir 					fprintf(stderr, "%s ERROR: %s\n",
218*cdf0e10cSrcweir 							options.getProgramName().getStr(),
219*cdf0e10cSrcweir 							OString("cannot dump Type '" + typeName + "'").getStr());
220*cdf0e10cSrcweir 					exit(99);
221*cdf0e10cSrcweir 				}
222*cdf0e10cSrcweir 			} while( nIndex != -1 );
223*cdf0e10cSrcweir 		} else
224*cdf0e10cSrcweir 		{
225*cdf0e10cSrcweir 			// produce all types
226*cdf0e10cSrcweir             codemaker::GeneratedTypeSet generated;
227*cdf0e10cSrcweir 			if (!produceAllTypes("/", typeMgr, generated, &options, sal_True))
228*cdf0e10cSrcweir 			{
229*cdf0e10cSrcweir 				fprintf(stderr, "%s ERROR: %s\n",
230*cdf0e10cSrcweir 						options.getProgramName().getStr(),
231*cdf0e10cSrcweir 						"an error occurs while dumping all types.");
232*cdf0e10cSrcweir 				exit(99);
233*cdf0e10cSrcweir 			}
234*cdf0e10cSrcweir 		}
235*cdf0e10cSrcweir 	}
236*cdf0e10cSrcweir 	catch( CannotDumpException& e)
237*cdf0e10cSrcweir 	{
238*cdf0e10cSrcweir 		fprintf(stderr, "%s ERROR: %s\n",
239*cdf0e10cSrcweir 				options.getProgramName().getStr(),
240*cdf0e10cSrcweir 				e.m_message.getStr());
241*cdf0e10cSrcweir 		exit(99);
242*cdf0e10cSrcweir 	}
243*cdf0e10cSrcweir 
244*cdf0e10cSrcweir 	return 0;
245*cdf0e10cSrcweir }
246*cdf0e10cSrcweir 
247*cdf0e10cSrcweir 
248