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