1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #include "codemaker/commonjava.hxx"
31 #include "ostringostreaminserter.hxx"
32 #include "skeletoncommon.hxx"
33 #include "skeletonjava.hxx"
37 using namespace ::rtl
;
38 using namespace ::codemaker::java
;
40 namespace skeletonmaker
{ namespace java
{
42 void generatePackage(std::ostream
& o
, const OString
& implname
)
44 sal_Int32 index
= implname
.lastIndexOf('.');
46 o
<< "package " << implname
.copy(0, index
) << ";\n\n";
49 void generateImports(std::ostream
& o
, ProgramOptions
const & options
,
50 const OString
& propertyhelper
,
51 bool serviceobject
, bool supportxcomponent
)
53 if (options
.componenttype
== 3)
54 o
<< "import com.sun.star.uno.UnoRuntime;\n";
55 o
<< "import com.sun.star.uno.XComponentContext;\n";
57 o
<< "import com.sun.star.lib.uno.helper.Factory;\n";
58 o
<< "import com.sun.star.lang.XSingleComponentFactory;\n";
59 o
<< "import com.sun.star.registry.XRegistryKey;\n";
62 if (!propertyhelper
.equals("_")) {
63 if (supportxcomponent
)
64 o
<< "import com.sun.star.lib.uno.helper.ComponentBase;\n";
66 o
<< "import com.sun.star.lib.uno.helper.WeakBase;\n";
68 if (!propertyhelper
.isEmpty()) {
69 if (propertyhelper
.equals("_")) {
70 o
<< "import com.sun.star.lib.uno.helper.PropertySet;\n";
71 o
<< "import com.sun.star.beans.PropertyAttribute;\n";
73 o
<< "import com.sun.star.uno.Type;\n";
74 o
<< "import com.sun.star.uno.Any;\n";
75 o
<< "import com.sun.star.beans.Ambiguous;\n";
76 o
<< "import com.sun.star.beans.Defaulted;\n";
77 o
<< "import com.sun.star.beans.Optional;\n";
78 o
<< "import com.sun.star.lib.uno.helper.PropertySetMixin;\n";
83 void generateCompFunctions(std::ostream
& o
, const OString
& classname
)
85 o
<< " public static XSingleComponentFactory __getComponentFactory("
86 " String sImplementationName ) {\n"
87 " XSingleComponentFactory xFactory = null;\n\n"
88 " if ( sImplementationName.equals( m_implementationName ) )\n"
89 " xFactory = Factory.createComponentFactory("
90 << classname
<< ".class, m_serviceNames);\n"
91 " return xFactory;\n }\n\n";
93 o
<< " public static boolean __writeRegistryServiceInfo("
94 " XRegistryKey xRegistryKey ) {\n"
95 " return Factory.writeRegistryServiceInfo(m_implementationName,\n"
101 void generateXServiceInfoBodies(std::ostream
& o
)
103 o
<< " // com.sun.star.lang.XServiceInfo:\n";
104 o
<< " public String getImplementationName() {\n"
105 << " return m_implementationName;\n }\n\n";
107 o
<< " public boolean supportsService( String sService ) {\n"
108 << " int len = m_serviceNames.length;\n\n"
109 << " for( int i=0; i < len; i++) {\n"
110 << " if (sService.equals(m_serviceNames[i]))\n"
112 << " }\n return false;\n }\n\n";
114 o
<< " public String[] getSupportedServiceNames() {\n"
115 << " return m_serviceNames;\n }\n\n";
118 void generateXPropertySetBodies(std::ostream
& o
)
120 o
<< " // com.sun.star.beans.XPropertySet:\n";
121 o
<< " public com.sun.star.beans.XPropertySetInfo getPropertySetInfo()\n"
122 " {\n return m_prophlp.getPropertySetInfo();\n }\n\n";
124 o
<< " public void setPropertyValue(String aPropertyName, "
125 "Object aValue) throws "
126 "com.sun.star.beans.UnknownPropertyException, "
127 "com.sun.star.beans.PropertyVetoException, "
128 "com.sun.star.lang.IllegalArgumentException,"
129 "com.sun.star.lang.WrappedTargetException\n {\n "
130 "m_prophlp.setPropertyValue(aPropertyName, aValue);\n }\n\n";
132 o
<< " public Object getPropertyValue(String "
133 "aPropertyName) throws com.sun.star.beans.UnknownPropertyException, "
134 "com.sun.star.lang.WrappedTargetException\n {\n return "
135 "m_prophlp.getPropertyValue(aPropertyName);\n }\n\n";
137 o
<< " public void addPropertyChangeListener(String aPropertyName"
138 ", com.sun.star.beans.XPropertyChangeListener xListener) throws "
139 "com.sun.star.beans.UnknownPropertyException, "
140 "com.sun.star.lang.WrappedTargetException\n {\n "
141 "m_prophlp.addPropertyChangeListener(aPropertyName, xListener);\n }\n\n";
143 o
<< " public void removePropertyChangeListener(String "
144 "aPropertyName, com.sun.star.beans.XPropertyChangeListener xListener) "
145 "throws com.sun.star.beans.UnknownPropertyException, "
146 "com.sun.star.lang.WrappedTargetException\n {\n "
147 "m_prophlp.removePropertyChangeListener(aPropertyName, xListener);\n"
150 o
<< " public void addVetoableChangeListener(String aPropertyName"
151 ", com.sun.star.beans.XVetoableChangeListener xListener) throws "
152 "com.sun.star.beans.UnknownPropertyException, "
153 "com.sun.star.lang.WrappedTargetException\n {\n "
154 "m_prophlp.addVetoableChangeListener(aPropertyName, xListener);\n }\n\n";
156 o
<< " public void removeVetoableChangeListener(String "
157 "aPropertyName, com.sun.star.beans.XVetoableChangeListener xListener) "
158 "throws com.sun.star.beans.UnknownPropertyException, "
159 "com.sun.star.lang.WrappedTargetException\n {\n "
160 "m_prophlp.removeVetoableChangeListener(aPropertyName, xListener);\n }\n\n";
163 void generateXFastPropertySetBodies(std::ostream
& o
)
165 o
<< " // com.sun.star.beans.XFastPropertySet:\n";
167 o
<< " public void setFastPropertyValue(int nHandle, Object "
168 "aValue) throws com.sun.star.beans.UnknownPropertyException, "
169 "com.sun.star.beans.PropertyVetoException, "
170 "com.sun.star.lang.IllegalArgumentException, "
171 "com.sun.star.lang.WrappedTargetException\n {\n "
172 "m_prophlp.setFastPropertyValue(nHandle, aValue);\n }\n\n";
174 o
<< " public Object getFastPropertyValue(int nHandle) throws "
175 "com.sun.star.beans.UnknownPropertyException, "
176 "com.sun.star.lang.WrappedTargetException\n {\n return "
177 "m_prophlp.getFastPropertyValue(nHandle);\n }\n\n";
180 void generateXPropertyAccessBodies(std::ostream
& o
)
182 o
<< " // com.sun.star.beans.XPropertyAccess:\n";
184 o
<< " public com.sun.star.beans.PropertyValue[] getPropertyValues()\n"
185 " {\n return m_prophlp.getPropertyValues();\n }\n\n";
187 o
<< " public void setPropertyValues(com.sun.star.beans.PropertyValue[] "
188 "aProps) throws com.sun.star.beans.UnknownPropertyException, "
189 "com.sun.star.beans.PropertyVetoException, "
190 "com.sun.star.lang.IllegalArgumentException, "
191 "com.sun.star.lang.WrappedTargetException\n {\n "
192 "m_prophlp.setPropertyValues(aProps);\n }\n\n";
196 bool checkAttribute(OStringBuffer
& attributeValue
, sal_uInt16 attribute
)
199 sal_uInt16 attributes
[9] = {
200 /* com::sun::star::beans::PropertyValue::MAYBEVOID */ 1,
201 /* com::sun::star::beans::PropertyValue::BOUND */ 2,
202 /* com::sun::star::beans::PropertyValue::CONSTRAINED */ 4,
203 /* com::sun::star::beans::PropertyValue::TRANSIENT */ 8,
204 /* com::sun::star::beans::PropertyValue::READONLY */ 16,
205 /* com::sun::star::beans::PropertyValue::MAYBEAMBIGIOUS */ 32,
206 /* com::sun::star::beans::PropertyValue::MAYBEDEFAULT */ 64,
207 /* com::sun::star::beans::PropertyValue::REMOVEABLE */ 128,
208 /* com::sun::star::beans::PropertyValue::OPTIONAL */ 256 };
210 for (sal_uInt16 i
= 0; i
< 9; i
++)
212 if (attribute
& attributes
[i
]) {
213 if (attributeValue
.getLength() > 0) {
215 attributeValue
.append("|");
217 switch (attributes
[i
])
220 attributeValue
.append("PropertyAttribute.MAYBEVOID");
223 attributeValue
.append("PropertyAttribute.BOUND");
226 attributeValue
.append("PropertyAttribute.CONSTRAINED");
229 attributeValue
.append("PropertyAttribute.TRANSIENT");
232 attributeValue
.append("PropertyAttribute.READONLY");
235 attributeValue
.append("PropertyAttribute.MAYBEAMBIGIOUS");
238 attributeValue
.append("PropertyAttribute.MAYBEDEFAULT");
241 attributeValue
.append("PropertyAttribute.REMOVEABLE");
244 attributeValue
.append("PropertyAttribute.OPTIONAL");
250 attributeValue
.insert(0, '(');
251 attributeValue
.append(')');
257 void registerProperties(std::ostream
& o
,
258 const AttributeInfo
& properties
,
259 const OString
& indentation
)
261 if (!properties
.empty()) {
263 OStringBuffer attributeValue
;
264 for (AttributeInfo::const_iterator
i(properties
.begin());
265 i
!= properties
.end(); ++i
)
267 if (i
->second
.second
> 0) {
268 cast
= checkAttribute(attributeValue
, i
->second
.second
);
271 attributeValue
.append('0');
274 o
<< indentation
<< "registerProperty(\"" << i
->first
275 << "\", \"m_" << i
->first
<< "\",\n"
276 << indentation
<< " ";
280 o
<< attributeValue
.makeStringAndClear() << ");\n";
285 void generateXLocalizableBodies(std::ostream
& o
) {
286 // com.sun.star.lang.XLocalizable:
288 o
<< " // com.sun.star.lang.XLocalizable:\n"
289 " public void setLocale(com.sun.star.lang.Locale eLocale)\n {\n"
290 " m_locale = eLocale;\n }\n\n";
293 o
<< " public com.sun.star.lang.Locale getLocale()\n {\n"
294 " return m_locale;\n }\n\n";
297 void generateXAddInBodies(std::ostream
& o
, ProgramOptions
const & options
)
299 // com.sun.star.sheet.XAddIn:
300 // getProgrammaticFuntionName
301 o
<< " // com.sun.star.sheet.XAddIn:\n"
302 " public String getProgrammaticFuntionName(String "
303 "aDisplayName)\n {\n"
305 " com.sun.star.container.XNameAccess xNAccess =\n"
306 " (com.sun.star.container.XNameAccess)UnoRuntime."
308 " com.sun.star.container.XNameAccess.class, m_xHAccess);"
309 "\n String functions[] = xNAccess.getElementNames();\n"
310 " String sDisplayName = \"\";\n"
311 " int len = functions.length;\n"
312 " for (int i=0; i < len; ++i) {\n"
313 " sDisplayName = com.sun.star.uno.AnyConverter.toString(\n"
314 " getAddinProperty(functions[i], \"\", sDISPLAYNAME));\n"
315 " if (sDisplayName.equals(aDisplayName))\n"
316 " return functions[i];\n }\n"
317 " }\n catch ( com.sun.star.uno.RuntimeException e ) {\n"
319 " catch ( com.sun.star.uno.Exception e ) {\n }\n\n"
320 " return \"\";\n }\n\n";
322 // getDisplayFunctionName
323 o
<< " public String getDisplayFunctionName(String "
324 "aProgrammaticName)\n {\n"
325 " return getAddinProperty(aProgrammaticName, \"\", sDISPLAYNAME);\n"
328 // getFunctionDescription
329 o
<< " public String getFunctionDescription(String "
330 "aProgrammaticName)\n {\n"
331 " return getAddinProperty(aProgrammaticName, \"\", sDESCRIPTION);\n"
334 // getDisplayArgumentName
335 o
<< " public String getDisplayArgumentName(String "
336 "aProgrammaticFunctionName, int nArgument)\n {\n";
338 o
<< " return getAddinProperty(aProgrammaticFunctionName,\n"
339 " m_functionMap.get(\n"
340 " aProgrammaticFunctionName).get("
342 " sDISPLAYNAME);\n }\n\n";
344 o
<< " return getAddinProperty(aProgrammaticFunctionName, (String)\n"
345 " ((java.util.Hashtable)m_functionMap."
346 "get(\n aProgrammaticFunctionName))."
347 "get(\n new Integer(nArgument))"
348 ", sDISPLAYNAME);\n }\n\n";
351 // getArgumentDescription
352 o
<< " public String getArgumentDescription(String "
353 "aProgrammaticFunctionName, int nArgument)\n {\n";
355 o
<< " return getAddinProperty(aProgrammaticFunctionName,\n"
356 " m_functionMap.get(\n"
357 " aProgrammaticFunctionName).get("
359 " sDESCRIPTION);\n }\n\n";
361 o
<< " return getAddinProperty(aProgrammaticFunctionName, (String)\n"
362 " ((java.util.Hashtable)m_functionMap."
363 "get(\n aProgrammaticFunctionName))."
364 "get(\n new Integer(nArgument))"
365 ", sDESCRIPTION);\n }\n\n";
367 // getProgrammaticCategoryName
368 o
<< " public String getProgrammaticCategoryName(String "
369 "aProgrammaticFunctionName)\n {\n"
370 " return getAddinProperty(aProgrammaticFunctionName, \"\", "
371 "sCATEGORY);\n }\n\n";
373 // getDisplayCategoryName
374 o
<< " public String getDisplayCategoryName(String "
375 "aProgrammaticFunctionName)\n {\n"
376 " return getAddinProperty(aProgrammaticFunctionName, \"\", "
377 "sCATEGORYDISPLAYNAME);\n }\n\n";
380 void generateXCompatibilityNamesBodies(std::ostream
& o
)
382 o
<< " // com.sun.star.sheet.XCompatibilityNames:\n"
383 " public com.sun.star.sheet.LocalizedName[] getCompatibilityNames("
384 "String aProgrammaticName)\n {\n"
385 " com.sun.star.sheet.LocalizedName[] seqLocalizedNames =\n"
386 " new com.sun.star.sheet.LocalizedName[0];\n\n try {\n";
388 o
<< " StringBuffer path = new StringBuffer(aProgrammaticName);\n"
389 " path.append(\"/CompatibilityName\");\n"
390 " String hname = path.toString();\n\n";
392 o
<< " if ( m_xCompAccess.hasByHierarchicalName(hname) ) {\n"
393 " com.sun.star.container.XNameAccess xNameAccess =\n"
394 " (com.sun.star.container.XNameAccess)UnoRuntime."
396 " com.sun.star.container.XNameAccess.class,\n"
397 " m_xCompAccess.getByHierarchicalName(hname));\n\n"
398 " String elems[] = xNameAccess.getElementNames();\n"
399 " int len = elems.length;\n"
400 " seqLocalizedNames = new com.sun.star.sheet.LocalizedName"
401 "[len];\n String sCompatibilityName = \"\";\n\n";
403 o
<< " for (int i=0; i < len; ++i) {\n"
404 " String sLocale = elems[i];\n"
405 " sCompatibilityName = com.sun.star.uno.AnyConverter."
406 "toString(\n xNameAccess.getByName(sLocale));\n\n"
407 " com.sun.star.lang.Locale aLocale = \n"
408 " new com.sun.star.lang.Locale();\n\n"
409 " String tokens[] = sLocale.split(\"-\");\n"
410 " int nToken = tokens.length;\n"
411 " if (nToken >= 1) aLocale.Language = tokens[0];\n"
412 " if (nToken >= 2) aLocale.Country = tokens[1];\n"
413 " if (nToken >= 3) {\n"
414 " StringBuffer buf = \n"
415 " new StringBuffer(tokens[2]);\n"
416 " for (int t=3; t < nToken; ++t)\n"
417 " buf.append(tokens[t]);\n\n"
418 " aLocale.Variant = buf.toString();\n"
420 " seqLocalizedNames[i].Locale = aLocale;\n"
421 " seqLocalizedNames[i].Name = sCompatibilityName;\n"
423 " catch ( com.sun.star.uno.RuntimeException e ) {\n"
425 " catch ( com.sun.star.uno.Exception e ) {\n }\n\n"
426 " return seqLocalizedNames;\n }\n\n";
429 void generateXInitializationBodies(std::ostream
& o
)
431 o
<< " // com.sun.star.lang.XInitialization:\n"
432 " public void initialize( Object[] object )\n"
433 " throws com.sun.star.uno.Exception\n {\n"
434 " if ( object.length > 0 )\n {\n"
435 " m_xFrame = (com.sun.star.frame.XFrame)UnoRuntime.queryInterface(\n"
436 " com.sun.star.frame.XFrame.class, object[0]);\n }\n }\n\n";
439 void generateXDispatchBodies(std::ostream
& o
, ProgramOptions
const & options
)
441 // com.sun.star.frame.XDispatch
443 o
<< " // com.sun.star.frame.XDispatch:\n"
444 " public void dispatch( com.sun.star.util.URL aURL,\n"
445 " com.sun.star.beans.PropertyValue[] aArguments )\n {\n";
447 ProtocolCmdMap::const_iterator iter
= options
.protocolCmdMap
.begin();
448 while (iter
!= options
.protocolCmdMap
.end()) {
449 o
<< " if ( aURL.Protocol.compareTo(\"" << (*iter
).first
450 << "\") == 0 )\n {\n";
452 for (std::vector
< OString
>::const_iterator i
= (*iter
).second
.begin();
453 i
!= (*iter
).second
.end(); ++i
) {
454 o
<< " if ( aURL.Path.compareTo(\"" << (*i
) << "\") == 0 )\n"
455 " {\n // add your own code here\n"
465 o
<< " public void addStatusListener( com.sun.star.frame.XStatusListener xControl,\n"
466 " com.sun.star.util.URL aURL )\n {\n"
467 " // add your own code here\n }\n\n";
469 // com.sun.star.frame.XDispatch
470 o
<< " public void removeStatusListener( com.sun.star.frame.XStatusListener xControl,\n"
471 " com.sun.star.util.URL aURL )\n {\n"
472 " // add your own code here\n }\n\n";
475 void generateXDispatchProviderBodies(std::ostream
& o
, ProgramOptions
const & options
)
477 // com.sun.star.frame.XDispatchProvider
479 o
<< " // com.sun.star.frame.XDispatchProvider:\n"
480 " public com.sun.star.frame.XDispatch queryDispatch( com.sun.star.util.URL aURL,\n"
481 " String sTargetFrameName,\n"
482 " int iSearchFlags )\n {\n";
484 ProtocolCmdMap::const_iterator iter
= options
.protocolCmdMap
.begin();
485 while (iter
!= options
.protocolCmdMap
.end()) {
486 o
<< " if ( aURL.Protocol.compareTo(\"" << (*iter
).first
487 << "\") == 0 )\n {\n";
489 for (std::vector
< OString
>::const_iterator i
= (*iter
).second
.begin();
490 i
!= (*iter
).second
.end(); ++i
) {
491 o
<< " if ( aURL.Path.compareTo(\"" << (*i
) << "\") == 0 )\n"
498 o
<< " return null;\n }\n\n";
501 o
<< " // com.sun.star.frame.XDispatchProvider:\n"
502 " public com.sun.star.frame.XDispatch[] queryDispatches(\n"
503 " com.sun.star.frame.DispatchDescriptor[] seqDescriptors )\n {\n"
504 " int nCount = seqDescriptors.length;\n"
505 " com.sun.star.frame.XDispatch[] seqDispatcher =\n"
506 " new com.sun.star.frame.XDispatch[seqDescriptors.length];\n\n"
507 " for( int i=0; i < nCount; ++i )\n {\n"
508 " seqDispatcher[i] = queryDispatch(seqDescriptors[i].FeatureURL,\n"
509 " seqDescriptors[i].FrameName,\n"
510 " seqDescriptors[i].SearchFlags );\n"
511 " }\n return seqDispatcher;\n }\n\n";
514 void generateMethodBodies(std::ostream
& o
,
515 ProgramOptions
const & options
,
516 TypeManager
const & manager
,
517 const boost::unordered_set
< OString
, OStringHash
>& interfaces
,
518 const OString
& indentation
, bool usepropertymixin
)
520 boost::unordered_set
< OString
, OStringHash
>::const_iterator iter
=
522 codemaker::GeneratedTypeSet generated
;
523 while (iter
!= interfaces
.end()) {
526 if (type
.equals("com.sun.star.lang.XServiceInfo")) {
527 generateXServiceInfoBodies(o
);
530 if (options
.componenttype
== 2) {
531 if (type
.equals("com.sun.star.lang.XServiceName")) {
532 o
<< " // com.sun.star.lang.XServiceName:\n"
533 " public String getServiceName() {\n"
534 " return sADDIN_SERVICENAME;\n }\n";
537 } else if (type
.equals("com.sun.star.sheet.XAddIn")) {
538 generateXAddInBodies(o
, options
);
541 // special handling of XLocalizable -> parent of XAddIn
542 if (!generated
.contains("com.sun.star.lang.XLocalizable")) {
543 generateXLocalizableBodies(o
);
544 generated
.add("com.sun.star.lang.XLocalizable");
547 } else if (type
.equals("com.sun.star.lang.XLocalizable")) {
548 generateXLocalizableBodies(o
);
551 } else if (type
.equals("com.sun.star.sheet.XCompatibilityNames")) {
552 generateXCompatibilityNamesBodies(o
);
557 if (options
.componenttype
== 3) {
558 if (type
.equals("com.sun.star.lang.XInitialization")) {
559 generateXInitializationBodies(o
);
562 } else if (type
.equals("com.sun.star.frame.XDispatch")) {
563 generateXDispatchBodies(o
, options
);
566 } else if (type
.equals("com.sun.star.frame.XDispatchProvider")) {
567 generateXDispatchProviderBodies(o
, options
);
572 typereg::Reader
reader(manager
.getTypeReader(type
.replace('.','/')));
573 printMethods(o
, options
, manager
, reader
, generated
, "_",
574 indentation
, true, usepropertymixin
);
579 static const char* propcomment
=
580 " // use the last parameter of the PropertySetMixin constructor\n"
581 " // for your optional attributes if necessary. See the documentation\n"
582 " // of the PropertySetMixin helper for further information.\n"
583 " // Ensure that your attributes are initialized correctly!\n";
586 void generateAddinConstructorAndHelper(std::ostream
& o
,
587 ProgramOptions
const & options
,
588 TypeManager
const & manager
, const OString
& classname
,
589 const boost::unordered_set
< OString
, OStringHash
>& services
,
590 const boost::unordered_set
< OString
, OStringHash
>& interfaces
)
592 o
<< " private com.sun.star.lang.Locale m_locale = "
593 "new com.sun.star.lang.Locale();\n";
595 if (!options
.backwardcompatible
) {
597 o
<< "\n public " << classname
<< "( XComponentContext context )\n"
598 " {\n m_xContext = context;\n }\n\n";
603 // get the one and only add-in service for later use
604 boost::unordered_set
< OString
, OStringHash
>::const_iterator iter
= services
.begin();
605 OString sAddinService
= (*iter
).replace('/', '.');
606 if (sAddinService
.equals("com.sun.star.sheet.AddIn")) {
607 sAddinService
= (*(++iter
)).replace('/', '.');
611 // add-in specific fields
612 o
<< "\n private static final String sADDIN_SERVICENAME = \""
613 << sAddinService
<< "\";\n\n";
614 o
<< " private static final String sDISPLAYNAME = "
616 " private static final String sDESCRIPTION = "
618 " private static final String sCATEGORY = \"Category\";\n"
619 " private static final String sCATEGORYDISPLAYNAME = "
620 "\"CategoryDisplayName\";\n\n";
622 o
<< " private com.sun.star.container.XHierarchicalNameAccess "
623 "m_xHAccess = null;\n"
624 " private com.sun.star.container.XHierarchicalNameAccess "
625 "m_xCompAccess = null;\n";
627 o
<< " private java.util.Hashtable<\n String, "
628 "java.util.Hashtable< Integer, String> > m_functionMap = null;\n\n";
630 o
<< " private java.util.Hashtable m_functionMap = null;\n\n";
633 o
<< "\n public " << classname
<< "( XComponentContext context )\n {\n"
634 " m_xContext = context;\n\n"
638 o
<< " m_functionMap = new java.util.Hashtable<\n"
639 " String, java.util.Hashtable< Integer, "
642 o
<< " m_functionMap = new java.util.Hashtable();\n\n";
645 generateFunctionParameterMap(o
, options
, manager
, interfaces
);
647 o
<< " com.sun.star.lang.XMultiServiceFactory xProvider = \n"
648 " (com.sun.star.lang.XMultiServiceFactory)UnoRuntime."
650 " com.sun.star.lang.XMultiServiceFactory.class,\n"
651 " m_xContext.getServiceManager().createInstanceWithContext("
652 "\n \"com.sun.star.configuration.ConfigurationProvider\""
653 ",\n m_xContext));\n\n";
655 o
<< " String sReadOnlyView = "
656 "\"com.sun.star.configuration.ConfigurationAccess\";\n\n";
658 o
<< " StringBuffer sPath = new StringBuffer(\n"
659 " \"/org.openoffice.Office.CalcAddIns/AddInInfo/\");\n"
660 " sPath.append(sADDIN_SERVICENAME);\n"
661 " sPath.append(\"/AddInFunctions\");\n\n";
663 o
<< " // create arguments: nodepath\n"
664 " com.sun.star.beans.PropertyValue aArgument = \n"
665 " new com.sun.star.beans.PropertyValue();\n"
666 " aArgument.Name = \"nodepath\";\n"
667 " aArgument.Value = new com.sun.star.uno.Any(\n"
668 " com.sun.star.uno.Type.STRING, sPath.toString());\n\n";
670 o
<< " Object aArguments[] = new Object[1];\n"
671 " aArguments[0] = new com.sun.star.uno.Any("
672 " new com.sun.star.uno.Type(\n"
673 " com.sun.star.beans.PropertyValue.class), aArgument);\n\n";
675 o
<< " // create the default view using default UI locale\n"
676 " Object xIface = \n"
677 " xProvider.createInstanceWithArguments(sReadOnlyView, "
680 o
<< " m_xHAccess = (com.sun.star.container.XHierarchicalNameAccess)\n"
681 " UnoRuntime.queryInterface(\n"
682 " com.sun.star.container.XHierarchicalNameAccess.class, "
685 o
<< " // extends arguments to create a view for all locales to get "
686 "simple\n // access to the compatibilityname property\n"
687 " aArguments = new Object[2];\n"
688 " aArguments[0] = new com.sun.star.uno.Any( "
689 "new com.sun.star.uno.Type(\n"
690 " com.sun.star.beans.PropertyValue.class), aArgument);\n"
691 " aArgument.Name = \"locale\";\n"
692 " aArgument.Value = new com.sun.star.uno.Any(\n"
693 " com.sun.star.uno.Type.STRING, \"*\");\n"
694 " aArguments[1] = new com.sun.star.uno.Any( "
695 " new com.sun.star.uno.Type(\n"
696 " com.sun.star.beans.PropertyValue.class), aArgument);\n\n";
698 o
<< " // create view for all locales\n"
699 " xIface = xProvider.createInstanceWithArguments(sReadOnlyView, "
701 " m_xCompAccess = (com.sun.star.container.XHierarchicalNameAccess)\n"
702 " UnoRuntime.queryInterface(\n"
703 " com.sun.star.container.XHierarchicalNameAccess.class, "
705 " catch ( com.sun.star.uno.Exception e ) {\n }\n }\n\n";
707 // add-in helper function
708 o
<< " // addin configuration property helper function:\n"
709 " String getAddinProperty(String funcName, "
710 "String paramName, String propName)\n {\n"
711 " try {\n StringBuffer buf = "
712 "new StringBuffer(funcName);\n\n"
713 " if (paramName.length() > 0) {\n"
714 " buf.append(\"/Parameters/\");\n"
715 " buf.append(paramName);\n }\n\n";
717 o
<< " com.sun.star.beans.XPropertySet xPropSet =\n"
718 " (com.sun.star.beans.XPropertySet)UnoRuntime."
720 " com.sun.star.beans.XPropertySet.class,\n"
721 " m_xHAccess.getByHierarchicalName(buf.toString()));\n\n"
722 " return com.sun.star.uno.AnyConverter.toString(\n"
723 " xPropSet.getPropertyValue(propName));\n }\n"
724 " catch ( com.sun.star.uno.RuntimeException e ) {\n"
726 " catch ( com.sun.star.uno.Exception e ) {\n }\n"
727 " return \"\";\n }\n\n";
731 void generateClassDefinition(std::ostream
& o
,
732 ProgramOptions
const & options
,
733 TypeManager
const & manager
,
734 const OString
& classname
,
735 const boost::unordered_set
< OString
, OStringHash
>& services
,
736 const boost::unordered_set
< OString
, OStringHash
>& interfaces
,
737 const AttributeInfo
& properties
,
738 const AttributeInfo
& attributes
,
739 const OString
& propertyhelper
, bool supportxcomponent
)
741 o
<< "\n\npublic final class " << classname
<< " extends ";
743 if (!interfaces
.empty()) {
744 if (propertyhelper
.equals("_")) {
745 o
<< "PropertySet\n";
747 if (supportxcomponent
)
748 o
<< "ComponentBase\n";
753 boost::unordered_set
< OString
, OStringHash
>::const_iterator iter
=
755 while (iter
!= interfaces
.end()) {
758 if (iter
!= interfaces
.end())
764 o
<< " private final XComponentContext m_xContext;\n";
766 // additional member for add-ons
767 if (options
.componenttype
== 3) {
768 o
<< " private com.sun.star.frame.XFrame m_xFrame;\n";
771 // check property helper
772 if (propertyhelper
.getLength() > 1)
773 o
<< " private final PropertySetMixin m_prophlp;\n";
775 o
<< " private static final String m_implementationName = "
776 << classname
<< ".class.getName();\n";
778 if (!services
.empty()) {
779 o
<< " private static final String[] m_serviceNames = {\n";
780 boost::unordered_set
< OString
, OStringHash
>::const_iterator iter
=
782 while (iter
!= services
.end()) {
783 o
<< " \"" << (*iter
).replace('/','.') << "\"";
785 if (iter
!= services
.end())
792 // attribute/property members
793 if (!properties
.empty()) {
794 AttributeInfo::const_iterator iter
=
796 o
<< " // properties\n";
797 while (iter
!= properties
.end()) {
799 printType(o
, options
, manager
, iter
->second
.first
.replace('.','/'),
801 o
<< " m_" << iter
->first
<< ";\n";
804 } else if (!attributes
.empty()) {
805 AttributeInfo::const_iterator iter
=
807 o
<< " // attributes\n";
808 while (iter
!= attributes
.end()) {
810 printType(o
, options
, manager
, iter
->second
.first
.replace('.','/'),
812 o
<< " m_" << iter
->first
<< " = ";
813 printType(o
, options
, manager
, iter
->second
.first
.replace('.','/'),
820 // special handling of calc add-ins
821 if (options
.componenttype
== 2)
823 generateAddinConstructorAndHelper(o
, options
, manager
, classname
,
824 services
, interfaces
);
826 o
<< "\n public " << classname
<< "( XComponentContext context )\n"
827 " {\n m_xContext = context;\n";
828 if (propertyhelper
.equals("_")) {
829 registerProperties(o
, properties
, " ");
831 if (propertyhelper
.getLength() > 1) {
833 << " m_prophlp = new PropertySetMixin(m_xContext, this,\n"
834 << " new Type(" << propertyhelper
835 << ".class), null);\n";
842 if (!services
.empty())
843 generateCompFunctions(o
, classname
);
845 generateMethodBodies(o
, options
, manager
, interfaces
,
846 " ", propertyhelper
.getLength() > 1);
848 // end of class definition
852 void generateSkeleton(ProgramOptions
const & options
,
853 TypeManager
const & manager
,
854 std::vector
< OString
> const & types
)
856 boost::unordered_set
< OString
, OStringHash
> interfaces
;
857 boost::unordered_set
< OString
, OStringHash
> services
;
858 AttributeInfo properties
;
859 AttributeInfo attributes
;
860 boost::unordered_set
< OString
, OStringHash
> propinterfaces
;
861 bool serviceobject
= false;
862 bool supportxcomponent
= false;
864 std::vector
< OString
>::const_iterator iter
= types
.begin();
865 while (iter
!= types
.end()) {
866 checkType(manager
, *iter
, interfaces
, services
, properties
);
870 if (options
.componenttype
== 3) {
871 // the Protocolhandler service is mandatory for an protocol handler add-on,
872 // so it is defaulted. The XDispatchProvider provides Dispatch objects for
873 // certain functions and the generated impl object implements XDispatch
874 // directly for simplicity reasons.
875 checkType(manager
, "com.sun.star.frame.ProtocolHandler",
876 interfaces
, services
, properties
);
877 checkType(manager
, "com.sun.star.frame.XDispatch",
878 interfaces
, services
, properties
);
881 if (options
.componenttype
== 2) {
882 if (services
.size() != 1) {
883 throw CannotDumpException(
884 "for calc add-in components one and only one service type is "
885 "necessary! Please reference a valid type with the '-t' option.");
888 // if backwardcompatible==true the AddIn service needs to be added to the
889 // suported service list, the necessary intefaces are mapped to the add-in
890 // configuration. Since OO.org 2.0.4 this is obsolete and the add-in is
891 // take form the configuration from Calc directly, this simplifies the
893 if (options
.backwardcompatible
) {
894 checkType(manager
, "com.sun.star.sheet.AddIn",
895 interfaces
, services
, properties
);
897 // special case for the optional XLocalization interface. It should be
898 // implemented always. But it is parent of the XAddIn and we need it only
899 // if backwardcompatible is false.
900 if (interfaces
.find("com.sun.star.lang.XLocalizable") == interfaces
.end()) {
901 interfaces
.insert("com.sun.star.lang.XLocalizable");
907 // check if service object or simple UNO object
908 if (!services
.empty())
909 serviceobject
= true;
911 OString propertyhelper
= checkPropertyHelper(
912 options
, manager
, services
, interfaces
, attributes
, propinterfaces
);
913 checkDefaultInterfaces(interfaces
, services
, propertyhelper
);
915 if (options
.componenttype
== 2) {
916 if (!propertyhelper
.isEmpty())
917 std::cerr
<< "WARNING: interfaces specifying calc add-in functions "
918 "shouldn't support attributes!\n";
921 supportxcomponent
= checkXComponentSupport(manager
, interfaces
);
923 OString compFileName
;
925 std::ostream
* pofs
= NULL
;
926 bool standardout
= getOutputStream(options
, ".java",
927 &pofs
, compFileName
, tmpFileName
);
930 if (!standardout
&& options
.license
) {
931 printLicenseHeader(*pofs
, compFileName
);
934 generatePackage(*pofs
, options
.implname
);
936 generateImports(*pofs
, options
, propertyhelper
,
937 serviceobject
, supportxcomponent
);
939 OString
classname(options
.implname
);
941 if ((index
= classname
.lastIndexOf('.')) > 0)
942 classname
= classname
.copy(index
+1);
944 generateClassDefinition(*pofs
, options
, manager
, classname
, services
,
945 interfaces
, properties
, attributes
, propertyhelper
,
948 if ( !standardout
&& pofs
&& ((std::ofstream
*)pofs
)->is_open()) {
949 ((std::ofstream
*)pofs
)->close();
951 OSL_VERIFY(makeValidTypeFile(compFileName
, tmpFileName
, sal_False
));
953 } catch(const CannotDumpException
& e
) {
955 std::cerr
<< "ERROR: " << e
.m_message
.getStr() << "\n";
956 if ( !standardout
) {
957 if (pofs
&& ((std::ofstream
*)pofs
)->is_open()) {
958 ((std::ofstream
*)pofs
)->close();
961 // remove existing type file if something goes wrong to ensure
963 if (fileExists(compFileName
))
964 removeTypeFile(compFileName
);
966 // remove tmp file if something goes wrong
967 removeTypeFile(tmpFileName
);
975 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */