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 <rtl/strbuf.hxx>
26 #include "skeletoncommon.hxx"
27 #include "skeletonjava.hxx"
31 using namespace ::codemaker::java
;
33 namespace skeletonmaker::java
{
35 static void generatePackage(std::ostream
& o
, std::string_view implname
)
37 size_t index
= implname
.rfind('.');
38 if (index
!= std::string_view::npos
)
39 o
<< "package " << implname
.substr(0, index
) << ";\n\n";
42 static void generateImports(std::ostream
& o
, ProgramOptions
const & options
,
43 std::u16string_view propertyhelper
,
44 bool serviceobject
, bool supportxcomponent
)
46 if (options
.componenttype
== 3)
47 o
<< "import com.sun.star.uno.UnoRuntime;\n";
48 o
<< "import com.sun.star.uno.XComponentContext;\n";
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";
55 if (propertyhelper
!= u
"_") {
56 if (supportxcomponent
)
57 o
<< "import com.sun.star.lib.uno.helper.ComponentBase;\n";
59 o
<< "import com.sun.star.lib.uno.helper.WeakBase;\n";
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";
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";
76 static void generateCompFunctions(std::ostream
& o
, const OString
& classname
)
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";
86 o
<< " public static boolean __writeRegistryServiceInfo("
87 " XRegistryKey xRegistryKey ) {\n"
88 " return Factory.writeRegistryServiceInfo(m_implementationName,\n"
94 static void generateXServiceInfoBodies(std::ostream
& o
)
96 o
<< " // com.sun.star.lang.XServiceInfo:\n";
97 o
<< " public String getImplementationName() {\n"
98 " return m_implementationName;\n }\n\n";
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"
105 " }\n return false;\n }\n\n";
107 o
<< " public String[] getSupportedServiceNames() {\n"
108 " return m_serviceNames;\n }\n\n";
111 void generateXPropertySetBodies(std::ostream
& o
)
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";
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";
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";
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";
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"
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";
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";
156 void generateXFastPropertySetBodies(std::ostream
& o
)
158 o
<< " // com.sun.star.beans.XFastPropertySet:\n";
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";
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";
173 void generateXPropertyAccessBodies(std::ostream
& o
)
175 o
<< " // com.sun.star.beans.XPropertyAccess:\n";
177 o
<< " public com.sun.star.beans.PropertyValue[] getPropertyValues()\n"
178 " {\n return m_prophlp.getPropertyValues();\n }\n\n";
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";
189 static bool checkAttribute(
190 OStringBuffer
& attributeValue
,
191 unoidl::AccumulationBasedServiceEntity::Property::Attributes attribute
)
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 };
205 for (sal_uInt16 i
= 0; i
< 9; i
++)
207 if (attribute
& attributes
[i
]) {
208 if (!attributeValue
.isEmpty()) {
210 attributeValue
.append("|");
212 switch (attributes
[i
])
215 attributeValue
.append("PropertyAttribute.MAYBEVOID");
218 attributeValue
.append("PropertyAttribute.BOUND");
221 attributeValue
.append("PropertyAttribute.CONSTRAINED");
224 attributeValue
.append("PropertyAttribute.TRANSIENT");
227 attributeValue
.append("PropertyAttribute.READONLY");
230 attributeValue
.append("PropertyAttribute.MAYBEAMBIGUOUS");
233 attributeValue
.append("PropertyAttribute.MAYBEDEFAULT");
236 attributeValue
.append("PropertyAttribute.REMOVABLE");
239 attributeValue
.append("PropertyAttribute.OPTIONAL");
245 attributeValue
.insert(0, '(');
246 attributeValue
.append(')');
252 static void registerProperties(std::ostream
& o
,
253 const AttributeInfo
& properties
,
254 const OString
& indentation
)
256 if (!properties
.empty()) {
258 OStringBuffer attributeValue
;
259 for (const auto& rProp
: properties
)
261 if (rProp
.attributes
!= 0) {
262 cast
= checkAttribute(attributeValue
, rProp
.attributes
);
265 attributeValue
.append('0');
268 o
<< indentation
<< "registerProperty(\"" << rProp
.name
269 << "\", \"m_" << rProp
.name
<< "\",\n"
270 << indentation
<< " ";
274 o
<< attributeValue
.makeStringAndClear() << ");\n";
279 static void generateXLocalizableBodies(std::ostream
& o
) {
280 // com.sun.star.lang.XLocalizable:
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";
287 o
<< " public com.sun.star.lang.Locale getLocale()\n {\n"
288 " return m_locale;\n }\n\n";
291 static void generateXAddInBodies(std::ostream
& o
)
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"
299 " com.sun.star.container.XNameAccess xNAccess =\n"
300 " (com.sun.star.container.XNameAccess)UnoRuntime."
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"
313 " catch ( com.sun.star.uno.Exception e ) {\n }\n\n"
314 " return \"\";\n }\n\n";
316 // getDisplayFunctionName
317 o
<< " public String getDisplayFunctionName(String "
318 "aProgrammaticName)\n {\n"
319 " return getAddinProperty(aProgrammaticName, \"\", sDISPLAYNAME);\n"
322 // getFunctionDescription
323 o
<< " public String getFunctionDescription(String "
324 "aProgrammaticName)\n {\n"
325 " return getAddinProperty(aProgrammaticName, \"\", sDESCRIPTION);\n"
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("
335 " sDISPLAYNAME);\n }\n\n";
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("
344 " sDESCRIPTION);\n }\n\n";
346 // getProgrammaticCategoryName
347 o
<< " public String getProgrammaticCategoryName(String "
348 "aProgrammaticFunctionName)\n {\n"
349 " return getAddinProperty(aProgrammaticFunctionName, \"\", "
350 "sCATEGORY);\n }\n\n";
352 // getDisplayCategoryName
353 o
<< " public String getDisplayCategoryName(String "
354 "aProgrammaticFunctionName)\n {\n"
355 " return getAddinProperty(aProgrammaticFunctionName, \"\", "
356 "sCATEGORYDISPLAYNAME);\n }\n\n";
359 static void generateXCompatibilityNamesBodies(std::ostream
& o
)
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";
367 o
<< " StringBuffer path = new StringBuffer(aProgrammaticName);\n"
368 " path.append(\"/CompatibilityName\");\n"
369 " String hname = path.toString();\n\n";
371 o
<< " if ( m_xCompAccess.hasByHierarchicalName(hname) ) {\n"
372 " com.sun.star.container.XNameAccess xNameAccess =\n"
373 " (com.sun.star.container.XNameAccess)UnoRuntime."
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";
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"
400 " seqLocalizedNames[i].Locale = aLocale;\n"
401 " seqLocalizedNames[i].Name = sCompatibilityName;\n"
403 " catch ( com.sun.star.uno.RuntimeException e ) {\n"
405 " catch ( com.sun.star.uno.Exception e ) {\n }\n\n"
406 " return seqLocalizedNames;\n }\n\n";
409 static void generateXInitializationBodies(std::ostream
& o
)
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";
419 static void generateXDispatchBodies(std::ostream
& o
, ProgramOptions
const & options
)
421 // com.sun.star.frame.XDispatch
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";
427 for (const auto& rEntry
: options
.protocolCmdMap
) {
428 o
<< " if ( aURL.Protocol.equals(\"" << rEntry
.first
431 for (const auto& rCmd
: rEntry
.second
) {
432 o
<< " if ( aURL.Path.equals(\"" << rCmd
<< "\") )\n"
433 " {\n // add your own code here\n"
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";
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";
452 static void generateXDispatchProviderBodies(std::ostream
& o
, ProgramOptions
const & options
)
454 // com.sun.star.frame.XDispatchProvider
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";
461 for (const auto& rEntry
: options
.protocolCmdMap
) {
462 o
<< " if ( aURL.Protocol.equals(\"" << rEntry
.first
465 for (const auto& rCmd
: rEntry
.second
) {
466 o
<< " if ( aURL.Path.equals(\"" << rCmd
<< "\") )\n"
472 o
<< " return null;\n }\n\n";
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";
488 static 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
)
494 codemaker::GeneratedTypeSet generated
;
495 for (const OUString
& type
: interfaces
) {
496 if (type
== "com.sun.star.lang.XServiceInfo") {
497 generateXServiceInfoBodies(o
);
498 generated
.add(u2b(type
));
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
));
507 } else if (type
== "com.sun.star.sheet.XAddIn") {
508 generateXAddInBodies(o
);
509 generated
.add(u2b(type
));
511 // special handling of XLocalizable -> parent of XAddIn
512 if (!generated
.contains("com.sun.star.lang.XLocalizable"_ostr
)) {
513 generateXLocalizableBodies(o
);
514 generated
.add("com.sun.star.lang.XLocalizable"_ostr
);
517 } else if (type
== "com.sun.star.lang.XLocalizable") {
518 generateXLocalizableBodies(o
);
519 generated
.add(u2b(type
));
521 } else if (type
== "com.sun.star.sheet.XCompatibilityNames") {
522 generateXCompatibilityNamesBodies(o
);
523 generated
.add(u2b(type
));
527 if (options
.componenttype
== 3) {
528 if (type
== "com.sun.star.lang.XInitialization") {
529 generateXInitializationBodies(o
);
530 generated
.add(u2b(type
));
532 } else if (type
== "com.sun.star.frame.XDispatch") {
533 generateXDispatchBodies(o
, options
);
534 generated
.add(u2b(type
));
536 } else if (type
== "com.sun.star.frame.XDispatchProvider") {
537 generateXDispatchProviderBodies(o
, options
);
538 generated
.add(u2b(type
));
542 printMethods(o
, options
, manager
, type
, generated
, "_"_ostr
,
543 indentation
, true, usepropertymixin
);
548 const 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";
555 static 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
)
561 o
<< " private com.sun.star.lang.Locale m_locale = "
562 "new com.sun.star.lang.Locale();\n";
564 if (!options
.backwardcompatible
) {
566 o
<< "\n public " << classname
<< "( XComponentContext context )\n"
567 " {\n m_xContext = context;\n }\n\n";
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
);
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 = "
585 " private static final String sDESCRIPTION = "
587 " private static final String sCATEGORY = \"Category\";\n"
588 " private static final String sCATEGORYDISPLAYNAME = "
589 "\"CategoryDisplayName\";\n\n";
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";
599 o
<< "\n public " << classname
<< "( XComponentContext context )\n {\n"
600 " m_xContext = context;\n\n"
603 o
<< " m_functionMap = new java.util.Hashtable<\n"
604 " String, java.util.Hashtable< Integer, String > >();\n\n";
606 generateFunctionParameterMap(o
, options
, manager
, interfaces
);
608 o
<< " com.sun.star.lang.XMultiServiceFactory xProvider = \n"
609 " (com.sun.star.lang.XMultiServiceFactory)UnoRuntime."
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";
616 o
<< " String sReadOnlyView = "
617 "\"com.sun.star.configuration.ConfigurationAccess\";\n\n";
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";
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";
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";
636 o
<< " // create the default view using default UI locale\n"
637 " Object xIface = \n"
638 " xProvider.createInstanceWithArguments(sReadOnlyView, "
641 o
<< " m_xHAccess = (com.sun.star.container.XHierarchicalNameAccess)\n"
642 " UnoRuntime.queryInterface(\n"
643 " com.sun.star.container.XHierarchicalNameAccess.class, "
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";
659 o
<< " // create view for all locales\n"
660 " xIface = xProvider.createInstanceWithArguments(sReadOnlyView, "
662 " m_xCompAccess = (com.sun.star.container.XHierarchicalNameAccess)\n"
663 " UnoRuntime.queryInterface(\n"
664 " com.sun.star.container.XHierarchicalNameAccess.class, "
666 " catch ( com.sun.star.uno.Exception e ) {\n }\n }\n\n";
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";
678 o
<< " com.sun.star.beans.XPropertySet xPropSet =\n"
679 " (com.sun.star.beans.XPropertySet)UnoRuntime."
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"
687 " catch ( com.sun.star.uno.Exception e ) {\n }\n"
688 " return \"\";\n }\n\n";
692 static 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
,
698 const AttributeInfo
& properties
,
699 const AttributeInfo
& attributes
,
700 const OUString
& propertyhelper
, bool supportxcomponent
)
702 o
<< "\n\npublic final class " << classname
<< " extends ";
704 if (!interfaces
.empty()) {
705 if (propertyhelper
== "_") {
706 o
<< "PropertySet\n";
708 if (supportxcomponent
)
709 o
<< "ComponentBase\n";
714 std::set
< OUString
>::const_iterator iter
= interfaces
.begin();
715 while (iter
!= interfaces
.end()) {
718 if (iter
!= interfaces
.end())
724 o
<< " private final XComponentContext m_xContext;\n";
726 // additional member for add-ons
727 if (options
.componenttype
== 3) {
728 o
<< " private com.sun.star.frame.XFrame m_xFrame;\n";
731 // check property helper
732 if (propertyhelper
.getLength() > 1)
733 o
<< " private final PropertySetMixin m_prophlp;\n";
735 o
<< " private static final String m_implementationName = "
736 << classname
<< ".class.getName();\n";
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('/','.') << "\"";
744 if (iter
!= services
.end())
751 // attribute/property members
752 if (!properties
.empty()) {
753 o
<< " // properties\n";
754 for (const auto& rProp
: properties
) {
756 printType(o
, options
, manager
, rProp
.type
, false);
757 o
<< " m_" << rProp
.name
<< ";\n";
759 } else if (!attributes
.empty()) {
760 o
<< " // attributes\n";
761 for (const auto& rAttr
: attributes
) {
763 printType(o
, options
, manager
, rAttr
.type
, false);
764 o
<< " m_" << rAttr
.name
<< " = ";
765 printType(o
, options
, manager
, rAttr
.type
, false, true);
770 // special handling of calc add-ins
771 if (options
.componenttype
== 2)
773 generateAddinConstructorAndHelper(o
, options
, manager
, classname
,
774 services
, interfaces
);
776 o
<< "\n public " << classname
<< "( XComponentContext context )\n"
777 " {\n m_xContext = context;\n";
778 if (propertyhelper
== "_") {
779 registerProperties(o
, properties
, " "_ostr
);
781 if (propertyhelper
.getLength() > 1) {
783 << " m_prophlp = new PropertySetMixin(m_xContext, this,\n"
784 " new Type(" << propertyhelper
785 << ".class), null);\n";
792 if (!services
.empty())
793 generateCompFunctions(o
, classname
);
795 generateMethodBodies(o
, options
, manager
, interfaces
,
796 " "_ostr
, propertyhelper
.getLength() > 1);
798 // end of class definition
802 void generateSkeleton(ProgramOptions
const & options
,
803 rtl::Reference
< TypeManager
> const & manager
,
804 std::vector
< OString
> const & types
)
806 std::set
< OUString
> interfaces
;
807 std::set
< OUString
> services
;
808 AttributeInfo properties
;
809 AttributeInfo attributes
;
810 std::set
< OUString
> propinterfaces
;
811 bool serviceobject
= false;
812 bool supportxcomponent
= false;
814 for (const auto& rType
: types
) {
815 checkType(manager
, b2u(rType
), interfaces
, services
, properties
);
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
);
829 if (options
.componenttype
== 2) {
830 if (services
.size() != 1) {
831 throw CannotDumpException(
832 "for calc add-in components one and only one service type is "
833 "necessary! Please reference a valid type with the '-t' option.");
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
841 if (options
.backwardcompatible
) {
842 checkType(manager
, "com.sun.star.sheet.AddIn",
843 interfaces
, services
, properties
);
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");
853 // check if service object or simple UNO object
854 if (!services
.empty())
855 serviceobject
= true;
857 OUString propertyhelper
= checkPropertyHelper(
858 options
, manager
, services
, interfaces
, attributes
, propinterfaces
);
859 checkDefaultInterfaces(interfaces
, services
, propertyhelper
);
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";
867 supportxcomponent
= checkXComponentSupport(manager
, interfaces
);
869 OString compFileName
;
871 std::ostream
* pofs
= nullptr;
872 bool standardout
= getOutputStream(options
, ".java"_ostr
,
873 &pofs
, compFileName
, tmpFileName
);
876 if (!standardout
&& options
.license
) {
877 printLicenseHeader(*pofs
);
880 generatePackage(*pofs
, options
.implname
);
882 generateImports(*pofs
, options
, propertyhelper
,
883 serviceobject
, supportxcomponent
);
885 OString
classname(options
.implname
);
887 if ((index
= classname
.lastIndexOf('.')) > 0)
888 classname
= classname
.copy(index
+1);
890 generateClassDefinition(*pofs
, options
, manager
, classname
, services
,
891 interfaces
, properties
, attributes
, propertyhelper
,
894 if ( !standardout
&& pofs
&& static_cast<std::ofstream
*>(pofs
)->is_open()) {
895 static_cast<std::ofstream
*>(pofs
)->close();
897 OSL_VERIFY(makeValidTypeFile(compFileName
, tmpFileName
, false));
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();
906 // remove existing type file if something goes wrong to ensure
908 if (fileExists(compFileName
))
909 removeTypeFile(compFileName
);
911 // remove tmp file if something goes wrong
912 removeTypeFile(tmpFileName
);
920 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */