1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #include "codemaker/commonjava.hxx"
29 
30 #include "skeletoncommon.hxx"
31 #include "skeletonjava.hxx"
32 
33 #include <iostream>
34 
35 using namespace ::rtl;
36 using namespace ::codemaker::java;
37 
38 namespace skeletonmaker { namespace java {
39 
40 void generatePackage(std::ostream & o, const OString & implname)
41 {
42     sal_Int32 index = implname.lastIndexOf('.');
43     if (index != -1)
44         o << "package " << implname.copy(0, index) << ";\n\n";
45 }
46 
47 void generateImports(std::ostream & o, ProgramOptions const & options,
48          const std::hash_set< OString, OStringHash >& /*interfaces*/,
49          const OString & propertyhelper,
50          bool serviceobject, bool supportxcomponent)
51 {
52     if (options.componenttype == 3)
53         o << "import com.sun.star.uno.UnoRuntime;\n";
54     o << "import com.sun.star.uno.XComponentContext;\n";
55     if (serviceobject) {
56         o << "import com.sun.star.lib.uno.helper.Factory;\n";
57         o << "import com.sun.star.lang.XSingleComponentFactory;\n";
58         o << "import com.sun.star.registry.XRegistryKey;\n";
59     }
60 
61     if  (!propertyhelper.equals("_")) {
62         if (supportxcomponent)
63             o << "import com.sun.star.lib.uno.helper.ComponentBase;\n";
64         else
65             o << "import com.sun.star.lib.uno.helper.WeakBase;\n";
66     }
67     if (propertyhelper.getLength() > 0) {
68         if (propertyhelper.equals("_")) {
69             o << "import com.sun.star.lib.uno.helper.PropertySet;\n";
70             o << "import com.sun.star.beans.PropertyAttribute;\n";
71         } else {
72             o << "import com.sun.star.uno.Type;\n";
73             o << "import com.sun.star.uno.Any;\n";
74             o << "import com.sun.star.beans.Ambiguous;\n";
75             o << "import com.sun.star.beans.Defaulted;\n";
76             o << "import com.sun.star.beans.Optional;\n";
77             o << "import com.sun.star.lib.uno.helper.PropertySetMixin;\n";
78         }
79     }
80 
81 
82 //     std::hash_set< OString, OStringHash >::const_iterator iter =
83 //                    interfaces.begin();
84 //     while (iter != interfaces.end())
85 //     {
86 //         o << "import " << ((*iter).getStr()) << ";\n";
87 //         iter++;
88 //     }
89 }
90 
91 void generateCompFunctions(std::ostream & o, const OString & classname)
92 {
93     o << "    public static XSingleComponentFactory __getComponentFactory("
94         " String sImplementationName ) {\n"
95         "        XSingleComponentFactory xFactory = null;\n\n"
96         "        if ( sImplementationName.equals( m_implementationName ) )\n"
97         "            xFactory = Factory.createComponentFactory("
98       << classname << ".class, m_serviceNames);\n"
99         "        return xFactory;\n    }\n\n";
100 
101     o << "    public static boolean __writeRegistryServiceInfo("
102         " XRegistryKey xRegistryKey ) {\n"
103         "        return Factory.writeRegistryServiceInfo(m_implementationName,\n"
104         "                                                m_serviceNames,\n"
105         "                                                xRegistryKey);\n"
106         "    }\n\n";
107 }
108 
109 void generateXServiceInfoBodies(std::ostream& o)
110 {
111     o << "    // com.sun.star.lang.XServiceInfo:\n";
112     o << "    public String getImplementationName() {\n"
113       << "         return m_implementationName;\n    }\n\n";
114 
115     o << "    public boolean supportsService( String sService ) {\n"
116       << "        int len = m_serviceNames.length;\n\n"
117       << "        for( int i=0; i < len; i++) {\n"
118       << "            if (sService.equals(m_serviceNames[i]))\n"
119       << "                return true;\n"
120       << "        }\n        return false;\n    }\n\n";
121 
122     o << "    public String[] getSupportedServiceNames() {\n"
123       << "        return m_serviceNames;\n    }\n\n";
124 }
125 
126 void generateXPropertySetBodies(std::ostream& o)
127 {
128     o << "    // com.sun.star.beans.XPropertySet:\n";
129     o << "    public com.sun.star.beans.XPropertySetInfo getPropertySetInfo()\n"
130       "    {\n        return m_prophlp.getPropertySetInfo();\n    }\n\n";
131 
132     o << "    public void setPropertyValue(String aPropertyName, "
133         "Object aValue) throws "
134         "com.sun.star.beans.UnknownPropertyException, "
135         "com.sun.star.beans.PropertyVetoException, "
136         "com.sun.star.lang.IllegalArgumentException,"
137         "com.sun.star.lang.WrappedTargetException\n    {\n        "
138         "m_prophlp.setPropertyValue(aPropertyName, aValue);\n    }\n\n";
139 
140     o << "    public Object getPropertyValue(String "
141         "aPropertyName) throws com.sun.star.beans.UnknownPropertyException, "
142         "com.sun.star.lang.WrappedTargetException\n    {\n        return "
143         "m_prophlp.getPropertyValue(aPropertyName);\n    }\n\n";
144 
145     o << "    public void addPropertyChangeListener(String aPropertyName"
146         ", com.sun.star.beans.XPropertyChangeListener xListener) throws "
147         "com.sun.star.beans.UnknownPropertyException, "
148         "com.sun.star.lang.WrappedTargetException\n    {\n        "
149         "m_prophlp.addPropertyChangeListener(aPropertyName, xListener);\n    }\n\n";
150 
151     o << "    public void removePropertyChangeListener(String "
152         "aPropertyName, com.sun.star.beans.XPropertyChangeListener xListener) "
153         "throws com.sun.star.beans.UnknownPropertyException, "
154         "com.sun.star.lang.WrappedTargetException\n    {\n        "
155         "m_prophlp.removePropertyChangeListener(aPropertyName, xListener);\n"
156         "    }\n\n";
157 
158     o << "    public void addVetoableChangeListener(String aPropertyName"
159         ", com.sun.star.beans.XVetoableChangeListener xListener) throws "
160         "com.sun.star.beans.UnknownPropertyException, "
161         "com.sun.star.lang.WrappedTargetException\n    {\n        "
162         "m_prophlp.addVetoableChangeListener(aPropertyName, xListener);\n    }\n\n";
163 
164     o << "    public void removeVetoableChangeListener(String "
165         "aPropertyName, com.sun.star.beans.XVetoableChangeListener xListener) "
166         "throws com.sun.star.beans.UnknownPropertyException, "
167         "com.sun.star.lang.WrappedTargetException\n    {\n        "
168         "m_prophlp.removeVetoableChangeListener(aPropertyName, xListener);\n }\n\n";
169 }
170 
171 void generateXFastPropertySetBodies(std::ostream& o)
172 {
173     o << "    // com.sun.star.beans.XFastPropertySet:\n";
174 
175     o << "    public void setFastPropertyValue(int nHandle, Object "
176         "aValue) throws com.sun.star.beans.UnknownPropertyException, "
177         "com.sun.star.beans.PropertyVetoException, "
178         "com.sun.star.lang.IllegalArgumentException, "
179         "com.sun.star.lang.WrappedTargetException\n    {\n        "
180         "m_prophlp.setFastPropertyValue(nHandle, aValue);\n    }\n\n";
181 
182     o << "    public Object getFastPropertyValue(int nHandle) throws "
183         "com.sun.star.beans.UnknownPropertyException, "
184         "com.sun.star.lang.WrappedTargetException\n    {\n        return "
185         "m_prophlp.getFastPropertyValue(nHandle);\n    }\n\n";
186 }
187 
188 void generateXPropertyAccessBodies(std::ostream& o)
189 {
190     o << "    // com.sun.star.beans.XPropertyAccess:\n";
191 
192     o << "    public com.sun.star.beans.PropertyValue[] getPropertyValues()\n"
193         " {\n        return m_prophlp.getPropertyValues();\n    }\n\n";
194 
195     o << "    public void setPropertyValues(com.sun.star.beans.PropertyValue[] "
196         "aProps) throws com.sun.star.beans.UnknownPropertyException, "
197         "com.sun.star.beans.PropertyVetoException, "
198         "com.sun.star.lang.IllegalArgumentException, "
199         "com.sun.star.lang.WrappedTargetException\n    {\n        "
200         "m_prophlp.setPropertyValues(aProps);\n    }\n\n";
201 }
202 
203 
204 bool checkAttribute(OStringBuffer& attributeValue, sal_uInt16 attribute)
205 {
206     bool cast = false;
207     sal_uInt16 attributes[9] = {
208         /* com::sun::star::beans::PropertyValue::MAYBEVOID */ 1,
209         /* com::sun::star::beans::PropertyValue::BOUND */ 2,
210         /* com::sun::star::beans::PropertyValue::CONSTRAINED */ 4,
211         /* com::sun::star::beans::PropertyValue::TRANSIENT */ 8,
212         /* com::sun::star::beans::PropertyValue::READONLY */ 16,
213         /* com::sun::star::beans::PropertyValue::MAYBEAMBIGIOUS */ 32,
214         /* com::sun::star::beans::PropertyValue::MAYBEDEFAULT */ 64,
215         /* com::sun::star::beans::PropertyValue::REMOVEABLE */ 128,
216         /* com::sun::star::beans::PropertyValue::OPTIONAL */ 256 };
217 
218     for (sal_uInt16 i = 0; i < 9; i++)
219     {
220         if (attribute & attributes[i]) {
221             if (attributeValue.getLength() > 0) {
222                 cast |= true;
223                 attributeValue.append("|");
224             }
225             switch (attributes[i])
226             {
227             case 1:
228                 attributeValue.append("PropertyAttribute.MAYBEVOID");
229                 break;
230             case 2:
231                 attributeValue.append("PropertyAttribute.BOUND");
232                 break;
233             case 4:
234                 attributeValue.append("PropertyAttribute.CONSTRAINED");
235                 break;
236             case 8:
237                 attributeValue.append("PropertyAttribute.TRANSIENT");
238                 break;
239             case 16:
240                 attributeValue.append("PropertyAttribute.READONLY");
241                 break;
242             case 32:
243                 attributeValue.append("PropertyAttribute.MAYBEAMBIGIOUS");
244                 break;
245             case 64:
246                 attributeValue.append("PropertyAttribute.MAYBEDEFAULT");
247                 break;
248             case 128:
249                 attributeValue.append("PropertyAttribute.REMOVEABLE");
250                 break;
251             case 256:
252                 attributeValue.append("PropertyAttribute.OPTIONAL");
253                 break;
254             }
255         }
256     }
257     if (cast) {
258         attributeValue.insert(0, '(');
259         attributeValue.append(')');
260     }
261 
262     return cast;
263 }
264 
265 void registerProperties(std::ostream& o,
266                         TypeManager const & /*manager*/,
267                         const AttributeInfo& properties,
268                         const OString& indentation)
269 {
270     if (!properties.empty()) {
271         bool cast = false;
272         OStringBuffer attributeValue;
273         for (AttributeInfo::const_iterator i(properties.begin());
274              i != properties.end(); ++i)
275         {
276             if (i->second.second > 0) {
277                 cast = checkAttribute(attributeValue, i->second.second);
278             } else {
279                 cast = true;
280                 attributeValue.append('0');
281             }
282 
283             o << indentation << "registerProperty(\"" << i->first
284               << "\", \"m_" << i->first << "\",\n"
285               << indentation << "      ";
286             if (cast)
287                 o << "(short)";
288 
289             o << attributeValue.makeStringAndClear() << ");\n";
290         }
291     }
292 }
293 
294 void generateXLocalizableBodies(std::ostream& o) {
295     // com.sun.star.lang.XLocalizable:
296     // setLocale
297     o << "    // com.sun.star.lang.XLocalizable:\n"
298         "    public void setLocale(com.sun.star.lang.Locale eLocale)\n    {\n"
299         "        m_locale = eLocale;\n    }\n\n";
300 
301     // getLocale
302     o << "    public com.sun.star.lang.Locale getLocale()\n    {\n"
303         "        return m_locale;\n    }\n\n";
304 }
305 
306 void generateXAddInBodies(std::ostream& o, ProgramOptions const & options)
307 {
308     // com.sun.star.sheet.XAddIn:
309     // getProgrammaticFuntionName
310     o << "    // com.sun.star.sheet.XAddIn:\n"
311         "    public String getProgrammaticFuntionName(String "
312         "aDisplayName)\n    {\n"
313         "        try {\n"
314         "            com.sun.star.container.XNameAccess xNAccess =\n"
315         "                (com.sun.star.container.XNameAccess)UnoRuntime."
316         "queryInterface(\n"
317         "                    com.sun.star.container.XNameAccess.class, m_xHAccess);"
318         "\n            String functions[] = xNAccess.getElementNames();\n"
319         "            String sDisplayName = \"\";\n"
320         "            int len = functions.length;\n"
321         "            for (int i=0; i < len; ++i) {\n"
322         "                sDisplayName = com.sun.star.uno.AnyConverter.toString(\n"
323         "                    getAddinProperty(functions[i], \"\", sDISPLAYNAME));\n"
324         "                if (sDisplayName.equals(aDisplayName))\n"
325         "                    return functions[i];\n            }\n"
326         "        }\n        catch ( com.sun.star.uno.RuntimeException e ) {\n"
327         "            throw e;\n        }\n"
328         "        catch ( com.sun.star.uno.Exception e ) {\n        }\n\n"
329         "        return \"\";\n    }\n\n";
330 
331     // getDisplayFunctionName
332     o << "    public String getDisplayFunctionName(String "
333         "aProgrammaticName)\n    {\n"
334         "        return getAddinProperty(aProgrammaticName, \"\", sDISPLAYNAME);\n"
335         "    }\n\n";
336 
337     // getFunctionDescription
338     o << "    public String getFunctionDescription(String "
339         "aProgrammaticName)\n    {\n"
340         "        return getAddinProperty(aProgrammaticName, \"\", sDESCRIPTION);\n"
341         "    }\n\n";
342 
343     // getDisplayArgumentName
344     o << "    public String getDisplayArgumentName(String "
345         "aProgrammaticFunctionName, int nArgument)\n    {\n";
346     if (options.java5) {
347         o << "        return getAddinProperty(aProgrammaticFunctionName,\n"
348             "                                m_functionMap.get(\n"
349             "                                    aProgrammaticFunctionName).get("
350             "nArgument),\n"
351             "                                sDISPLAYNAME);\n    }\n\n";
352     } else {
353         o << "        return getAddinProperty(aProgrammaticFunctionName, (String)\n"
354             "                                ((java.util.Hashtable)m_functionMap."
355             "get(\n                                    aProgrammaticFunctionName))."
356             "get(\n                                        new Integer(nArgument))"
357             ", sDISPLAYNAME);\n    }\n\n";
358     }
359 
360     // getArgumentDescription
361     o << "    public String getArgumentDescription(String "
362         "aProgrammaticFunctionName, int nArgument)\n    {\n";
363     if (options.java5) {
364         o << "        return getAddinProperty(aProgrammaticFunctionName,\n"
365             "                                m_functionMap.get(\n"
366             "                                    aProgrammaticFunctionName).get("
367             "nArgument),\n"
368             "                                sDESCRIPTION);\n    }\n\n";
369     } else {
370         o << "        return getAddinProperty(aProgrammaticFunctionName, (String)\n"
371             "                                ((java.util.Hashtable)m_functionMap."
372             "get(\n                                    aProgrammaticFunctionName))."
373             "get(\n                                        new Integer(nArgument))"
374             ", sDESCRIPTION);\n    }\n\n";
375     }
376     // getProgrammaticCategoryName
377     o << "    public String getProgrammaticCategoryName(String "
378         "aProgrammaticFunctionName)\n    {\n"
379         "        return getAddinProperty(aProgrammaticFunctionName, \"\", "
380         "sCATEGORY);\n    }\n\n";
381 
382     // getDisplayCategoryName
383     o << "    public String getDisplayCategoryName(String "
384         "aProgrammaticFunctionName)\n    {\n"
385         "        return getAddinProperty(aProgrammaticFunctionName, \"\", "
386         "sCATEGORYDISPLAYNAME);\n    }\n\n";
387 }
388 
389 void generateXCompatibilityNamesBodies(std::ostream& o)
390 {
391     o << "    // com.sun.star.sheet.XCompatibilityNames:\n"
392         "    public com.sun.star.sheet.LocalizedName[] getCompatibilityNames("
393         "String aProgrammaticName)\n    {\n"
394         "        com.sun.star.sheet.LocalizedName[] seqLocalizedNames =\n"
395         "            new com.sun.star.sheet.LocalizedName[0];\n\n        try {\n";
396 
397     o << "            StringBuffer path = new StringBuffer(aProgrammaticName);\n"
398         "            path.append(\"/CompatibilityName\");\n"
399         "            String hname = path.toString();\n\n";
400 
401     o << "            if ( m_xCompAccess.hasByHierarchicalName(hname) ) {\n"
402         "                com.sun.star.container.XNameAccess xNameAccess =\n"
403         "                    (com.sun.star.container.XNameAccess)UnoRuntime."
404         "queryInterface(\n"
405         "                        com.sun.star.container.XNameAccess.class,\n"
406         "                        m_xCompAccess.getByHierarchicalName(hname));\n\n"
407         "                String elems[] = xNameAccess.getElementNames();\n"
408         "                int len = elems.length;\n"
409         "                seqLocalizedNames = new com.sun.star.sheet.LocalizedName"
410         "[len];\n                String sCompatibilityName = \"\";\n\n";
411 
412     o << "                for (int i=0; i < len; ++i) {\n"
413         "                    String sLocale = elems[i];\n"
414         "                    sCompatibilityName = com.sun.star.uno.AnyConverter."
415         "toString(\n                        xNameAccess.getByName(sLocale));\n\n"
416         "                    com.sun.star.lang.Locale aLocale = \n"
417         "                        new com.sun.star.lang.Locale();\n\n"
418         "                    String tokens[] = sLocale.split(\"-\");\n"
419         "                    int nToken = tokens.length;\n"
420         "                    if (nToken >= 1) aLocale.Language = tokens[0];\n"
421         "                    if (nToken >= 2) aLocale.Country = tokens[1];\n"
422         "                    if (nToken >= 3)  {\n"
423         "                        StringBuffer buf = \n"
424         "                            new StringBuffer(tokens[2]);\n"
425         "                        for (int t=3; t < nToken; ++t)\n"
426         "                            buf.append(tokens[t]);\n\n"
427         "                        aLocale.Variant = buf.toString();\n"
428         "                    }\n\n"
429         "                    seqLocalizedNames[i].Locale = aLocale;\n"
430         "                    seqLocalizedNames[i].Name = sCompatibilityName;\n"
431         "                }\n        }\n        }\n"
432         "        catch ( com.sun.star.uno.RuntimeException e ) {\n"
433         "            throw e;\n        }\n"
434         "        catch ( com.sun.star.uno.Exception e ) {\n        }\n\n"
435         "        return seqLocalizedNames;\n    }\n\n";
436 }
437 
438 void generateXInitializationBodies(std::ostream& o)
439 {
440     o << "    // com.sun.star.lang.XInitialization:\n"
441         "    public void initialize( Object[] object )\n"
442         "        throws com.sun.star.uno.Exception\n    {\n"
443         "        if ( object.length > 0 )\n        {\n"
444         "            m_xFrame = (com.sun.star.frame.XFrame)UnoRuntime.queryInterface(\n"
445         "                com.sun.star.frame.XFrame.class, object[0]);\n        }\n    }\n\n";
446 }
447 
448 void generateXDispatchBodies(std::ostream& o, ProgramOptions const & options)
449 {
450     // com.sun.star.frame.XDispatch
451     // dispatch
452     o << "    // com.sun.star.frame.XDispatch:\n"
453         "     public void dispatch( com.sun.star.util.URL aURL,\n"
454         "                           com.sun.star.beans.PropertyValue[] aArguments )\n    {\n";
455 
456     ProtocolCmdMap::const_iterator iter = options.protocolCmdMap.begin();
457     while (iter != options.protocolCmdMap.end()) {
458         o << "         if ( aURL.Protocol.compareTo(\"" << (*iter).first
459           << "\") == 0 )\n        {\n";
460 
461         for (std::vector< OString >::const_iterator i = (*iter).second.begin();
462              i != (*iter).second.end(); ++i) {
463             o << "            if ( aURL.Path.compareTo(\"" << (*i) << "\") == 0 )\n"
464                 "            {\n                // add your own code here\n"
465                 "                return;\n            }\n";
466         }
467 
468         o << "        }\n";
469         iter++;
470     }
471     o << "    }\n\n";
472 
473     // addStatusListener
474     o << "    public void addStatusListener( com.sun.star.frame.XStatusListener xControl,\n"
475         "                                    com.sun.star.util.URL aURL )\n    {\n"
476         "        // add your own code here\n    }\n\n";
477 
478     // com.sun.star.frame.XDispatch
479     o << "    public void removeStatusListener( com.sun.star.frame.XStatusListener xControl,\n"
480         "                                       com.sun.star.util.URL aURL )\n    {\n"
481         "        // add your own code here\n    }\n\n";
482 }
483 
484 void generateXDispatchProviderBodies(std::ostream& o, ProgramOptions const & options)
485 {
486     // com.sun.star.frame.XDispatchProvider
487     // queryDispatch
488     o << "    // com.sun.star.frame.XDispatchProvider:\n"
489         "    public com.sun.star.frame.XDispatch queryDispatch( com.sun.star.util.URL aURL,\n"
490         "                                                       String sTargetFrameName,\n"
491         "                                                       int iSearchFlags )\n    {\n";
492 
493     ProtocolCmdMap::const_iterator iter = options.protocolCmdMap.begin();
494     while (iter != options.protocolCmdMap.end()) {
495         o << "        if ( aURL.Protocol.compareTo(\"" << (*iter).first
496           << "\") == 0 )\n        {\n";
497 
498         for (std::vector< OString >::const_iterator i = (*iter).second.begin();
499              i != (*iter).second.end(); ++i) {
500             o << "            if ( aURL.Path.compareTo(\"" << (*i) << "\") == 0 )\n"
501                 "                return this;\n";
502         }
503 
504         o << "        }\n";
505         iter++;
506     }
507     o << "        return null;\n    }\n\n";
508 
509     // queryDispatches
510     o << "    // com.sun.star.frame.XDispatchProvider:\n"
511         "    public com.sun.star.frame.XDispatch[] queryDispatches(\n"
512         "         com.sun.star.frame.DispatchDescriptor[] seqDescriptors )\n    {\n"
513         "        int nCount = seqDescriptors.length;\n"
514         "        com.sun.star.frame.XDispatch[] seqDispatcher =\n"
515         "            new com.sun.star.frame.XDispatch[seqDescriptors.length];\n\n"
516         "        for( int i=0; i < nCount; ++i )\n        {\n"
517         "            seqDispatcher[i] = queryDispatch(seqDescriptors[i].FeatureURL,\n"
518         "                                             seqDescriptors[i].FrameName,\n"
519         "                                             seqDescriptors[i].SearchFlags );\n"
520         "        }\n        return seqDispatcher;\n    }\n\n";
521 }
522 
523 void generateMethodBodies(std::ostream& o,
524          ProgramOptions const & options,
525          TypeManager const & manager,
526          const std::hash_set< OString, OStringHash >& interfaces,
527          const OString& indentation, bool usepropertymixin)
528 {
529     std::hash_set< OString, OStringHash >::const_iterator iter =
530         interfaces.begin();
531     codemaker::GeneratedTypeSet generated;
532     while (iter != interfaces.end()) {
533         OString type(*iter);
534         iter++;
535         if (type.equals("com.sun.star.lang.XServiceInfo")) {
536             generateXServiceInfoBodies(o);
537             generated.add(type);
538         } else {
539             if (options.componenttype == 2) {
540                 if (type.equals("com.sun.star.lang.XServiceName")) {
541                     o << "    // com.sun.star.lang.XServiceName:\n"
542                         "    public String getServiceName() {\n"
543                         "        return sADDIN_SERVICENAME;\n    }\n";
544                     generated.add(type);
545                     continue;
546                 } else if (type.equals("com.sun.star.sheet.XAddIn")) {
547                     generateXAddInBodies(o, options);
548                     generated.add(type);
549 
550                     // special handling of XLocalizable -> parent of XAddIn
551                     if (!generated.contains("com.sun.star.lang.XLocalizable")) {
552                         generateXLocalizableBodies(o);
553                         generated.add("com.sun.star.lang.XLocalizable");
554                     }
555                     continue;
556                 } else if (type.equals("com.sun.star.lang.XLocalizable")) {
557                     generateXLocalizableBodies(o);
558                     generated.add(type);
559                     continue;
560                 } else if (type.equals("com.sun.star.sheet.XCompatibilityNames")) {
561                     generateXCompatibilityNamesBodies(o);
562                     generated.add(type);
563                     continue;
564                 }
565             }
566             if (options.componenttype == 3) {
567                 if (type.equals("com.sun.star.lang.XInitialization")) {
568                     generateXInitializationBodies(o);
569                     generated.add(type);
570                     continue;
571                 } else if (type.equals("com.sun.star.frame.XDispatch")) {
572                     generateXDispatchBodies(o, options);
573                     generated.add(type);
574                     continue;
575                 } else if (type.equals("com.sun.star.frame.XDispatchProvider")) {
576                     generateXDispatchProviderBodies(o, options);
577                     generated.add(type);
578                     continue;
579                 }
580             }
581             typereg::Reader reader(manager.getTypeReader(type.replace('.','/')));
582             printMethods(o, options, manager, reader, generated, "_",
583                          indentation, true, usepropertymixin);
584         }
585     }
586 }
587 
588 static const char* propcomment=
589 "        // use the last parameter of the PropertySetMixin constructor\n"
590 "        // for your optional attributes if necessary. See the documentation\n"
591 "        // of the PropertySetMixin helper for further information.\n"
592 "        // Ensure that your attributes are initialized correctly!\n";
593 
594 
595 void generateAddinConstructorAndHelper(std::ostream& o,
596          ProgramOptions const & options,
597          TypeManager const & manager, const OString & classname,
598          const std::hash_set< OString, OStringHash >& services,
599          const std::hash_set< OString, OStringHash >& interfaces)
600 {
601     o << "    private com.sun.star.lang.Locale m_locale = "
602         "new com.sun.star.lang.Locale();\n";
603 
604     if (!options.backwardcompatible) {
605         // Constructor
606         o << "\n    public " << classname << "( XComponentContext context )\n"
607             "    {\n        m_xContext = context;\n    }\n\n";
608         return;
609     }
610 
611 
612     // get the one and only add-in service for later use
613     std::hash_set< OString, OStringHash >::const_iterator iter = services.begin();
614     OString sAddinService = (*iter).replace('/', '.');
615     if (sAddinService.equals("com.sun.star.sheet.AddIn")) {
616         sAddinService = (*(++iter)).replace('/', '.');
617     }
618 
619 
620     // add-in specific fields
621     o << "\n    private static final String sADDIN_SERVICENAME = \""
622       << sAddinService << "\";\n\n";
623     o << "    private static final String sDISPLAYNAME = "
624         "\"DisplayName\";\n"
625         "    private static final String sDESCRIPTION = "
626         "\"Description\";\n"
627         "    private static final String sCATEGORY = \"Category\";\n"
628         "    private static final String sCATEGORYDISPLAYNAME = "
629         "\"CategoryDisplayName\";\n\n";
630 
631     o << "    private com.sun.star.container.XHierarchicalNameAccess  "
632         "m_xHAccess = null;\n"
633         "    private com.sun.star.container.XHierarchicalNameAccess  "
634         "m_xCompAccess = null;\n";
635     if (options.java5) {
636         o << "    private java.util.Hashtable<\n        String, "
637             "java.util.Hashtable< Integer, String> > m_functionMap = null;\n\n";
638     } else {
639         o << "    private java.util.Hashtable m_functionMap = null;\n\n";
640     }
641     // Constructor
642     o << "\n    public " << classname << "( XComponentContext context )\n    {\n"
643         "        m_xContext = context;\n\n"
644         "        try {\n";
645 
646     if (options.java5) {
647         o << "        m_functionMap = new java.util.Hashtable<\n"
648             "            String, java.util.Hashtable< Integer, "
649             "String > >();\n\n";
650     } else {
651         o << "        m_functionMap = new java.util.Hashtable();\n\n";
652     }
653 
654     generateFunctionParameterMap(o, options,  manager, interfaces);
655 
656     o << "        com.sun.star.lang.XMultiServiceFactory xProvider = \n"
657         "            (com.sun.star.lang.XMultiServiceFactory)UnoRuntime."
658         "queryInterface(\n"
659         "                com.sun.star.lang.XMultiServiceFactory.class,\n"
660         "                m_xContext.getServiceManager().createInstanceWithContext("
661         "\n                    \"com.sun.star.configuration.ConfigurationProvider\""
662         ",\n                    m_xContext));\n\n";
663 
664     o << "        String sReadOnlyView = "
665         "\"com.sun.star.configuration.ConfigurationAccess\";\n\n";
666 
667     o << "        StringBuffer sPath = new StringBuffer(\n"
668         "             \"/org.openoffice.Office.CalcAddIns/AddInInfo/\");\n"
669         "        sPath.append(sADDIN_SERVICENAME);\n"
670         "        sPath.append(\"/AddInFunctions\");\n\n";
671 
672     o << "        // create arguments: nodepath\n"
673         "         com.sun.star.beans.PropertyValue aArgument = \n"
674         "             new com.sun.star.beans.PropertyValue();\n"
675         "         aArgument.Name = \"nodepath\";\n"
676         "         aArgument.Value = new com.sun.star.uno.Any(\n"
677         "             com.sun.star.uno.Type.STRING, sPath.toString());\n\n";
678 
679     o << "        Object aArguments[] = new Object[1];\n"
680         "        aArguments[0] = new com.sun.star.uno.Any("
681         " new com.sun.star.uno.Type(\n"
682         "            com.sun.star.beans.PropertyValue.class), aArgument);\n\n";
683 
684     o << "        // create the default view using default UI locale\n"
685         "         Object xIface = \n"
686         "             xProvider.createInstanceWithArguments(sReadOnlyView, "
687         "aArguments);\n\n";
688 
689     o << "        m_xHAccess = (com.sun.star.container.XHierarchicalNameAccess)\n"
690         "             UnoRuntime.queryInterface(\n"
691         "                 com.sun.star.container.XHierarchicalNameAccess.class, "
692         "xIface);\n\n";
693 
694     o << "        // extends arguments to create a view for all locales to get "
695         "simple\n        // access to the compatibilityname property\n"
696         "        aArguments = new Object[2];\n"
697         "        aArguments[0] = new com.sun.star.uno.Any( "
698         "new com.sun.star.uno.Type(\n"
699         "            com.sun.star.beans.PropertyValue.class), aArgument);\n"
700         "        aArgument.Name = \"locale\";\n"
701         "        aArgument.Value = new com.sun.star.uno.Any(\n"
702         "            com.sun.star.uno.Type.STRING, \"*\");\n"
703         "        aArguments[1] = new com.sun.star.uno.Any( "
704         " new com.sun.star.uno.Type(\n"
705         "            com.sun.star.beans.PropertyValue.class), aArgument);\n\n";
706 
707     o << "        // create view for all locales\n"
708         "        xIface = xProvider.createInstanceWithArguments(sReadOnlyView, "
709         "aArguments);\n\n"
710         "        m_xCompAccess = (com.sun.star.container.XHierarchicalNameAccess)\n"
711         "            UnoRuntime.queryInterface(\n"
712         "                com.sun.star.container.XHierarchicalNameAccess.class, "
713         "xIface);\n        }\n"
714         "        catch ( com.sun.star.uno.Exception e ) {\n        }\n    }\n\n";
715 
716     // add-in helper function
717     o << "    // addin configuration property helper function:\n"
718         "    String getAddinProperty(String funcName, "
719         "String paramName, String propName)\n    {\n"
720         "        try {\n            StringBuffer buf = "
721         "new StringBuffer(funcName);\n\n"
722         "            if (paramName.length() > 0) {\n"
723         "                buf.append(\"/Parameters/\");\n"
724         "                buf.append(paramName);\n            }\n\n";
725 
726     o << "            com.sun.star.beans.XPropertySet xPropSet =\n"
727         "                (com.sun.star.beans.XPropertySet)UnoRuntime."
728         "queryInterface(\n"
729         "                    com.sun.star.beans.XPropertySet.class,\n"
730         "                    m_xHAccess.getByHierarchicalName(buf.toString()));\n\n"
731         "            return com.sun.star.uno.AnyConverter.toString(\n"
732         "                xPropSet.getPropertyValue(propName));\n        }\n"
733         "        catch ( com.sun.star.uno.RuntimeException e ) {\n"
734         "            throw e;\n        }\n"
735         "        catch ( com.sun.star.uno.Exception e ) {\n        }\n"
736         "        return \"\";\n    }\n\n";
737 }
738 
739 
740 void generateClassDefinition(std::ostream& o,
741          ProgramOptions const & options,
742          TypeManager const & manager,
743          const OString & classname,
744          const std::hash_set< OString, OStringHash >& services,
745          const std::hash_set< OString, OStringHash >& interfaces,
746          const AttributeInfo& properties,
747          const AttributeInfo& attributes,
748          const OString& propertyhelper, bool supportxcomponent)
749 {
750     o << "\n\npublic final class " << classname << " extends ";
751 
752     if (!interfaces.empty()) {
753         if (propertyhelper.equals("_")) {
754                 o << "PropertySet\n";
755         } else {
756             if (supportxcomponent)
757                 o << "ComponentBase\n";
758             else
759                 o << "WeakBase\n";
760         }
761         o << "   implements ";
762         std::hash_set< OString, OStringHash >::const_iterator iter =
763             interfaces.begin();
764         while (iter != interfaces.end()) {
765             o << (*iter);
766             iter++;
767             if (iter != interfaces.end())
768                 o << ",\n              ";
769         }
770     }
771     o << "\n{\n";
772 
773     o << "    private final XComponentContext m_xContext;\n";
774 
775     // additional member for add-ons
776     if (options.componenttype == 3) {
777         o << "    private com.sun.star.frame.XFrame m_xFrame;\n";
778     }
779 
780     // check property helper
781     if (propertyhelper.getLength() > 1)
782         o << "    private final PropertySetMixin m_prophlp;\n";
783 
784     o << "    private static final String m_implementationName = "
785       << classname << ".class.getName();\n";
786 
787     if (!services.empty()) {
788         o << "    private static final String[] m_serviceNames = {\n";
789         std::hash_set< OString, OStringHash >::const_iterator iter =
790             services.begin();
791         while (iter != services.end()) {
792             o << "        \"" << (*iter).replace('/','.') << "\"";
793             iter++;
794             if (iter != services.end())
795                 o << ",\n";
796             else
797                 o << " };\n\n";
798         }
799     }
800 
801     // attribute/property members
802     if (!properties.empty()) {
803         AttributeInfo::const_iterator iter =
804             properties.begin();
805         o << "    // properties\n";
806         while (iter != properties.end()) {
807             o << "    protected ";
808             printType(o, options, manager, iter->second.first.replace('.','/'),
809                       false, false);
810             o << " m_" << iter->first << ";\n";
811             iter++;
812         }
813     } else if (!attributes.empty()) {
814         AttributeInfo::const_iterator iter =
815             attributes.begin();
816         o << "    // attributes\n";
817         while (iter != attributes.end()) {
818             o << "    private ";
819             printType(o, options, manager, iter->second.first.replace('.','/'),
820                       false, false);
821             o << " m_" << iter->first << " = ";
822             printType(o, options, manager, iter->second.first.replace('.','/'),
823                       false, true);
824             o <<";\n";
825             iter++;
826         }
827     }
828 
829     // special handling of calc add-ins
830     if (options.componenttype == 2)
831     {
832         generateAddinConstructorAndHelper(o, options, manager, classname,
833                                           services, interfaces);
834     } else {
835         o << "\n    public " << classname << "( XComponentContext context )\n"
836             "    {\n        m_xContext = context;\n";
837         if (propertyhelper.equals("_")) {
838             registerProperties(o, manager, properties, "        ");
839         } else {
840             if (propertyhelper.getLength() > 1) {
841                 o << propcomment
842                   << "        m_prophlp = new PropertySetMixin(m_xContext, this,\n"
843                   << "            new Type(" << propertyhelper
844                   << ".class), null);\n";
845             }
846         }
847         o << "    };\n\n";
848 
849     }
850 
851     if (!services.empty())
852         generateCompFunctions(o, classname);
853 
854     generateMethodBodies(o, options, manager, interfaces,
855                          "    ", propertyhelper.getLength() > 1);
856 
857     // end of class definition
858     o << "}\n";
859 }
860 
861 void generateSkeleton(ProgramOptions const & options,
862                       TypeManager const & manager,
863                       std::vector< OString > const & types,
864                       OString const & /*delegate*/)
865 {
866     std::hash_set< OString, OStringHash > interfaces;
867     std::hash_set< OString, OStringHash > services;
868     AttributeInfo properties;
869     AttributeInfo attributes;
870     std::hash_set< OString, OStringHash > propinterfaces;
871     bool serviceobject = false;
872     bool supportxcomponent = false;
873 
874     std::vector< OString >::const_iterator iter = types.begin();
875     while (iter != types.end()) {
876         checkType(manager, *iter, interfaces, services, properties);
877         iter++;
878     }
879 
880     if (options.componenttype == 3) {
881         // the Protocolhandler service is mandatory for an protocol handler add-on,
882         // so it is defaulted. The XDispatchProvider provides Dispatch objects for
883         // certain functions and the generated impl object implements XDispatch
884         // directly for simplicity reasons.
885         checkType(manager, "com.sun.star.frame.ProtocolHandler",
886                   interfaces, services, properties);
887         checkType(manager, "com.sun.star.frame.XDispatch",
888                   interfaces, services, properties);
889 
890 
891 //         ProtocolCmdMap::const_iterator iter2 = options.protocolCmdMap.begin();
892 //         while (iter2 != options.protocolCmdMap.end()) {
893 //             fprintf(stdout, "prt=%s\n", (*iter2).first.getStr());
894 
895 //             for (std::vector< OString >::const_iterator i = (*iter2).second.begin();
896 //                  i != (*iter2).second.end(); ++i) {
897 //                 fprintf(stdout, "cmd=%s\n", (*i).getStr());
898 //             }
899 //             iter2++;
900 //         }
901 //         return;
902     }
903 
904     if (options.componenttype == 2) {
905         if (services.size() != 1) {
906             throw CannotDumpException(
907                 "for calc add-in components one and only one service type is "
908                 "necessary! Please reference a valid type with the '-t' option.");
909         }
910 
911         // if backwardcompatible==true the AddIn service needs to be added to the
912         // suported service list, the necessary intefaces are mapped to the add-in
913         // configuration. Since OO.org 2.0.4 this is obsolete and the add-in is
914         // take form the configuration from Calc directly, this simplifies the
915         // add-in code
916         if (options.backwardcompatible) {
917             checkType(manager, "com.sun.star.sheet.AddIn",
918                       interfaces, services, properties);
919         } else {
920             // special case for the optional XLocalization interface. It should be
921             // implemented always. But it is parent of the XAddIn and we need it only
922             // if backwardcompatible is false.
923             if (interfaces.find("com.sun.star.lang.XLocalizable") == interfaces.end()) {
924                 interfaces.insert("com.sun.star.lang.XLocalizable");
925             }
926         }
927     }
928 
929 
930     // check if service object or simple UNO object
931     if (!services.empty())
932         serviceobject = true;
933 
934     OString propertyhelper = checkPropertyHelper(
935         options, manager, services, interfaces, attributes, propinterfaces);
936     checkDefaultInterfaces(interfaces, services, propertyhelper);
937 
938     if (options.componenttype == 2) {
939         if (propertyhelper.getLength() > 0)
940             std::cerr << "WARNING: interfaces specifying calc add-in functions "
941                 "shouldn't support attributes!\n";
942     }
943 
944     supportxcomponent = checkXComponentSupport(manager, interfaces);
945 
946     OString compFileName;
947     OString tmpFileName;
948     std::ostream* pofs = NULL;
949     bool standardout = getOutputStream(options, ".java",
950                                        &pofs, compFileName, tmpFileName);
951 
952     try {
953         if (!standardout && options.license) {
954             printLicenseHeader(*pofs, compFileName);
955         }
956 
957         generatePackage(*pofs, options.implname);
958 
959         generateImports(*pofs, options, interfaces, propertyhelper,
960                         serviceobject, supportxcomponent);
961 
962         OString classname(options.implname);
963         sal_Int32 index = 0;
964         if ((index = classname.lastIndexOf('.')) > 0)
965             classname = classname.copy(index+1);
966 
967         generateClassDefinition(*pofs, options, manager, classname, services,
968                                 interfaces, properties, attributes, propertyhelper,
969                                 supportxcomponent);
970 
971         if ( !standardout && pofs && ((std::ofstream*)pofs)->is_open()) {
972             ((std::ofstream*)pofs)->close();
973             delete pofs;
974             OSL_VERIFY(makeValidTypeFile(compFileName, tmpFileName, sal_False));
975         }
976     } catch(CannotDumpException& e) {
977 
978         std::cerr << "ERROR: " << e.m_message.getStr() << "\n";
979         if ( !standardout ) {
980             if (pofs && ((std::ofstream*)pofs)->is_open()) {
981                 ((std::ofstream*)pofs)->close();
982                 delete pofs;
983             }
984             // remove existing type file if something goes wrong to ensure
985             // consistency
986             if (fileExists(compFileName))
987                 removeTypeFile(compFileName);
988 
989             // remove tmp file if something goes wrong
990             removeTypeFile(tmpFileName);
991         }
992     }
993 }
994 
995 } }
996 
997 
998