1 | // |
2 | // (c) Copyright, Moebius Solutions, 2002 |
3 | // |
4 | // All Rights Reserved |
5 | // |
6 | // This material may be reproduced by or for the U. S. Government |
7 | // pursuant to the copyright license under the clause at |
8 | // DFARS 252.227-7013 (OCT 1988). |
9 | // |
10 | /* |
11 | * $Id: JniHeaderClassGenerator.java,v 1.1.1.5 2004/05/25 20:23:29 hastings Exp $ |
12 | */ |
13 | package com.moesol.generator; |
14 | |
15 | import java.io.*; |
16 | import java.lang.reflect.*; |
17 | import java.util.*; |
18 | import com.moesol.generator.core.*; |
19 | |
20 | /** |
21 | * Abstract base class for code generators |
22 | */ |
23 | public class JniHeaderClassGenerator extends ClassGenerator { |
24 | public JniHeaderClassGenerator(Class clazz, boolean quiet, TranslationContext ctx) { |
25 | super(clazz, quiet, ctx); |
26 | } |
27 | |
28 | /** Template method */ |
29 | protected boolean shouldGenerate() { |
30 | return Modifier.isPublic(m_class.getModifiers()); |
31 | } |
32 | /** Template method */ |
33 | protected void extendClassProperties(Properties p) { |
34 | p.setProperty("namespace_start", getNamespaceStart()); |
35 | p.setProperty("namespace_end", getNamespaceEnd()); |
36 | } |
37 | /** Template method */ |
38 | protected String getTemplateName() throws ApplyException { |
39 | return "jni.h.class.tmpl"; |
40 | } |
41 | /** Template method */ |
42 | protected ClassGeneratorVisitor createClassVisitor(Writer out) throws ApplyException { |
43 | return new JniHeaderClassGeneratorVisitor(out, getTranslationContext()); |
44 | } |
45 | /** Template method */ |
46 | protected String getFileSuffix(){ |
47 | return ".h"; |
48 | } |
49 | |
50 | // TODO visit package names |
51 | private String getNamespaceStart() { |
52 | StringBuffer result = new StringBuffer(); |
53 | String name = m_class.getPackage().getName(); |
54 | int last_dot = 0; |
55 | for (int i = 0; i < name.length(); i++) { |
56 | if (name.charAt(i) == '.') { |
57 | result.append("namespace "); |
58 | result.append(name.substring(last_dot, i)); |
59 | result.append(" { "); |
60 | last_dot = i + 1; |
61 | } |
62 | } |
63 | if (last_dot > 0) { |
64 | result.append("namespace "); |
65 | result.append(name.substring(last_dot)); |
66 | result.append(" { "); |
67 | } |
68 | return result.toString(); |
69 | } |
70 | private String getNamespaceEnd() { |
71 | StringBuffer result = new StringBuffer(); |
72 | String name = m_class.getPackage().getName(); |
73 | int last_dot = 0; |
74 | for (int i = 0; i < name.length(); i++) { |
75 | if (name.charAt(i) == '.') { |
76 | result.append("} "); |
77 | last_dot = i + 1; |
78 | } |
79 | } |
80 | if (last_dot > 0) { |
81 | result.append("} "); |
82 | } |
83 | return result.toString(); |
84 | } |
85 | |
86 | // additional method for each #{method} in the template... |
87 | } |