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
{ namespace java
{
35 void generatePackage(std::ostream
& o
, const OString
& implname
)
37 sal_Int32 index
= implname
.lastIndexOf('.');
39 o
<< "package " << implname
.copy(0, index
) << ";\n\n";
42 void generateImports(std::ostream
& o
, ProgramOptions
const & options
,
43 const OUString
& 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
!= "_") {
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
.isEmpty()) {
62 if (propertyhelper
== "_") {
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 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 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";
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::MAYBEAMBIGIOUS */ 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.MAYBEAMBIGIOUS");
233 attributeValue
.append("PropertyAttribute.MAYBEDEFAULT");
236 attributeValue
.append("PropertyAttribute.REMOVABLE");
239 attributeValue
.append("PropertyAttribute.OPTIONAL");
245 attributeValue
.insert(0, '(');
246 attributeValue
.append(')');
252 void registerProperties(std::ostream
& o
,
253 const AttributeInfo
& properties
,
254 const OString
& indentation
)
256 if (!properties
.empty()) {
258 OStringBuffer attributeValue
;
259 for (AttributeInfo::const_iterator
i(properties
.begin());
260 i
!= properties
.end(); ++i
)
262 if (i
->attributes
!= 0) {
263 cast
= checkAttribute(attributeValue
, i
->attributes
);
266 attributeValue
.append('0');
269 o
<< indentation
<< "registerProperty(\"" << i
->name
270 << "\", \"m_" << i
->name
<< "\",\n"
271 << indentation
<< " ";
275 o
<< attributeValue
.makeStringAndClear() << ");\n";
280 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 void generateXAddInBodies(std::ostream
& o
, ProgramOptions
const &)
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 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 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 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 ProtocolCmdMap::const_iterator iter
= options
.protocolCmdMap
.begin();
429 while (iter
!= options
.protocolCmdMap
.end()) {
430 o
<< " if ( aURL.Protocol.equals(\"" << (*iter
).first
433 for (std::vector
< OString
>::const_iterator i
= (*iter
).second
.begin();
434 i
!= (*iter
).second
.end(); ++i
) {
435 o
<< " if ( aURL.Path.equals(\"" << (*i
) << "\") )\n"
436 " {\n // add your own code here\n"
446 o
<< " public void addStatusListener( com.sun.star.frame.XStatusListener xControl,\n"
447 " com.sun.star.util.URL aURL )\n {\n"
448 " // add your own code here\n }\n\n";
450 // com.sun.star.frame.XDispatch
451 o
<< " public void removeStatusListener( com.sun.star.frame.XStatusListener xControl,\n"
452 " com.sun.star.util.URL aURL )\n {\n"
453 " // add your own code here\n }\n\n";
456 void generateXDispatchProviderBodies(std::ostream
& o
, ProgramOptions
const & options
)
458 // com.sun.star.frame.XDispatchProvider
460 o
<< " // com.sun.star.frame.XDispatchProvider:\n"
461 " public com.sun.star.frame.XDispatch queryDispatch( com.sun.star.util.URL aURL,\n"
462 " String sTargetFrameName,\n"
463 " int iSearchFlags )\n {\n";
465 ProtocolCmdMap::const_iterator iter
= options
.protocolCmdMap
.begin();
466 while (iter
!= options
.protocolCmdMap
.end()) {
467 o
<< " if ( aURL.Protocol.equals(\"" << (*iter
).first
470 for (std::vector
< OString
>::const_iterator i
= (*iter
).second
.begin();
471 i
!= (*iter
).second
.end(); ++i
) {
472 o
<< " if ( aURL.Path.equals(\"" << (*i
) << "\") )\n"
479 o
<< " return null;\n }\n\n";
482 o
<< " // com.sun.star.frame.XDispatchProvider:\n"
483 " public com.sun.star.frame.XDispatch[] queryDispatches(\n"
484 " com.sun.star.frame.DispatchDescriptor[] seqDescriptors )\n {\n"
485 " int nCount = seqDescriptors.length;\n"
486 " com.sun.star.frame.XDispatch[] seqDispatcher =\n"
487 " new com.sun.star.frame.XDispatch[seqDescriptors.length];\n\n"
488 " for( int i=0; i < nCount; ++i )\n {\n"
489 " seqDispatcher[i] = queryDispatch(seqDescriptors[i].FeatureURL,\n"
490 " seqDescriptors[i].FrameName,\n"
491 " seqDescriptors[i].SearchFlags );\n"
492 " }\n return seqDispatcher;\n }\n\n";
495 void generateMethodBodies(std::ostream
& o
,
496 ProgramOptions
const & options
,
497 rtl::Reference
< TypeManager
> const & manager
,
498 const std::set
< OUString
>& interfaces
,
499 const OString
& indentation
, bool usepropertymixin
)
501 std::set
< OUString
>::const_iterator iter
= interfaces
.begin();
502 codemaker::GeneratedTypeSet generated
;
503 while (iter
!= interfaces
.end()) {
504 OUString
type(*iter
);
506 if (type
== "com.sun.star.lang.XServiceInfo") {
507 generateXServiceInfoBodies(o
);
508 generated
.add(u2b(type
));
510 if (options
.componenttype
== 2) {
511 if (type
== "com.sun.star.lang.XServiceName") {
512 o
<< " // com.sun.star.lang.XServiceName:\n"
513 " public String getServiceName() {\n"
514 " return sADDIN_SERVICENAME;\n }\n";
515 generated
.add(u2b(type
));
517 } else if (type
== "com.sun.star.sheet.XAddIn") {
518 generateXAddInBodies(o
, options
);
519 generated
.add(u2b(type
));
521 // special handling of XLocalizable -> parent of XAddIn
522 if (!generated
.contains("com.sun.star.lang.XLocalizable")) {
523 generateXLocalizableBodies(o
);
524 generated
.add("com.sun.star.lang.XLocalizable");
527 } else if (type
== "com.sun.star.lang.XLocalizable") {
528 generateXLocalizableBodies(o
);
529 generated
.add(u2b(type
));
531 } else if (type
== "com.sun.star.sheet.XCompatibilityNames") {
532 generateXCompatibilityNamesBodies(o
);
533 generated
.add(u2b(type
));
537 if (options
.componenttype
== 3) {
538 if (type
== "com.sun.star.lang.XInitialization") {
539 generateXInitializationBodies(o
);
540 generated
.add(u2b(type
));
542 } else if (type
== "com.sun.star.frame.XDispatch") {
543 generateXDispatchBodies(o
, options
);
544 generated
.add(u2b(type
));
546 } else if (type
== "com.sun.star.frame.XDispatchProvider") {
547 generateXDispatchProviderBodies(o
, options
);
548 generated
.add(u2b(type
));
552 printMethods(o
, options
, manager
, type
, generated
, "_",
553 indentation
, true, usepropertymixin
);
558 static const char* propcomment
=
559 " // use the last parameter of the PropertySetMixin constructor\n"
560 " // for your optional attributes if necessary. See the documentation\n"
561 " // of the PropertySetMixin helper for further information.\n"
562 " // Ensure that your attributes are initialized correctly!\n";
565 void generateAddinConstructorAndHelper(std::ostream
& o
,
566 ProgramOptions
const & options
,
567 rtl::Reference
< TypeManager
> const & manager
, const OString
& classname
,
568 const std::set
< OUString
>& services
,
569 const std::set
< OUString
>& interfaces
)
571 o
<< " private com.sun.star.lang.Locale m_locale = "
572 "new com.sun.star.lang.Locale();\n";
574 if (!options
.backwardcompatible
) {
576 o
<< "\n public " << classname
<< "( XComponentContext context )\n"
577 " {\n m_xContext = context;\n }\n\n";
582 // get the one and only add-in service for later use
583 std::set
< OUString
>::const_iterator iter
= services
.begin();
584 OUString sAddinService
= *iter
;
585 if (sAddinService
== "com.sun.star.sheet.AddIn") {
586 sAddinService
= *(++iter
);
590 // add-in specific fields
591 o
<< "\n private static final String sADDIN_SERVICENAME = \""
592 << sAddinService
<< "\";\n\n";
593 o
<< " private static final String sDISPLAYNAME = "
595 " private static final String sDESCRIPTION = "
597 " private static final String sCATEGORY = \"Category\";\n"
598 " private static final String sCATEGORYDISPLAYNAME = "
599 "\"CategoryDisplayName\";\n\n";
601 o
<< " private com.sun.star.container.XHierarchicalNameAccess "
602 "m_xHAccess = null;\n"
603 " private com.sun.star.container.XHierarchicalNameAccess "
604 "m_xCompAccess = null;\n";
605 o
<< " private java.util.Hashtable<\n String, "
606 "java.util.Hashtable< Integer, String> > m_functionMap = null;\n\n";
609 o
<< "\n public " << classname
<< "( XComponentContext context )\n {\n"
610 " m_xContext = context;\n\n"
613 o
<< " m_functionMap = new java.util.Hashtable<\n"
614 " String, java.util.Hashtable< Integer, String > >();\n\n";
616 generateFunctionParameterMap(o
, options
, manager
, interfaces
);
618 o
<< " com.sun.star.lang.XMultiServiceFactory xProvider = \n"
619 " (com.sun.star.lang.XMultiServiceFactory)UnoRuntime."
621 " com.sun.star.lang.XMultiServiceFactory.class,\n"
622 " m_xContext.getServiceManager().createInstanceWithContext("
623 "\n \"com.sun.star.configuration.ConfigurationProvider\""
624 ",\n m_xContext));\n\n";
626 o
<< " String sReadOnlyView = "
627 "\"com.sun.star.configuration.ConfigurationAccess\";\n\n";
629 o
<< " StringBuffer sPath = new StringBuffer(\n"
630 " \"/org.openoffice.Office.CalcAddIns/AddInInfo/\");\n"
631 " sPath.append(sADDIN_SERVICENAME);\n"
632 " sPath.append(\"/AddInFunctions\");\n\n";
634 o
<< " // create arguments: nodepath\n"
635 " com.sun.star.beans.PropertyValue aArgument = \n"
636 " new com.sun.star.beans.PropertyValue();\n"
637 " aArgument.Name = \"nodepath\";\n"
638 " aArgument.Value = new com.sun.star.uno.Any(\n"
639 " com.sun.star.uno.Type.STRING, sPath.toString());\n\n";
641 o
<< " Object aArguments[] = new Object[1];\n"
642 " aArguments[0] = new com.sun.star.uno.Any("
643 " new com.sun.star.uno.Type(\n"
644 " com.sun.star.beans.PropertyValue.class), aArgument);\n\n";
646 o
<< " // create the default view using default UI locale\n"
647 " Object xIface = \n"
648 " xProvider.createInstanceWithArguments(sReadOnlyView, "
651 o
<< " m_xHAccess = (com.sun.star.container.XHierarchicalNameAccess)\n"
652 " UnoRuntime.queryInterface(\n"
653 " com.sun.star.container.XHierarchicalNameAccess.class, "
656 o
<< " // extends arguments to create a view for all locales to get "
657 "simple\n // access to the compatibilityname property\n"
658 " aArguments = new Object[2];\n"
659 " aArguments[0] = new com.sun.star.uno.Any( "
660 "new com.sun.star.uno.Type(\n"
661 " com.sun.star.beans.PropertyValue.class), aArgument);\n"
662 " aArgument.Name = \"locale\";\n"
663 " aArgument.Value = new com.sun.star.uno.Any(\n"
664 " com.sun.star.uno.Type.STRING, \"*\");\n"
665 " aArguments[1] = new com.sun.star.uno.Any( "
666 " new com.sun.star.uno.Type(\n"
667 " com.sun.star.beans.PropertyValue.class), aArgument);\n\n";
669 o
<< " // create view for all locales\n"
670 " xIface = xProvider.createInstanceWithArguments(sReadOnlyView, "
672 " m_xCompAccess = (com.sun.star.container.XHierarchicalNameAccess)\n"
673 " UnoRuntime.queryInterface(\n"
674 " com.sun.star.container.XHierarchicalNameAccess.class, "
676 " catch ( com.sun.star.uno.Exception e ) {\n }\n }\n\n";
678 // add-in helper function
679 o
<< " // addin configuration property helper function:\n"
680 " String getAddinProperty(String funcName, "
681 "String paramName, String propName)\n {\n"
682 " try {\n StringBuffer buf = "
683 "new StringBuffer(funcName);\n\n"
684 " if (paramName.length() > 0) {\n"
685 " buf.append(\"/Parameters/\");\n"
686 " buf.append(paramName);\n }\n\n";
688 o
<< " com.sun.star.beans.XPropertySet xPropSet =\n"
689 " (com.sun.star.beans.XPropertySet)UnoRuntime."
691 " com.sun.star.beans.XPropertySet.class,\n"
692 " m_xHAccess.getByHierarchicalName(buf.toString()));\n\n"
693 " return com.sun.star.uno.AnyConverter.toString(\n"
694 " xPropSet.getPropertyValue(propName));\n }\n"
695 " catch ( com.sun.star.uno.RuntimeException e ) {\n"
697 " catch ( com.sun.star.uno.Exception e ) {\n }\n"
698 " return \"\";\n }\n\n";
702 void generateClassDefinition(std::ostream
& o
,
703 ProgramOptions
const & options
,
704 rtl::Reference
< TypeManager
> const & manager
,
705 const OString
& classname
,
706 const std::set
< OUString
>& services
,
707 const std::set
< OUString
>& interfaces
,
708 const AttributeInfo
& properties
,
709 const AttributeInfo
& attributes
,
710 const OUString
& propertyhelper
, bool supportxcomponent
)
712 o
<< "\n\npublic final class " << classname
<< " extends ";
714 if (!interfaces
.empty()) {
715 if (propertyhelper
== "_") {
716 o
<< "PropertySet\n";
718 if (supportxcomponent
)
719 o
<< "ComponentBase\n";
724 std::set
< OUString
>::const_iterator iter
= interfaces
.begin();
725 while (iter
!= interfaces
.end()) {
728 if (iter
!= interfaces
.end())
734 o
<< " private final XComponentContext m_xContext;\n";
736 // additional member for add-ons
737 if (options
.componenttype
== 3) {
738 o
<< " private com.sun.star.frame.XFrame m_xFrame;\n";
741 // check property helper
742 if (propertyhelper
.getLength() > 1)
743 o
<< " private final PropertySetMixin m_prophlp;\n";
745 o
<< " private static final String m_implementationName = "
746 << classname
<< ".class.getName();\n";
748 if (!services
.empty()) {
749 o
<< " private static final String[] m_serviceNames = {\n";
750 std::set
< OUString
>::const_iterator iter
= services
.begin();
751 while (iter
!= services
.end()) {
752 o
<< " \"" << (*iter
).replace('/','.') << "\"";
754 if (iter
!= services
.end())
761 // attribute/property members
762 if (!properties
.empty()) {
763 AttributeInfo::const_iterator iter
=
765 o
<< " // properties\n";
766 while (iter
!= properties
.end()) {
768 printType(o
, options
, manager
, iter
->type
, false);
769 o
<< " m_" << iter
->name
<< ";\n";
772 } else if (!attributes
.empty()) {
773 AttributeInfo::const_iterator iter
=
775 o
<< " // attributes\n";
776 while (iter
!= attributes
.end()) {
778 printType(o
, options
, manager
, iter
->type
, false);
779 o
<< " m_" << iter
->name
<< " = ";
780 printType(o
, options
, manager
, iter
->type
, false, true);
786 // special handling of calc add-ins
787 if (options
.componenttype
== 2)
789 generateAddinConstructorAndHelper(o
, options
, manager
, classname
,
790 services
, interfaces
);
792 o
<< "\n public " << classname
<< "( XComponentContext context )\n"
793 " {\n m_xContext = context;\n";
794 if (propertyhelper
== "_") {
795 registerProperties(o
, properties
, " ");
797 if (propertyhelper
.getLength() > 1) {
799 << " m_prophlp = new PropertySetMixin(m_xContext, this,\n"
800 " new Type(" << propertyhelper
801 << ".class), null);\n";
808 if (!services
.empty())
809 generateCompFunctions(o
, classname
);
811 generateMethodBodies(o
, options
, manager
, interfaces
,
812 " ", propertyhelper
.getLength() > 1);
814 // end of class definition
818 void generateSkeleton(ProgramOptions
const & options
,
819 rtl::Reference
< TypeManager
> const & manager
,
820 std::vector
< OString
> const & types
)
822 std::set
< OUString
> interfaces
;
823 std::set
< OUString
> services
;
824 AttributeInfo properties
;
825 AttributeInfo attributes
;
826 std::set
< OUString
> propinterfaces
;
827 bool serviceobject
= false;
828 bool supportxcomponent
= false;
830 std::vector
< OString
>::const_iterator iter
= types
.begin();
831 while (iter
!= types
.end()) {
832 checkType(manager
, b2u(*iter
), interfaces
, services
, properties
);
836 if (options
.componenttype
== 3) {
837 // the Protocolhandler service is mandatory for an protocol handler add-on,
838 // so it is defaulted. The XDispatchProvider provides Dispatch objects for
839 // certain functions and the generated impl object implements XDispatch
840 // directly for simplicity reasons.
841 checkType(manager
, "com.sun.star.frame.ProtocolHandler",
842 interfaces
, services
, properties
);
843 checkType(manager
, "com.sun.star.frame.XDispatch",
844 interfaces
, services
, properties
);
847 if (options
.componenttype
== 2) {
848 if (services
.size() != 1) {
849 throw CannotDumpException(
850 "for calc add-in components one and only one service type is "
851 "necessary! Please reference a valid type with the '-t' option.");
854 // if backwardcompatible==true the AddIn service needs to be added to the
855 // supported service list, the necessary intefaces are mapped to the add-in
856 // configuration. Since OO.org 2.0.4 this is obsolete and the add-in is
857 // take form the configuration from Calc directly, this simplifies the
859 if (options
.backwardcompatible
) {
860 checkType(manager
, "com.sun.star.sheet.AddIn",
861 interfaces
, services
, properties
);
863 // special case for the optional XLocalization interface. It should be
864 // implemented always. But it is parent of the XAddIn and we need it only
865 // if backwardcompatible is false.
866 if (interfaces
.find("com.sun.star.lang.XLocalizable") == interfaces
.end()) {
867 interfaces
.insert("com.sun.star.lang.XLocalizable");
873 // check if service object or simple UNO object
874 if (!services
.empty())
875 serviceobject
= true;
877 OUString propertyhelper
= checkPropertyHelper(
878 options
, manager
, services
, interfaces
, attributes
, propinterfaces
);
879 checkDefaultInterfaces(interfaces
, services
, propertyhelper
);
881 if (options
.componenttype
== 2) {
882 if (!propertyhelper
.isEmpty())
883 std::cerr
<< "WARNING: interfaces specifying calc add-in functions "
884 "shouldn't support attributes!\n";
887 supportxcomponent
= checkXComponentSupport(manager
, interfaces
);
889 OString compFileName
;
891 std::ostream
* pofs
= nullptr;
892 bool standardout
= getOutputStream(options
, ".java",
893 &pofs
, compFileName
, tmpFileName
);
896 if (!standardout
&& options
.license
) {
897 printLicenseHeader(*pofs
, compFileName
);
900 generatePackage(*pofs
, options
.implname
);
902 generateImports(*pofs
, options
, propertyhelper
,
903 serviceobject
, supportxcomponent
);
905 OString
classname(options
.implname
);
907 if ((index
= classname
.lastIndexOf('.')) > 0)
908 classname
= classname
.copy(index
+1);
910 generateClassDefinition(*pofs
, options
, manager
, classname
, services
,
911 interfaces
, properties
, attributes
, propertyhelper
,
914 if ( !standardout
&& pofs
&& static_cast<std::ofstream
*>(pofs
)->is_open()) {
915 static_cast<std::ofstream
*>(pofs
)->close();
917 OSL_VERIFY(makeValidTypeFile(compFileName
, tmpFileName
, false));
919 } catch (CannotDumpException
& e
) {
920 std::cerr
<< "ERROR: " << e
.getMessage() << "\n";
921 if ( !standardout
) {
922 if (pofs
&& static_cast<std::ofstream
*>(pofs
)->is_open()) {
923 static_cast<std::ofstream
*>(pofs
)->close();
926 // remove existing type file if something goes wrong to ensure
928 if (fileExists(compFileName
))
929 removeTypeFile(compFileName
);
931 // remove tmp file if something goes wrong
932 removeTypeFile(tmpFileName
);
940 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */