| 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: TranslationContext.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.util.*; |
| 17 | import com.moesol.generator.core.*; |
| 18 | |
| 19 | /** |
| 20 | * Provide the translation options, mappings and access to .tmpl |
| 21 | * files. The path to the translation map is given in the |
| 22 | * constructor. The class then uses the directory of the map file to |
| 23 | * find the template files. |
| 24 | */ |
| 25 | public class TranslationContext { |
| 26 | public TranslationContext(String map_path) |
| 27 | throws IOException, FileNotFoundException |
| 28 | { |
| 29 | m_map_path = new File(map_path); |
| 30 | m_map_directory = m_map_path.getParentFile(); |
| 31 | if (m_map_directory == null) { |
| 32 | m_map_directory = new File("."); |
| 33 | } |
| 34 | initTranslationMap(); |
| 35 | } |
| 36 | private void initTranslationMap() |
| 37 | throws IOException, FileNotFoundException |
| 38 | { |
| 39 | m_default_map = new Properties(); |
| 40 | m_default_map.load(new FileInputStream(m_map_path)); |
| 41 | m_translation_map = m_default_map; |
| 42 | } |
| 43 | |
| 44 | /** @return a property from the translation map */ |
| 45 | public String getProperty(String key) throws ApplyException { |
| 46 | return getTranslationMap().getProperty(key); |
| 47 | } |
| 48 | /** @return the translation map */ |
| 49 | public Properties getTranslationMap() { |
| 50 | return m_translation_map; |
| 51 | } |
| 52 | public InputStream getTmplAsStream(String name) { |
| 53 | try { |
| 54 | return new FileInputStream(new File(m_map_directory, name)); |
| 55 | } catch (FileNotFoundException e) { |
| 56 | return null; |
| 57 | } |
| 58 | } |
| 59 | /** |
| 60 | * Allows the mapping to be spread out over many files, one for |
| 61 | * each class. Call with null to clear the class map. |
| 62 | */ |
| 63 | public void setMapForClass(Class c) |
| 64 | throws IOException |
| 65 | { |
| 66 | if (c == null) { |
| 67 | m_translation_map = m_default_map; |
| 68 | return; |
| 69 | } |
| 70 | try { |
| 71 | m_translation_map = new Properties(m_default_map); |
| 72 | m_translation_map.load(new FileInputStream(new File(m_map_directory, getMapName(c)))); |
| 73 | } catch (FileNotFoundException e) { |
| 74 | // JMV does not have files for each class yet |
| 75 | m_translation_map = m_default_map; |
| 76 | } |
| 77 | } |
| 78 | public void setTraceFinds(String name) { |
| 79 | m_trace_finds = name; |
| 80 | } |
| 81 | /** |
| 82 | * Set to override the default language search prefix |
| 83 | * For example, search for load.method instead of |
| 84 | * java.method. |
| 85 | */ |
| 86 | public void setPrefix(String prefix) { |
| 87 | m_prefix = prefix; |
| 88 | } |
| 89 | public String getPrefix() { |
| 90 | return m_prefix; |
| 91 | } |
| 92 | /** |
| 93 | * Set to override the default language suffix. |
| 94 | * e.g. Foo.java => default suffix is .java |
| 95 | * FooWriter.java => suffix is changed to Writer.java |
| 96 | */ |
| 97 | public void setSuffix(String suffix) { |
| 98 | m_suffix = suffix; |
| 99 | } |
| 100 | public String getSuffix() { |
| 101 | return m_suffix; |
| 102 | } |
| 103 | public String getTraceFinds() { |
| 104 | return m_trace_finds; |
| 105 | } |
| 106 | /** |
| 107 | * Set to allow missing translations to key method skipping. |
| 108 | */ |
| 109 | public void setSkipOnMissingTranslation(boolean skip) { |
| 110 | m_skip_on_missing_translation = skip; |
| 111 | } |
| 112 | public boolean getSkipOnMissingTranslation() { |
| 113 | return m_skip_on_missing_translation; |
| 114 | } |
| 115 | public void setGenProtected(boolean v) { |
| 116 | m_gen_protected = v; |
| 117 | } |
| 118 | public boolean genIncludesProtected() { |
| 119 | return m_gen_protected || m_gen_package || m_gen_private; |
| 120 | } |
| 121 | public void setGenPackage(boolean v) { |
| 122 | m_gen_package = v; |
| 123 | } |
| 124 | public boolean genIncludesPackage() { |
| 125 | return m_gen_package || m_gen_private; |
| 126 | } |
| 127 | public void setGenPrivate(boolean v) { |
| 128 | m_gen_private = v; |
| 129 | } |
| 130 | public boolean genIncludesPrivate() { |
| 131 | return m_gen_private; |
| 132 | } |
| 133 | public void setNoNative(boolean v) { |
| 134 | m_no_native = v; |
| 135 | } |
| 136 | public boolean getNoNative() { |
| 137 | return m_no_native; |
| 138 | } |
| 139 | public void setNoValues(boolean v) { |
| 140 | m_no_values = v; |
| 141 | } |
| 142 | public boolean getNoValues() { |
| 143 | return m_no_values; |
| 144 | } |
| 145 | public void setNoSync(boolean v) { |
| 146 | m_no_sync = v; |
| 147 | } |
| 148 | public boolean getNoSync() { |
| 149 | return m_no_sync; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * compute the name of a mapping from a class |
| 154 | */ |
| 155 | private String getMapName(Class c) { |
| 156 | return ClassGenerator.stripPackageName(c.getName()) + ".txt"; |
| 157 | } |
| 158 | private File m_map_path; |
| 159 | private File m_map_directory; |
| 160 | private Properties m_translation_map = null; |
| 161 | private Properties m_default_map = null; |
| 162 | private boolean m_skip_on_missing_translation = false; |
| 163 | private String m_prefix = null; |
| 164 | private String m_suffix = null; |
| 165 | private String m_trace_finds = null; |
| 166 | private boolean m_gen_protected = false; |
| 167 | private boolean m_gen_package = false; |
| 168 | private boolean m_gen_private = false; |
| 169 | private boolean m_no_native = false; |
| 170 | private boolean m_no_values = false; |
| 171 | private boolean m_no_sync = false; |
| 172 | } |