xref: /trunk/main/codemaker/source/javamaker/javamaker.cxx (revision 9d37da743abb7db0a497688391f6e55a19f27c44)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_codemaker.hxx"
26 
27 #include <stdio.h>
28 
29 #include "sal/main.h"
30 
31 #include "codemaker/typemanager.hxx"
32 #include "codemaker/generatedtypeset.hxx"
33 #include "javaoptions.hxx"
34 #include "javatype.hxx"
35 
36 using namespace rtl;
37 
38 sal_Bool produceAllTypes(RegistryKey& rTypeKey, sal_Bool bIsExtraType,
39                          TypeManager const & typeMgr,
40                          codemaker::GeneratedTypeSet & generated,
41                          JavaOptions* pOptions,
42                          sal_Bool bFullScope)
43 {
44     OString typeName = typeMgr.getTypeName(rTypeKey);
45 
46     if (!produceType(rTypeKey, bIsExtraType, typeMgr, generated, pOptions))
47     {
48         fprintf(stderr, "%s ERROR: %s\n",
49                 pOptions->getProgramName().getStr(),
50                 OString("cannot dump Type '" + typeName + "'").getStr());
51         exit(99);
52     }
53 
54     RegistryKeyList typeKeys = typeMgr.getTypeKeys(typeName);
55     RegistryKeyList::const_iterator iter = typeKeys.begin();
56     RegistryKey key, subKey;
57     RegistryKeyArray subKeys;
58 
59     while (iter != typeKeys.end())
60     {
61         key = (*iter).first;
62 
63         if (!(*iter).second  && !key.openSubKeys(OUString(), subKeys))
64         {
65             for (sal_uInt32 i = 0; i < subKeys.getLength(); i++)
66             {
67                 subKey = subKeys.getElement(i);
68                 if (bFullScope)
69                 {
70                     if (!produceAllTypes(
71                             subKey, (*iter).second,
72                             typeMgr, generated, pOptions, sal_True))
73                         return sal_False;
74                 } else
75                 {
76                     if (!produceType(subKey, (*iter).second,
77                                      typeMgr, generated, pOptions))
78                         return sal_False;
79                 }
80             }
81         }
82 
83         ++iter;
84     }
85 
86     return sal_True;
87 }
88 
89 sal_Bool produceAllTypes(const OString& typeName,
90                          TypeManager const & typeMgr,
91                          codemaker::GeneratedTypeSet & generated,
92                          JavaOptions* pOptions,
93                          sal_Bool bFullScope)
94 {
95     if (!produceType(typeName, typeMgr, generated, pOptions))
96     {
97         fprintf(stderr, "%s ERROR: %s\n",
98                 pOptions->getProgramName().getStr(),
99                 OString("cannot dump Type '" + typeName + "'").getStr());
100         exit(99);
101     }
102 
103     RegistryKeyList typeKeys = typeMgr.getTypeKeys(typeName);
104     RegistryKeyList::const_iterator iter = typeKeys.begin();
105     RegistryKey key, subKey;
106     RegistryKeyArray subKeys;
107 
108     while (iter != typeKeys.end())
109     {
110         key = (*iter).first;
111         if (!(*iter).second  && !key.openSubKeys(OUString(), subKeys))
112         {
113             for (sal_uInt32 i = 0; i < subKeys.getLength(); i++)
114             {
115                 subKey = subKeys.getElement(i);
116                 if (bFullScope)
117                 {
118                     if (!produceAllTypes(
119                             subKey, (*iter).second,
120                             typeMgr, generated, pOptions, sal_True))
121                         return sal_False;
122                 } else
123                 {
124                     if (!produceType(subKey, (*iter).second,
125                                      typeMgr, generated, pOptions))
126                         return sal_False;
127                 }
128             }
129         }
130 
131         ++iter;
132     }
133 
134     return sal_True;
135 }
136 
137 SAL_IMPLEMENT_MAIN_WITH_ARGS(argc, argv)
138 {
139     JavaOptions options;
140 
141     try
142     {
143         if (!options.initOptions(argc, argv))
144         {
145             exit(1);
146         }
147     }
148     catch( IllegalArgument& e)
149     {
150         fprintf(stderr, "Illegal option: %s\n", e.m_message.getStr());
151         exit(99);
152     }
153 
154     RegistryTypeManager typeMgr;
155 
156     if (!typeMgr.init(options.getInputFiles(), options.getExtraInputFiles()))
157     {
158         fprintf(stderr, "%s : init registries failed, check your registry files.\n", options.getProgramName().getStr());
159         exit(99);
160     }
161 
162     if (options.isValid("-B"))
163     {
164         typeMgr.setBase(options.getOption("-B"));
165     }
166 
167     try
168     {
169         if (options.isValid("-T"))
170         {
171             OString tOption(options.getOption("-T"));
172             sal_Int32 nIndex = 0;
173 
174             codemaker::GeneratedTypeSet generated;
175             OString typeName, tmpName;
176             sal_Bool ret = sal_False;
177             do
178             {
179                 typeName = tOption.getToken(0, ';', nIndex);
180 
181                 sal_Int32 nPos = typeName.lastIndexOf( '.' );
182                 tmpName = typeName.copy( nPos != -1 ? nPos+1 : 0 );
183                 if (tmpName == "*")
184                 {
185                     // produce this type and his scope.
186                     if (typeName.equals("*"))
187                     {
188                         tmpName = "/";
189                     } else
190                     {
191                         tmpName = typeName.copy(0, typeName.lastIndexOf('.')).replace('.', '/');
192                         if ( tmpName.isEmpty() )
193                             tmpName = "/";
194                         else
195                             tmpName.replace('.', '/');
196                     }
197                     // related to task #116780# the scope is recursively
198                     // generated.  bFullScope = true
199                     ret = produceAllTypes(
200                         tmpName, typeMgr, generated, &options, sal_True);
201                 } else
202                 {
203                     // produce only this type
204                     ret = produceType(
205                         typeName.replace('.', '/'), typeMgr, generated,
206                         &options);
207                 }
208 
209                 if (!ret)
210                 {
211                     fprintf(stderr, "%s ERROR: %s\n",
212                             options.getProgramName().getStr(),
213                             OString("cannot dump Type '" + typeName + "'").getStr());
214                     exit(99);
215                 }
216             } while( nIndex != -1 );
217         } else
218         {
219             // produce all types
220             codemaker::GeneratedTypeSet generated;
221             if (!produceAllTypes("/", typeMgr, generated, &options, sal_True))
222             {
223                 fprintf(stderr, "%s ERROR: %s\n",
224                         options.getProgramName().getStr(),
225                         "an error occurs while dumping all types.");
226                 exit(99);
227             }
228         }
229     }
230     catch( CannotDumpException& e)
231     {
232         fprintf(stderr, "%s ERROR: %s\n",
233                 options.getProgramName().getStr(),
234                 e.m_message.getStr());
235         exit(99);
236     }
237 
238     return 0;
239 }
240