~ubuntu-branches/ubuntu/quantal/hawtjni/quantal

« back to all changes in this revision

Viewing changes to hawtjni-generator/src/main/java/org/fusesource/hawtjni/generator/JNIGenerator.java

  • Committer: Bazaar Package Importer
  • Author(s): Miguel Landaeta
  • Date: 2010-08-05 19:40:25 UTC
  • Revision ID: james.westby@ubuntu.com-20100805194025-3004hn889accwu2i
Tags: upstream-1.0~+git0c502e20c4
ImportĀ upstreamĀ versionĀ 1.0~+git0c502e20c4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*******************************************************************************
 
2
 * Copyright (c) 2009 Progress Software, Inc.
 
3
 * Copyright (c) 2004, 2006 IBM Corporation and others.
 
4
 *
 
5
 * All rights reserved. This program and the accompanying materials
 
6
 * are made available under the terms of the Eclipse Public License v1.0
 
7
 * which accompanies this distribution, and is available at
 
8
 * http://www.eclipse.org/legal/epl-v10.html
 
9
 *
 
10
 *******************************************************************************/
 
11
package org.fusesource.hawtjni.generator;
 
12
 
 
13
import java.io.BufferedReader;
 
14
import java.io.FileReader;
 
15
import java.io.IOException;
 
16
import java.io.PrintStream;
 
17
import java.lang.reflect.Modifier;
 
18
import java.util.ArrayList;
 
19
import java.util.Collections;
 
20
import java.util.Comparator;
 
21
import java.util.List;
 
22
 
 
23
import org.fusesource.hawtjni.generator.model.JNIClass;
 
24
import org.fusesource.hawtjni.generator.model.JNIField;
 
25
import org.fusesource.hawtjni.generator.model.JNIMethod;
 
26
import org.fusesource.hawtjni.generator.model.JNIType;
 
27
import org.fusesource.hawtjni.runtime.ClassFlag;
 
28
 
 
29
/**
 
30
 * 
 
31
 * @author <a href="http://hiramchirino.com">Hiram Chirino</a>
 
32
 */
 
33
public abstract class JNIGenerator {
 
34
    
 
35
    static final String delimiter = System.getProperty("line.separator");
 
36
    static final String JNI64 = "JNI64";
 
37
 
 
38
    ArrayList<JNIClass> classes;
 
39
    String copyright = "";
 
40
    boolean isCPP;
 
41
    PrintStream output = System.out;
 
42
    ProgressMonitor progress;
 
43
    private String outputName;
 
44
    
 
45
    static String fixDelimiter(String str) {
 
46
        if (delimiter.equals("\n")) {
 
47
            return str;
 
48
        }
 
49
        return str.replaceAll("\n", delimiter);
 
50
    }
 
51
 
 
52
    static String getFunctionName(JNIMethod method) {
 
53
        return getFunctionName(method, method.getParameterTypes());
 
54
    }
 
55
 
 
56
    static String getFunctionName(JNIMethod method, List<JNIType> paramTypes) {
 
57
        if ((method.getModifiers() & Modifier.NATIVE) == 0)
 
58
            return method.getName();
 
59
        String function = toC(method.getName());
 
60
        if (!method.isNativeUnique()) {
 
61
            StringBuffer buffer = new StringBuffer();
 
62
            buffer.append(function);
 
63
            buffer.append("__");
 
64
            for (JNIType paramType : paramTypes) {
 
65
                buffer.append(toC(paramType.getTypeSignature(false)));
 
66
            }
 
67
            return buffer.toString();
 
68
        }
 
69
        return function;
 
70
    }
 
71
 
 
72
    static String loadFile(String file) {
 
73
        try {
 
74
            FileReader fr = new FileReader(file);
 
75
            BufferedReader br = new BufferedReader(fr);
 
76
            StringBuffer str = new StringBuffer();
 
77
            char[] buffer = new char[1024];
 
78
            int read;
 
79
            while ((read = br.read(buffer)) != -1) {
 
80
                str.append(buffer, 0, read);
 
81
            }
 
82
            fr.close();
 
83
            return str.toString();
 
84
        } catch (IOException e) {
 
85
            throw new RuntimeException("File not found:" + file, e);
 
86
        }
 
87
    }
 
88
 
 
89
    public static void sortMethods(List<JNIMethod> methods) {
 
90
        Collections.sort(methods, new Comparator<JNIMethod>() {
 
91
            public int compare(JNIMethod mth1, JNIMethod mth2) {
 
92
                int result = mth1.getName().compareTo(mth2.getName());
 
93
                return result != 0 ? result : getFunctionName(mth1).compareTo(getFunctionName(mth2));
 
94
            }
 
95
        });
 
96
    }
 
97
 
 
98
    static void sortFields(List<JNIField> fields) {
 
99
        Collections.sort(fields, new Comparator<JNIField>() {
 
100
            public int compare(JNIField a, JNIField b) {
 
101
                return a.getName().compareTo(b.getName());
 
102
            }
 
103
        });
 
104
    }
 
105
 
 
106
    static void sortClasses(ArrayList<JNIClass> classes) {
 
107
        Collections.sort(classes, new Comparator<JNIClass>() {
 
108
            public int compare(JNIClass a, JNIClass b) {
 
109
                return a.getName().compareTo(b.getName());
 
110
            }
 
111
        });
 
112
    }
 
113
 
 
114
    static String toC(String str) {
 
115
        int length = str.length();
 
116
        StringBuffer buffer = new StringBuffer(length * 2);
 
117
        for (int i = 0; i < length; i++) {
 
118
            char c = str.charAt(i);
 
119
            switch (c) {
 
120
            case '_':
 
121
                buffer.append("_1");
 
122
                break;
 
123
            case ';':
 
124
                buffer.append("_2");
 
125
                break;
 
126
            case '[':
 
127
                buffer.append("_3");
 
128
                break;
 
129
            case '.':
 
130
                buffer.append("_");
 
131
                break;
 
132
            case '/':
 
133
                buffer.append("_");
 
134
                break;
 
135
            default:
 
136
                if( 
 
137
                   ('a' <= c && c <= 'z')
 
138
                   || ('A' <= c && c <= 'Z')                        
 
139
                   || ('0' <= c && c <= '9')                        
 
140
                ) { 
 
141
                    buffer.append(c);
 
142
                } else {
 
143
                    buffer.append(String.format("_0%04x",(int)c));
 
144
                }
 
145
            }
 
146
        }
 
147
        return buffer.toString();
 
148
    }
 
149
 
 
150
    public abstract void generate(JNIClass clazz);
 
151
 
 
152
    public void generateCopyright() {
 
153
    }
 
154
 
 
155
    public void generateIncludes() {
 
156
    }
 
157
 
 
158
    public void generate() {
 
159
        if (classes == null)
 
160
            return;
 
161
        generateCopyright();
 
162
        generateIncludes();
 
163
        sortClasses(classes);
 
164
        for (JNIClass clazz : classes) {
 
165
            if (clazz.getFlag(ClassFlag.CPP)) {
 
166
                isCPP = true;
 
167
                break;
 
168
            }
 
169
        }
 
170
        generate(classes);
 
171
        output.flush();
 
172
    }
 
173
 
 
174
    protected void generate(ArrayList<JNIClass> classes) {
 
175
        for (JNIClass clazz : classes) {
 
176
            if (clazz.getGenerate())
 
177
                generate(clazz);
 
178
            if (progress != null)
 
179
                progress.step();
 
180
        }
 
181
    }
 
182
 
 
183
    public boolean getCPP() {
 
184
        return isCPP;
 
185
    }
 
186
 
 
187
    public String getDelimiter() {
 
188
        return delimiter;
 
189
    }
 
190
 
 
191
    public PrintStream getOutput() {
 
192
        return output;
 
193
    }
 
194
 
 
195
    public String getOutputName() {
 
196
        return outputName;
 
197
    }
 
198
 
 
199
    public void setOutputName(String outputName) {
 
200
        this.outputName = outputName;
 
201
    }
 
202
 
 
203
    public ProgressMonitor getProgressMonitor() {
 
204
        return progress;
 
205
    }
 
206
 
 
207
    public void output(String str) {
 
208
        output.print(str);
 
209
    }
 
210
 
 
211
    public void outputln() {
 
212
        output(getDelimiter());
 
213
    }
 
214
 
 
215
    public void outputln(String str) {
 
216
        output(str);
 
217
        output(getDelimiter());
 
218
    }
 
219
 
 
220
    public void setClasses(ArrayList<JNIClass> classes) {
 
221
        this.classes = classes;
 
222
    }
 
223
 
 
224
    public void setOutput(PrintStream output) {
 
225
        this.output = output;
 
226
    }
 
227
 
 
228
    public void setProgressMonitor(ProgressMonitor progress) {
 
229
        this.progress = progress;
 
230
    }
 
231
    public String getCopyright() {
 
232
        return copyright;
 
233
    }
 
234
 
 
235
    public void setCopyright(String copyright) {
 
236
        this.copyright = copyright;
 
237
    }
 
238
 
 
239
}