Author: max.andersen(a)jboss.com
Date: 2007-09-25 11:06:43 -0400 (Tue, 25 Sep 2007)
New Revision: 3790
Modified:
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/model/impl/ExporterFactory.java
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/launch/CodeGenerationLaunchDelegate.java
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/launch/CodeGenerationSettingsTab.java
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/launch/ExporterAttributes.java
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/launch/ExporterSettingsTab.java
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/launch/PathHelper.java
Log:
JBIDE-946 support for eclipse variables
Modified:
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/model/impl/ExporterFactory.java
===================================================================
---
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/model/impl/ExporterFactory.java 2007-09-25
14:55:42 UTC (rev 3789)
+++
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/console/model/impl/ExporterFactory.java 2007-09-25
15:06:43 UTC (rev 3790)
@@ -1,22 +1,24 @@
package org.hibernate.eclipse.console.model.impl;
import java.io.File;
+import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
-import org.apache.lucene.util.StringHelper;
import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.variables.IStringVariableManager;
+import org.eclipse.core.variables.VariablesPlugin;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.hibernate.cfg.Configuration;
import org.hibernate.console.HibernateConsoleRuntimeException;
import org.hibernate.eclipse.launch.HibernateLaunchConstants;
import org.hibernate.eclipse.launch.PathHelper;
-
import org.hibernate.tool.hbm2x.Exporter;
import org.hibernate.tool.hbm2x.GenericExporter;
+import org.hibernate.util.StringHelper;
/**
* ExporterFactory is used in UI to hold additional configuration for Exporter
definitions
@@ -146,8 +148,22 @@
return inputProperties.containsKey( string );
}
- public Exporter createConfiguredExporter(Configuration cfg, File outputdir,
- String[] templatePaths, Properties globalProperties) {
+ /** Method that resolves an expression through eclipses built-in variable manager.
+ * @throws CoreException if expression could not be evaluated. */
+ private String resolve(String expression) throws CoreException {
+ if(expression==null) return null;
+ IStringVariableManager variableManager =
VariablesPlugin.getDefault().getStringVariableManager();
+
+ return variableManager.performStringSubstitution(expression, false);
+ }
+
+ /**
+ * Creates exporter with the specified settings; also resolves any relevant properties
via Eclipse VariablesPlugin.
+ * @throws CoreException in case of resolve variables issues.
+ */
+ public Exporter createConfiguredExporter(Configuration cfg, String
defaultOutputDirectory,
+ String customTemplatePath, Properties globalProperties) throws CoreException {
+
Exporter exporter = getExporterDefinition().createExporterInstance();
Properties props = new Properties();
@@ -156,41 +172,41 @@
exporter.setProperties(props);
- exporter.setOutputDirectory(outputdir);
+ exporter.setOutputDirectory(new File(defaultOutputDirectory));
if(props.containsKey("outputdir")) {
- String loc =
PathHelper.getLocationAsStringPath(props.getProperty("outputdir"));
+ String resolvedOutputDir = resolve(props.getProperty("outputdir"));
+ String loc = PathHelper.getLocationAsStringPath(resolvedOutputDir);
if(loc==null) {
- throw new HibernateConsoleRuntimeException("Output directory '" +
props.getProperty("outputdir") + "' in " +
getExporterDefinition().getDescription() + " does not exist.");
+ throw new HibernateConsoleRuntimeException("Output directory '" +
resolvedOutputDir + "' in " + getExporterDefinition().getDescription() +
" does not exist.");
}
props.remove("outputdir"); // done to avoid validation check in hibernate
tools templates
- if(org.hibernate.util.StringHelper.isNotEmpty(loc)) { // only set if something valid
found
+ if(StringHelper.isNotEmpty(loc)) { // only set if something valid found
exporter.setOutputDirectory(new File(loc));
}
- } else {
-
- }
+ }
exporter.setConfiguration(cfg);
+ List templatePathList = new ArrayList();
if(props.containsKey("template_path")) {
- String locationAsStringPath =
PathHelper.getLocationAsStringPath(props.getProperty("template_path"));
+ String resolveTemplatePath = resolve(props.getProperty("template_path"));
+ String locationAsStringPath =
PathHelper.getLocationAsStringPath(resolveTemplatePath);
if(locationAsStringPath==null) {
- throw new HibernateConsoleRuntimeException("Template directory '" +
props.getProperty("template_path") + "' in " +
getExporterDefinition().getDescription() + " does not exist.");
- }
-
- String[] newPath = new String[templatePaths.length+1];
- System.arraycopy(templatePaths, 0, newPath, 0, templatePaths.length);
-
- newPath[templatePaths.length] = locationAsStringPath;
-
- exporter.setTemplatePath(newPath);
+ throw new HibernateConsoleRuntimeException("Template directory '" +
resolveTemplatePath + "' in " + getExporterDefinition().getDescription() +
" does not exist.");
+ } else {
+ templatePathList.add(locationAsStringPath);
+ }
props.remove("template_path"); // done to avoid validation check in
hibernate tools templates
- } else {
- exporter.setTemplatePath(templatePaths);
}
+ String resolvedCustomTemplatePath = resolve(customTemplatePath);
+ if(StringHelper.isNotEmpty(resolvedCustomTemplatePath)) {
+ templatePathList.add(resolvedCustomTemplatePath);
+ }
+ exporter.setTemplatePath((String[]) templatePathList.toArray(new
String[templatePathList.size()]));
+
- // special handling for GenericExporter (should be delegated via plugin.xml)
- if(exporter instanceof GenericExporter) {
+ // special handling for GenericExporter (TODO: be delegated via plugin.xml)
+ if(getExporterDefinition().getId().equals("org.hibernate.tools.hbmtemplate"))
{
GenericExporter ge = (GenericExporter) exporter;
ge.setFilePattern(props.getProperty("file_pattern", null));
Modified:
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/launch/CodeGenerationLaunchDelegate.java
===================================================================
---
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/launch/CodeGenerationLaunchDelegate.java 2007-09-25
14:55:42 UTC (rev 3789)
+++
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/launch/CodeGenerationLaunchDelegate.java 2007-09-25
15:06:43 UTC (rev 3790)
@@ -197,7 +197,7 @@
return null;
- final String outputPathRes = PathHelper.getLocationAsStringPath(
attributes.getOutputPath() );
+ final String outputPathRes =
PathHelper.getLocationAsStringPath(attributes.getOutputPath());
final String templatePath =
PathHelper.getLocationAsStringPath(attributes.getTemplatePath());
@@ -216,12 +216,11 @@
private ArtifactCollector artifactCollector = new ArtifactCollector();
public Object execute() {
- File outputdir = new File( outputPathRes );
- String[] templatePaths = new String[0];
-
- if(StringHelper.isNotEmpty(templatePath)) {
- templatePaths = new String[] { templatePath };
+ String templatePaths = null;
+
+ if(StringHelper.isNotEmpty(templatePath)) {
+ templatePaths = templatePath;
}
// Global properties
@@ -236,7 +235,12 @@
Properties globalProperties = new Properties();
globalProperties.putAll(props);
- Exporter exporter =
exporterFactories[i].createConfiguredExporter(cfg, outputdir, templatePaths,
globalProperties);
+ Exporter exporter;
+ try {
+ exporter = exporterFactories[i].createConfiguredExporter(cfg, outputPathRes,
templatePaths, globalProperties);
+ } catch (CoreException e) {
+ throw new HibernateConsoleRuntimeException("Error while setting up " +
exporterFactories[i].getExporterDefinition(), e);
+ }
exporter.start();
monitor.worked(1);
Modified:
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/launch/CodeGenerationSettingsTab.java
===================================================================
---
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/launch/CodeGenerationSettingsTab.java 2007-09-25
14:55:42 UTC (rev 3789)
+++
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/launch/CodeGenerationSettingsTab.java 2007-09-25
15:06:43 UTC (rev 3790)
@@ -28,6 +28,8 @@
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
+import org.eclipse.core.variables.IStringVariableManager;
+import org.eclipse.core.variables.VariablesPlugin;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.debug.ui.AbstractLaunchConfigurationTab;
@@ -121,7 +123,7 @@
outputdir = new StringButtonDialogField(new IStringButtonAdapter() {
public void changeControlPressed(DialogField field) {
- IPath[] paths = DialogSelectionHelper.chooseFileEntries(getShell(),
getOutputDirectory(), new IPath[0], "Select output directory", "Choose
directory in which the generated files will be stored", new String[]
{"cfg.xml"}, false, true, false);
+ IPath[] paths = DialogSelectionHelper.chooseFileEntries(getShell(),
PathHelper.pathOrNull(outputdir.getText()), new IPath[0], "Select output
directory", "Choose directory in which the generated files will be stored",
new String[] {"cfg.xml"}, false, true, false);
if(paths!=null && paths.length==1) {
outputdir.setText( ( (paths[0]).toOSString() ) );
}
@@ -262,7 +264,7 @@
return;
}
- String msg = PathHelper.checkDirectory(getOutputDirectory(), "output
directory", false);
+ String msg = PathHelper.checkDirectory(outputdir.getText(), "output
directory", false);
if (msg!=null) {
updateStatus(msg);
@@ -286,7 +288,7 @@
}
if(useOwnTemplates.isSelected() ) {
- msg = PathHelper.checkDirectory(getTemplateDirectory(), "template
directory", true);
+ msg = PathHelper.checkDirectory(templatedir.getText(), "template
directory", true);
if (msg!=null) {
updateStatus(msg);
return;
@@ -357,15 +359,20 @@
return reverseengineer.isSelected();
}
- public IPath getOutputDirectory() {
- return PathHelper.pathOrNull(outputdir.getText() );
+ private String resolve(String expression) {
+ if(expression==null) return null;
+ IStringVariableManager variableManager =
VariablesPlugin.getDefault().getStringVariableManager();
+
+ try {
+ return variableManager.performStringSubstitution(expression, false);
+ } catch (CoreException e) {
+ // ignore possible errors during substitution and just return the orginal expression
+ return expression;
+ }
}
-
- public IPath getTemplateDirectory() {
- return PathHelper.pathOrNull(templatedir.getText() );
- }
-
- public String getOutputPackage() {
+
+
+ String getOutputPackage() {
return packageName.getText();
}
Modified:
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/launch/ExporterAttributes.java
===================================================================
---
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/launch/ExporterAttributes.java 2007-09-25
14:55:42 UTC (rev 3789)
+++
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/launch/ExporterAttributes.java 2007-09-25
15:06:43 UTC (rev 3790)
@@ -161,7 +161,7 @@
public static void saveExporterFactories(
ILaunchConfigurationWorkingCopy configuration,
- List exporterFactories, List enabledExporters, Set deletedExporterIds) {
+ List exporterFactories, Set enabledExporters, Set deletedExporterIds) {
List names = new ArrayList();
Modified:
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/launch/ExporterSettingsTab.java
===================================================================
---
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/launch/ExporterSettingsTab.java 2007-09-25
14:55:42 UTC (rev 3789)
+++
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/launch/ExporterSettingsTab.java 2007-09-25
15:06:43 UTC (rev 3790)
@@ -96,7 +96,7 @@
private Button enableJDK5;
- private List selectedExporters;
+ private Set selectedExporters;
private Set deletedExporterIds;
@@ -125,7 +125,7 @@
* @see IDialogPage#createControl(Composite)
*/
public void createControl(Composite parent) {
- selectedExporters = new ArrayList();
+ selectedExporters = new HashSet();
deletedExporterIds = new HashSet();
// ScrolledComposite scrolled = new ScrolledComposite(parent,
// SWT.V_SCROLL | SWT.H_SCROLL);
@@ -356,7 +356,9 @@
ExporterFactory exporterFactory = new ExporterFactory( expDef, initialName );
observableFactoryList.add(exporterFactory);
+ selectedExporters.add(exporterFactory);
((CheckboxTableViewer)getTableViewer()).setChecked(exporterFactory, true);
+
}
protected void handleRemove() {
@@ -720,7 +722,7 @@
String str = (String) ef.getProperties().get("outputdir");
String msg = null;
if(str!=null) {
- msg = PathHelper.checkDirectory(PathHelper.pathOrNull(str), "Output directory
for " + ef.getExporterDefinition().getDescription(), false);
+ msg = PathHelper.checkDirectory(str, "Output directory for " +
ef.getExporterDefinition().getDescription(), false);
if(msg!=null) {
updateStatus(msg);
return;
@@ -729,7 +731,7 @@
str = (String) ef.getProperties().get("template_path");
if(str!=null) {
- msg = PathHelper.checkDirectory(PathHelper.pathOrNull(str), "Template directory
for " + ef.getExporterDefinition().getDescription(), true);
+ msg = PathHelper.checkDirectory(str, "Template directory for " +
ef.getExporterDefinition().getDescription(), true);
if(msg!=null) {
updateStatus(msg);
return;
Modified:
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/launch/PathHelper.java
===================================================================
---
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/launch/PathHelper.java 2007-09-25
14:55:42 UTC (rev 3789)
+++
trunk/hibernatetools/plugins/org.hibernate.eclipse.console/src/org/hibernate/eclipse/launch/PathHelper.java 2007-09-25
15:06:43 UTC (rev 3790)
@@ -6,12 +6,16 @@
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
+import org.eclipse.core.variables.IStringVariableManager;
+import org.eclipse.core.variables.VariablesPlugin;
// TODO: move to internal.
public class PathHelper {
public static String getLocationAsStringPath(String path) {
+ if(path==null) return null;
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IResource member = PathHelper.findMember(root, path);
if(member==null) {
@@ -38,15 +42,55 @@
else return resource.getRawLocation();
}
+
+ static private String resolve(String expression) {
+ if(expression==null) return null;
+ IStringVariableManager variableManager =
VariablesPlugin.getDefault().getStringVariableManager();
+
+ try {
+ return variableManager.performStringSubstitution(expression, false);
+ } catch (CoreException e) {
+ return expression;
+ }
+ }
+
public static Path pathOrNull(String p) {
+ return pathOrNull(p, false);
+ }
+
+ public static Path pathOrNull(String p, boolean resolveVariables) {
+ if(resolveVariables && p!=null) {
+ p = resolve(p);
+ }
if(p==null || p.trim().length()==0) {
return null;
} else {
return new Path(p);
}
}
-
- static public String checkDirectory(IPath path, String name, boolean checkFilesystem) {
+
+ /**
+ * Checks if directory exists.
+ * Handles variables replacement too.
+ *
+ * @param strpath
+ * @param name
+ * @param checkFilesystem
+ * @return
+ */
+ static public String checkDirectory(String strpath, String name, boolean
checkFilesystem) {
+ if(strpath.indexOf("${") >= 0) { //$NON-NLS-1$
+ IStringVariableManager manager =
VariablesPlugin.getDefault().getStringVariableManager();
+ try {
+ manager.validateStringVariables(strpath);
+ }
+ catch (CoreException e) {
+ return name + " has invalid variable references [" + e.getMessage() +
"]";
+ }
+ }
+
+ IPath path = pathOrNull(resolve(strpath));
+
if (checkFilesystem) {
if (path != null && new File(path.toOSString()).exists()) {
return null;
@@ -64,10 +108,11 @@
} else {
return name + " has to be a folder or project [" + path +
"]";
}
- } else {
+ } else {
return name + " does not exist [" + path + "]";
}
return null;
}
+
}