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