1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: javacompskeleton.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include "codemaker/commonjava.hxx"
33 #include "skeletoncommon.hxx"
34 #include "skeletonjava.hxx"
38 using namespace ::rtl
;
39 using namespace ::codemaker::java
;
41 namespace skeletonmaker
{ namespace java
{
43 void generatePackage(std::ostream
& o
, const OString
& implname
)
45 sal_Int32 index
= implname
.lastIndexOf('.');
47 o
<< "package " << implname
.copy(0, index
) << ";\n\n";
50 void generateImports(std::ostream
& o
, ProgramOptions
const & options
,
51 const std::hash_set
< OString
, OStringHash
>& /*interfaces*/,
52 const OString
& propertyhelper
,
53 bool serviceobject
, bool supportxcomponent
)
55 if (options
.componenttype
== 3)
56 o
<< "import com.sun.star.uno.UnoRuntime;\n";
57 o
<< "import com.sun.star.uno.XComponentContext;\n";
59 o
<< "import com.sun.star.lib.uno.helper.Factory;\n";
60 o
<< "import com.sun.star.lang.XSingleComponentFactory;\n";
61 o
<< "import com.sun.star.registry.XRegistryKey;\n";
64 if (!propertyhelper
.equals("_")) {
65 if (supportxcomponent
)
66 o
<< "import com.sun.star.lib.uno.helper.ComponentBase;\n";
68 o
<< "import com.sun.star.lib.uno.helper.WeakBase;\n";
70 if (propertyhelper
.getLength() > 0) {
71 if (propertyhelper
.equals("_")) {
72 o
<< "import com.sun.star.lib.uno.helper.PropertySet;\n";
73 o
<< "import com.sun.star.beans.PropertyAttribute;\n";
75 o
<< "import com.sun.star.uno.Type;\n";
76 o
<< "import com.sun.star.uno.Any;\n";
77 o
<< "import com.sun.star.beans.Ambiguous;\n";
78 o
<< "import com.sun.star.beans.Defaulted;\n";
79 o
<< "import com.sun.star.beans.Optional;\n";
80 o
<< "import com.sun.star.lib.uno.helper.PropertySetMixin;\n";
85 // std::hash_set< OString, OStringHash >::const_iterator iter =
86 // interfaces.begin();
87 // while (iter != interfaces.end())
89 // o << "import " << ((*iter).getStr()) << ";\n";
94 void generateCompFunctions(std::ostream
& o
, const OString
& classname
)
96 o
<< " public static XSingleComponentFactory __getComponentFactory("
97 " String sImplementationName ) {\n"
98 " XSingleComponentFactory xFactory = null;\n\n"
99 " if ( sImplementationName.equals( m_implementationName ) )\n"
100 " xFactory = Factory.createComponentFactory("
101 << classname
<< ".class, m_serviceNames);\n"
102 " return xFactory;\n }\n\n";
104 o
<< " public static boolean __writeRegistryServiceInfo("
105 " XRegistryKey xRegistryKey ) {\n"
106 " return Factory.writeRegistryServiceInfo(m_implementationName,\n"
112 void generateXServiceInfoBodies(std::ostream
& o
)
114 o
<< " // com.sun.star.lang.XServiceInfo:\n";
115 o
<< " public String getImplementationName() {\n"
116 << " return m_implementationName;\n }\n\n";
118 o
<< " public boolean supportsService( String sService ) {\n"
119 << " int len = m_serviceNames.length;\n\n"
120 << " for( int i=0; i < len; i++) {\n"
121 << " if (sService.equals(m_serviceNames[i]))\n"
123 << " }\n return false;\n }\n\n";
125 o
<< " public String[] getSupportedServiceNames() {\n"
126 << " return m_serviceNames;\n }\n\n";
129 void generateXPropertySetBodies(std::ostream
& o
)
131 o
<< " // com.sun.star.beans.XPropertySet:\n";
132 o
<< " public com.sun.star.beans.XPropertySetInfo getPropertySetInfo()\n"
133 " {\n return m_prophlp.getPropertySetInfo();\n }\n\n";
135 o
<< " public void setPropertyValue(String aPropertyName, "
136 "Object aValue) throws "
137 "com.sun.star.beans.UnknownPropertyException, "
138 "com.sun.star.beans.PropertyVetoException, "
139 "com.sun.star.lang.IllegalArgumentException,"
140 "com.sun.star.lang.WrappedTargetException\n {\n "
141 "m_prophlp.setPropertyValue(aPropertyName, aValue);\n }\n\n";
143 o
<< " public Object getPropertyValue(String "
144 "aPropertyName) throws com.sun.star.beans.UnknownPropertyException, "
145 "com.sun.star.lang.WrappedTargetException\n {\n return "
146 "m_prophlp.getPropertyValue(aPropertyName);\n }\n\n";
148 o
<< " public void addPropertyChangeListener(String aPropertyName"
149 ", com.sun.star.beans.XPropertyChangeListener xListener) throws "
150 "com.sun.star.beans.UnknownPropertyException, "
151 "com.sun.star.lang.WrappedTargetException\n {\n "
152 "m_prophlp.addPropertyChangeListener(aPropertyName, xListener);\n }\n\n";
154 o
<< " public void removePropertyChangeListener(String "
155 "aPropertyName, com.sun.star.beans.XPropertyChangeListener xListener) "
156 "throws com.sun.star.beans.UnknownPropertyException, "
157 "com.sun.star.lang.WrappedTargetException\n {\n "
158 "m_prophlp.removePropertyChangeListener(aPropertyName, xListener);\n"
161 o
<< " public void addVetoableChangeListener(String aPropertyName"
162 ", com.sun.star.beans.XVetoableChangeListener xListener) throws "
163 "com.sun.star.beans.UnknownPropertyException, "
164 "com.sun.star.lang.WrappedTargetException\n {\n "
165 "m_prophlp.addVetoableChangeListener(aPropertyName, xListener);\n }\n\n";
167 o
<< " public void removeVetoableChangeListener(String "
168 "aPropertyName, com.sun.star.beans.XVetoableChangeListener xListener) "
169 "throws com.sun.star.beans.UnknownPropertyException, "
170 "com.sun.star.lang.WrappedTargetException\n {\n "
171 "m_prophlp.removeVetoableChangeListener(aPropertyName, xListener);\n }\n\n";
174 void generateXFastPropertySetBodies(std::ostream
& o
)
176 o
<< " // com.sun.star.beans.XFastPropertySet:\n";
178 o
<< " public void setFastPropertyValue(int nHandle, Object "
179 "aValue) throws com.sun.star.beans.UnknownPropertyException, "
180 "com.sun.star.beans.PropertyVetoException, "
181 "com.sun.star.lang.IllegalArgumentException, "
182 "com.sun.star.lang.WrappedTargetException\n {\n "
183 "m_prophlp.setFastPropertyValue(nHandle, aValue);\n }\n\n";
185 o
<< " public Object getFastPropertyValue(int nHandle) throws "
186 "com.sun.star.beans.UnknownPropertyException, "
187 "com.sun.star.lang.WrappedTargetException\n {\n return "
188 "m_prophlp.getFastPropertyValue(nHandle);\n }\n\n";
191 void generateXPropertyAccessBodies(std::ostream
& o
)
193 o
<< " // com.sun.star.beans.XPropertyAccess:\n";
195 o
<< " public com.sun.star.beans.PropertyValue[] getPropertyValues()\n"
196 " {\n return m_prophlp.getPropertyValues();\n }\n\n";
198 o
<< " public void setPropertyValues(com.sun.star.beans.PropertyValue[] "
199 "aProps) throws com.sun.star.beans.UnknownPropertyException, "
200 "com.sun.star.beans.PropertyVetoException, "
201 "com.sun.star.lang.IllegalArgumentException, "
202 "com.sun.star.lang.WrappedTargetException\n {\n "
203 "m_prophlp.setPropertyValues(aProps);\n }\n\n";
207 bool checkAttribute(OStringBuffer
& attributeValue
, sal_uInt16 attribute
)
210 sal_uInt16 attributes
[9] = {
211 /* com::sun::star::beans::PropertyValue::MAYBEVOID */ 1,
212 /* com::sun::star::beans::PropertyValue::BOUND */ 2,
213 /* com::sun::star::beans::PropertyValue::CONSTRAINED */ 4,
214 /* com::sun::star::beans::PropertyValue::TRANSIENT */ 8,
215 /* com::sun::star::beans::PropertyValue::READONLY */ 16,
216 /* com::sun::star::beans::PropertyValue::MAYBEAMBIGIOUS */ 32,
217 /* com::sun::star::beans::PropertyValue::MAYBEDEFAULT */ 64,
218 /* com::sun::star::beans::PropertyValue::REMOVEABLE */ 128,
219 /* com::sun::star::beans::PropertyValue::OPTIONAL */ 256 };
221 for (sal_uInt16 i
= 0; i
< 9; i
++)
223 if (attribute
& attributes
[i
]) {
224 if (attributeValue
.getLength() > 0) {
226 attributeValue
.append("|");
228 switch (attributes
[i
])
231 attributeValue
.append("PropertyAttribute.MAYBEVOID");
234 attributeValue
.append("PropertyAttribute.BOUND");
237 attributeValue
.append("PropertyAttribute.CONSTRAINED");
240 attributeValue
.append("PropertyAttribute.TRANSIENT");
243 attributeValue
.append("PropertyAttribute.READONLY");
246 attributeValue
.append("PropertyAttribute.MAYBEAMBIGIOUS");
249 attributeValue
.append("PropertyAttribute.MAYBEDEFAULT");
252 attributeValue
.append("PropertyAttribute.REMOVEABLE");
255 attributeValue
.append("PropertyAttribute.OPTIONAL");
261 attributeValue
.insert(0, '(');
262 attributeValue
.append(')');
268 void registerProperties(std::ostream
& o
,
269 TypeManager
const & /*manager*/,
270 const AttributeInfo
& properties
,
271 const OString
& indentation
)
273 if (!properties
.empty()) {
275 OStringBuffer attributeValue
;
276 for (AttributeInfo::const_iterator
i(properties
.begin());
277 i
!= properties
.end(); ++i
)
279 if (i
->second
.second
> 0) {
280 cast
= checkAttribute(attributeValue
, i
->second
.second
);
283 attributeValue
.append('0');
286 o
<< indentation
<< "registerProperty(\"" << i
->first
287 << "\", \"m_" << i
->first
<< "\",\n"
288 << indentation
<< " ";
292 o
<< attributeValue
.makeStringAndClear() << ");\n";
297 void generateXLocalizableBodies(std::ostream
& o
) {
298 // com.sun.star.lang.XLocalizable:
300 o
<< " // com.sun.star.lang.XLocalizable:\n"
301 " public void setLocale(com.sun.star.lang.Locale eLocale)\n {\n"
302 " m_locale = eLocale;\n }\n\n";
305 o
<< " public com.sun.star.lang.Locale getLocale()\n {\n"
306 " return m_locale;\n }\n\n";
309 void generateXAddInBodies(std::ostream
& o
, ProgramOptions
const & options
)
311 // com.sun.star.sheet.XAddIn:
312 // getProgrammaticFuntionName
313 o
<< " // com.sun.star.sheet.XAddIn:\n"
314 " public String getProgrammaticFuntionName(String "
315 "aDisplayName)\n {\n"
317 " com.sun.star.container.XNameAccess xNAccess =\n"
318 " (com.sun.star.container.XNameAccess)UnoRuntime."
320 " com.sun.star.container.XNameAccess.class, m_xHAccess);"
321 "\n String functions[] = xNAccess.getElementNames();\n"
322 " String sDisplayName = \"\";\n"
323 " int len = functions.length;\n"
324 " for (int i=0; i < len; ++i) {\n"
325 " sDisplayName = com.sun.star.uno.AnyConverter.toString(\n"
326 " getAddinProperty(functions[i], \"\", sDISPLAYNAME));\n"
327 " if (sDisplayName.equals(aDisplayName))\n"
328 " return functions[i];\n }\n"
329 " }\n catch ( com.sun.star.uno.RuntimeException e ) {\n"
331 " catch ( com.sun.star.uno.Exception e ) {\n }\n\n"
332 " return \"\";\n }\n\n";
334 // getDisplayFunctionName
335 o
<< " public String getDisplayFunctionName(String "
336 "aProgrammaticName)\n {\n"
337 " return getAddinProperty(aProgrammaticName, \"\", sDISPLAYNAME);\n"
340 // getFunctionDescription
341 o
<< " public String getFunctionDescription(String "
342 "aProgrammaticName)\n {\n"
343 " return getAddinProperty(aProgrammaticName, \"\", sDESCRIPTION);\n"
346 // getDisplayArgumentName
347 o
<< " public String getDisplayArgumentName(String "
348 "aProgrammaticFunctionName, int nArgument)\n {\n";
350 o
<< " return getAddinProperty(aProgrammaticFunctionName,\n"
351 " m_functionMap.get(\n"
352 " aProgrammaticFunctionName).get("
354 " sDISPLAYNAME);\n }\n\n";
356 o
<< " return getAddinProperty(aProgrammaticFunctionName, (String)\n"
357 " ((java.util.Hashtable)m_functionMap."
358 "get(\n aProgrammaticFunctionName))."
359 "get(\n new Integer(nArgument))"
360 ", sDISPLAYNAME);\n }\n\n";
363 // getArgumentDescription
364 o
<< " public String getArgumentDescription(String "
365 "aProgrammaticFunctionName, int nArgument)\n {\n";
367 o
<< " return getAddinProperty(aProgrammaticFunctionName,\n"
368 " m_functionMap.get(\n"
369 " aProgrammaticFunctionName).get("
371 " sDESCRIPTION);\n }\n\n";
373 o
<< " return getAddinProperty(aProgrammaticFunctionName, (String)\n"
374 " ((java.util.Hashtable)m_functionMap."
375 "get(\n aProgrammaticFunctionName))."
376 "get(\n new Integer(nArgument))"
377 ", sDESCRIPTION);\n }\n\n";
379 // getProgrammaticCategoryName
380 o
<< " public String getProgrammaticCategoryName(String "
381 "aProgrammaticFunctionName)\n {\n"
382 " return getAddinProperty(aProgrammaticFunctionName, \"\", "
383 "sCATEGORY);\n }\n\n";
385 // getDisplayCategoryName
386 o
<< " public String getDisplayCategoryName(String "
387 "aProgrammaticFunctionName)\n {\n"
388 " return getAddinProperty(aProgrammaticFunctionName, \"\", "
389 "sCATEGORYDISPLAYNAME);\n }\n\n";
392 void generateXCompatibilityNamesBodies(std::ostream
& o
)
394 o
<< " // com.sun.star.sheet.XCompatibilityNames:\n"
395 " public com.sun.star.sheet.LocalizedName[] getCompatibilityNames("
396 "String aProgrammaticName)\n {\n"
397 " com.sun.star.sheet.LocalizedName[] seqLocalizedNames =\n"
398 " new com.sun.star.sheet.LocalizedName[0];\n\n try {\n";
400 o
<< " StringBuffer path = new StringBuffer(aProgrammaticName);\n"
401 " path.append(\"/CompatibilityName\");\n"
402 " String hname = path.toString();\n\n";
404 o
<< " if ( m_xCompAccess.hasByHierarchicalName(hname) ) {\n"
405 " com.sun.star.container.XNameAccess xNameAccess =\n"
406 " (com.sun.star.container.XNameAccess)UnoRuntime."
408 " com.sun.star.container.XNameAccess.class,\n"
409 " m_xCompAccess.getByHierarchicalName(hname));\n\n"
410 " String elems[] = xNameAccess.getElementNames();\n"
411 " int len = elems.length;\n"
412 " seqLocalizedNames = new com.sun.star.sheet.LocalizedName"
413 "[len];\n String sCompatibilityName = \"\";\n\n";
415 o
<< " for (int i=0; i < len; ++i) {\n"
416 " String sLocale = elems[i];\n"
417 " sCompatibilityName = com.sun.star.uno.AnyConverter."
418 "toString(\n xNameAccess.getByName(sLocale));\n\n"
419 " com.sun.star.lang.Locale aLocale = \n"
420 " new com.sun.star.lang.Locale();\n\n"
421 " String tokens[] = sLocale.split(\"-\");\n"
422 " int nToken = tokens.length;\n"
423 " if (nToken >= 1) aLocale.Language = tokens[0];\n"
424 " if (nToken >= 2) aLocale.Country = tokens[1];\n"
425 " if (nToken >= 3) {\n"
426 " StringBuffer buf = \n"
427 " new StringBuffer(tokens[2]);\n"
428 " for (int t=3; t < nToken; ++t)\n"
429 " buf.append(tokens[t]);\n\n"
430 " aLocale.Variant = buf.toString();\n"
432 " seqLocalizedNames[i].Locale = aLocale;\n"
433 " seqLocalizedNames[i].Name = sCompatibilityName;\n"
435 " catch ( com.sun.star.uno.RuntimeException e ) {\n"
437 " catch ( com.sun.star.uno.Exception e ) {\n }\n\n"
438 " return seqLocalizedNames;\n }\n\n";
441 void generateXInitializationBodies(std::ostream
& o
)
443 o
<< " // com.sun.star.lang.XInitialization:\n"
444 " public void initialize( Object[] object )\n"
445 " throws com.sun.star.uno.Exception\n {\n"
446 " if ( object.length > 0 )\n {\n"
447 " m_xFrame = (com.sun.star.frame.XFrame)UnoRuntime.queryInterface(\n"
448 " com.sun.star.frame.XFrame.class, object[0]);\n }\n }\n\n";
451 void generateXDispatchBodies(std::ostream
& o
, ProgramOptions
const & options
)
453 // com.sun.star.frame.XDispatch
455 o
<< " // com.sun.star.frame.XDispatch:\n"
456 " public void dispatch( com.sun.star.util.URL aURL,\n"
457 " com.sun.star.beans.PropertyValue[] aArguments )\n {\n";
459 ProtocolCmdMap::const_iterator iter
= options
.protocolCmdMap
.begin();
460 while (iter
!= options
.protocolCmdMap
.end()) {
461 o
<< " if ( aURL.Protocol.compareTo(\"" << (*iter
).first
462 << "\") == 0 )\n {\n";
464 for (std::vector
< OString
>::const_iterator i
= (*iter
).second
.begin();
465 i
!= (*iter
).second
.end(); ++i
) {
466 o
<< " if ( aURL.Path.compareTo(\"" << (*i
) << "\") == 0 )\n"
467 " {\n // add your own code here\n"
477 o
<< " public void addStatusListener( com.sun.star.frame.XStatusListener xControl,\n"
478 " com.sun.star.util.URL aURL )\n {\n"
479 " // add your own code here\n }\n\n";
481 // com.sun.star.frame.XDispatch
482 o
<< " public void removeStatusListener( com.sun.star.frame.XStatusListener xControl,\n"
483 " com.sun.star.util.URL aURL )\n {\n"
484 " // add your own code here\n }\n\n";
487 void generateXDispatchProviderBodies(std::ostream
& o
, ProgramOptions
const & options
)
489 // com.sun.star.frame.XDispatchProvider
491 o
<< " // com.sun.star.frame.XDispatchProvider:\n"
492 " public com.sun.star.frame.XDispatch queryDispatch( com.sun.star.util.URL aURL,\n"
493 " String sTargetFrameName,\n"
494 " int iSearchFlags )\n {\n";
496 ProtocolCmdMap::const_iterator iter
= options
.protocolCmdMap
.begin();
497 while (iter
!= options
.protocolCmdMap
.end()) {
498 o
<< " if ( aURL.Protocol.compareTo(\"" << (*iter
).first
499 << "\") == 0 )\n {\n";
501 for (std::vector
< OString
>::const_iterator i
= (*iter
).second
.begin();
502 i
!= (*iter
).second
.end(); ++i
) {
503 o
<< " if ( aURL.Path.compareTo(\"" << (*i
) << "\") == 0 )\n"
510 o
<< " return null;\n }\n\n";
513 o
<< " // com.sun.star.frame.XDispatchProvider:\n"
514 " public com.sun.star.frame.XDispatch[] queryDispatches(\n"
515 " com.sun.star.frame.DispatchDescriptor[] seqDescriptors )\n {\n"
516 " int nCount = seqDescriptors.length;\n"
517 " com.sun.star.frame.XDispatch[] seqDispatcher =\n"
518 " new com.sun.star.frame.XDispatch[seqDescriptors.length];\n\n"
519 " for( int i=0; i < nCount; ++i )\n {\n"
520 " seqDispatcher[i] = queryDispatch(seqDescriptors[i].FeatureURL,\n"
521 " seqDescriptors[i].FrameName,\n"
522 " seqDescriptors[i].SearchFlags );\n"
523 " }\n return seqDispatcher;\n }\n\n";
526 void generateMethodBodies(std::ostream
& o
,
527 ProgramOptions
const & options
,
528 TypeManager
const & manager
,
529 const std::hash_set
< OString
, OStringHash
>& interfaces
,
530 const OString
& indentation
, bool usepropertymixin
)
532 std::hash_set
< OString
, OStringHash
>::const_iterator iter
=
534 codemaker::GeneratedTypeSet generated
;
535 while (iter
!= interfaces
.end()) {
538 if (type
.equals("com.sun.star.lang.XServiceInfo")) {
539 generateXServiceInfoBodies(o
);
542 if (options
.componenttype
== 2) {
543 if (type
.equals("com.sun.star.lang.XServiceName")) {
544 o
<< " // com.sun.star.lang.XServiceName:\n"
545 " public String getServiceName() {\n"
546 " return sADDIN_SERVICENAME;\n }\n";
549 } else if (type
.equals("com.sun.star.sheet.XAddIn")) {
550 generateXAddInBodies(o
, options
);
553 // special handling of XLocalizable -> parent of XAddIn
554 if (!generated
.contains("com.sun.star.lang.XLocalizable")) {
555 generateXLocalizableBodies(o
);
556 generated
.add("com.sun.star.lang.XLocalizable");
559 } else if (type
.equals("com.sun.star.lang.XLocalizable")) {
560 generateXLocalizableBodies(o
);
563 } else if (type
.equals("com.sun.star.sheet.XCompatibilityNames")) {
564 generateXCompatibilityNamesBodies(o
);
569 if (options
.componenttype
== 3) {
570 if (type
.equals("com.sun.star.lang.XInitialization")) {
571 generateXInitializationBodies(o
);
574 } else if (type
.equals("com.sun.star.frame.XDispatch")) {
575 generateXDispatchBodies(o
, options
);
578 } else if (type
.equals("com.sun.star.frame.XDispatchProvider")) {
579 generateXDispatchProviderBodies(o
, options
);
584 typereg::Reader
reader(manager
.getTypeReader(type
.replace('.','/')));
585 printMethods(o
, options
, manager
, reader
, generated
, "_",
586 indentation
, true, usepropertymixin
);
591 static const char* propcomment
=
592 " // use the last parameter of the PropertySetMixin constructor\n"
593 " // for your optional attributes if necessary. See the documentation\n"
594 " // of the PropertySetMixin helper for further information.\n"
595 " // Ensure that your attributes are initialized correctly!\n";
598 void generateAddinConstructorAndHelper(std::ostream
& o
,
599 ProgramOptions
const & options
,
600 TypeManager
const & manager
, const OString
& classname
,
601 const std::hash_set
< OString
, OStringHash
>& services
,
602 const std::hash_set
< OString
, OStringHash
>& interfaces
)
604 o
<< " private com.sun.star.lang.Locale m_locale = "
605 "new com.sun.star.lang.Locale();\n";
607 if (!options
.backwardcompatible
) {
609 o
<< "\n public " << classname
<< "( XComponentContext context )\n"
610 " {\n m_xContext = context;\n }\n\n";
615 // get the one and only add-in service for later use
616 std::hash_set
< OString
, OStringHash
>::const_iterator iter
= services
.begin();
617 OString sAddinService
= (*iter
).replace('/', '.');
618 if (sAddinService
.equals("com.sun.star.sheet.AddIn")) {
619 sAddinService
= (*(++iter
)).replace('/', '.');
623 // add-in specific fields
624 o
<< "\n private static final String sADDIN_SERVICENAME = \""
625 << sAddinService
<< "\";\n\n";
626 o
<< " private static final String sDISPLAYNAME = "
628 " private static final String sDESCRIPTION = "
630 " private static final String sCATEGORY = \"Category\";\n"
631 " private static final String sCATEGORYDISPLAYNAME = "
632 "\"CategoryDisplayName\";\n\n";
634 o
<< " private com.sun.star.container.XHierarchicalNameAccess "
635 "m_xHAccess = null;\n"
636 " private com.sun.star.container.XHierarchicalNameAccess "
637 "m_xCompAccess = null;\n";
639 o
<< " private java.util.Hashtable<\n String, "
640 "java.util.Hashtable< Integer, String> > m_functionMap = null;\n\n";
642 o
<< " private java.util.Hashtable m_functionMap = null;\n\n";
645 o
<< "\n public " << classname
<< "( XComponentContext context )\n {\n"
646 " m_xContext = context;\n\n"
650 o
<< " m_functionMap = new java.util.Hashtable<\n"
651 " String, java.util.Hashtable< Integer, "
654 o
<< " m_functionMap = new java.util.Hashtable();\n\n";
657 generateFunctionParameterMap(o
, options
, manager
, interfaces
);
659 o
<< " com.sun.star.lang.XMultiServiceFactory xProvider = \n"
660 " (com.sun.star.lang.XMultiServiceFactory)UnoRuntime."
662 " com.sun.star.lang.XMultiServiceFactory.class,\n"
663 " m_xContext.getServiceManager().createInstanceWithContext("
664 "\n \"com.sun.star.configuration.ConfigurationProvider\""
665 ",\n m_xContext));\n\n";
667 o
<< " String sReadOnlyView = "
668 "\"com.sun.star.configuration.ConfigurationAccess\";\n\n";
670 o
<< " StringBuffer sPath = new StringBuffer(\n"
671 " \"/org.openoffice.Office.CalcAddIns/AddInInfo/\");\n"
672 " sPath.append(sADDIN_SERVICENAME);\n"
673 " sPath.append(\"/AddInFunctions\");\n\n";
675 o
<< " // create arguments: nodepath\n"
676 " com.sun.star.beans.PropertyValue aArgument = \n"
677 " new com.sun.star.beans.PropertyValue();\n"
678 " aArgument.Name = \"nodepath\";\n"
679 " aArgument.Value = new com.sun.star.uno.Any(\n"
680 " com.sun.star.uno.Type.STRING, sPath.toString());\n\n";
682 o
<< " Object aArguments[] = new Object[1];\n"
683 " aArguments[0] = new com.sun.star.uno.Any("
684 " new com.sun.star.uno.Type(\n"
685 " com.sun.star.beans.PropertyValue.class), aArgument);\n\n";
687 o
<< " // create the default view using default UI locale\n"
688 " Object xIface = \n"
689 " xProvider.createInstanceWithArguments(sReadOnlyView, "
692 o
<< " m_xHAccess = (com.sun.star.container.XHierarchicalNameAccess)\n"
693 " UnoRuntime.queryInterface(\n"
694 " com.sun.star.container.XHierarchicalNameAccess.class, "
697 o
<< " // extends arguments to create a view for all locales to get "
698 "simple\n // access to the compatibilityname property\n"
699 " aArguments = new Object[2];\n"
700 " aArguments[0] = new com.sun.star.uno.Any( "
701 "new com.sun.star.uno.Type(\n"
702 " com.sun.star.beans.PropertyValue.class), aArgument);\n"
703 " aArgument.Name = \"locale\";\n"
704 " aArgument.Value = new com.sun.star.uno.Any(\n"
705 " com.sun.star.uno.Type.STRING, \"*\");\n"
706 " aArguments[1] = new com.sun.star.uno.Any( "
707 " new com.sun.star.uno.Type(\n"
708 " com.sun.star.beans.PropertyValue.class), aArgument);\n\n";
710 o
<< " // create view for all locales\n"
711 " xIface = xProvider.createInstanceWithArguments(sReadOnlyView, "
713 " m_xCompAccess = (com.sun.star.container.XHierarchicalNameAccess)\n"
714 " UnoRuntime.queryInterface(\n"
715 " com.sun.star.container.XHierarchicalNameAccess.class, "
717 " catch ( com.sun.star.uno.Exception e ) {\n }\n }\n\n";
719 // add-in helper function
720 o
<< " // addin configuration property helper function:\n"
721 " String getAddinProperty(String funcName, "
722 "String paramName, String propName)\n {\n"
723 " try {\n StringBuffer buf = "
724 "new StringBuffer(funcName);\n\n"
725 " if (paramName.length() > 0) {\n"
726 " buf.append(\"/Parameters/\");\n"
727 " buf.append(paramName);\n }\n\n";
729 o
<< " com.sun.star.beans.XPropertySet xPropSet =\n"
730 " (com.sun.star.beans.XPropertySet)UnoRuntime."
732 " com.sun.star.beans.XPropertySet.class,\n"
733 " m_xHAccess.getByHierarchicalName(buf.toString()));\n\n"
734 " return com.sun.star.uno.AnyConverter.toString(\n"
735 " xPropSet.getPropertyValue(propName));\n }\n"
736 " catch ( com.sun.star.uno.RuntimeException e ) {\n"
738 " catch ( com.sun.star.uno.Exception e ) {\n }\n"
739 " return \"\";\n }\n\n";
743 void generateClassDefinition(std::ostream
& o
,
744 ProgramOptions
const & options
,
745 TypeManager
const & manager
,
746 const OString
& classname
,
747 const std::hash_set
< OString
, OStringHash
>& services
,
748 const std::hash_set
< OString
, OStringHash
>& interfaces
,
749 const AttributeInfo
& properties
,
750 const AttributeInfo
& attributes
,
751 const OString
& propertyhelper
, bool supportxcomponent
)
753 o
<< "\n\npublic final class " << classname
<< " extends ";
755 if (!interfaces
.empty()) {
756 if (propertyhelper
.equals("_")) {
757 o
<< "PropertySet\n";
759 if (supportxcomponent
)
760 o
<< "ComponentBase\n";
765 std::hash_set
< OString
, OStringHash
>::const_iterator iter
=
767 while (iter
!= interfaces
.end()) {
770 if (iter
!= interfaces
.end())
776 o
<< " private final XComponentContext m_xContext;\n";
778 // additional member for add-ons
779 if (options
.componenttype
== 3) {
780 o
<< " private com.sun.star.frame.XFrame m_xFrame;\n";
783 // check property helper
784 if (propertyhelper
.getLength() > 1)
785 o
<< " private final PropertySetMixin m_prophlp;\n";
787 o
<< " private static final String m_implementationName = "
788 << classname
<< ".class.getName();\n";
790 if (!services
.empty()) {
791 o
<< " private static final String[] m_serviceNames = {\n";
792 std::hash_set
< OString
, OStringHash
>::const_iterator iter
=
794 while (iter
!= services
.end()) {
795 o
<< " \"" << (*iter
).replace('/','.') << "\"";
797 if (iter
!= services
.end())
804 // attribute/property members
805 if (!properties
.empty()) {
806 AttributeInfo::const_iterator iter
=
808 o
<< " // properties\n";
809 while (iter
!= properties
.end()) {
811 printType(o
, options
, manager
, iter
->second
.first
.replace('.','/'),
813 o
<< " m_" << iter
->first
<< ";\n";
816 } else if (!attributes
.empty()) {
817 AttributeInfo::const_iterator iter
=
819 o
<< " // attributes\n";
820 while (iter
!= attributes
.end()) {
822 printType(o
, options
, manager
, iter
->second
.first
.replace('.','/'),
824 o
<< " m_" << iter
->first
<< " = ";
825 printType(o
, options
, manager
, iter
->second
.first
.replace('.','/'),
832 // special handling of calc add-ins
833 if (options
.componenttype
== 2)
835 generateAddinConstructorAndHelper(o
, options
, manager
, classname
,
836 services
, interfaces
);
838 o
<< "\n public " << classname
<< "( XComponentContext context )\n"
839 " {\n m_xContext = context;\n";
840 if (propertyhelper
.equals("_")) {
841 registerProperties(o
, manager
, properties
, " ");
843 if (propertyhelper
.getLength() > 1) {
845 << " m_prophlp = new PropertySetMixin(m_xContext, this,\n"
846 << " new Type(" << propertyhelper
847 << ".class), null);\n";
854 if (!services
.empty())
855 generateCompFunctions(o
, classname
);
857 generateMethodBodies(o
, options
, manager
, interfaces
,
858 " ", propertyhelper
.getLength() > 1);
860 // end of class definition
864 void generateSkeleton(ProgramOptions
const & options
,
865 TypeManager
const & manager
,
866 std::vector
< OString
> const & types
,
867 OString
const & /*delegate*/)
869 std::hash_set
< OString
, OStringHash
> interfaces
;
870 std::hash_set
< OString
, OStringHash
> services
;
871 AttributeInfo properties
;
872 AttributeInfo attributes
;
873 std::hash_set
< OString
, OStringHash
> propinterfaces
;
874 bool serviceobject
= false;
875 bool supportxcomponent
= false;
877 std::vector
< OString
>::const_iterator iter
= types
.begin();
878 while (iter
!= types
.end()) {
879 checkType(manager
, *iter
, interfaces
, services
, properties
);
883 if (options
.componenttype
== 3) {
884 // the Protocolhandler service is mandatory for an protocol handler add-on,
885 // so it is defaulted. The XDispatchProvider provides Dispatch objects for
886 // certain functions and the generated impl object implements XDispatch
887 // directly for simplicity reasons.
888 checkType(manager
, "com.sun.star.frame.ProtocolHandler",
889 interfaces
, services
, properties
);
890 checkType(manager
, "com.sun.star.frame.XDispatch",
891 interfaces
, services
, properties
);
894 // ProtocolCmdMap::const_iterator iter2 = options.protocolCmdMap.begin();
895 // while (iter2 != options.protocolCmdMap.end()) {
896 // fprintf(stdout, "prt=%s\n", (*iter2).first.getStr());
898 // for (std::vector< OString >::const_iterator i = (*iter2).second.begin();
899 // i != (*iter2).second.end(); ++i) {
900 // fprintf(stdout, "cmd=%s\n", (*i).getStr());
907 if (options
.componenttype
== 2) {
908 if (services
.size() != 1) {
909 throw CannotDumpException(
910 "for calc add-in components one and only one service type is "
911 "necessary! Please reference a valid type with the '-t' option.");
914 // if backwardcompatible==true the AddIn service needs to be added to the
915 // suported service list, the necessary intefaces are mapped to the add-in
916 // configuration. Since OO.org 2.0.4 this is obsolete and the add-in is
917 // take form the configuration from Calc directly, this simplifies the
919 if (options
.backwardcompatible
) {
920 checkType(manager
, "com.sun.star.sheet.AddIn",
921 interfaces
, services
, properties
);
923 // special case for the optional XLocalization interface. It should be
924 // implemented always. But it is parent of the XAddIn and we need it only
925 // if backwardcompatible is false.
926 if (interfaces
.find("com.sun.star.lang.XLocalizable") == interfaces
.end()) {
927 interfaces
.insert("com.sun.star.lang.XLocalizable");
933 // check if service object or simple UNO object
934 if (!services
.empty())
935 serviceobject
= true;
937 OString propertyhelper
= checkPropertyHelper(
938 options
, manager
, services
, interfaces
, attributes
, propinterfaces
);
939 checkDefaultInterfaces(interfaces
, services
, propertyhelper
);
941 if (options
.componenttype
== 2) {
942 if (propertyhelper
.getLength() > 0)
943 std::cerr
<< "WARNING: interfaces specifying calc add-in functions "
944 "shouldn't support attributes!\n";
947 supportxcomponent
= checkXComponentSupport(manager
, interfaces
);
949 OString compFileName
;
951 std::ostream
* pofs
= NULL
;
952 bool standardout
= getOutputStream(options
, ".java",
953 &pofs
, compFileName
, tmpFileName
);
956 if (!standardout
&& options
.license
) {
957 printLicenseHeader(*pofs
, compFileName
);
960 generatePackage(*pofs
, options
.implname
);
962 generateImports(*pofs
, options
, interfaces
, propertyhelper
,
963 serviceobject
, supportxcomponent
);
965 OString
classname(options
.implname
);
967 if ((index
= classname
.lastIndexOf('.')) > 0)
968 classname
= classname
.copy(index
+1);
970 generateClassDefinition(*pofs
, options
, manager
, classname
, services
,
971 interfaces
, properties
, attributes
, propertyhelper
,
974 if ( !standardout
&& pofs
&& ((std::ofstream
*)pofs
)->is_open()) {
975 ((std::ofstream
*)pofs
)->close();
977 OSL_VERIFY(makeValidTypeFile(compFileName
, tmpFileName
, sal_False
));
979 } catch(CannotDumpException
& e
) {
981 std::cerr
<< "ERROR: " << e
.m_message
.getStr() << "\n";
982 if ( !standardout
) {
983 if (pofs
&& ((std::ofstream
*)pofs
)->is_open()) {
984 ((std::ofstream
*)pofs
)->close();
987 // remove existing type file if something goes wrong to ensure
989 if (fileExists(compFileName
))
990 removeTypeFile(compFileName
);
992 // remove tmp file if something goes wrong
993 removeTypeFile(tmpFileName
);