Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMikhail Sennikovsky2007-05-11 16:21:53 +0000
committerMikhail Sennikovsky2007-05-11 16:21:53 +0000
commit20a94ee53c0dd84ca2a45107ae99bcf92dad80b4 (patch)
treeeb18611d0c5d82bc25a191a21657af29c4c3c698 /core/org.eclipse.cdt.core/templateengine
parentd3ac3402c74fea598ba998d0973efe8e0d09eae8 (diff)
downloadorg.eclipse.cdt-20a94ee53c0dd84ca2a45107ae99bcf92dad80b4.tar.gz
org.eclipse.cdt-20a94ee53c0dd84ca2a45107ae99bcf92dad80b4.tar.xz
org.eclipse.cdt-20a94ee53c0dd84ca2a45107ae99bcf92dad80b4.zip
Fix from Bala Toraty with some modifications to [Bug 184452] [Template Engine] It should be possible to externalize strings in template definitions
Diffstat (limited to 'core/org.eclipse.cdt.core/templateengine')
-rw-r--r--core/org.eclipse.cdt.core/templateengine/org/eclipse/cdt/core/templateengine/TemplateCore.java17
-rw-r--r--core/org.eclipse.cdt.core/templateengine/org/eclipse/cdt/core/templateengine/TemplateDescriptor.java8
-rw-r--r--core/org.eclipse.cdt.core/templateengine/org/eclipse/cdt/core/templateengine/TemplateEngineHelper.java84
3 files changed, 103 insertions, 6 deletions
diff --git a/core/org.eclipse.cdt.core/templateengine/org/eclipse/cdt/core/templateengine/TemplateCore.java b/core/org.eclipse.cdt.core/templateengine/org/eclipse/cdt/core/templateengine/TemplateCore.java
index 9de6efd5fce..edab12bc362 100644
--- a/core/org.eclipse.cdt.core/templateengine/org/eclipse/cdt/core/templateengine/TemplateCore.java
+++ b/core/org.eclipse.cdt.core/templateengine/org/eclipse/cdt/core/templateengine/TemplateCore.java
@@ -11,6 +11,7 @@
package org.eclipse.cdt.core.templateengine;
import java.io.IOException;
+import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
@@ -75,7 +76,7 @@ public class TemplateCore {
protected TemplateCore(TemplateInfo templateInfo) throws IOException, SAXException, ParserConfigurationException {
this.templateInfo = templateInfo;
- templateDescriptor = new TemplateDescriptor(TemplateEngineHelper.getTemplateResourceURL(templateInfo.getPluginId(), templateInfo.getTemplatePath()));
+ templateDescriptor = new TemplateDescriptor(TemplateEngineHelper.getTemplateResourceURL(templateInfo.getPluginId(), templateInfo.getTemplatePath()), templateInfo.getPluginId());
valueStore = new ValueStore/*<String, String>*/(this);
valueStore.putAll(templateDescriptor.getTemplateDefaults(templateDescriptor.getRootElement()));
valueStore.putAll(TemplateEngine.getDefault().getSharedDefaults());
@@ -141,7 +142,7 @@ public class TemplateCore {
if (description == null) {
description = templateDescriptor.getRootElement().getAttribute(DESCRIPTION).trim();
}
- return description;
+ return TemplateEngineHelper.externalizeTemplateString(templateInfo, description);
}
/**
@@ -171,7 +172,7 @@ public class TemplateCore {
if (label == null) {
label = templateDescriptor.getRootElement().getAttribute(LABEL).trim();
}
- return label;
+ return TemplateEngineHelper.externalizeTemplateString(templateInfo, label);
}
/**
@@ -247,13 +248,20 @@ public class TemplateCore {
}
public Object/*V*/ put(Object/*K*/ key, Object/*V*/ value) {
+ value = TemplateEngineHelper.externalizeTemplateString(template.getTemplateInfo(), (String)value);
Object/*V*/ v = super.put(key, value);
template.setDirty();
return v;
}
public void putAll(Map/*<? extends K, ? extends V>*/ map) {
- super.putAll(map);
+ Collection keys = map.keySet();
+ for (Iterator iterator = keys.iterator(); iterator.hasNext();) {
+ Object key = iterator.next();
+ Object value = map.get(key);
+ value = TemplateEngineHelper.externalizeTemplateString(template.getTemplateInfo(), (String) value);
+ super.put(key, value);
+ }
template.setDirty();
}
@@ -262,6 +270,5 @@ public class TemplateCore {
template.setDirty();
return v;
}
-
}
}
diff --git a/core/org.eclipse.cdt.core/templateengine/org/eclipse/cdt/core/templateengine/TemplateDescriptor.java b/core/org.eclipse.cdt.core/templateengine/org/eclipse/cdt/core/templateengine/TemplateDescriptor.java
index 36cfd87d80e..f868b2362a5 100644
--- a/core/org.eclipse.cdt.core/templateengine/org/eclipse/cdt/core/templateengine/TemplateDescriptor.java
+++ b/core/org.eclipse.cdt.core/templateengine/org/eclipse/cdt/core/templateengine/TemplateDescriptor.java
@@ -41,6 +41,7 @@ public class TemplateDescriptor {
private Document document;
private Element rootElement;
private List/*<String>*/ persistVector;
+ private String pluginId;
/**
* Constructor which construct the Document based the URL
@@ -49,10 +50,11 @@ public class TemplateDescriptor {
* @throws IOException
* @throws ParserConfigurationException
*/
- public TemplateDescriptor(URL descriptorURL) throws SAXException, IOException, ParserConfigurationException {
+ public TemplateDescriptor(URL descriptorURL, String id) throws SAXException, IOException, ParserConfigurationException {
document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(descriptorURL.openStream());
rootElement = document.getDocumentElement();
persistVector = new ArrayList/*<String>*/();
+ pluginId = id;
}
/**
@@ -195,4 +197,8 @@ public class TemplateDescriptor {
public List/*<String>*/ getPersistTrueIDs() {
return persistVector;
}
+
+ public String getPluginId() {
+ return pluginId;
+ }
}
diff --git a/core/org.eclipse.cdt.core/templateengine/org/eclipse/cdt/core/templateengine/TemplateEngineHelper.java b/core/org.eclipse.cdt.core/templateengine/org/eclipse/cdt/core/templateengine/TemplateEngineHelper.java
index 2f662c92df2..26d45b44a42 100644
--- a/core/org.eclipse.cdt.core/templateengine/org/eclipse/cdt/core/templateengine/TemplateEngineHelper.java
+++ b/core/org.eclipse.cdt.core/templateengine/org/eclipse/cdt/core/templateengine/TemplateEngineHelper.java
@@ -12,7 +12,10 @@ package org.eclipse.cdt.core.templateengine;
import java.io.File;
import java.io.IOException;
+import java.io.InputStream;
import java.net.URL;
+import java.util.Locale;
+import java.util.Properties;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.core.resources.IWorkspace;
@@ -23,6 +26,7 @@ import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Plugin;
+import org.osgi.framework.Bundle;
/**
* Acts as an Helper class for Template Engine
@@ -33,9 +37,11 @@ public class TemplateEngineHelper {
public static final String OPEN_MARKER = "$("; //$NON-NLS-1$
public static final String CLOSE_MARKER = ")"; //$NON-NLS-1$
+ public static final String STRING_EXTERNALIZATION_MARKER = "%"; //$NON-NLS-1$
public static final String LOGGER_FILE_NAME = "Process"; //$NON-NLS-1$
// This is used while getting the Plugin Path.
public static final String PROJRESOURCE = "plugin.xml"; //$NON-NLS-1$
+ public static final String PLUGIN_ID = "pluginId"; //$NON-NLS-1$
public static final String BOOLTRUE = "true"; //$NON-NLS-1$
public static final String ID = "id"; //$NON-NLS-1$
public static final String VALUE = "value"; //$NON-NLS-1$
@@ -218,4 +224,82 @@ public class TemplateEngineHelper {
}
return FileLocator.toFileURL(entry);
}
+
+ public static String externalizeTemplateString(TemplateInfo ti, String key) {
+ if (key.startsWith(STRING_EXTERNALIZATION_MARKER)) {
+ String pluginId = ti.getPluginId();
+ String path = ti.getTemplatePath();
+ IPath p = new Path(path);
+ String propertiesPath = "template.properties";
+ if(p.segmentCount() != 0){
+ p = p.removeLastSegments(1);
+ propertiesPath = p.append(propertiesPath).toString();
+ }
+ return externalizeTemplateString(pluginId, propertiesPath, key);
+ }
+ return key;
+ }
+
+ public static String externalizeTemplateString(String pluginId, String location, String key) {
+ String value = key;
+ if (key.startsWith(STRING_EXTERNALIZATION_MARKER)) {
+ try {
+ value = location != null ? getValueFromProperties(pluginId, location, key.substring(1)) : null;
+ if (value == null) {
+ value = getValueFromProperties(pluginId, "plugin.properties", key.substring(1));
+ }
+ } catch (IOException e) {
+ value = key;
+ e.printStackTrace();
+ }
+
+ if (value == null) {
+ value = key;
+ }
+ }
+ return value;
+ }
+
+ private static String getValueFromProperties(String pluginId, String propertiesFile, String key) throws IOException {
+ String value = null;
+ Bundle b = Platform.getBundle(pluginId);
+// URL url= b.getResource(propertiesFile);
+ URL url= getResourceURL(b, propertiesFile);
+ if (url != null) {
+ InputStream in= url.openStream();
+ Properties p = new Properties();
+ p.load(in);
+ value = (String) p.get(key);
+ }
+ return value;
+
+ }
+
+ private static URL getResourceURL(Bundle bundle, String propertiesFile) {
+ // Get the properties in the following order
+ // propertiesFile_lang_country_variant
+ // propertiesFile_lang_country
+ // propertiesFile_lang
+ // propertiesFile
+
+ URL url = null;
+ Locale locale = Locale.getDefault();
+ String lang = locale.getLanguage();
+ String country = locale.getCountry();
+ String variant = locale.getVariant();
+
+ url = bundle.getResource(propertiesFile + "_" + lang + "_" + country + "_" + variant);
+
+ if (url == null) {
+ url = bundle.getResource(propertiesFile + "_" + lang + "_" + country);
+ }
+ if (url == null) {
+ url = bundle.getResource(propertiesFile + "_" + lang);
+ }
+ if (url == null) {
+ url = bundle.getResource(propertiesFile);
+ }
+
+ return url;
+ }
}

Back to the top