LibreOffice Module unodevtools (master) 1
javacompskeleton.cxx
Go to the documentation of this file.
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
20#include <sal/config.h>
21
23#include <codemaker/global.hxx>
24#include <rtl/strbuf.hxx>
25
26#include "skeletoncommon.hxx"
27#include "skeletonjava.hxx"
28
29#include <iostream>
30
31using namespace ::codemaker::java;
32
34
35static void generatePackage(std::ostream & o, std::string_view implname)
36{
37 size_t index = implname.rfind('.');
38 if (index != std::string_view::npos)
39 o << "package " << implname.substr(0, index) << ";\n\n";
40}
41
42static void generateImports(std::ostream & o, ProgramOptions const & options,
43 std::u16string_view propertyhelper,
44 bool serviceobject, bool supportxcomponent)
45{
46 if (options.componenttype == 3)
47 o << "import com.sun.star.uno.UnoRuntime;\n";
48 o << "import com.sun.star.uno.XComponentContext;\n";
49 if (serviceobject) {
50 o << "import com.sun.star.lib.uno.helper.Factory;\n";
51 o << "import com.sun.star.lang.XSingleComponentFactory;\n";
52 o << "import com.sun.star.registry.XRegistryKey;\n";
53 }
54
55 if (propertyhelper != u"_") {
56 if (supportxcomponent)
57 o << "import com.sun.star.lib.uno.helper.ComponentBase;\n";
58 else
59 o << "import com.sun.star.lib.uno.helper.WeakBase;\n";
60 }
61 if (!propertyhelper.empty()) {
62 if (propertyhelper == u"_") {
63 o << "import com.sun.star.lib.uno.helper.PropertySet;\n";
64 o << "import com.sun.star.beans.PropertyAttribute;\n";
65 } else {
66 o << "import com.sun.star.uno.Type;\n";
67 o << "import com.sun.star.uno.Any;\n";
68 o << "import com.sun.star.beans.Ambiguous;\n";
69 o << "import com.sun.star.beans.Defaulted;\n";
70 o << "import com.sun.star.beans.Optional;\n";
71 o << "import com.sun.star.lib.uno.helper.PropertySetMixin;\n";
72 }
73 }
74}
75
76static void generateCompFunctions(std::ostream & o, const OString & classname)
77{
78 o << " public static XSingleComponentFactory __getComponentFactory("
79 " String sImplementationName ) {\n"
80 " XSingleComponentFactory xFactory = null;\n\n"
81 " if ( sImplementationName.equals( m_implementationName ) )\n"
82 " xFactory = Factory.createComponentFactory("
83 << classname << ".class, m_serviceNames);\n"
84 " return xFactory;\n }\n\n";
85
86 o << " public static boolean __writeRegistryServiceInfo("
87 " XRegistryKey xRegistryKey ) {\n"
88 " return Factory.writeRegistryServiceInfo(m_implementationName,\n"
89 " m_serviceNames,\n"
90 " xRegistryKey);\n"
91 " }\n\n";
92}
93
94static void generateXServiceInfoBodies(std::ostream& o)
95{
96 o << " // com.sun.star.lang.XServiceInfo:\n";
97 o << " public String getImplementationName() {\n"
98 " return m_implementationName;\n }\n\n";
99
100 o << " public boolean supportsService( String sService ) {\n"
101 " int len = m_serviceNames.length;\n\n"
102 " for( int i=0; i < len; i++) {\n"
103 " if (sService.equals(m_serviceNames[i]))\n"
104 " return true;\n"
105 " }\n return false;\n }\n\n";
106
107 o << " public String[] getSupportedServiceNames() {\n"
108 " return m_serviceNames;\n }\n\n";
109}
110
111void generateXPropertySetBodies(std::ostream& o)
112{
113 o << " // com.sun.star.beans.XPropertySet:\n";
114 o << " public com.sun.star.beans.XPropertySetInfo getPropertySetInfo()\n"
115 " {\n return m_prophlp.getPropertySetInfo();\n }\n\n";
116
117 o << " public void setPropertyValue(String aPropertyName, "
118 "Object aValue) throws "
119 "com.sun.star.beans.UnknownPropertyException, "
120 "com.sun.star.beans.PropertyVetoException, "
121 "com.sun.star.lang.IllegalArgumentException,"
122 "com.sun.star.lang.WrappedTargetException\n {\n "
123 "m_prophlp.setPropertyValue(aPropertyName, aValue);\n }\n\n";
124
125 o << " public Object getPropertyValue(String "
126 "aPropertyName) throws com.sun.star.beans.UnknownPropertyException, "
127 "com.sun.star.lang.WrappedTargetException\n {\n return "
128 "m_prophlp.getPropertyValue(aPropertyName);\n }\n\n";
129
130 o << " public void addPropertyChangeListener(String aPropertyName"
131 ", com.sun.star.beans.XPropertyChangeListener xListener) throws "
132 "com.sun.star.beans.UnknownPropertyException, "
133 "com.sun.star.lang.WrappedTargetException\n {\n "
134 "m_prophlp.addPropertyChangeListener(aPropertyName, xListener);\n }\n\n";
135
136 o << " public void removePropertyChangeListener(String "
137 "aPropertyName, com.sun.star.beans.XPropertyChangeListener xListener) "
138 "throws com.sun.star.beans.UnknownPropertyException, "
139 "com.sun.star.lang.WrappedTargetException\n {\n "
140 "m_prophlp.removePropertyChangeListener(aPropertyName, xListener);\n"
141 " }\n\n";
142
143 o << " public void addVetoableChangeListener(String aPropertyName"
144 ", com.sun.star.beans.XVetoableChangeListener xListener) throws "
145 "com.sun.star.beans.UnknownPropertyException, "
146 "com.sun.star.lang.WrappedTargetException\n {\n "
147 "m_prophlp.addVetoableChangeListener(aPropertyName, xListener);\n }\n\n";
148
149 o << " public void removeVetoableChangeListener(String "
150 "aPropertyName, com.sun.star.beans.XVetoableChangeListener xListener) "
151 "throws com.sun.star.beans.UnknownPropertyException, "
152 "com.sun.star.lang.WrappedTargetException\n {\n "
153 "m_prophlp.removeVetoableChangeListener(aPropertyName, xListener);\n }\n\n";
154}
155
157{
158 o << " // com.sun.star.beans.XFastPropertySet:\n";
159
160 o << " public void setFastPropertyValue(int nHandle, Object "
161 "aValue) throws com.sun.star.beans.UnknownPropertyException, "
162 "com.sun.star.beans.PropertyVetoException, "
163 "com.sun.star.lang.IllegalArgumentException, "
164 "com.sun.star.lang.WrappedTargetException\n {\n "
165 "m_prophlp.setFastPropertyValue(nHandle, aValue);\n }\n\n";
166
167 o << " public Object getFastPropertyValue(int nHandle) throws "
168 "com.sun.star.beans.UnknownPropertyException, "
169 "com.sun.star.lang.WrappedTargetException\n {\n return "
170 "m_prophlp.getFastPropertyValue(nHandle);\n }\n\n";
171}
172
173void generateXPropertyAccessBodies(std::ostream& o)
174{
175 o << " // com.sun.star.beans.XPropertyAccess:\n";
176
177 o << " public com.sun.star.beans.PropertyValue[] getPropertyValues()\n"
178 " {\n return m_prophlp.getPropertyValues();\n }\n\n";
179
180 o << " public void setPropertyValues(com.sun.star.beans.PropertyValue[] "
181 "aProps) throws com.sun.star.beans.UnknownPropertyException, "
182 "com.sun.star.beans.PropertyVetoException, "
183 "com.sun.star.lang.IllegalArgumentException, "
184 "com.sun.star.lang.WrappedTargetException\n {\n "
185 "m_prophlp.setPropertyValues(aProps);\n }\n\n";
186}
187
188
189static bool checkAttribute(
190 OStringBuffer& attributeValue,
192{
193 bool cast = false;
194 sal_uInt16 attributes[9] = {
195 /* com::sun::star::beans::PropertyValue::MAYBEVOID */ 1,
196 /* com::sun::star::beans::PropertyValue::BOUND */ 2,
197 /* com::sun::star::beans::PropertyValue::CONSTRAINED */ 4,
198 /* com::sun::star::beans::PropertyValue::TRANSIENT */ 8,
199 /* com::sun::star::beans::PropertyValue::READONLY */ 16,
200 /* com::sun::star::beans::PropertyValue::MAYBEAMBIGUOUS */ 32,
201 /* com::sun::star::beans::PropertyValue::MAYBEDEFAULT */ 64,
202 /* com::sun::star::beans::PropertyValue::REMOVABLE */ 128,
203 /* com::sun::star::beans::PropertyValue::OPTIONAL */ 256 };
204
205 for (sal_uInt16 i = 0; i < 9; i++)
206 {
207 if (attribute & attributes[i]) {
208 if (!attributeValue.isEmpty()) {
209 cast = true;
210 attributeValue.append("|");
211 }
212 switch (attributes[i])
213 {
214 case 1:
215 attributeValue.append("PropertyAttribute.MAYBEVOID");
216 break;
217 case 2:
218 attributeValue.append("PropertyAttribute.BOUND");
219 break;
220 case 4:
221 attributeValue.append("PropertyAttribute.CONSTRAINED");
222 break;
223 case 8:
224 attributeValue.append("PropertyAttribute.TRANSIENT");
225 break;
226 case 16:
227 attributeValue.append("PropertyAttribute.READONLY");
228 break;
229 case 32:
230 attributeValue.append("PropertyAttribute.MAYBEAMBIGUOUS");
231 break;
232 case 64:
233 attributeValue.append("PropertyAttribute.MAYBEDEFAULT");
234 break;
235 case 128:
236 attributeValue.append("PropertyAttribute.REMOVABLE");
237 break;
238 case 256:
239 attributeValue.append("PropertyAttribute.OPTIONAL");
240 break;
241 }
242 }
243 }
244 if (cast) {
245 attributeValue.insert(0, '(');
246 attributeValue.append(')');
247 }
248
249 return cast;
250}
251
252static void registerProperties(std::ostream& o,
254 const OString& indentation)
255{
256 if (!properties.empty()) {
257 bool cast = false;
258 OStringBuffer attributeValue;
259 for (const auto& rProp : properties)
260 {
261 if (rProp.attributes != 0) {
262 cast = checkAttribute(attributeValue, rProp.attributes);
263 } else {
264 cast = true;
265 attributeValue.append('0');
266 }
267
268 o << indentation << "registerProperty(\"" << rProp.name
269 << "\", \"m_" << rProp.name << "\",\n"
270 << indentation << " ";
271 if (cast)
272 o << "(short)";
273
274 o << attributeValue.makeStringAndClear() << ");\n";
275 }
276 }
277}
278
279static void generateXLocalizableBodies(std::ostream& o) {
280 // com.sun.star.lang.XLocalizable:
281 // setLocale
282 o << " // com.sun.star.lang.XLocalizable:\n"
283 " public void setLocale(com.sun.star.lang.Locale eLocale)\n {\n"
284 " m_locale = eLocale;\n }\n\n";
285
286 // getLocale
287 o << " public com.sun.star.lang.Locale getLocale()\n {\n"
288 " return m_locale;\n }\n\n";
289}
290
291static void generateXAddInBodies(std::ostream& o)
292{
293 // com.sun.star.sheet.XAddIn:
294 // getProgrammaticFuntionName
295 o << " // com.sun.star.sheet.XAddIn:\n"
296 " public String getProgrammaticFuntionName(String "
297 "aDisplayName)\n {\n"
298 " try {\n"
299 " com.sun.star.container.XNameAccess xNAccess =\n"
300 " (com.sun.star.container.XNameAccess)UnoRuntime."
301 "queryInterface(\n"
302 " com.sun.star.container.XNameAccess.class, m_xHAccess);"
303 "\n String functions[] = xNAccess.getElementNames();\n"
304 " String sDisplayName = \"\";\n"
305 " int len = functions.length;\n"
306 " for (int i=0; i < len; ++i) {\n"
307 " sDisplayName = com.sun.star.uno.AnyConverter.toString(\n"
308 " getAddinProperty(functions[i], \"\", sDISPLAYNAME));\n"
309 " if (sDisplayName.equals(aDisplayName))\n"
310 " return functions[i];\n }\n"
311 " }\n catch ( com.sun.star.uno.RuntimeException e ) {\n"
312 " throw e;\n }\n"
313 " catch ( com.sun.star.uno.Exception e ) {\n }\n\n"
314 " return \"\";\n }\n\n";
315
316 // getDisplayFunctionName
317 o << " public String getDisplayFunctionName(String "
318 "aProgrammaticName)\n {\n"
319 " return getAddinProperty(aProgrammaticName, \"\", sDISPLAYNAME);\n"
320 " }\n\n";
321
322 // getFunctionDescription
323 o << " public String getFunctionDescription(String "
324 "aProgrammaticName)\n {\n"
325 " return getAddinProperty(aProgrammaticName, \"\", sDESCRIPTION);\n"
326 " }\n\n";
327
328 // getDisplayArgumentName
329 o << " public String getDisplayArgumentName(String "
330 "aProgrammaticFunctionName, int nArgument)\n {\n";
331 o << " return getAddinProperty(aProgrammaticFunctionName,\n"
332 " m_functionMap.get(\n"
333 " aProgrammaticFunctionName).get("
334 "nArgument),\n"
335 " sDISPLAYNAME);\n }\n\n";
336
337 // getArgumentDescription
338 o << " public String getArgumentDescription(String "
339 "aProgrammaticFunctionName, int nArgument)\n {\n";
340 o << " return getAddinProperty(aProgrammaticFunctionName,\n"
341 " m_functionMap.get(\n"
342 " aProgrammaticFunctionName).get("
343 "nArgument),\n"
344 " sDESCRIPTION);\n }\n\n";
345
346 // getProgrammaticCategoryName
347 o << " public String getProgrammaticCategoryName(String "
348 "aProgrammaticFunctionName)\n {\n"
349 " return getAddinProperty(aProgrammaticFunctionName, \"\", "
350 "sCATEGORY);\n }\n\n";
351
352 // getDisplayCategoryName
353 o << " public String getDisplayCategoryName(String "
354 "aProgrammaticFunctionName)\n {\n"
355 " return getAddinProperty(aProgrammaticFunctionName, \"\", "
356 "sCATEGORYDISPLAYNAME);\n }\n\n";
357}
358
359static void generateXCompatibilityNamesBodies(std::ostream& o)
360{
361 o << " // com.sun.star.sheet.XCompatibilityNames:\n"
362 " public com.sun.star.sheet.LocalizedName[] getCompatibilityNames("
363 "String aProgrammaticName)\n {\n"
364 " com.sun.star.sheet.LocalizedName[] seqLocalizedNames =\n"
365 " new com.sun.star.sheet.LocalizedName[0];\n\n try {\n";
366
367 o << " StringBuffer path = new StringBuffer(aProgrammaticName);\n"
368 " path.append(\"/CompatibilityName\");\n"
369 " String hname = path.toString();\n\n";
370
371 o << " if ( m_xCompAccess.hasByHierarchicalName(hname) ) {\n"
372 " com.sun.star.container.XNameAccess xNameAccess =\n"
373 " (com.sun.star.container.XNameAccess)UnoRuntime."
374 "queryInterface(\n"
375 " com.sun.star.container.XNameAccess.class,\n"
376 " m_xCompAccess.getByHierarchicalName(hname));\n\n"
377 " String elems[] = xNameAccess.getElementNames();\n"
378 " int len = elems.length;\n"
379 " seqLocalizedNames = new com.sun.star.sheet.LocalizedName"
380 "[len];\n String sCompatibilityName = \"\";\n\n";
381
382 o << " for (int i=0; i < len; ++i) {\n"
383 " String sLocale = elems[i];\n"
384 " sCompatibilityName = com.sun.star.uno.AnyConverter."
385 "toString(\n xNameAccess.getByName(sLocale));\n\n"
386 " com.sun.star.lang.Locale aLocale = \n"
387 " new com.sun.star.lang.Locale();\n\n"
388 /* FIXME-BCP47: this will break. */
389 " String tokens[] = sLocale.split(\"-\");\n"
390 " int nToken = tokens.length;\n"
391 " if (nToken >= 1) aLocale.Language = tokens[0];\n"
392 " if (nToken >= 2) aLocale.Country = tokens[1];\n"
393 " if (nToken >= 3) {\n"
394 " StringBuffer buf = \n"
395 " new StringBuffer(tokens[2]);\n"
396 " for (int t=3; t < nToken; ++t)\n"
397 " buf.append(tokens[t]);\n\n"
398 " aLocale.Variant = buf.toString();\n"
399 " }\n\n"
400 " seqLocalizedNames[i].Locale = aLocale;\n"
401 " seqLocalizedNames[i].Name = sCompatibilityName;\n"
402 " }\n }\n }\n"
403 " catch ( com.sun.star.uno.RuntimeException e ) {\n"
404 " throw e;\n }\n"
405 " catch ( com.sun.star.uno.Exception e ) {\n }\n\n"
406 " return seqLocalizedNames;\n }\n\n";
407}
408
409static void generateXInitializationBodies(std::ostream& o)
410{
411 o << " // com.sun.star.lang.XInitialization:\n"
412 " public void initialize( Object[] object )\n"
413 " throws com.sun.star.uno.Exception\n {\n"
414 " if ( object.length > 0 )\n {\n"
415 " m_xFrame = (com.sun.star.frame.XFrame)UnoRuntime.queryInterface(\n"
416 " com.sun.star.frame.XFrame.class, object[0]);\n }\n }\n\n";
417}
418
419static void generateXDispatchBodies(std::ostream& o, ProgramOptions const & options)
420{
421 // com.sun.star.frame.XDispatch
422 // dispatch
423 o << " // com.sun.star.frame.XDispatch:\n"
424 " public void dispatch( com.sun.star.util.URL aURL,\n"
425 " com.sun.star.beans.PropertyValue[] aArguments )\n {\n";
426
427 for (const auto& rEntry : options.protocolCmdMap) {
428 o << " if ( aURL.Protocol.equals(\"" << rEntry.first
429 << "\") )\n {\n";
430
431 for (const auto& rCmd : rEntry.second) {
432 o << " if ( aURL.Path.equals(\"" << rCmd << "\") )\n"
433 " {\n // add your own code here\n"
434 " return;\n }\n";
435 }
436
437 o << " }\n";
438 }
439 o << " }\n\n";
440
441 // addStatusListener
442 o << " public void addStatusListener( com.sun.star.frame.XStatusListener xControl,\n"
443 " com.sun.star.util.URL aURL )\n {\n"
444 " // add your own code here\n }\n\n";
445
446 // com.sun.star.frame.XDispatch
447 o << " public void removeStatusListener( com.sun.star.frame.XStatusListener xControl,\n"
448 " com.sun.star.util.URL aURL )\n {\n"
449 " // add your own code here\n }\n\n";
450}
451
452static void generateXDispatchProviderBodies(std::ostream& o, ProgramOptions const & options)
453{
454 // com.sun.star.frame.XDispatchProvider
455 // queryDispatch
456 o << " // com.sun.star.frame.XDispatchProvider:\n"
457 " public com.sun.star.frame.XDispatch queryDispatch( com.sun.star.util.URL aURL,\n"
458 " String sTargetFrameName,\n"
459 " int iSearchFlags )\n {\n";
460
461 for (const auto& rEntry : options.protocolCmdMap) {
462 o << " if ( aURL.Protocol.equals(\"" << rEntry.first
463 << "\") )\n {\n";
464
465 for (const auto& rCmd : rEntry.second) {
466 o << " if ( aURL.Path.equals(\"" << rCmd << "\") )\n"
467 " return this;\n";
468 }
469
470 o << " }\n";
471 }
472 o << " return null;\n }\n\n";
473
474 // queryDispatches
475 o << " // com.sun.star.frame.XDispatchProvider:\n"
476 " public com.sun.star.frame.XDispatch[] queryDispatches(\n"
477 " com.sun.star.frame.DispatchDescriptor[] seqDescriptors )\n {\n"
478 " int nCount = seqDescriptors.length;\n"
479 " com.sun.star.frame.XDispatch[] seqDispatcher =\n"
480 " new com.sun.star.frame.XDispatch[seqDescriptors.length];\n\n"
481 " for( int i=0; i < nCount; ++i )\n {\n"
482 " seqDispatcher[i] = queryDispatch(seqDescriptors[i].FeatureURL,\n"
483 " seqDescriptors[i].FrameName,\n"
484 " seqDescriptors[i].SearchFlags );\n"
485 " }\n return seqDispatcher;\n }\n\n";
486}
487
488static void generateMethodBodies(std::ostream& o,
489 ProgramOptions const & options,
490 rtl::Reference< TypeManager > const & manager,
491 const std::set< OUString >& interfaces,
492 const OString& indentation, bool usepropertymixin)
493{
495 for (const OUString& type : interfaces) {
496 if (type == "com.sun.star.lang.XServiceInfo") {
498 generated.add(u2b(type));
499 } else {
500 if (options.componenttype == 2) {
501 if (type == "com.sun.star.lang.XServiceName") {
502 o << " // com.sun.star.lang.XServiceName:\n"
503 " public String getServiceName() {\n"
504 " return sADDIN_SERVICENAME;\n }\n";
505 generated.add(u2b(type));
506 continue;
507 } else if (type == "com.sun.star.sheet.XAddIn") {
509 generated.add(u2b(type));
510
511 // special handling of XLocalizable -> parent of XAddIn
512 if (!generated.contains("com.sun.star.lang.XLocalizable")) {
514 generated.add("com.sun.star.lang.XLocalizable");
515 }
516 continue;
517 } else if (type == "com.sun.star.lang.XLocalizable") {
519 generated.add(u2b(type));
520 continue;
521 } else if (type == "com.sun.star.sheet.XCompatibilityNames") {
523 generated.add(u2b(type));
524 continue;
525 }
526 }
527 if (options.componenttype == 3) {
528 if (type == "com.sun.star.lang.XInitialization") {
530 generated.add(u2b(type));
531 continue;
532 } else if (type == "com.sun.star.frame.XDispatch") {
533 generateXDispatchBodies(o, options);
534 generated.add(u2b(type));
535 continue;
536 } else if (type == "com.sun.star.frame.XDispatchProvider") {
538 generated.add(u2b(type));
539 continue;
540 }
541 }
542 printMethods(o, options, manager, type, generated, "_",
543 indentation, true, usepropertymixin);
544 }
545 }
546}
547
548const char* const propcomment=
549" // use the last parameter of the PropertySetMixin constructor\n"
550" // for your optional attributes if necessary. See the documentation\n"
551" // of the PropertySetMixin helper for further information.\n"
552" // Ensure that your attributes are initialized correctly!\n";
553
554
555static void generateAddinConstructorAndHelper(std::ostream& o,
556 ProgramOptions const & options,
557 rtl::Reference< TypeManager > const & manager, const OString & classname,
558 const std::set< OUString >& services,
559 const std::set< OUString >& interfaces)
560{
561 o << " private com.sun.star.lang.Locale m_locale = "
562 "new com.sun.star.lang.Locale();\n";
563
564 if (!options.backwardcompatible) {
565 // Constructor
566 o << "\n public " << classname << "( XComponentContext context )\n"
567 " {\n m_xContext = context;\n }\n\n";
568 return;
569 }
570
571
572 // get the one and only add-in service for later use
573 std::set< OUString >::const_iterator iter = services.begin();
574 OUString sAddinService = *iter;
575 if (sAddinService == "com.sun.star.sheet.AddIn") {
576 sAddinService = *(++iter);
577 }
578
579
580 // add-in specific fields
581 o << "\n private static final String sADDIN_SERVICENAME = \""
582 << sAddinService << "\";\n\n";
583 o << " private static final String sDISPLAYNAME = "
584 "\"DisplayName\";\n"
585 " private static final String sDESCRIPTION = "
586 "\"Description\";\n"
587 " private static final String sCATEGORY = \"Category\";\n"
588 " private static final String sCATEGORYDISPLAYNAME = "
589 "\"CategoryDisplayName\";\n\n";
590
591 o << " private com.sun.star.container.XHierarchicalNameAccess "
592 "m_xHAccess = null;\n"
593 " private com.sun.star.container.XHierarchicalNameAccess "
594 "m_xCompAccess = null;\n";
595 o << " private java.util.Hashtable<\n String, "
596 "java.util.Hashtable< Integer, String> > m_functionMap = null;\n\n";
597
598 // Constructor
599 o << "\n public " << classname << "( XComponentContext context )\n {\n"
600 " m_xContext = context;\n\n"
601 " try {\n";
602
603 o << " m_functionMap = new java.util.Hashtable<\n"
604 " String, java.util.Hashtable< Integer, String > >();\n\n";
605
606 generateFunctionParameterMap(o, options, manager, interfaces);
607
608 o << " com.sun.star.lang.XMultiServiceFactory xProvider = \n"
609 " (com.sun.star.lang.XMultiServiceFactory)UnoRuntime."
610 "queryInterface(\n"
611 " com.sun.star.lang.XMultiServiceFactory.class,\n"
612 " m_xContext.getServiceManager().createInstanceWithContext("
613 "\n \"com.sun.star.configuration.ConfigurationProvider\""
614 ",\n m_xContext));\n\n";
615
616 o << " String sReadOnlyView = "
617 "\"com.sun.star.configuration.ConfigurationAccess\";\n\n";
618
619 o << " StringBuffer sPath = new StringBuffer(\n"
620 " \"/org.openoffice.Office.CalcAddIns/AddInInfo/\");\n"
621 " sPath.append(sADDIN_SERVICENAME);\n"
622 " sPath.append(\"/AddInFunctions\");\n\n";
623
624 o << " // create arguments: nodepath\n"
625 " com.sun.star.beans.PropertyValue aArgument = \n"
626 " new com.sun.star.beans.PropertyValue();\n"
627 " aArgument.Name = \"nodepath\";\n"
628 " aArgument.Value = new com.sun.star.uno.Any(\n"
629 " com.sun.star.uno.Type.STRING, sPath.toString());\n\n";
630
631 o << " Object aArguments[] = new Object[1];\n"
632 " aArguments[0] = new com.sun.star.uno.Any("
633 " new com.sun.star.uno.Type(\n"
634 " com.sun.star.beans.PropertyValue.class), aArgument);\n\n";
635
636 o << " // create the default view using default UI locale\n"
637 " Object xIface = \n"
638 " xProvider.createInstanceWithArguments(sReadOnlyView, "
639 "aArguments);\n\n";
640
641 o << " m_xHAccess = (com.sun.star.container.XHierarchicalNameAccess)\n"
642 " UnoRuntime.queryInterface(\n"
643 " com.sun.star.container.XHierarchicalNameAccess.class, "
644 "xIface);\n\n";
645
646 o << " // extends arguments to create a view for all locales to get "
647 "simple\n // access to the compatibilityname property\n"
648 " aArguments = new Object[2];\n"
649 " aArguments[0] = new com.sun.star.uno.Any( "
650 "new com.sun.star.uno.Type(\n"
651 " com.sun.star.beans.PropertyValue.class), aArgument);\n"
652 " aArgument.Name = \"locale\";\n"
653 " aArgument.Value = new com.sun.star.uno.Any(\n"
654 " com.sun.star.uno.Type.STRING, \"*\");\n"
655 " aArguments[1] = new com.sun.star.uno.Any( "
656 " new com.sun.star.uno.Type(\n"
657 " com.sun.star.beans.PropertyValue.class), aArgument);\n\n";
658
659 o << " // create view for all locales\n"
660 " xIface = xProvider.createInstanceWithArguments(sReadOnlyView, "
661 "aArguments);\n\n"
662 " m_xCompAccess = (com.sun.star.container.XHierarchicalNameAccess)\n"
663 " UnoRuntime.queryInterface(\n"
664 " com.sun.star.container.XHierarchicalNameAccess.class, "
665 "xIface);\n }\n"
666 " catch ( com.sun.star.uno.Exception e ) {\n }\n }\n\n";
667
668 // add-in helper function
669 o << " // addin configuration property helper function:\n"
670 " String getAddinProperty(String funcName, "
671 "String paramName, String propName)\n {\n"
672 " try {\n StringBuffer buf = "
673 "new StringBuffer(funcName);\n\n"
674 " if (paramName.length() > 0) {\n"
675 " buf.append(\"/Parameters/\");\n"
676 " buf.append(paramName);\n }\n\n";
677
678 o << " com.sun.star.beans.XPropertySet xPropSet =\n"
679 " (com.sun.star.beans.XPropertySet)UnoRuntime."
680 "queryInterface(\n"
681 " com.sun.star.beans.XPropertySet.class,\n"
682 " m_xHAccess.getByHierarchicalName(buf.toString()));\n\n"
683 " return com.sun.star.uno.AnyConverter.toString(\n"
684 " xPropSet.getPropertyValue(propName));\n }\n"
685 " catch ( com.sun.star.uno.RuntimeException e ) {\n"
686 " throw e;\n }\n"
687 " catch ( com.sun.star.uno.Exception e ) {\n }\n"
688 " return \"\";\n }\n\n";
689}
690
691
692static void generateClassDefinition(std::ostream& o,
693 ProgramOptions const & options,
694 rtl::Reference< TypeManager > const & manager,
695 const OString & classname,
696 const std::set< OUString >& services,
697 const std::set< OUString >& interfaces,
699 const AttributeInfo& attributes,
700 const OUString& propertyhelper, bool supportxcomponent)
701{
702 o << "\n\npublic final class " << classname << " extends ";
703
704 if (!interfaces.empty()) {
705 if (propertyhelper == "_") {
706 o << "PropertySet\n";
707 } else {
708 if (supportxcomponent)
709 o << "ComponentBase\n";
710 else
711 o << "WeakBase\n";
712 }
713 o << " implements ";
714 std::set< OUString >::const_iterator iter = interfaces.begin();
715 while (iter != interfaces.end()) {
716 o << (*iter);
717 ++iter;
718 if (iter != interfaces.end())
719 o << ",\n ";
720 }
721 }
722 o << "\n{\n";
723
724 o << " private final XComponentContext m_xContext;\n";
725
726 // additional member for add-ons
727 if (options.componenttype == 3) {
728 o << " private com.sun.star.frame.XFrame m_xFrame;\n";
729 }
730
731 // check property helper
732 if (propertyhelper.getLength() > 1)
733 o << " private final PropertySetMixin m_prophlp;\n";
734
735 o << " private static final String m_implementationName = "
736 << classname << ".class.getName();\n";
737
738 if (!services.empty()) {
739 o << " private static final String[] m_serviceNames = {\n";
740 std::set< OUString >::const_iterator iter = services.begin();
741 while (iter != services.end()) {
742 o << " \"" << (*iter).replace('/','.') << "\"";
743 ++iter;
744 if (iter != services.end())
745 o << ",\n";
746 else
747 o << " };\n\n";
748 }
749 }
750
751 // attribute/property members
752 if (!properties.empty()) {
753 o << " // properties\n";
754 for (const auto& rProp : properties) {
755 o << " protected ";
756 printType(o, options, manager, rProp.type, false);
757 o << " m_" << rProp.name << ";\n";
758 }
759 } else if (!attributes.empty()) {
760 o << " // attributes\n";
761 for (const auto& rAttr : attributes) {
762 o << " private ";
763 printType(o, options, manager, rAttr.type, false);
764 o << " m_" << rAttr.name << " = ";
765 printType(o, options, manager, rAttr.type, false, true);
766 o <<";\n";
767 }
768 }
769
770 // special handling of calc add-ins
771 if (options.componenttype == 2)
772 {
773 generateAddinConstructorAndHelper(o, options, manager, classname,
774 services, interfaces);
775 } else {
776 o << "\n public " << classname << "( XComponentContext context )\n"
777 " {\n m_xContext = context;\n";
778 if (propertyhelper == "_") {
780 } else {
781 if (propertyhelper.getLength() > 1) {
782 o << propcomment
783 << " m_prophlp = new PropertySetMixin(m_xContext, this,\n"
784 " new Type(" << propertyhelper
785 << ".class), null);\n";
786 }
787 }
788 o << " };\n\n";
789
790 }
791
792 if (!services.empty())
793 generateCompFunctions(o, classname);
794
795 generateMethodBodies(o, options, manager, interfaces,
796 " ", propertyhelper.getLength() > 1);
797
798 // end of class definition
799 o << "}\n";
800}
801
802void generateSkeleton(ProgramOptions const & options,
803 rtl::Reference< TypeManager > const & manager,
804 std::vector< OString > const & types)
805{
806 std::set< OUString > interfaces;
807 std::set< OUString > services;
809 AttributeInfo attributes;
810 std::set< OUString > propinterfaces;
811 bool serviceobject = false;
812 bool supportxcomponent = false;
813
814 for (const auto& rType : types) {
815 checkType(manager, b2u(rType), interfaces, services, properties);
816 }
817
818 if (options.componenttype == 3) {
819 // the Protocolhandler service is mandatory for a protocol handler add-on,
820 // so it is defaulted. The XDispatchProvider provides Dispatch objects for
821 // certain functions and the generated impl object implements XDispatch
822 // directly for simplicity reasons.
823 checkType(manager, "com.sun.star.frame.ProtocolHandler",
824 interfaces, services, properties);
825 checkType(manager, "com.sun.star.frame.XDispatch",
826 interfaces, services, properties);
827 }
828
829 if (options.componenttype == 2) {
830 if (services.size() != 1) {
832 "for calc add-in components one and only one service type is "
833 "necessary! Please reference a valid type with the '-t' option.");
834 }
835
836 // if backwardcompatible==true the AddIn service needs to be added to the
837 // supported service list, the necessary interfaces are mapped to the add-in
838 // configuration. Since OO.org 2.0.4 this is obsolete and the add-in is
839 // taken from the configuration from Calc directly, this simplifies the
840 // add-in code
841 if (options.backwardcompatible) {
842 checkType(manager, "com.sun.star.sheet.AddIn",
843 interfaces, services, properties);
844 } else {
845 // special case for the optional XLocalization interface. It should be
846 // implemented always. But it is parent of the XAddIn and we need it only
847 // if backwardcompatible is false.
848 interfaces.insert("com.sun.star.lang.XLocalizable");
849 }
850 }
851
852
853 // check if service object or simple UNO object
854 if (!services.empty())
855 serviceobject = true;
856
857 OUString propertyhelper = checkPropertyHelper(
858 options, manager, services, interfaces, attributes, propinterfaces);
859 checkDefaultInterfaces(interfaces, services, propertyhelper);
860
861 if (options.componenttype == 2) {
862 if (!propertyhelper.isEmpty())
863 std::cerr << "WARNING: interfaces specifying calc add-in functions "
864 "shouldn't support attributes!\n";
865 }
866
867 supportxcomponent = checkXComponentSupport(manager, interfaces);
868
869 OString compFileName;
870 OString tmpFileName;
871 std::ostream* pofs = nullptr;
872 bool standardout = getOutputStream(options, ".java",
873 &pofs, compFileName, tmpFileName);
874
875 try {
876 if (!standardout && options.license) {
877 printLicenseHeader(*pofs);
878 }
879
880 generatePackage(*pofs, options.implname);
881
882 generateImports(*pofs, options, propertyhelper,
883 serviceobject, supportxcomponent);
884
885 OString classname(options.implname);
886 sal_Int32 index = 0;
887 if ((index = classname.lastIndexOf('.')) > 0)
888 classname = classname.copy(index+1);
889
890 generateClassDefinition(*pofs, options, manager, classname, services,
891 interfaces, properties, attributes, propertyhelper,
892 supportxcomponent);
893
894 if ( !standardout && pofs && static_cast<std::ofstream*>(pofs)->is_open()) {
895 static_cast<std::ofstream*>(pofs)->close();
896 delete pofs;
897 OSL_VERIFY(makeValidTypeFile(compFileName, tmpFileName, false));
898 }
899 } catch (CannotDumpException & e) {
900 std::cerr << "ERROR: " << e.getMessage() << "\n";
901 if ( !standardout ) {
902 if (pofs && static_cast<std::ofstream*>(pofs)->is_open()) {
903 static_cast<std::ofstream*>(pofs)->close();
904 delete pofs;
905 }
906 // remove existing type file if something goes wrong to ensure
907 // consistency
908 if (fileExists(compFileName))
909 removeTypeFile(compFileName);
910
911 // remove tmp file if something goes wrong
912 removeTypeFile(tmpFileName);
913 }
914 }
915}
916
917}
918
919
920/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const OUString & getMessage() const
bool contains(OString const &type) const
void add(OString const &type)
rtl::Reference< ParseManager > manager
bool close
float u
bool removeTypeFile(const OString &fileName)
bool fileExists(const OString &fileName)
bool makeValidTypeFile(const OString &targetFileName, const OString &tmpFileName, bool bFileCheck)
int i
index
static void generateXCompatibilityNamesBodies(std::ostream &o)
static void generateXServiceInfoBodies(std::ostream &o)
static void generateAddinConstructorAndHelper(std::ostream &o, ProgramOptions const &options, rtl::Reference< TypeManager > const &manager, const OString &classname, const std::set< OUString > &services, const std::set< OUString > &interfaces)
static void generatePackage(std::ostream &o, std::string_view implname)
static void generateCompFunctions(std::ostream &o, const OString &classname)
static void generateXAddInBodies(std::ostream &o)
static void registerProperties(std::ostream &o, const AttributeInfo &properties, const OString &indentation)
static void generateXInitializationBodies(std::ostream &o)
void generateXFastPropertySetBodies(std::ostream &o)
static bool checkAttribute(OStringBuffer &attributeValue, unoidl::AccumulationBasedServiceEntity::Property::Attributes attribute)
static void generateMethodBodies(std::ostream &o, ProgramOptions const &options, rtl::Reference< TypeManager > const &manager, const std::set< OUString > &interfaces, const OString &indentation, bool usepropertymixin)
static void generateXDispatchBodies(std::ostream &o, ProgramOptions const &options)
const char *const propcomment
void generateXPropertySetBodies(std::ostream &o)
static void generateClassDefinition(std::ostream &o, ProgramOptions const &options, rtl::Reference< TypeManager > const &manager, const OString &classname, const std::set< OUString > &services, const std::set< OUString > &interfaces, const AttributeInfo &properties, const AttributeInfo &attributes, const OUString &propertyhelper, bool supportxcomponent)
static void printType(std::ostream &o, ProgramOptions const &options, rtl::Reference< TypeManager > const &manager, codemaker::UnoType::Sort sort, std::u16string_view nucleus, sal_Int32 rank, std::vector< OUString > const &arguments, bool referenceType, bool defaultvalue)
static void generateImports(std::ostream &o, ProgramOptions const &options, std::u16string_view propertyhelper, bool serviceobject, bool supportxcomponent)
void generateXPropertyAccessBodies(std::ostream &o)
static void generateXLocalizableBodies(std::ostream &o)
void printMethods(std::ostream &o, ProgramOptions const &options, rtl::Reference< TypeManager > const &manager, OUString const &name, codemaker::GeneratedTypeSet &generated, OString const &delegate, OString const &indentation, bool defaultvalue, bool usepropertymixin)
static void generateXDispatchProviderBodies(std::ostream &o, ProgramOptions const &options)
void generateSkeleton(ProgramOptions const &options, rtl::Reference< TypeManager > const &manager, std::vector< OString > const &types)
OUString checkPropertyHelper(ProgramOptions const &options, rtl::Reference< TypeManager > const &manager, const std::set< OUString > &services, const std::set< OUString > &interfaces, AttributeInfo &attributes, std::set< OUString > &propinterfaces)
bool getOutputStream(ProgramOptions const &options, OString const &extension, std::ostream **ppOutputStream, OString &targetSourceFileName, OString &tmpSourceFileName)
create dependent on the output path, the implementation name and the extension a new output file.
::std::vector< unoidl::AccumulationBasedServiceEntity::Property > AttributeInfo
void checkDefaultInterfaces(std::set< OUString > &interfaces, const std::set< OUString > &services, std::u16string_view propertyhelper)
static bool checkXComponentSupport(rtl::Reference< TypeManager > const &manager, OUString const &name)
void checkType(rtl::Reference< TypeManager > const &manager, OUString const &name, std::set< OUString > &interfaceTypes, std::set< OUString > &serviceTypes, AttributeInfo &properties)
void printLicenseHeader(std::ostream &o)
print the standard OpenOffice.org license header
static void generateFunctionParameterMap(std::ostream &o, ProgramOptions const &options, rtl::Reference< TypeManager > const &manager, OUString const &name, ::codemaker::GeneratedTypeSet &generated, bool &bFirst)
sal_Int32 attribute
OUString b2u(std::string_view s)
OString u2b(std::u16string_view s)
ResultType type