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 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_codemaker.hxx"
30 
31 #include "sal/config.h"
32 
33 #include "codemaker/commonjava.hxx"
34 
35 #include "codemaker/options.hxx"
36 #include "codemaker/typemanager.hxx"
37 #include "codemaker/unotype.hxx"
38 
39 #include "osl/diagnose.h"
40 #include "registry/reader.hxx"
41 #include "registry/types.h"
42 #include "rtl/strbuf.h"
43 #include "rtl/string.h"
44 #include "rtl/string.hxx"
45 #include "rtl/ustring.hxx"
46 #include "sal/types.h"
47 
48 #include <vector>
49 
50 namespace codemaker { namespace java {
51 
52 rtl::OString translateUnoToJavaType(
53     codemaker::UnoType::Sort sort, RTTypeClass typeClass,
54     rtl::OString const & nucleus, bool referenceType)
55 {
56     rtl::OStringBuffer buf;
57     if (sort == codemaker::UnoType::SORT_COMPLEX) {
58         if (typeClass == RT_TYPE_INTERFACE
59             && nucleus == rtl::OString("com/sun/star/uno/XInterface"))
60         {
61             buf.append(RTL_CONSTASCII_STRINGPARAM("java/lang/Object"));
62         } else {
63             //TODO: check that nucleus is a valid (Java-modified UTF-8)
64             // identifier
65             buf.append(nucleus);
66         }
67     } else {
68         rtl::OString const javaTypes[codemaker::UnoType::SORT_ANY + 1][2] = {
69             { rtl::OString(RTL_CONSTASCII_STRINGPARAM("void")),
70               rtl::OString(RTL_CONSTASCII_STRINGPARAM("java/lang/Void")) },
71             { rtl::OString(RTL_CONSTASCII_STRINGPARAM("boolean")),
72               rtl::OString(RTL_CONSTASCII_STRINGPARAM("java/lang/Boolean")) },
73             { rtl::OString(RTL_CONSTASCII_STRINGPARAM("byte")),
74               rtl::OString(RTL_CONSTASCII_STRINGPARAM("java/lang/Byte")) },
75             { rtl::OString(RTL_CONSTASCII_STRINGPARAM("short")),
76               rtl::OString(RTL_CONSTASCII_STRINGPARAM("java/lang/Short")) },
77             { rtl::OString(RTL_CONSTASCII_STRINGPARAM("short")),
78               rtl::OString(RTL_CONSTASCII_STRINGPARAM("java/lang/Short")) },
79             { rtl::OString(RTL_CONSTASCII_STRINGPARAM("int")),
80               rtl::OString(RTL_CONSTASCII_STRINGPARAM("java/lang/Integer")) },
81             { rtl::OString(RTL_CONSTASCII_STRINGPARAM("int")),
82               rtl::OString(RTL_CONSTASCII_STRINGPARAM("java/lang/Integer")) },
83             { rtl::OString(RTL_CONSTASCII_STRINGPARAM("long")),
84               rtl::OString(RTL_CONSTASCII_STRINGPARAM("java/lang/Long")) },
85             { rtl::OString(RTL_CONSTASCII_STRINGPARAM("long")),
86               rtl::OString(RTL_CONSTASCII_STRINGPARAM("java/lang/Long")) },
87             { rtl::OString(RTL_CONSTASCII_STRINGPARAM("float")),
88               rtl::OString(RTL_CONSTASCII_STRINGPARAM("java/lang/Float")) },
89             { rtl::OString(RTL_CONSTASCII_STRINGPARAM("double")),
90               rtl::OString(RTL_CONSTASCII_STRINGPARAM("java/lang/Double")) },
91             { rtl::OString(RTL_CONSTASCII_STRINGPARAM("char")),
92               rtl::OString(RTL_CONSTASCII_STRINGPARAM("java/lang/Character")) },
93             { rtl::OString(RTL_CONSTASCII_STRINGPARAM("java/lang/String")),
94               rtl::OString(RTL_CONSTASCII_STRINGPARAM("java/lang/String")) },
95             { rtl::OString(RTL_CONSTASCII_STRINGPARAM("com/sun/star/uno/Type")),
96               rtl::OString(RTL_CONSTASCII_STRINGPARAM("com/sun/star/uno/Type")) },
97             { rtl::OString(RTL_CONSTASCII_STRINGPARAM("java/lang/Object")),
98               rtl::OString(RTL_CONSTASCII_STRINGPARAM("java/lang/Object")) } };
99         buf.append(javaTypes[sort][referenceType]);
100     }
101     return buf.makeStringAndClear();
102 }
103 
104 rtl::OString translateUnoToJavaIdentifier(
105     rtl::OString const & identifier, rtl::OString const & prefix)
106 {
107     if (identifier == "abstract"
108         || identifier == "assert" // since Java 1.4
109         || identifier == "boolean"
110         || identifier == "break"
111         || identifier == "byte"
112         || identifier == "case"
113         || identifier == "catch"
114         || identifier == "char"
115         || identifier == "class"
116         || identifier == "const"
117         || identifier == "continue"
118         || identifier == "default"
119         || identifier == "do"
120         || identifier == "double"
121         || identifier == "else"
122         || identifier == "enum" // probable addition in Java 1.5
123         || identifier == "extends"
124         || identifier == "final"
125         || identifier == "finally"
126         || identifier == "float"
127         || identifier == "for"
128         || identifier == "goto"
129         || identifier == "if"
130         || identifier == "implements"
131         || identifier == "import"
132         || identifier == "instanceof"
133         || identifier == "int"
134         || identifier == "interface"
135         || identifier == "long"
136         || identifier == "native"
137         || identifier == "new"
138         || identifier == "package"
139         || identifier == "private"
140         || identifier == "protected"
141         || identifier == "public"
142         || identifier == "return"
143         || identifier == "short"
144         || identifier == "static"
145         || identifier == "strictfp"
146         || identifier == "super"
147         || identifier == "switch"
148         || identifier == "synchronized"
149         || identifier == "this"
150         || identifier == "throw"
151         || identifier == "throws"
152         || identifier == "transient"
153         || identifier == "try"
154         || identifier == "void"
155         || identifier == "volatile"
156         || identifier == "while")
157     {
158         rtl::OStringBuffer buf(prefix);
159         buf.append('_');
160         buf.append(identifier);
161         return buf.makeStringAndClear();
162     } else {
163         return identifier;
164     }
165 }
166 
167 } }
168