1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <sal/config.h>
22 #include <codemaker/commonjava.hxx>
23 #include <codemaker/global.hxx>
24 #include <osl/diagnose.h>
25 #include <rtl/strbuf.hxx>
27 #include "skeletoncommon.hxx"
28 #include "skeletonjava.hxx"
32 using namespace ::codemaker::java
;
34 namespace skeletonmaker::java
{
36 static void generatePackage(std::ostream
& o
, std::string_view implname
)
38 size_t index
= implname
.rfind('.');
39 if (index
!= std::string_view::npos
)
40 o
<< "package " << implname
.substr(0, index
) << ";\n\n";
43 static void generateImports(std::ostream
& o
, ProgramOptions
const & options
,
44 std::u16string_view propertyhelper
,
45 bool serviceobject
, bool supportxcomponent
)
47 if (options
.componenttype
== 3)
48 o
<< "import com.sun.star.uno.UnoRuntime;\n";
49 o
<< "import com.sun.star.uno.XComponentContext;\n";
51 o
<< "import com.sun.star.lib.uno.helper.Factory;\n";
52 o
<< "import com.sun.star.lang.XSingleComponentFactory;\n";
53 o
<< "import com.sun.star.registry.XRegistryKey;\n";
56 if (propertyhelper
!= u
"_") {
57 if (supportxcomponent
)
58 o
<< "import com.sun.star.lib.uno.helper.ComponentBase;\n";
60 o
<< "import com.sun.star.lib.uno.helper.WeakBase;\n";
62 if (!propertyhelper
.empty()) {
63 if (propertyhelper
== u
"_") {
64 o
<< "import com.sun.star.lib.uno.helper.PropertySet;\n";
65 o
<< "import com.sun.star.beans.PropertyAttribute;\n";
67 o
<< "import com.sun.star.uno.Type;\n";
68 o
<< "import com.sun.star.uno.Any;\n";
69 o
<< "import com.sun.star.beans.Ambiguous;\n";
70 o
<< "import com.sun.star.beans.Defaulted;\n";
71 o
<< "import com.sun.star.beans.Optional;\n";
72 o
<< "import com.sun.star.lib.uno.helper.PropertySetMixin;\n";
77 static void generateCompFunctions(std::ostream
& o
, const OString
& classname
)
79 o
<< " public static XSingleComponentFactory __getComponentFactory("
80 " String sImplementationName ) {\n"
81 " XSingleComponentFactory xFactory = null;\n\n"
82 " if ( sImplementationName.equals( m_implementationName ) )\n"
83 " xFactory = Factory.createComponentFactory("
84 << classname
<< ".class, m_serviceNames);\n"
85 " return xFactory;\n }\n\n";
87 o
<< " public static boolean __writeRegistryServiceInfo("
88 " XRegistryKey xRegistryKey ) {\n"
89 " return Factory.writeRegistryServiceInfo(m_implementationName,\n"
95 static void generateXServiceInfoBodies(std::ostream
& o
)
97 o
<< " // com.sun.star.lang.XServiceInfo:\n";
98 o
<< " public String getImplementationName() {\n"
99 " return m_implementationName;\n }\n\n";
101 o
<< " public boolean supportsService( String sService ) {\n"
102 " int len = m_serviceNames.length;\n\n"
103 " for( int i=0; i < len; i++) {\n"
104 " if (sService.equals(m_serviceNames[i]))\n"
106 " }\n return false;\n }\n\n";
108 o
<< " public String[] getSupportedServiceNames() {\n"
109 " return m_serviceNames;\n }\n\n";
112 void generateXPropertySetBodies(std::ostream
& o
)
114 o
<< " // com.sun.star.beans.XPropertySet:\n";
115 o
<< " public com.sun.star.beans.XPropertySetInfo getPropertySetInfo()\n"
116 " {\n return m_prophlp.getPropertySetInfo();\n }\n\n";
118 o
<< " public void setPropertyValue(String aPropertyName, "
119 "Object aValue) throws "
120 "com.sun.star.beans.UnknownPropertyException, "
121 "com.sun.star.beans.PropertyVetoException, "
122 "com.sun.star.lang.IllegalArgumentException,"
123 "com.sun.star.lang.WrappedTargetException\n {\n "
124 "m_prophlp.setPropertyValue(aPropertyName, aValue);\n }\n\n";
126 o
<< " public Object getPropertyValue(String "
127 "aPropertyName) throws com.sun.star.beans.UnknownPropertyException, "
128 "com.sun.star.lang.WrappedTargetException\n {\n return "
129 "m_prophlp.getPropertyValue(aPropertyName);\n }\n\n";
131 o
<< " public void addPropertyChangeListener(String aPropertyName"
132 ", com.sun.star.beans.XPropertyChangeListener xListener) throws "
133 "com.sun.star.beans.UnknownPropertyException, "
134 "com.sun.star.lang.WrappedTargetException\n {\n "
135 "m_prophlp.addPropertyChangeListener(aPropertyName, xListener);\n }\n\n";
137 o
<< " public void removePropertyChangeListener(String "
138 "aPropertyName, com.sun.star.beans.XPropertyChangeListener xListener) "
139 "throws com.sun.star.beans.UnknownPropertyException, "
140 "com.sun.star.lang.WrappedTargetException\n {\n "
141 "m_prophlp.removePropertyChangeListener(aPropertyName, xListener);\n"
144 o
<< " public void addVetoableChangeListener(String aPropertyName"
145 ", com.sun.star.beans.XVetoableChangeListener xListener) throws "
146 "com.sun.star.beans.UnknownPropertyException, "
147 "com.sun.star.lang.WrappedTargetException\n {\n "
148 "m_prophlp.addVetoableChangeListener(aPropertyName, xListener);\n }\n\n";
150 o
<< " public void removeVetoableChangeListener(String "
151 "aPropertyName, com.sun.star.beans.XVetoableChangeListener xListener) "
152 "throws com.sun.star.beans.UnknownPropertyException, "
153 "com.sun.star.lang.WrappedTargetException\n {\n "
154 "m_prophlp.removeVetoableChangeListener(aPropertyName, xListener);\n }\n\n";
157 void generateXFastPropertySetBodies(std::ostream
& o
)
159 o
<< " // com.sun.star.beans.XFastPropertySet:\n";
161 o
<< " public void setFastPropertyValue(int nHandle, Object "
162 "aValue) throws com.sun.star.beans.UnknownPropertyException, "
163 "com.sun.star.beans.PropertyVetoException, "
164 "com.sun.star.lang.IllegalArgumentException, "
165 "com.sun.star.lang.WrappedTargetException\n {\n "
166 "m_prophlp.setFastPropertyValue(nHandle, aValue);\n }\n\n";
168 o
<< " public Object getFastPropertyValue(int nHandle) throws "
169 "com.sun.star.beans.UnknownPropertyException, "
170 "com.sun.star.lang.WrappedTargetException\n {\n return "
171 "m_prophlp.getFastPropertyValue(nHandle);\n }\n\n";
174 void generateXPropertyAccessBodies(std::ostream
& o
)
176 o
<< " // com.sun.star.beans.XPropertyAccess:\n";
178 o
<< " public com.sun.star.beans.PropertyValue[] getPropertyValues()\n"
179 " {\n return m_prophlp.getPropertyValues();\n }\n\n";
181 o
<< " public void setPropertyValues(com.sun.star.beans.PropertyValue[] "
182 "aProps) throws com.sun.star.beans.UnknownPropertyException, "
183 "com.sun.star.beans.PropertyVetoException, "
184 "com.sun.star.lang.IllegalArgumentException, "
185 "com.sun.star.lang.WrappedTargetException\n {\n "
186 "m_prophlp.setPropertyValues(aProps);\n }\n\n";
190 static bool checkAttribute(
191 OStringBuffer
& attributeValue
,
192 unoidl::AccumulationBasedServiceEntity::Property::Attributes attribute
)
195 sal_uInt16 attributes
[9] = {
196 /* com::sun::star::beans::PropertyValue::MAYBEVOID */ 1,
197 /* com::sun::star::beans::PropertyValue::BOUND */ 2,
198 /* com::sun::star::beans::PropertyValue::CONSTRAINED */ 4,
199 /* com::sun::star::beans::PropertyValue::TRANSIENT */ 8,
200 /* com::sun::star::beans::PropertyValue::READONLY */ 16,
201 /* com::sun::star::beans::PropertyValue::MAYBEAMBIGUOUS */ 32,
202 /* com::sun::star::beans::PropertyValue::MAYBEDEFAULT */ 64,
203 /* com::sun::star::beans::PropertyValue::REMOVABLE */ 128,
204 /* com::sun::star::beans::PropertyValue::OPTIONAL */ 256 };
206 for (sal_uInt16 i
= 0; i
< 9; i
++)
208 if (attribute
& attributes
[i
]) {
209 if (!attributeValue
.isEmpty()) {
211 attributeValue
.append("|");
213 switch (attributes
[i
])
216 attributeValue
.append("PropertyAttribute.MAYBEVOID");
219 attributeValue
.append("PropertyAttribute.BOUND");
222 attributeValue
.append("PropertyAttribute.CONSTRAINED");
225 attributeValue
.append("PropertyAttribute.TRANSIENT");
228 attributeValue
.append("PropertyAttribute.READONLY");
231 attributeValue
.append("PropertyAttribute.MAYBEAMBIGUOUS");
234 attributeValue
.append("PropertyAttribute.MAYBEDEFAULT");
237 attributeValue
.append("PropertyAttribute.REMOVABLE");
240 attributeValue
.append("PropertyAttribute.OPTIONAL");
246 attributeValue
.insert(0, '(');
247 attributeValue
.append(')');
253 static void registerProperties(std::ostream
& o
,
254 const AttributeInfo
& properties
,
255 const OString
& indentation
)
257 if (!properties
.empty()) {
259 OStringBuffer attributeValue
;
260 for (const auto& rProp
: properties
)
262 if (rProp
.attributes
!= 0) {
263 cast
= checkAttribute(attributeValue
, rProp
.attributes
);
266 attributeValue
.append('0');
269 o
<< indentation
<< "registerProperty(\"" << rProp
.name
270 << "\", \"m_" << rProp
.name
<< "\",\n"
271 << indentation
<< " ";
275 o
<< attributeValue
.makeStringAndClear() << ");\n";
280 static void generateXLocalizableBodies(std::ostream
& o
) {
281 // com.sun.star.lang.XLocalizable:
283 o
<< " // com.sun.star.lang.XLocalizable:\n"
284 " public void setLocale(com.sun.star.lang.Locale eLocale)\n {\n"
285 " m_locale = eLocale;\n }\n\n";
288 o
<< " public com.sun.star.lang.Locale getLocale()\n {\n"
289 " return m_locale;\n }\n\n";
292 static void generateXAddInBodies(std::ostream
& o
)
294 // com.sun.star.sheet.XAddIn:
295 // getProgrammaticFuntionName
296 o
<< " // com.sun.star.sheet.XAddIn:\n"
297 " public String getProgrammaticFuntionName(String "
298 "aDisplayName)\n {\n"
300 " com.sun.star.container.XNameAccess xNAccess =\n"
301 " (com.sun.star.container.XNameAccess)UnoRuntime."
303 " com.sun.star.container.XNameAccess.class, m_xHAccess);"
304 "\n String functions[] = xNAccess.getElementNames();\n"
305 " String sDisplayName = \"\";\n"
306 " int len = functions.length;\n"
307 " for (int i=0; i < len; ++i) {\n"
308 " sDisplayName = com.sun.star.uno.AnyConverter.toString(\n"
309 " getAddinProperty(functions[i], \"\", sDISPLAYNAME));\n"
310 " if (sDisplayName.equals(aDisplayName))\n"
311 " return functions[i];\n }\n"
312 " }\n catch ( com.sun.star.uno.RuntimeException e ) {\n"
314 " catch ( com.sun.star.uno.Exception e ) {\n }\n\n"
315 " return \"\";\n }\n\n";
317 // getDisplayFunctionName
318 o
<< " public String getDisplayFunctionName(String "
319 "aProgrammaticName)\n {\n"
320 " return getAddinProperty(aProgrammaticName, \"\", sDISPLAYNAME);\n"
323 // getFunctionDescription
324 o
<< " public String getFunctionDescription(String "
325 "aProgrammaticName)\n {\n"
326 " return getAddinProperty(aProgrammaticName, \"\", sDESCRIPTION);\n"
329 // getDisplayArgumentName
330 o
<< " public String getDisplayArgumentName(String "
331 "aProgrammaticFunctionName, int nArgument)\n {\n";
332 o
<< " return getAddinProperty(aProgrammaticFunctionName,\n"
333 " m_functionMap.get(\n"
334 " aProgrammaticFunctionName).get("
336 " sDISPLAYNAME);\n }\n\n";
338 // getArgumentDescription
339 o
<< " public String getArgumentDescription(String "
340 "aProgrammaticFunctionName, int nArgument)\n {\n";
341 o
<< " return getAddinProperty(aProgrammaticFunctionName,\n"
342 " m_functionMap.get(\n"
343 " aProgrammaticFunctionName).get("
345 " sDESCRIPTION);\n }\n\n";
347 // getProgrammaticCategoryName
348 o
<< " public String getProgrammaticCategoryName(String "
349 "aProgrammaticFunctionName)\n {\n"
350 " return getAddinProperty(aProgrammaticFunctionName, \"\", "
351 "sCATEGORY);\n }\n\n";
353 // getDisplayCategoryName
354 o
<< " public String getDisplayCategoryName(String "
355 "aProgrammaticFunctionName)\n {\n"
356 " return getAddinProperty(aProgrammaticFunctionName, \"\", "
357 "sCATEGORYDISPLAYNAME);\n }\n\n";
360 static void generateXCompatibilityNamesBodies(std::ostream
& o
)
362 o
<< " // com.sun.star.sheet.XCompatibilityNames:\n"
363 " public com.sun.star.sheet.LocalizedName[] getCompatibilityNames("
364 "String aProgrammaticName)\n {\n"
365 " com.sun.star.sheet.LocalizedName[] seqLocalizedNames =\n"
366 " new com.sun.star.sheet.LocalizedName[0];\n\n try {\n";
368 o
<< " StringBuffer path = new StringBuffer(aProgrammaticName);\n"
369 " path.append(\"/CompatibilityName\");\n"
370 " String hname = path.toString();\n\n";
372 o
<< " if ( m_xCompAccess.hasByHierarchicalName(hname) ) {\n"
373 " com.sun.star.container.XNameAccess xNameAccess =\n"
374 " (com.sun.star.container.XNameAccess)UnoRuntime."
376 " com.sun.star.container.XNameAccess.class,\n"
377 " m_xCompAccess.getByHierarchicalName(hname));\n\n"
378 " String elems[] = xNameAccess.getElementNames();\n"
379 " int len = elems.length;\n"
380 " seqLocalizedNames = new com.sun.star.sheet.LocalizedName"
381 "[len];\n String sCompatibilityName = \"\";\n\n";
383 o
<< " for (int i=0; i < len; ++i) {\n"
384 " String sLocale = elems[i];\n"
385 " sCompatibilityName = com.sun.star.uno.AnyConverter."
386 "toString(\n xNameAccess.getByName(sLocale));\n\n"
387 " com.sun.star.lang.Locale aLocale = \n"
388 " new com.sun.star.lang.Locale();\n\n"
389 /* FIXME-BCP47: this will break. */
390 " String tokens[] = sLocale.split(\"-\");\n"
391 " int nToken = tokens.length;\n"
392 " if (nToken >= 1) aLocale.Language = tokens[0];\n"
393 " if (nToken >= 2) aLocale.Country = tokens[1];\n"
394 " if (nToken >= 3) {\n"
395 " StringBuffer buf = \n"
396 " new StringBuffer(tokens[2]);\n"
397 " for (int t=3; t < nToken; ++t)\n"
398 " buf.append(tokens[t]);\n\n"
399 " aLocale.Variant = buf.toString();\n"
401 " seqLocalizedNames[i].Locale = aLocale;\n"
402 " seqLocalizedNames[i].Name = sCompatibilityName;\n"
404 " catch ( com.sun.star.uno.RuntimeException e ) {\n"
406 " catch ( com.sun.star.uno.Exception e ) {\n }\n\n"
407 " return seqLocalizedNames;\n }\n\n";
410 static void generateXInitializationBodies(std::ostream
& o
)
412 o
<< " // com.sun.star.lang.XInitialization:\n"
413 " public void initialize( Object[] object )\n"
414 " throws com.sun.star.uno.Exception\n {\n"
415 " if ( object.length > 0 )\n {\n"
416 " m_xFrame = (com.sun.star.frame.XFrame)UnoRuntime.queryInterface(\n"
417 " com.sun.star.frame.XFrame.class, object[0]);\n }\n }\n\n";
420 static void generateXDispatchBodies(std::ostream
& o
, ProgramOptions
const & options
)
422 // com.sun.star.frame.XDispatch
424 o
<< " // com.sun.star.frame.XDispatch:\n"
425 " public void dispatch( com.sun.star.util.URL aURL,\n"
426 " com.sun.star.beans.PropertyValue[] aArguments )\n {\n";
428 for (const auto& rEntry
: options
.protocolCmdMap
) {
429 o
<< " if ( aURL.Protocol.equals(\"" << rEntry
.first
432 for (const auto& rCmd
: rEntry
.second
) {
433 o
<< " if ( aURL.Path.equals(\"" << rCmd
<< "\") )\n"
434 " {\n // add your own code here\n"
443 o
<< " public void addStatusListener( com.sun.star.frame.XStatusListener xControl,\n"
444 " com.sun.star.util.URL aURL )\n {\n"
445 " // add your own code here\n }\n\n";
447 // com.sun.star.frame.XDispatch
448 o
<< " public void removeStatusListener( com.sun.star.frame.XStatusListener xControl,\n"
449 " com.sun.star.util.URL aURL )\n {\n"
450 " // add your own code here\n }\n\n";
453 static void generateXDispatchProviderBodies(std::ostream
& o
, ProgramOptions
const & options
)
455 // com.sun.star.frame.XDispatchProvider
457 o
<< " // com.sun.star.frame.XDispatchProvider:\n"
458 " public com.sun.star.frame.XDispatch queryDispatch( com.sun.star.util.URL aURL,\n"
459 " String sTargetFrameName,\n"
460 " int iSearchFlags )\n {\n";
462 for (const auto& rEntry
: options
.protocolCmdMap
) {
463 o
<< " if ( aURL.Protocol.equals(\"" << rEntry
.first
466 for (const auto& rCmd
: rEntry
.second
) {
467 o
<< " if ( aURL.Path.equals(\"" << rCmd
<< "\") )\n"
473 o
<< " return null;\n }\n\n";
476 o
<< " // com.sun.star.frame.XDispatchProvider:\n"
477 " public com.sun.star.frame.XDispatch[] queryDispatches(\n"
478 " com.sun.star.frame.DispatchDescriptor[] seqDescriptors )\n {\n"
479 " int nCount = seqDescriptors.length;\n"
480 " com.sun.star.frame.XDispatch[] seqDispatcher =\n"
481 " new com.sun.star.frame.XDispatch[seqDescriptors.length];\n\n"
482 " for( int i=0; i < nCount; ++i )\n {\n"
483 " seqDispatcher[i] = queryDispatch(seqDescriptors[i].FeatureURL,\n"
484 " seqDescriptors[i].FrameName,\n"
485 " seqDescriptors[i].SearchFlags );\n"
486 " }\n return seqDispatcher;\n }\n\n";
489 static void generateMethodBodies(std::ostream
& o
,
490 ProgramOptions
const & options
,
491 rtl::Reference
< TypeManager
> const & manager
,
492 const std::set
< OUString
>& interfaces
,
493 const OString
& indentation
, bool usepropertymixin
)
495 codemaker::GeneratedTypeSet generated
;
496 for (const OUString
& type
: interfaces
) {
497 if (type
== "com.sun.star.lang.XServiceInfo") {
498 generateXServiceInfoBodies(o
);
499 generated
.add(u2b(type
));
501 if (options
.componenttype
== 2) {
502 if (type
== "com.sun.star.lang.XServiceName") {
503 o
<< " // com.sun.star.lang.XServiceName:\n"
504 " public String getServiceName() {\n"
505 " return sADDIN_SERVICENAME;\n }\n";
506 generated
.add(u2b(type
));
508 } else if (type
== "com.sun.star.sheet.XAddIn") {
509 generateXAddInBodies(o
);
510 generated
.add(u2b(type
));
512 // special handling of XLocalizable -> parent of XAddIn
513 if (!generated
.contains("com.sun.star.lang.XLocalizable"_ostr
)) {
514 generateXLocalizableBodies(o
);
515 generated
.add("com.sun.star.lang.XLocalizable"_ostr
);
518 } else if (type
== "com.sun.star.lang.XLocalizable") {
519 generateXLocalizableBodies(o
);
520 generated
.add(u2b(type
));
522 } else if (type
== "com.sun.star.sheet.XCompatibilityNames") {
523 generateXCompatibilityNamesBodies(o
);
524 generated
.add(u2b(type
));
528 if (options
.componenttype
== 3) {
529 if (type
== "com.sun.star.lang.XInitialization") {
530 generateXInitializationBodies(o
);
531 generated
.add(u2b(type
));
533 } else if (type
== "com.sun.star.frame.XDispatch") {
534 generateXDispatchBodies(o
, options
);
535 generated
.add(u2b(type
));
537 } else if (type
== "com.sun.star.frame.XDispatchProvider") {
538 generateXDispatchProviderBodies(o
, options
);
539 generated
.add(u2b(type
));
543 printMethods(o
, options
, manager
, type
, generated
, "_"_ostr
,
544 indentation
, true, usepropertymixin
);
549 const char* const propcomment
=
550 " // use the last parameter of the PropertySetMixin constructor\n"
551 " // for your optional attributes if necessary. See the documentation\n"
552 " // of the PropertySetMixin helper for further information.\n"
553 " // Ensure that your attributes are initialized correctly!\n";
556 static void generateAddinConstructorAndHelper(std::ostream
& o
,
557 ProgramOptions
const & options
,
558 rtl::Reference
< TypeManager
> const & manager
, const OString
& classname
,
559 const std::set
< OUString
>& services
,
560 const std::set
< OUString
>& interfaces
)
562 o
<< " private com.sun.star.lang.Locale m_locale = "
563 "new com.sun.star.lang.Locale();\n";
565 if (!options
.backwardcompatible
) {
567 o
<< "\n public " << classname
<< "( XComponentContext context )\n"
568 " {\n m_xContext = context;\n }\n\n";
573 // get the one and only add-in service for later use
574 std::set
< OUString
>::const_iterator iter
= services
.begin();
575 OUString sAddinService
= *iter
;
576 if (sAddinService
== "com.sun.star.sheet.AddIn") {
577 sAddinService
= *(++iter
);
581 // add-in specific fields
582 o
<< "\n private static final String sADDIN_SERVICENAME = \""
583 << sAddinService
<< "\";\n\n";
584 o
<< " private static final String sDISPLAYNAME = "
586 " private static final String sDESCRIPTION = "
588 " private static final String sCATEGORY = \"Category\";\n"
589 " private static final String sCATEGORYDISPLAYNAME = "
590 "\"CategoryDisplayName\";\n\n";
592 o
<< " private com.sun.star.container.XHierarchicalNameAccess "
593 "m_xHAccess = null;\n"
594 " private com.sun.star.container.XHierarchicalNameAccess "
595 "m_xCompAccess = null;\n";
596 o
<< " private java.util.Hashtable<\n String, "
597 "java.util.Hashtable< Integer, String> > m_functionMap = null;\n\n";
600 o
<< "\n public " << classname
<< "( XComponentContext context )\n {\n"
601 " m_xContext = context;\n\n"
604 o
<< " m_functionMap = new java.util.Hashtable<\n"
605 " String, java.util.Hashtable< Integer, String > >();\n\n";
607 generateFunctionParameterMap(o
, options
, manager
, interfaces
);
609 o
<< " com.sun.star.lang.XMultiServiceFactory xProvider = \n"
610 " (com.sun.star.lang.XMultiServiceFactory)UnoRuntime."
612 " com.sun.star.lang.XMultiServiceFactory.class,\n"
613 " m_xContext.getServiceManager().createInstanceWithContext("
614 "\n \"com.sun.star.configuration.ConfigurationProvider\""
615 ",\n m_xContext));\n\n";
617 o
<< " String sReadOnlyView = "
618 "\"com.sun.star.configuration.ConfigurationAccess\";\n\n";
620 o
<< " StringBuffer sPath = new StringBuffer(\n"
621 " \"/org.openoffice.Office.CalcAddIns/AddInInfo/\");\n"
622 " sPath.append(sADDIN_SERVICENAME);\n"
623 " sPath.append(\"/AddInFunctions\");\n\n";
625 o
<< " // create arguments: nodepath\n"
626 " com.sun.star.beans.PropertyValue aArgument = \n"
627 " new com.sun.star.beans.PropertyValue();\n"
628 " aArgument.Name = \"nodepath\";\n"
629 " aArgument.Value = new com.sun.star.uno.Any(\n"
630 " com.sun.star.uno.Type.STRING, sPath.toString());\n\n";
632 o
<< " Object aArguments[] = new Object[1];\n"
633 " aArguments[0] = new com.sun.star.uno.Any("
634 " new com.sun.star.uno.Type(\n"
635 " com.sun.star.beans.PropertyValue.class), aArgument);\n\n";
637 o
<< " // create the default view using default UI locale\n"
638 " Object xIface = \n"
639 " xProvider.createInstanceWithArguments(sReadOnlyView, "
642 o
<< " m_xHAccess = (com.sun.star.container.XHierarchicalNameAccess)\n"
643 " UnoRuntime.queryInterface(\n"
644 " com.sun.star.container.XHierarchicalNameAccess.class, "
647 o
<< " // extends arguments to create a view for all locales to get "
648 "simple\n // access to the compatibilityname property\n"
649 " aArguments = new Object[2];\n"
650 " aArguments[0] = new com.sun.star.uno.Any( "
651 "new com.sun.star.uno.Type(\n"
652 " com.sun.star.beans.PropertyValue.class), aArgument);\n"
653 " aArgument.Name = \"locale\";\n"
654 " aArgument.Value = new com.sun.star.uno.Any(\n"
655 " com.sun.star.uno.Type.STRING, \"*\");\n"
656 " aArguments[1] = new com.sun.star.uno.Any( "
657 " new com.sun.star.uno.Type(\n"
658 " com.sun.star.beans.PropertyValue.class), aArgument);\n\n";
660 o
<< " // create view for all locales\n"
661 " xIface = xProvider.createInstanceWithArguments(sReadOnlyView, "
663 " m_xCompAccess = (com.sun.star.container.XHierarchicalNameAccess)\n"
664 " UnoRuntime.queryInterface(\n"
665 " com.sun.star.container.XHierarchicalNameAccess.class, "
667 " catch ( com.sun.star.uno.Exception e ) {\n }\n }\n\n";
669 // add-in helper function
670 o
<< " // addin configuration property helper function:\n"
671 " String getAddinProperty(String funcName, "
672 "String paramName, String propName)\n {\n"
673 " try {\n StringBuffer buf = "
674 "new StringBuffer(funcName);\n\n"
675 " if (paramName.length() > 0) {\n"
676 " buf.append(\"/Parameters/\");\n"
677 " buf.append(paramName);\n }\n\n";
679 o
<< " com.sun.star.beans.XPropertySet xPropSet =\n"
680 " (com.sun.star.beans.XPropertySet)UnoRuntime."
682 " com.sun.star.beans.XPropertySet.class,\n"
683 " m_xHAccess.getByHierarchicalName(buf.toString()));\n\n"
684 " return com.sun.star.uno.AnyConverter.toString(\n"
685 " xPropSet.getPropertyValue(propName));\n }\n"
686 " catch ( com.sun.star.uno.RuntimeException e ) {\n"
688 " catch ( com.sun.star.uno.Exception e ) {\n }\n"
689 " return \"\";\n }\n\n";
693 static void generateClassDefinition(std::ostream
& o
,
694 ProgramOptions
const & options
,
695 rtl::Reference
< TypeManager
> const & manager
,
696 const OString
& classname
,
697 const std::set
< OUString
>& services
,
698 const std::set
< OUString
>& interfaces
,
699 const AttributeInfo
& properties
,
700 const AttributeInfo
& attributes
,
701 const OUString
& propertyhelper
, bool supportxcomponent
)
703 o
<< "\n\npublic final class " << classname
<< " extends ";
705 if (!interfaces
.empty()) {
706 if (propertyhelper
== "_") {
707 o
<< "PropertySet\n";
709 if (supportxcomponent
)
710 o
<< "ComponentBase\n";
715 std::set
< OUString
>::const_iterator iter
= interfaces
.begin();
716 while (iter
!= interfaces
.end()) {
719 if (iter
!= interfaces
.end())
725 o
<< " private final XComponentContext m_xContext;\n";
727 // additional member for add-ons
728 if (options
.componenttype
== 3) {
729 o
<< " private com.sun.star.frame.XFrame m_xFrame;\n";
732 // check property helper
733 if (propertyhelper
.getLength() > 1)
734 o
<< " private final PropertySetMixin m_prophlp;\n";
736 o
<< " private static final String m_implementationName = "
737 << classname
<< ".class.getName();\n";
739 if (!services
.empty()) {
740 o
<< " private static final String[] m_serviceNames = {\n";
741 std::set
< OUString
>::const_iterator iter
= services
.begin();
742 while (iter
!= services
.end()) {
743 o
<< " \"" << (*iter
).replace('/','.') << "\"";
745 if (iter
!= services
.end())
752 // attribute/property members
753 if (!properties
.empty()) {
754 o
<< " // properties\n";
755 for (const auto& rProp
: properties
) {
757 printType(o
, options
, manager
, rProp
.type
, false);
758 o
<< " m_" << rProp
.name
<< ";\n";
760 } else if (!attributes
.empty()) {
761 o
<< " // attributes\n";
762 for (const auto& rAttr
: attributes
) {
764 printType(o
, options
, manager
, rAttr
.type
, false);
765 o
<< " m_" << rAttr
.name
<< " = ";
766 printType(o
, options
, manager
, rAttr
.type
, false, true);
771 // special handling of calc add-ins
772 if (options
.componenttype
== 2)
774 generateAddinConstructorAndHelper(o
, options
, manager
, classname
,
775 services
, interfaces
);
777 o
<< "\n public " << classname
<< "( XComponentContext context )\n"
778 " {\n m_xContext = context;\n";
779 if (propertyhelper
== "_") {
780 registerProperties(o
, properties
, " "_ostr
);
782 if (propertyhelper
.getLength() > 1) {
784 << " m_prophlp = new PropertySetMixin(m_xContext, this,\n"
785 " new Type(" << propertyhelper
786 << ".class), null);\n";
793 if (!services
.empty())
794 generateCompFunctions(o
, classname
);
796 generateMethodBodies(o
, options
, manager
, interfaces
,
797 " "_ostr
, propertyhelper
.getLength() > 1);
799 // end of class definition
803 void generateSkeleton(ProgramOptions
const & options
,
804 rtl::Reference
< TypeManager
> const & manager
,
805 std::vector
< OString
> const & types
)
807 std::set
< OUString
> interfaces
;
808 std::set
< OUString
> services
;
809 AttributeInfo properties
;
810 AttributeInfo attributes
;
811 std::set
< OUString
> propinterfaces
;
812 bool serviceobject
= false;
813 bool supportxcomponent
= false;
815 for (const auto& rType
: types
) {
816 checkType(manager
, b2u(rType
), interfaces
, services
, properties
);
819 if (options
.componenttype
== 3) {
820 // the Protocolhandler service is mandatory for a protocol handler add-on,
821 // so it is defaulted. The XDispatchProvider provides Dispatch objects for
822 // certain functions and the generated impl object implements XDispatch
823 // directly for simplicity reasons.
824 checkType(manager
, u
"com.sun.star.frame.ProtocolHandler"_ustr
,
825 interfaces
, services
, properties
);
826 checkType(manager
, u
"com.sun.star.frame.XDispatch"_ustr
,
827 interfaces
, services
, properties
);
830 if (options
.componenttype
== 2) {
831 if (services
.size() != 1) {
832 throw CannotDumpException(
833 u
"for calc add-in components one and only one service type is "
834 "necessary! Please reference a valid type with the '-t' option."_ustr
);
837 // if backwardcompatible==true the AddIn service needs to be added to the
838 // supported service list, the necessary interfaces are mapped to the add-in
839 // configuration. Since OO.org 2.0.4 this is obsolete and the add-in is
840 // taken from the configuration from Calc directly, this simplifies the
842 if (options
.backwardcompatible
) {
843 checkType(manager
, u
"com.sun.star.sheet.AddIn"_ustr
,
844 interfaces
, services
, properties
);
846 // special case for the optional XLocalization interface. It should be
847 // implemented always. But it is parent of the XAddIn and we need it only
848 // if backwardcompatible is false.
849 interfaces
.insert(u
"com.sun.star.lang.XLocalizable"_ustr
);
854 // check if service object or simple UNO object
855 if (!services
.empty())
856 serviceobject
= true;
858 OUString propertyhelper
= checkPropertyHelper(
859 options
, manager
, services
, interfaces
, attributes
, propinterfaces
);
860 checkDefaultInterfaces(interfaces
, services
, propertyhelper
);
862 if (options
.componenttype
== 2) {
863 if (!propertyhelper
.isEmpty())
864 std::cerr
<< "WARNING: interfaces specifying calc add-in functions "
865 "shouldn't support attributes!\n";
868 supportxcomponent
= checkXComponentSupport(manager
, interfaces
);
870 OString compFileName
;
872 std::ostream
* pofs
= nullptr;
873 bool standardout
= getOutputStream(options
, ".java"_ostr
,
874 &pofs
, compFileName
, tmpFileName
);
877 if (!standardout
&& options
.license
) {
878 printLicenseHeader(*pofs
);
881 generatePackage(*pofs
, options
.implname
);
883 generateImports(*pofs
, options
, propertyhelper
,
884 serviceobject
, supportxcomponent
);
886 OString
classname(options
.implname
);
888 if ((index
= classname
.lastIndexOf('.')) > 0)
889 classname
= classname
.copy(index
+1);
891 generateClassDefinition(*pofs
, options
, manager
, classname
, services
,
892 interfaces
, properties
, attributes
, propertyhelper
,
895 if ( !standardout
&& pofs
&& static_cast<std::ofstream
*>(pofs
)->is_open()) {
896 static_cast<std::ofstream
*>(pofs
)->close();
898 OSL_VERIFY(makeValidTypeFile(compFileName
, tmpFileName
, false));
900 } catch (CannotDumpException
& e
) {
901 std::cerr
<< "ERROR: " << e
.getMessage() << "\n";
902 if ( !standardout
) {
903 if (pofs
&& static_cast<std::ofstream
*>(pofs
)->is_open()) {
904 static_cast<std::ofstream
*>(pofs
)->close();
907 // remove existing type file if something goes wrong to ensure
909 if (fileExists(compFileName
))
910 removeTypeFile(compFileName
);
912 // remove tmp file if something goes wrong
913 removeTypeFile(tmpFileName
);
921 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */