1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-06-09 10:46:02 +02:00

Fix from Bala Toraty with some modifications to [Bug 184452] [Template Engine] It should be possible to externalize strings in template definitions

This commit is contained in:
Mikhail Sennikovsky 2007-05-11 16:21:53 +00:00
parent d3ac3402c7
commit 20a94ee53c
10 changed files with 233 additions and 33 deletions

View file

@ -0,0 +1,29 @@
###############################################################################
# Copyright (c) 2007 Symbian Software Private Ltd. and others.
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Eclipse Public License v1.0
# which accompanies this distribution, and is available at
# http://www.eclipse.org/legal/epl-v10.html
#
# Contributors:
# Bala Torati (Symbian) - initial API and implementation
###############################################################################
#Template Default Values
HelloWorld.CCtemplate.label=Hello World C++ Example
HelloWorld.CCtemplate.description=A skeletal Managed C++ Hello World project. Creates a folder for sources and another for include.
HelloWorld.CAnsitemplate.label=Hello World ANSI C Example
HelloWorld.CAnsitemplate.description=A skeletal Managed C Hello World project. Creates a folder for sources and another for include.
HelloWorld.basics.label=Basic Settings
HelloWorld.basics.description=Basic properties of a project
HelloWorld.author.label=Author
HelloWorld.author.description=Name of the author
HelloWorld.copyright.label=Copyright notice
HelloWorld.copyright.description=Your copyright notice
HelloWorld.copyright.default=Your copyright notice
HelloWorld.message.default=!!!Hello World!!!
HelloWorld.message.description=Your hello world greeting message
HelloWorld.message.label=Hello world greeting
HelloWorld.sourceDir.label=Source
HelloWorld.sourceDir.description=Directory for hello world project source files
HelloWorld.sourceDir.default=src

View file

@ -1,35 +1,44 @@
<?xml version="1.0" encoding="ISO-8859-1"?> <?xml version="1.0" encoding="ISO-8859-1"?>
<template type="ProjTempl" version="1.0" supplier="Eclipse.org" revision="1.0" author="Bala Torati (Symbian)" <template type="ProjTempl" version="1.0" supplier="Eclipse.org" revision="1.0" author="Bala Torati (Symbian)"
copyright="Copyright (c) 2007 Symbian Software Limited and others. All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available athttp://www.eclipse.org/legal/epl-v10.html" copyright="Copyright (c) 2007 Symbian Software Limited and others. All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available athttp://www.eclipse.org/legal/epl-v10.html"
id="HelloWorldCProject" label="Hello World ANSI C Example" description="A skeletal Managed C Hello World project. Creates a folder for sources and another for include." id="HelloWorldCProject" label="%HelloWorld.CAnsitemplate.label" description="%HelloWorld.CAnsitemplate.description"
help="help.html"> help="help.html">
<property-group id="basics" label="Basic Settings" description="Basic properties of a project" type="PAGES-ONLY" help="help.html">
<property-group id="basics" label="%HelloWorld.basics.label" description="%HelloWorld.basics.description" type="PAGES-ONLY" help="help.html">
<property id="author" <property id="author"
label="Author" label="%HelloWorld.author.label"
description="Name of the author" description="%HelloWorld.author.description"
type="input" type="input"
pattern=".*" pattern=".*"
default="" default=""
hidden="false" hidden="false"
persist="true"/> persist="true"/>
<property id="copyright" <property id="copyright"
label="Copyright notice" label="%HelloWorld.copyright.label"
description="Your copyright notice" description="%HelloWorld.copyright.description"
type="input" type="input"
pattern=".*" pattern=".*"
default="Your copyright notice" default="%HelloWorld.copyright.default"
hidden="false" hidden="false"
persist="true"/> persist="true"/>
<property id="message" <property id="message"
label="Hello world greeting" label="%HelloWorld.message.label"
description="Your hello world greeting message" description="%HelloWorld.message.description"
type="input" type="input"
pattern=".*" pattern=".*"
default="Hello World!!!" default="%HelloWorld.message.default"
hidden="false" hidden="false"
persist="true"/> persist="true"/>
<property id="sourceDir"
label="%HelloWorld.sourceDir.label"
description="%HelloWorld.sourceDir.description"
type="input"
default="%HelloWorld.sourceDir.default"
pattern="[a-zA-Z0-9]+"
mandatory="true"
persist="true"/>
</property-group> </property-group>
<process type="org.eclipse.cdt.managedbuilder.core.NewManagedProject"> <process type="org.eclipse.cdt.managedbuilder.core.NewManagedProject">
<simple name="name" value="$(projectName)" /> <simple name="name" value="$(projectName)" />
<simple name="artifactExtension" value="exe" /> <simple name="artifactExtension" value="exe" />

View file

@ -0,0 +1,29 @@
###############################################################################
# Copyright (c) 2007 Symbian Software Private Ltd. and others.
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Eclipse Public License v1.0
# which accompanies this distribution, and is available at
# http://www.eclipse.org/legal/epl-v10.html
#
# Contributors:
# Bala Torati (Symbian) - initial API and implementation
###############################################################################
#Template Default Values
HelloWorld.CCtemplate.label=Hello World C++ Example
HelloWorld.CCtemplate.description=A skeletal Managed C++ Hello World project. Creates a folder for sources and another for include.
HelloWorld.CAnsitemplate.label=Hello World ANSI C Example
HelloWorld.CAnsitemplate.description=A skeletal Managed C Hello World project. Creates a folder for sources and another for include.
HelloWorld.basics.label=Basic Settings
HelloWorld.basics.description=Basic properties of a project
HelloWorld.author.label=Author
HelloWorld.author.description=Name of the author
HelloWorld.copyright.label=Copyright notice
HelloWorld.copyright.description=Your copyright notice
HelloWorld.copyright.default=Your copyright notice
HelloWorld.message.default=!!!Hello World!!!
HelloWorld.message.description=Your hello world greeting message
HelloWorld.message.label=Hello world greeting
HelloWorld.sourceDir.label=Source
HelloWorld.sourceDir.description=Directory for hello world project source files
HelloWorld.sourceDir.default=src

View file

@ -1,38 +1,38 @@
<?xml version="1.0" encoding="ISO-8859-1"?> <?xml version="1.0" encoding="ISO-8859-1"?>
<template type="ProjTempl" version="1.0" supplier="Eclipse.org" revision="1.0" author="Bala Torati (Symbian)" <template type="ProjTempl" version="1.0" supplier="Eclipse.org" revision="1.0" author="Bala Torati (Symbian)"
copyright="Copyright (c) 2007 Symbian Software Limited and others. All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available athttp://www.eclipse.org/legal/epl-v10.html" copyright="Copyright (c) 2007 Symbian Software Limited and others. All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available athttp://www.eclipse.org/legal/epl-v10.html"
id="HelloWorldCCProject" label="Hello World C++ Example" description="A skeletal Managed C++ Hello World project. Creates a folder for sources and another for include." id="HelloWorldCCProject" label="%HelloWorld.CCtemplate.label" description="%HelloWorld.CCtemplate.description"
help="help.html"> help="help.html">
<property-group id="basics" label="Basic Settings" description="Basic properties of a project" type="PAGES-ONLY" help="help.html"> <property-group id="basics" label="%HelloWorld.basics.label" description="%HelloWorld.basics.description" type="PAGES-ONLY" help="help.html">
<property id="author" <property id="author"
label="Author" label="%HelloWorld.author.label"
description="Name of the author" description="%HelloWorld.author.description"
type="input" type="input"
pattern=".*" pattern=".*"
default="" default=""
hidden="false" hidden="false"
persist="true"/> persist="true"/>
<property id="copyright" <property id="copyright"
label="Copyright notice" label="%HelloWorld.copyright.label"
description="Your copyright notice" description="%HelloWorld.copyright.description"
type="input" type="input"
pattern=".*" pattern=".*"
default="Your copyright notice" default="%HelloWorld.copyright.default"
hidden="false" hidden="false"
persist="true"/> persist="true"/>
<property id="message" <property id="message"
label="Hello world greeting" label="%HelloWorld.message.label"
description="Your hello world greeting message" description="%HelloWorld.message.description"
type="input" type="input"
pattern=".*" pattern=".*"
default="Hello World!!!" default="%HelloWorld.message.default"
hidden="false" hidden="false"
persist="true"/> persist="true"/>
<property id="sourceDir" <property id="sourceDir"
label="Source" label="%HelloWorld.sourceDir.label"
description="Directory for hello world project source files" description="%HelloWorld.sourceDir.description"
type="input" type="input"
default="src" default="%HelloWorld.sourceDir.default"
pattern="[a-zA-Z0-9]+" pattern="[a-zA-Z0-9]+"
mandatory="true" mandatory="true"
persist="true"/> persist="true"/>

View file

@ -11,6 +11,7 @@
package org.eclipse.cdt.core.templateengine; package org.eclipse.cdt.core.templateengine;
import java.io.IOException; import java.io.IOException;
import java.util.Collection;
import java.util.Comparator; import java.util.Comparator;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
@ -75,7 +76,7 @@ public class TemplateCore {
protected TemplateCore(TemplateInfo templateInfo) throws IOException, SAXException, ParserConfigurationException { protected TemplateCore(TemplateInfo templateInfo) throws IOException, SAXException, ParserConfigurationException {
this.templateInfo = templateInfo; 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 = new ValueStore/*<String, String>*/(this);
valueStore.putAll(templateDescriptor.getTemplateDefaults(templateDescriptor.getRootElement())); valueStore.putAll(templateDescriptor.getTemplateDefaults(templateDescriptor.getRootElement()));
valueStore.putAll(TemplateEngine.getDefault().getSharedDefaults()); valueStore.putAll(TemplateEngine.getDefault().getSharedDefaults());
@ -141,7 +142,7 @@ public class TemplateCore {
if (description == null) { if (description == null) {
description = templateDescriptor.getRootElement().getAttribute(DESCRIPTION).trim(); description = templateDescriptor.getRootElement().getAttribute(DESCRIPTION).trim();
} }
return description; return TemplateEngineHelper.externalizeTemplateString(templateInfo, description);
} }
/** /**
@ -171,7 +172,7 @@ public class TemplateCore {
if (label == null) { if (label == null) {
label = templateDescriptor.getRootElement().getAttribute(LABEL).trim(); 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) { public Object/*V*/ put(Object/*K*/ key, Object/*V*/ value) {
value = TemplateEngineHelper.externalizeTemplateString(template.getTemplateInfo(), (String)value);
Object/*V*/ v = super.put(key, value); Object/*V*/ v = super.put(key, value);
template.setDirty(); template.setDirty();
return v; return v;
} }
public void putAll(Map/*<? extends K, ? extends V>*/ map) { 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(); template.setDirty();
} }
@ -262,6 +270,5 @@ public class TemplateCore {
template.setDirty(); template.setDirty();
return v; return v;
} }
} }
} }

View file

@ -41,6 +41,7 @@ public class TemplateDescriptor {
private Document document; private Document document;
private Element rootElement; private Element rootElement;
private List/*<String>*/ persistVector; private List/*<String>*/ persistVector;
private String pluginId;
/** /**
* Constructor which construct the Document based the URL * Constructor which construct the Document based the URL
@ -49,10 +50,11 @@ public class TemplateDescriptor {
* @throws IOException * @throws IOException
* @throws ParserConfigurationException * @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()); document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(descriptorURL.openStream());
rootElement = document.getDocumentElement(); rootElement = document.getDocumentElement();
persistVector = new ArrayList/*<String>*/(); persistVector = new ArrayList/*<String>*/();
pluginId = id;
} }
/** /**
@ -195,4 +197,8 @@ public class TemplateDescriptor {
public List/*<String>*/ getPersistTrueIDs() { public List/*<String>*/ getPersistTrueIDs() {
return persistVector; return persistVector;
} }
public String getPluginId() {
return pluginId;
}
} }

View file

@ -12,7 +12,10 @@ package org.eclipse.cdt.core.templateengine;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.net.URL; import java.net.URL;
import java.util.Locale;
import java.util.Properties;
import org.eclipse.cdt.core.CCorePlugin; import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.core.resources.IWorkspace; 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.Path;
import org.eclipse.core.runtime.Platform; import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Plugin; import org.eclipse.core.runtime.Plugin;
import org.osgi.framework.Bundle;
/** /**
* Acts as an Helper class for Template Engine * 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 OPEN_MARKER = "$("; //$NON-NLS-1$
public static final String CLOSE_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$ public static final String LOGGER_FILE_NAME = "Process"; //$NON-NLS-1$
// This is used while getting the Plugin Path. // This is used while getting the Plugin Path.
public static final String PROJRESOURCE = "plugin.xml"; //$NON-NLS-1$ 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 BOOLTRUE = "true"; //$NON-NLS-1$
public static final String ID = "id"; //$NON-NLS-1$ public static final String ID = "id"; //$NON-NLS-1$
public static final String VALUE = "value"; //$NON-NLS-1$ public static final String VALUE = "value"; //$NON-NLS-1$
@ -218,4 +224,82 @@ public class TemplateEngineHelper {
} }
return FileLocator.toFileURL(entry); 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;
}
} }

View file

@ -59,7 +59,7 @@ public class Template extends TemplateCore {
public Template(TemplateInfo templateInfo) throws IOException, ProcessFailureException, SAXException, ParserConfigurationException { public Template(TemplateInfo templateInfo) throws IOException, ProcessFailureException, SAXException, ParserConfigurationException {
super(templateInfo); super(templateInfo);
templateDescriptor = getTemplateDescriptor(); templateDescriptor = getTemplateDescriptor();
uiElementTreeBuilderManager = new UIElementTreeBuilderManager(new UIElementTreeBuilderHelper(templateDescriptor)); uiElementTreeBuilderManager = new UIElementTreeBuilderManager(new UIElementTreeBuilderHelper(templateDescriptor, templateInfo));
uiPagesProvider = new UIPagesProvider(); uiPagesProvider = new UIPagesProvider();
} }

View file

@ -10,7 +10,13 @@
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.ui.templateengine.uitree; package org.eclipse.cdt.ui.templateengine.uitree;
import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.eclipse.cdt.core.templateengine.TemplateEngineHelper;
import org.eclipse.cdt.core.templateengine.TemplateInfo;
/** /**
* *
@ -24,4 +30,31 @@ import java.util.HashMap;
public class UIAttributes/*<K, V>*/ extends HashMap/*<String, String>*/ { public class UIAttributes/*<K, V>*/ extends HashMap/*<String, String>*/ {
private static final long serialVersionUID = 0000000000L; private static final long serialVersionUID = 0000000000L;
private TemplateInfo templateInfo;
UIAttributes(TemplateInfo templateInfo) {
this.templateInfo = templateInfo;
}
public Object/*V*/ put(Object/*K*/ key, Object/*V*/ value) {
value = TemplateEngineHelper.externalizeTemplateString(templateInfo, (String)value);
Object/*V*/ v = super.put(key, value);
return v;
}
public void putAll(Map/*<? extends K, ? extends V>*/ map) {
Collection keys = map.keySet();
for (Iterator iterator = keys.iterator(); iterator.hasNext();) {
Object key = iterator.next();
Object value = map.get(key);
value = TemplateEngineHelper.externalizeTemplateString(templateInfo, (String) value);
super.put(key, value);
}
}
public Object/*V*/ remove(Object key) {
Object/*V*/ v = super.remove(key);
return v;
}
} }

View file

@ -20,6 +20,7 @@ import org.w3c.dom.Node;
import org.eclipse.cdt.core.templateengine.TemplateDescriptor; import org.eclipse.cdt.core.templateengine.TemplateDescriptor;
import org.eclipse.cdt.core.templateengine.TemplateEngine; import org.eclipse.cdt.core.templateengine.TemplateEngine;
import org.eclipse.cdt.core.templateengine.TemplateEngineHelper; import org.eclipse.cdt.core.templateengine.TemplateEngineHelper;
import org.eclipse.cdt.core.templateengine.TemplateInfo;
import org.eclipse.cdt.ui.templateengine.uitree.uiwidgets.UIBooleanWidget; import org.eclipse.cdt.ui.templateengine.uitree.uiwidgets.UIBooleanWidget;
import org.eclipse.cdt.ui.templateengine.uitree.uiwidgets.UIBrowseWidget; import org.eclipse.cdt.ui.templateengine.uitree.uiwidgets.UIBrowseWidget;
import org.eclipse.cdt.ui.templateengine.uitree.uiwidgets.UISelectWidget; import org.eclipse.cdt.ui.templateengine.uitree.uiwidgets.UISelectWidget;
@ -39,6 +40,7 @@ public class UIElementTreeBuilderHelper implements IUIElementTreeBuilderHelper {
* TemplateDescriptor representing the TemplaeDescriptor XML. * TemplateDescriptor representing the TemplaeDescriptor XML.
*/ */
private TemplateDescriptor templateDescriptor = null; private TemplateDescriptor templateDescriptor = null;
private TemplateInfo templateInfo;
private Element element; private Element element;
@ -47,8 +49,9 @@ public class UIElementTreeBuilderHelper implements IUIElementTreeBuilderHelper {
* *
* @param templateDescriptor * @param templateDescriptor
*/ */
public UIElementTreeBuilderHelper(TemplateDescriptor templateDescriptor) { public UIElementTreeBuilderHelper(TemplateDescriptor templateDescriptor, TemplateInfo templateInfo) {
this.templateDescriptor = templateDescriptor; this.templateDescriptor = templateDescriptor;
this.templateInfo = templateInfo;
} }
/** /**
@ -72,7 +75,7 @@ public class UIElementTreeBuilderHelper implements IUIElementTreeBuilderHelper {
this.element = element; this.element = element;
UIElement retUIElement = null; UIElement retUIElement = null;
UIAttributes/*<String, String>*/ uiAttributes = new UIAttributes/*<String, String>*/(); UIAttributes/*<String, String>*/ uiAttributes = new UIAttributes/*<String, String>*/(templateInfo);
NamedNodeMap list = element.getAttributes(); NamedNodeMap list = element.getAttributes();
for (int i = 0, s = list.getLength(); i < s; i++) { for (int i = 0, s = list.getLength(); i < s; i++) {