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 ::rtl
;
32 using namespace ::codemaker::java
;
34 namespace skeletonmaker
{ namespace java
{
36 void generatePackage(std::ostream
& o
, const OString
& implname
)
38 sal_Int32 index
= implname
.lastIndexOf('.');
40 o
<< "package " << implname
.copy(0, index
) << ";\n\n";
43 void generateImports(std::ostream
& o
, ProgramOptions
const & options
,
44 const OUString
& 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
.equals("_")) {
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
.isEmpty()) {
63 if (propertyhelper
.equals("_")) {
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 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 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";
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::MAYBEAMBIGIOUS */ 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
.getLength() > 0) {
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.MAYBEAMBIGIOUS");
234 attributeValue
.append("PropertyAttribute.MAYBEDEFAULT");
237 attributeValue
.append("PropertyAttribute.REMOVABLE");
240 attributeValue
.append("PropertyAttribute.OPTIONAL");
246 attributeValue
.insert(0, '(');
247 attributeValue
.append(')');
253 void registerProperties(std::ostream
& o
,
254 const AttributeInfo
& properties
,
255 const OString
& indentation
)
257 if (!properties
.empty()) {
259 OStringBuffer attributeValue
;
260 for (AttributeInfo::const_iterator
i(properties
.begin());
261 i
!= properties
.end(); ++i
)
263 if (i
->attributes
!= 0) {
264 cast
= checkAttribute(attributeValue
, i
->attributes
);
267 attributeValue
.append('0');
270 o
<< indentation
<< "registerProperty(\"" << i
->name
271 << "\", \"m_" << i
->name
<< "\",\n"
272 << indentation
<< " ";
276 o
<< attributeValue
.makeStringAndClear() << ");\n";
281 void generateXLocalizableBodies(std::ostream
& o
) {
282 // com.sun.star.lang.XLocalizable:
284 o
<< " // com.sun.star.lang.XLocalizable:\n"
285 " public void setLocale(com.sun.star.lang.Locale eLocale)\n {\n"
286 " m_locale = eLocale;\n }\n\n";
289 o
<< " public com.sun.star.lang.Locale getLocale()\n {\n"
290 " return m_locale;\n }\n\n";
293 void generateXAddInBodies(std::ostream
& o
, ProgramOptions
const & options
)
295 // com.sun.star.sheet.XAddIn:
296 // getProgrammaticFuntionName
297 o
<< " // com.sun.star.sheet.XAddIn:\n"
298 " public String getProgrammaticFuntionName(String "
299 "aDisplayName)\n {\n"
301 " com.sun.star.container.XNameAccess xNAccess =\n"
302 " (com.sun.star.container.XNameAccess)UnoRuntime."
304 " com.sun.star.container.XNameAccess.class, m_xHAccess);"
305 "\n String functions[] = xNAccess.getElementNames();\n"
306 " String sDisplayName = \"\";\n"
307 " int len = functions.length;\n"
308 " for (int i=0; i < len; ++i) {\n"
309 " sDisplayName = com.sun.star.uno.AnyConverter.toString(\n"
310 " getAddinProperty(functions[i], \"\", sDISPLAYNAME));\n"
311 " if (sDisplayName.equals(aDisplayName))\n"
312 " return functions[i];\n }\n"
313 " }\n catch ( com.sun.star.uno.RuntimeException e ) {\n"
315 " catch ( com.sun.star.uno.Exception e ) {\n }\n\n"
316 " return \"\";\n }\n\n";
318 // getDisplayFunctionName
319 o
<< " public String getDisplayFunctionName(String "
320 "aProgrammaticName)\n {\n"
321 " return getAddinProperty(aProgrammaticName, \"\", sDISPLAYNAME);\n"
324 // getFunctionDescription
325 o
<< " public String getFunctionDescription(String "
326 "aProgrammaticName)\n {\n"
327 " return getAddinProperty(aProgrammaticName, \"\", sDESCRIPTION);\n"
330 // getDisplayArgumentName
331 o
<< " public String getDisplayArgumentName(String "
332 "aProgrammaticFunctionName, int nArgument)\n {\n";
334 o
<< " return getAddinProperty(aProgrammaticFunctionName,\n"
335 " m_functionMap.get(\n"
336 " aProgrammaticFunctionName).get("
338 " sDISPLAYNAME);\n }\n\n";
340 o
<< " return getAddinProperty(aProgrammaticFunctionName, (String)\n"
341 " ((java.util.Hashtable)m_functionMap."
342 "get(\n aProgrammaticFunctionName))."
343 "get(\n new Integer(nArgument))"
344 ", sDISPLAYNAME);\n }\n\n";
347 // getArgumentDescription
348 o
<< " public String getArgumentDescription(String "
349 "aProgrammaticFunctionName, int nArgument)\n {\n";
351 o
<< " return getAddinProperty(aProgrammaticFunctionName,\n"
352 " m_functionMap.get(\n"
353 " aProgrammaticFunctionName).get("
355 " sDESCRIPTION);\n }\n\n";
357 o
<< " return getAddinProperty(aProgrammaticFunctionName, (String)\n"
358 " ((java.util.Hashtable)m_functionMap."
359 "get(\n aProgrammaticFunctionName))."
360 "get(\n new Integer(nArgument))"
361 ", sDESCRIPTION);\n }\n\n";
363 // getProgrammaticCategoryName
364 o
<< " public String getProgrammaticCategoryName(String "
365 "aProgrammaticFunctionName)\n {\n"
366 " return getAddinProperty(aProgrammaticFunctionName, \"\", "
367 "sCATEGORY);\n }\n\n";
369 // getDisplayCategoryName
370 o
<< " public String getDisplayCategoryName(String "
371 "aProgrammaticFunctionName)\n {\n"
372 " return getAddinProperty(aProgrammaticFunctionName, \"\", "
373 "sCATEGORYDISPLAYNAME);\n }\n\n";
376 void generateXCompatibilityNamesBodies(std::ostream
& o
)
378 o
<< " // com.sun.star.sheet.XCompatibilityNames:\n"
379 " public com.sun.star.sheet.LocalizedName[] getCompatibilityNames("
380 "String aProgrammaticName)\n {\n"
381 " com.sun.star.sheet.LocalizedName[] seqLocalizedNames =\n"
382 " new com.sun.star.sheet.LocalizedName[0];\n\n try {\n";
384 o
<< " StringBuffer path = new StringBuffer(aProgrammaticName);\n"
385 " path.append(\"/CompatibilityName\");\n"
386 " String hname = path.toString();\n\n";
388 o
<< " if ( m_xCompAccess.hasByHierarchicalName(hname) ) {\n"
389 " com.sun.star.container.XNameAccess xNameAccess =\n"
390 " (com.sun.star.container.XNameAccess)UnoRuntime."
392 " com.sun.star.container.XNameAccess.class,\n"
393 " m_xCompAccess.getByHierarchicalName(hname));\n\n"
394 " String elems[] = xNameAccess.getElementNames();\n"
395 " int len = elems.length;\n"
396 " seqLocalizedNames = new com.sun.star.sheet.LocalizedName"
397 "[len];\n String sCompatibilityName = \"\";\n\n";
399 o
<< " for (int i=0; i < len; ++i) {\n"
400 " String sLocale = elems[i];\n"
401 " sCompatibilityName = com.sun.star.uno.AnyConverter."
402 "toString(\n xNameAccess.getByName(sLocale));\n\n"
403 " com.sun.star.lang.Locale aLocale = \n"
404 " new com.sun.star.lang.Locale();\n\n"
405 " String tokens[] = sLocale.split(\"-\");\n"
406 " int nToken = tokens.length;\n"
407 " if (nToken >= 1) aLocale.Language = tokens[0];\n"
408 " if (nToken >= 2) aLocale.Country = tokens[1];\n"
409 " if (nToken >= 3) {\n"
410 " StringBuffer buf = \n"
411 " new StringBuffer(tokens[2]);\n"
412 " for (int t=3; t < nToken; ++t)\n"
413 " buf.append(tokens[t]);\n\n"
414 " aLocale.Variant = buf.toString();\n"
416 " seqLocalizedNames[i].Locale = aLocale;\n"
417 " seqLocalizedNames[i].Name = sCompatibilityName;\n"
419 " catch ( com.sun.star.uno.RuntimeException e ) {\n"
421 " catch ( com.sun.star.uno.Exception e ) {\n }\n\n"
422 " return seqLocalizedNames;\n }\n\n";
425 void generateXInitializationBodies(std::ostream
& o
)
427 o
<< " // com.sun.star.lang.XInitialization:\n"
428 " public void initialize( Object[] object )\n"
429 " throws com.sun.star.uno.Exception\n {\n"
430 " if ( object.length > 0 )\n {\n"
431 " m_xFrame = (com.sun.star.frame.XFrame)UnoRuntime.queryInterface(\n"
432 " com.sun.star.frame.XFrame.class, object[0]);\n }\n }\n\n";
435 void generateXDispatchBodies(std::ostream
& o
, ProgramOptions
const & options
)
437 // com.sun.star.frame.XDispatch
439 o
<< " // com.sun.star.frame.XDispatch:\n"
440 " public void dispatch( com.sun.star.util.URL aURL,\n"
441 " com.sun.star.beans.PropertyValue[] aArguments )\n {\n";
443 ProtocolCmdMap::const_iterator iter
= options
.protocolCmdMap
.begin();
444 while (iter
!= options
.protocolCmdMap
.end()) {
445 o
<< " if ( aURL.Protocol.compareTo(\"" << (*iter
).first
446 << "\") == 0 )\n {\n";
448 for (std::vector
< OString
>::const_iterator i
= (*iter
).second
.begin();
449 i
!= (*iter
).second
.end(); ++i
) {
450 o
<< " if ( aURL.Path.compareTo(\"" << (*i
) << "\") == 0 )\n"
451 " {\n // add your own code here\n"
461 o
<< " public void addStatusListener( com.sun.star.frame.XStatusListener xControl,\n"
462 " com.sun.star.util.URL aURL )\n {\n"
463 " // add your own code here\n }\n\n";
465 // com.sun.star.frame.XDispatch
466 o
<< " public void removeStatusListener( com.sun.star.frame.XStatusListener xControl,\n"
467 " com.sun.star.util.URL aURL )\n {\n"
468 " // add your own code here\n }\n\n";
471 void generateXDispatchProviderBodies(std::ostream
& o
, ProgramOptions
const & options
)
473 // com.sun.star.frame.XDispatchProvider
475 o
<< " // com.sun.star.frame.XDispatchProvider:\n"
476 " public com.sun.star.frame.XDispatch queryDispatch( com.sun.star.util.URL aURL,\n"
477 " String sTargetFrameName,\n"
478 " int iSearchFlags )\n {\n";
480 ProtocolCmdMap::const_iterator iter
= options
.protocolCmdMap
.begin();
481 while (iter
!= options
.protocolCmdMap
.end()) {
482 o
<< " if ( aURL.Protocol.compareTo(\"" << (*iter
).first
483 << "\") == 0 )\n {\n";
485 for (std::vector
< OString
>::const_iterator i
= (*iter
).second
.begin();
486 i
!= (*iter
).second
.end(); ++i
) {
487 o
<< " if ( aURL.Path.compareTo(\"" << (*i
) << "\") == 0 )\n"
494 o
<< " return null;\n }\n\n";
497 o
<< " // com.sun.star.frame.XDispatchProvider:\n"
498 " public com.sun.star.frame.XDispatch[] queryDispatches(\n"
499 " com.sun.star.frame.DispatchDescriptor[] seqDescriptors )\n {\n"
500 " int nCount = seqDescriptors.length;\n"
501 " com.sun.star.frame.XDispatch[] seqDispatcher =\n"
502 " new com.sun.star.frame.XDispatch[seqDescriptors.length];\n\n"
503 " for( int i=0; i < nCount; ++i )\n {\n"
504 " seqDispatcher[i] = queryDispatch(seqDescriptors[i].FeatureURL,\n"
505 " seqDescriptors[i].FrameName,\n"
506 " seqDescriptors[i].SearchFlags );\n"
507 " }\n return seqDispatcher;\n }\n\n";
510 void generateMethodBodies(std::ostream
& o
,
511 ProgramOptions
const & options
,
512 rtl::Reference
< TypeManager
> const & manager
,
513 const std::set
< OUString
>& interfaces
,
514 const OString
& indentation
, bool usepropertymixin
)
516 std::set
< OUString
>::const_iterator iter
= interfaces
.begin();
517 codemaker::GeneratedTypeSet generated
;
518 while (iter
!= interfaces
.end()) {
519 OUString
type(*iter
);
521 if (type
.equals("com.sun.star.lang.XServiceInfo")) {
522 generateXServiceInfoBodies(o
);
523 generated
.add(u2b(type
));
525 if (options
.componenttype
== 2) {
526 if (type
.equals("com.sun.star.lang.XServiceName")) {
527 o
<< " // com.sun.star.lang.XServiceName:\n"
528 " public String getServiceName() {\n"
529 " return sADDIN_SERVICENAME;\n }\n";
530 generated
.add(u2b(type
));
532 } else if (type
.equals("com.sun.star.sheet.XAddIn")) {
533 generateXAddInBodies(o
, options
);
534 generated
.add(u2b(type
));
536 // special handling of XLocalizable -> parent of XAddIn
537 if (!generated
.contains("com.sun.star.lang.XLocalizable")) {
538 generateXLocalizableBodies(o
);
539 generated
.add("com.sun.star.lang.XLocalizable");
542 } else if (type
.equals("com.sun.star.lang.XLocalizable")) {
543 generateXLocalizableBodies(o
);
544 generated
.add(u2b(type
));
546 } else if (type
.equals("com.sun.star.sheet.XCompatibilityNames")) {
547 generateXCompatibilityNamesBodies(o
);
548 generated
.add(u2b(type
));
552 if (options
.componenttype
== 3) {
553 if (type
.equals("com.sun.star.lang.XInitialization")) {
554 generateXInitializationBodies(o
);
555 generated
.add(u2b(type
));
557 } else if (type
.equals("com.sun.star.frame.XDispatch")) {
558 generateXDispatchBodies(o
, options
);
559 generated
.add(u2b(type
));
561 } else if (type
.equals("com.sun.star.frame.XDispatchProvider")) {
562 generateXDispatchProviderBodies(o
, options
);
563 generated
.add(u2b(type
));
567 printMethods(o
, options
, manager
, type
, generated
, "_",
568 indentation
, true, usepropertymixin
);
573 static const char* propcomment
=
574 " // use the last parameter of the PropertySetMixin constructor\n"
575 " // for your optional attributes if necessary. See the documentation\n"
576 " // of the PropertySetMixin helper for further information.\n"
577 " // Ensure that your attributes are initialized correctly!\n";
580 void generateAddinConstructorAndHelper(std::ostream
& o
,
581 ProgramOptions
const & options
,
582 rtl::Reference
< TypeManager
> const & manager
, const OString
& classname
,
583 const std::set
< OUString
>& services
,
584 const std::set
< OUString
>& interfaces
)
586 o
<< " private com.sun.star.lang.Locale m_locale = "
587 "new com.sun.star.lang.Locale();\n";
589 if (!options
.backwardcompatible
) {
591 o
<< "\n public " << classname
<< "( XComponentContext context )\n"
592 " {\n m_xContext = context;\n }\n\n";
597 // get the one and only add-in service for later use
598 std::set
< OUString
>::const_iterator iter
= services
.begin();
599 OUString sAddinService
= *iter
;
600 if (sAddinService
== "com.sun.star.sheet.AddIn") {
601 sAddinService
= *(++iter
);
605 // add-in specific fields
606 o
<< "\n private static final String sADDIN_SERVICENAME = \""
607 << sAddinService
<< "\";\n\n";
608 o
<< " private static final String sDISPLAYNAME = "
610 " private static final String sDESCRIPTION = "
612 " private static final String sCATEGORY = \"Category\";\n"
613 " private static final String sCATEGORYDISPLAYNAME = "
614 "\"CategoryDisplayName\";\n\n";
616 o
<< " private com.sun.star.container.XHierarchicalNameAccess "
617 "m_xHAccess = null;\n"
618 " private com.sun.star.container.XHierarchicalNameAccess "
619 "m_xCompAccess = null;\n";
621 o
<< " private java.util.Hashtable<\n String, "
622 "java.util.Hashtable< Integer, String> > m_functionMap = null;\n\n";
624 o
<< " private java.util.Hashtable m_functionMap = null;\n\n";
627 o
<< "\n public " << classname
<< "( XComponentContext context )\n {\n"
628 " m_xContext = context;\n\n"
632 o
<< " m_functionMap = new java.util.Hashtable<\n"
633 " String, java.util.Hashtable< Integer, "
636 o
<< " m_functionMap = new java.util.Hashtable();\n\n";
639 generateFunctionParameterMap(o
, options
, manager
, interfaces
);
641 o
<< " com.sun.star.lang.XMultiServiceFactory xProvider = \n"
642 " (com.sun.star.lang.XMultiServiceFactory)UnoRuntime."
644 " com.sun.star.lang.XMultiServiceFactory.class,\n"
645 " m_xContext.getServiceManager().createInstanceWithContext("
646 "\n \"com.sun.star.configuration.ConfigurationProvider\""
647 ",\n m_xContext));\n\n";
649 o
<< " String sReadOnlyView = "
650 "\"com.sun.star.configuration.ConfigurationAccess\";\n\n";
652 o
<< " StringBuffer sPath = new StringBuffer(\n"
653 " \"/org.openoffice.Office.CalcAddIns/AddInInfo/\");\n"
654 " sPath.append(sADDIN_SERVICENAME);\n"
655 " sPath.append(\"/AddInFunctions\");\n\n";
657 o
<< " // create arguments: nodepath\n"
658 " com.sun.star.beans.PropertyValue aArgument = \n"
659 " new com.sun.star.beans.PropertyValue();\n"
660 " aArgument.Name = \"nodepath\";\n"
661 " aArgument.Value = new com.sun.star.uno.Any(\n"
662 " com.sun.star.uno.Type.STRING, sPath.toString());\n\n";
664 o
<< " Object aArguments[] = new Object[1];\n"
665 " aArguments[0] = 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 the default view using default UI locale\n"
670 " Object xIface = \n"
671 " xProvider.createInstanceWithArguments(sReadOnlyView, "
674 o
<< " m_xHAccess = (com.sun.star.container.XHierarchicalNameAccess)\n"
675 " UnoRuntime.queryInterface(\n"
676 " com.sun.star.container.XHierarchicalNameAccess.class, "
679 o
<< " // extends arguments to create a view for all locales to get "
680 "simple\n // access to the compatibilityname property\n"
681 " aArguments = new Object[2];\n"
682 " aArguments[0] = new com.sun.star.uno.Any( "
683 "new com.sun.star.uno.Type(\n"
684 " com.sun.star.beans.PropertyValue.class), aArgument);\n"
685 " aArgument.Name = \"locale\";\n"
686 " aArgument.Value = new com.sun.star.uno.Any(\n"
687 " com.sun.star.uno.Type.STRING, \"*\");\n"
688 " aArguments[1] = new com.sun.star.uno.Any( "
689 " new com.sun.star.uno.Type(\n"
690 " com.sun.star.beans.PropertyValue.class), aArgument);\n\n";
692 o
<< " // create view for all locales\n"
693 " xIface = xProvider.createInstanceWithArguments(sReadOnlyView, "
695 " m_xCompAccess = (com.sun.star.container.XHierarchicalNameAccess)\n"
696 " UnoRuntime.queryInterface(\n"
697 " com.sun.star.container.XHierarchicalNameAccess.class, "
699 " catch ( com.sun.star.uno.Exception e ) {\n }\n }\n\n";
701 // add-in helper function
702 o
<< " // addin configuration property helper function:\n"
703 " String getAddinProperty(String funcName, "
704 "String paramName, String propName)\n {\n"
705 " try {\n StringBuffer buf = "
706 "new StringBuffer(funcName);\n\n"
707 " if (paramName.length() > 0) {\n"
708 " buf.append(\"/Parameters/\");\n"
709 " buf.append(paramName);\n }\n\n";
711 o
<< " com.sun.star.beans.XPropertySet xPropSet =\n"
712 " (com.sun.star.beans.XPropertySet)UnoRuntime."
714 " com.sun.star.beans.XPropertySet.class,\n"
715 " m_xHAccess.getByHierarchicalName(buf.toString()));\n\n"
716 " return com.sun.star.uno.AnyConverter.toString(\n"
717 " xPropSet.getPropertyValue(propName));\n }\n"
718 " catch ( com.sun.star.uno.RuntimeException e ) {\n"
720 " catch ( com.sun.star.uno.Exception e ) {\n }\n"
721 " return \"\";\n }\n\n";
725 void generateClassDefinition(std::ostream
& o
,
726 ProgramOptions
const & options
,
727 rtl::Reference
< TypeManager
> const & manager
,
728 const OString
& classname
,
729 const std::set
< OUString
>& services
,
730 const std::set
< OUString
>& interfaces
,
731 const AttributeInfo
& properties
,
732 const AttributeInfo
& attributes
,
733 const OUString
& propertyhelper
, bool supportxcomponent
)
735 o
<< "\n\npublic final class " << classname
<< " extends ";
737 if (!interfaces
.empty()) {
738 if (propertyhelper
.equals("_")) {
739 o
<< "PropertySet\n";
741 if (supportxcomponent
)
742 o
<< "ComponentBase\n";
747 std::set
< OUString
>::const_iterator iter
= interfaces
.begin();
748 while (iter
!= interfaces
.end()) {
751 if (iter
!= interfaces
.end())
757 o
<< " private final XComponentContext m_xContext;\n";
759 // additional member for add-ons
760 if (options
.componenttype
== 3) {
761 o
<< " private com.sun.star.frame.XFrame m_xFrame;\n";
764 // check property helper
765 if (propertyhelper
.getLength() > 1)
766 o
<< " private final PropertySetMixin m_prophlp;\n";
768 o
<< " private static final String m_implementationName = "
769 << classname
<< ".class.getName();\n";
771 if (!services
.empty()) {
772 o
<< " private static final String[] m_serviceNames = {\n";
773 std::set
< OUString
>::const_iterator iter
= services
.begin();
774 while (iter
!= services
.end()) {
775 o
<< " \"" << (*iter
).replace('/','.') << "\"";
777 if (iter
!= services
.end())
784 // attribute/property members
785 if (!properties
.empty()) {
786 AttributeInfo::const_iterator iter
=
788 o
<< " // properties\n";
789 while (iter
!= properties
.end()) {
791 printType(o
, options
, manager
, iter
->type
, false, false);
792 o
<< " m_" << iter
->name
<< ";\n";
795 } else if (!attributes
.empty()) {
796 AttributeInfo::const_iterator iter
=
798 o
<< " // attributes\n";
799 while (iter
!= attributes
.end()) {
801 printType(o
, options
, manager
, iter
->type
, false, false);
802 o
<< " m_" << iter
->name
<< " = ";
803 printType(o
, options
, manager
, iter
->type
, false, true);
809 // special handling of calc add-ins
810 if (options
.componenttype
== 2)
812 generateAddinConstructorAndHelper(o
, options
, manager
, classname
,
813 services
, interfaces
);
815 o
<< "\n public " << classname
<< "( XComponentContext context )\n"
816 " {\n m_xContext = context;\n";
817 if (propertyhelper
.equals("_")) {
818 registerProperties(o
, properties
, " ");
820 if (propertyhelper
.getLength() > 1) {
822 << " m_prophlp = new PropertySetMixin(m_xContext, this,\n"
823 << " new Type(" << propertyhelper
824 << ".class), null);\n";
831 if (!services
.empty())
832 generateCompFunctions(o
, classname
);
834 generateMethodBodies(o
, options
, manager
, interfaces
,
835 " ", propertyhelper
.getLength() > 1);
837 // end of class definition
841 void generateSkeleton(ProgramOptions
const & options
,
842 rtl::Reference
< TypeManager
> const & manager
,
843 std::vector
< OString
> const & types
)
845 std::set
< OUString
> interfaces
;
846 std::set
< OUString
> services
;
847 AttributeInfo properties
;
848 AttributeInfo attributes
;
849 std::set
< OUString
> propinterfaces
;
850 bool serviceobject
= false;
851 bool supportxcomponent
= false;
853 std::vector
< OString
>::const_iterator iter
= types
.begin();
854 while (iter
!= types
.end()) {
855 checkType(manager
, b2u(*iter
), interfaces
, services
, properties
);
859 if (options
.componenttype
== 3) {
860 // the Protocolhandler service is mandatory for an protocol handler add-on,
861 // so it is defaulted. The XDispatchProvider provides Dispatch objects for
862 // certain functions and the generated impl object implements XDispatch
863 // directly for simplicity reasons.
864 checkType(manager
, "com.sun.star.frame.ProtocolHandler",
865 interfaces
, services
, properties
);
866 checkType(manager
, "com.sun.star.frame.XDispatch",
867 interfaces
, services
, properties
);
870 if (options
.componenttype
== 2) {
871 if (services
.size() != 1) {
872 throw CannotDumpException(
873 "for calc add-in components one and only one service type is "
874 "necessary! Please reference a valid type with the '-t' option.");
877 // if backwardcompatible==true the AddIn service needs to be added to the
878 // suported service list, the necessary intefaces are mapped to the add-in
879 // configuration. Since OO.org 2.0.4 this is obsolete and the add-in is
880 // take form the configuration from Calc directly, this simplifies the
882 if (options
.backwardcompatible
) {
883 checkType(manager
, "com.sun.star.sheet.AddIn",
884 interfaces
, services
, properties
);
886 // special case for the optional XLocalization interface. It should be
887 // implemented always. But it is parent of the XAddIn and we need it only
888 // if backwardcompatible is false.
889 if (interfaces
.find("com.sun.star.lang.XLocalizable") == interfaces
.end()) {
890 interfaces
.insert("com.sun.star.lang.XLocalizable");
896 // check if service object or simple UNO object
897 if (!services
.empty())
898 serviceobject
= true;
900 OUString propertyhelper
= checkPropertyHelper(
901 options
, manager
, services
, interfaces
, attributes
, propinterfaces
);
902 checkDefaultInterfaces(interfaces
, services
, propertyhelper
);
904 if (options
.componenttype
== 2) {
905 if (!propertyhelper
.isEmpty())
906 std::cerr
<< "WARNING: interfaces specifying calc add-in functions "
907 "shouldn't support attributes!\n";
910 supportxcomponent
= checkXComponentSupport(manager
, interfaces
);
912 OString compFileName
;
914 std::ostream
* pofs
= NULL
;
915 bool standardout
= getOutputStream(options
, ".java",
916 &pofs
, compFileName
, tmpFileName
);
919 if (!standardout
&& options
.license
) {
920 printLicenseHeader(*pofs
, compFileName
);
923 generatePackage(*pofs
, options
.implname
);
925 generateImports(*pofs
, options
, propertyhelper
,
926 serviceobject
, supportxcomponent
);
928 OString
classname(options
.implname
);
930 if ((index
= classname
.lastIndexOf('.')) > 0)
931 classname
= classname
.copy(index
+1);
933 generateClassDefinition(*pofs
, options
, manager
, classname
, services
,
934 interfaces
, properties
, attributes
, propertyhelper
,
937 if ( !standardout
&& pofs
&& ((std::ofstream
*)pofs
)->is_open()) {
938 ((std::ofstream
*)pofs
)->close();
940 OSL_VERIFY(makeValidTypeFile(compFileName
, tmpFileName
, sal_False
));
942 } catch (CannotDumpException
& e
) {
943 std::cerr
<< "ERROR: " << e
.getMessage() << "\n";
944 if ( !standardout
) {
945 if (pofs
&& ((std::ofstream
*)pofs
)->is_open()) {
946 ((std::ofstream
*)pofs
)->close();
949 // remove existing type file if something goes wrong to ensure
951 if (fileExists(compFileName
))
952 removeTypeFile(compFileName
);
954 // remove tmp file if something goes wrong
955 removeTypeFile(tmpFileName
);
963 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */