summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpelder2006-03-02 16:18:30 (EST)
committer pelder2006-03-02 16:18:30 (EST)
commit9260f0ade88139a3ae1fdd41c026f1bc5cd545a6 (patch)
tree7b89624132eb8a3e1723b5e78fb4ecd3259237ad
parent25c4f97feaf2a2d4951cd680c81b8586c53bab7e (diff)
downloadorg.eclipse.jet-9260f0ade88139a3ae1fdd41c026f1bc5cd545a6.zip
org.eclipse.jet-9260f0ade88139a3ae1fdd41c026f1bc5cd545a6.tar.gz
org.eclipse.jet-9260f0ade88139a3ae1fdd41c026f1bc5cd545a6.tar.bz2
[130169] Improve the re-usability of the Workspace tag actions
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/JET2Context.java4
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/workspace/CopyFileTag.java30
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/workspace/FileTag.java3
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/workspace/FolderTag.java1
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/workspace/ProjectTag.java1
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/workspace/WsCopyBinaryFileAction.java95
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/workspace/WsCopyTextFileAction.java99
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/workspace/WsFileFromWriterAction.java107
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/workspace/WsFolderAction.java83
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/workspace/WsProjectAction.java (renamed from plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/workspace/WsProjectAction.java)66
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/workspace/AbstractWorkspaceAction.java54
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/workspace/ActionsUtil.java429
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/workspace/IWorkspaceAction.java10
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/workspace/WorkspaceContextExtender.java57
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/workspace/WsCopyBinaryFileAction.java198
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/workspace/WsCopyTextFileAction.java229
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/workspace/WsFileFromWriterAction.java194
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/workspace/WsFolderAction.java133
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/transform/TransformContextExtender.java10
19 files changed, 944 insertions, 859 deletions
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/JET2Context.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/JET2Context.java
index 49c11b2..35aae86 100644
--- a/plugins/org.eclipse.jet/src/org/eclipse/jet/JET2Context.java
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/JET2Context.java
@@ -23,7 +23,6 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
-import org.eclipse.core.runtime.CoreException;
import org.eclipse.emf.common.util.BasicDiagnostic;
import org.eclipse.emf.common.util.Diagnostic;
import org.eclipse.jet.internal.l10n.JET2Messages;
@@ -300,8 +299,7 @@ public final class JET2Context
* @param message
* @param e
*/
- // Used once: WorkspaceContextExtender.ContextData.commit()
- public void logError(String templatePath, TagInfo tagInfo, String message, CoreException e)
+ public void logError(String templatePath, TagInfo tagInfo, String message, Throwable e)
{
log(Diagnostic.ERROR, templatePath, tagInfo, message, e);
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/workspace/CopyFileTag.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/workspace/CopyFileTag.java
index 5f5ffea..fae2f9f 100644
--- a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/workspace/CopyFileTag.java
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/workspace/CopyFileTag.java
@@ -18,13 +18,11 @@
package org.eclipse.jet.internal.taglib.workspace;
-import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import org.eclipse.core.resources.IFile;
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.jet.JET2Context;
@@ -32,10 +30,9 @@ import org.eclipse.jet.JET2Writer;
import org.eclipse.jet.taglib.AbstractEmptyTag;
import org.eclipse.jet.taglib.JET2TagException;
import org.eclipse.jet.taglib.TagInfo;
+import org.eclipse.jet.taglib.workspace.ActionsUtil;
import org.eclipse.jet.taglib.workspace.IWorkspaceAction;
import org.eclipse.jet.taglib.workspace.WorkspaceContextExtender;
-import org.eclipse.jet.taglib.workspace.WsCopyBinaryFileAction;
-import org.eclipse.jet.taglib.workspace.WsCopyTextFileAction;
import org.eclipse.jet.transform.TransformContextExtender;
@@ -116,28 +113,17 @@ public class CopyFileTag extends AbstractEmptyTag
}
IWorkspaceAction action;
- try
- {
- if (textCopy)
- {
- final String sourceFileContents = WsCopyTextFileAction.readTextFile(sourceURL, srcEncoding);
- action = new WsCopyTextFileAction(context, tce.getTemplatePath(), td, sourceFileContents, file, replaceExisting, targetEncoding);
- }
- else
- {
- final byte[] contents = WsCopyBinaryFileAction.readBinaryFile(sourceURL);
- action = new WsCopyBinaryFileAction(context, tce.getTemplatePath(), td, contents, file, replaceExisting);
- }
- wsExtender.addAction(action);
- }
- catch (CoreException e)
+ if (textCopy)
{
- throw new JET2TagException(e);
+ final String sourceFileContents = ActionsUtil.readTextFile(sourceURL, srcEncoding);
+ action = new WsCopyTextFileAction(context, tce.getTemplatePath(), td, sourceFileContents, file, replaceExisting, targetEncoding);
}
- catch (IOException e)
+ else
{
- throw new JET2TagException(e);
+ final byte[] contents = ActionsUtil.readBinaryFile(sourceURL);
+ action = new WsCopyBinaryFileAction(context, tce.getTemplatePath(), td, contents, file, replaceExisting);
}
+ wsExtender.addAction(action);
}
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/workspace/FileTag.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/workspace/FileTag.java
index e646b88..a3c45da 100644
--- a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/workspace/FileTag.java
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/workspace/FileTag.java
@@ -29,7 +29,6 @@ import org.eclipse.jet.taglib.AbstractEmptyTag;
import org.eclipse.jet.taglib.JET2TagException;
import org.eclipse.jet.taglib.TagInfo;
import org.eclipse.jet.taglib.workspace.WorkspaceContextExtender;
-import org.eclipse.jet.taglib.workspace.WsFileFromWriterAction;
import org.eclipse.jet.transform.TransformContextExtender;
@@ -102,8 +101,6 @@ public class FileTag extends AbstractEmptyTag
TransformContextExtender tce = new TransformContextExtender(context);
tce.execute(templatePath, contentWriter);
- contentWriter.finalizeContent(file);
-
WsFileFromWriterAction fileAction = new WsFileFromWriterAction(
context,
tce.getTemplatePath(),
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/workspace/FolderTag.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/workspace/FolderTag.java
index 9af0bd1..8a4081c 100644
--- a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/workspace/FolderTag.java
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/workspace/FolderTag.java
@@ -28,7 +28,6 @@ import org.eclipse.jet.taglib.AbstractContainerTag;
import org.eclipse.jet.taglib.JET2TagException;
import org.eclipse.jet.taglib.TagInfo;
import org.eclipse.jet.taglib.workspace.WorkspaceContextExtender;
-import org.eclipse.jet.taglib.workspace.WsFolderAction;
import org.eclipse.jet.transform.TransformContextExtender;
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/workspace/ProjectTag.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/workspace/ProjectTag.java
index 9c3d620..da7d9ad 100644
--- a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/workspace/ProjectTag.java
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/workspace/ProjectTag.java
@@ -26,7 +26,6 @@ import org.eclipse.jet.taglib.AbstractContainerTag;
import org.eclipse.jet.taglib.JET2TagException;
import org.eclipse.jet.taglib.TagInfo;
import org.eclipse.jet.taglib.workspace.WorkspaceContextExtender;
-import org.eclipse.jet.taglib.workspace.WsProjectAction;
import org.eclipse.jet.transform.TransformContextExtender;
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/workspace/WsCopyBinaryFileAction.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/workspace/WsCopyBinaryFileAction.java
new file mode 100644
index 0000000..855b082
--- /dev/null
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/workspace/WsCopyBinaryFileAction.java
@@ -0,0 +1,95 @@
+/**
+ * <copyright>
+ *
+ * Copyright (c) 2006 IBM Corporation 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:
+ * IBM - Initial API and implementation
+ *
+ * </copyright>
+ *
+ * $Id$
+ */
+
+package org.eclipse.jet.internal.taglib.workspace;
+
+
+import java.text.MessageFormat;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.jet.JET2Context;
+import org.eclipse.jet.internal.l10n.JET2Messages;
+import org.eclipse.jet.internal.runtime.RuntimeLoggerContextExtender;
+import org.eclipse.jet.taglib.JET2TagException;
+import org.eclipse.jet.taglib.TagInfo;
+import org.eclipse.jet.taglib.workspace.AbstractWorkspaceAction;
+import org.eclipse.jet.taglib.workspace.ActionsUtil;
+import org.eclipse.jet.taglib.workspace.IWorkspaceAction;
+
+
+/**
+ * Copy a binary file from a source URL to a target workspace file.
+ *
+ */
+public class WsCopyBinaryFileAction extends AbstractWorkspaceAction implements IWorkspaceAction
+{
+
+ private final IFile file;
+
+ private final boolean replace;
+
+ private final byte[] contents;
+
+ private final JET2Context context;
+
+ public WsCopyBinaryFileAction(
+ JET2Context context,
+ String templatePath,
+ TagInfo tagInfo,
+ byte[] contents,
+ IFile targetFile,
+ boolean replaceExisting)
+ {
+ super(tagInfo, templatePath);
+ this.context = context;
+ this.contents = contents;
+ this.file = targetFile;
+ this.replace = replaceExisting;
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.internal.taglib.workspace.IWorkspaceAction#getResource()
+ */
+ public IResource getResource()
+ {
+ return file;
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.internal.taglib.workspace.IWorkspaceAction#requiresValidateEdit()
+ */
+ public boolean requiresValidateEdit()
+ {
+ return replace && file.exists() && file.isReadOnly() /* && content has changed */;
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.internal.taglib.workspace.IWorkspaceAction#performAction(org.eclipse.core.runtime.IProgressMonitor)
+ */
+ public void performAction(IProgressMonitor monitor) throws JET2TagException
+ {
+ ActionsUtil.writeBinaryFile(file, replace, contents, monitor);
+
+ final String fileMessage = MessageFormat.format(
+ JET2Messages.WsCopyBinaryFileAction_WritingFile,
+ new Object []{ file.getFullPath().toString() });
+ RuntimeLoggerContextExtender.log(context, fileMessage, getTagInfo(), getTemplatePath());
+ }
+
+}
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/workspace/WsCopyTextFileAction.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/workspace/WsCopyTextFileAction.java
new file mode 100644
index 0000000..2b1cd68
--- /dev/null
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/workspace/WsCopyTextFileAction.java
@@ -0,0 +1,99 @@
+/**
+ * <copyright>
+ *
+ * Copyright (c) 2006 IBM Corporation 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:
+ * IBM - Initial API and implementation
+ *
+ * </copyright>
+ *
+ * $Id$
+ */
+
+package org.eclipse.jet.internal.taglib.workspace;
+
+
+import java.text.MessageFormat;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.jet.JET2Context;
+import org.eclipse.jet.internal.l10n.JET2Messages;
+import org.eclipse.jet.internal.runtime.RuntimeLoggerContextExtender;
+import org.eclipse.jet.taglib.JET2TagException;
+import org.eclipse.jet.taglib.TagInfo;
+import org.eclipse.jet.taglib.workspace.AbstractWorkspaceAction;
+import org.eclipse.jet.taglib.workspace.ActionsUtil;
+import org.eclipse.jet.taglib.workspace.IWorkspaceAction;
+
+
+/**
+ * Copy a binary file from a source URL to a target workspace file.
+ *
+ */
+public class WsCopyTextFileAction extends AbstractWorkspaceAction implements IWorkspaceAction
+{
+
+ private final IFile file;
+
+ private final boolean replace;
+
+ private final String targetEncoding;
+
+ private final String sourceFileContents;
+
+ private final JET2Context context;
+
+ public WsCopyTextFileAction(
+ JET2Context context,
+ String templatePath,
+ TagInfo td,
+ String sourceFileContents,
+ IFile file,
+ boolean replaceExisting,
+ String targetEncoding)
+ {
+ super(td, templatePath);
+ this.context = context;
+ this.sourceFileContents = sourceFileContents;
+ this.file = file;
+ this.replace = replaceExisting;
+ this.targetEncoding = targetEncoding;
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.internal.taglib.workspace.IWorkspaceAction#getResource()
+ */
+ public IResource getResource()
+ {
+ return file;
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.internal.taglib.workspace.IWorkspaceAction#requiresValidateEdit()
+ */
+ public boolean requiresValidateEdit()
+ {
+ return replace && file.exists() && file.isReadOnly() /* && content has changed */;
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.internal.taglib.workspace.IWorkspaceAction#performAction(org.eclipse.core.runtime.IProgressMonitor)
+ */
+ public void performAction(IProgressMonitor monitor) throws JET2TagException
+ {
+ ActionsUtil.writeTextFile(file, replace, targetEncoding, false, sourceFileContents, monitor);
+
+ final String fileMessage = MessageFormat.format(
+ JET2Messages.WsFileFromWriterAction_WritingFile,
+ new Object []{ file.getFullPath().toString() });
+ RuntimeLoggerContextExtender.log(context, fileMessage, getTagInfo(), getTemplatePath());
+ }
+
+}
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/workspace/WsFileFromWriterAction.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/workspace/WsFileFromWriterAction.java
new file mode 100644
index 0000000..622ee88
--- /dev/null
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/workspace/WsFileFromWriterAction.java
@@ -0,0 +1,107 @@
+/**
+ * <copyright>
+ *
+ * Copyright (c) 2005 IBM Corporation 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:
+ * IBM - Initial API and implementation
+ *
+ * </copyright>
+ *
+ * $Id$
+ */
+
+package org.eclipse.jet.internal.taglib.workspace;
+
+
+import java.text.MessageFormat;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.jet.JET2Context;
+import org.eclipse.jet.JET2Writer;
+import org.eclipse.jet.internal.l10n.JET2Messages;
+import org.eclipse.jet.internal.runtime.RuntimeLoggerContextExtender;
+import org.eclipse.jet.taglib.JET2TagException;
+import org.eclipse.jet.taglib.TagInfo;
+import org.eclipse.jet.taglib.workspace.AbstractWorkspaceAction;
+import org.eclipse.jet.taglib.workspace.ActionsUtil;
+import org.eclipse.jet.taglib.workspace.IWorkspaceAction;
+
+
+/**
+ * Create or update a text file with the contents of a writer..
+ *
+ */
+public class WsFileFromWriterAction extends AbstractWorkspaceAction implements IWorkspaceAction
+{
+
+ private final IFile file;
+
+ private final JET2Writer writer;
+
+ private String encoding;
+
+ private final boolean replace;
+
+ private final boolean derived;
+
+ private final JET2Context context;
+
+ /**
+ *
+ * @param context
+ * @param templatePath
+ * @param tagInfo
+ * @param file
+ * @param writer
+ * @param replace
+ * @param derived
+ */
+ public WsFileFromWriterAction(
+ JET2Context context,
+ String templatePath,
+ TagInfo tagInfo,
+ IFile file,
+ JET2Writer writer,
+ boolean replace,
+ boolean derived)
+ {
+ super(tagInfo, templatePath);
+ this.context = context;
+ this.file = file;
+ this.writer = writer;
+ this.replace = replace;
+ this.derived = derived;
+ }
+
+ public IResource getResource()
+ {
+ return file;
+ }
+
+ public boolean requiresValidateEdit()
+ {
+ return replace && file.exists() && file.isReadOnly() /* && content has changed */;
+ }
+
+ public void performAction(IProgressMonitor monitor) throws JET2TagException
+ {
+ final String fileMessage = MessageFormat.format(
+ JET2Messages.WsFileFromWriterAction_WritingFile,
+ new Object []{ file.getFullPath().toString() });
+ ActionsUtil.writeTextFileFromWriter(file, replace, encoding, derived, writer, monitor);
+ RuntimeLoggerContextExtender.log(context, fileMessage, getTagInfo(), getTemplatePath());
+ }
+
+ public void setEncoding(String encoding)
+ {
+ this.encoding = encoding;
+ }
+
+}
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/workspace/WsFolderAction.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/workspace/WsFolderAction.java
new file mode 100644
index 0000000..76de65d
--- /dev/null
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/workspace/WsFolderAction.java
@@ -0,0 +1,83 @@
+/**
+ * <copyright>
+ *
+ * Copyright (c) 2005 IBM Corporation 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:
+ * IBM - Initial API and implementation
+ *
+ * </copyright>
+ *
+ * $Id$
+ */
+
+package org.eclipse.jet.internal.taglib.workspace;
+
+
+import java.text.MessageFormat;
+
+import org.eclipse.core.resources.IFolder;
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.jet.JET2Context;
+import org.eclipse.jet.internal.l10n.JET2Messages;
+import org.eclipse.jet.internal.runtime.RuntimeLoggerContextExtender;
+import org.eclipse.jet.taglib.JET2TagException;
+import org.eclipse.jet.taglib.TagInfo;
+import org.eclipse.jet.taglib.workspace.AbstractWorkspaceAction;
+import org.eclipse.jet.taglib.workspace.ActionsUtil;
+import org.eclipse.jet.taglib.workspace.IWorkspaceAction;
+
+
+/**
+ * Represent a Folder action. A folder action will:
+ * <bl>
+ * <li>create a folder (and any uncreated parent folders), if it does not exist</li>
+ * </bl>
+ *
+ */
+public class WsFolderAction extends AbstractWorkspaceAction implements IWorkspaceAction
+{
+
+ private final IFolder folder;
+
+ private final JET2Context context;
+
+ /**
+ *
+ * @param context
+ * @param templatePath
+ * @param tagInfo
+ * @param folder
+ */
+ public WsFolderAction(JET2Context context, String templatePath, TagInfo tagInfo, IFolder folder)
+ {
+ super(tagInfo, templatePath);
+ this.context = context;
+ this.folder = folder;
+ }
+
+ public IResource getResource()
+ {
+ return folder;
+ }
+
+ public boolean requiresValidateEdit()
+ {
+ return false;
+ }
+
+ public void performAction(IProgressMonitor monitor) throws JET2TagException
+ {
+ ActionsUtil.ensureFolderExists(folder, monitor);
+ RuntimeLoggerContextExtender.log(context, MessageFormat.format(
+ JET2Messages.WsFolderAction_CreatingFolder,
+ new Object []{ folder.getFullPath().toString() }), getTagInfo(), getTemplatePath());
+
+ }
+
+}
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/workspace/WsProjectAction.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/workspace/WsProjectAction.java
index 554922e..76afd14 100644
--- a/plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/workspace/WsProjectAction.java
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/workspace/WsProjectAction.java
@@ -15,20 +15,22 @@
* $Id$
*/
-package org.eclipse.jet.taglib.workspace;
+package org.eclipse.jet.internal.taglib.workspace;
import java.text.MessageFormat;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
-import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
-import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.jet.JET2Context;
import org.eclipse.jet.internal.l10n.JET2Messages;
import org.eclipse.jet.internal.runtime.RuntimeLoggerContextExtender;
+import org.eclipse.jet.taglib.JET2TagException;
import org.eclipse.jet.taglib.TagInfo;
+import org.eclipse.jet.taglib.workspace.AbstractWorkspaceAction;
+import org.eclipse.jet.taglib.workspace.ActionsUtil;
+import org.eclipse.jet.taglib.workspace.IWorkspaceAction;
/**
@@ -39,13 +41,9 @@ import org.eclipse.jet.taglib.TagInfo;
* </bl>
*
*/
-public class WsProjectAction implements IWorkspaceAction
+public class WsProjectAction extends AbstractWorkspaceAction implements IWorkspaceAction
{
- private final String templatePath;
-
- private final TagInfo tagInfo;
-
private final IProject project;
private final JET2Context context;
@@ -59,10 +57,8 @@ public class WsProjectAction implements IWorkspaceAction
*/
public WsProjectAction(JET2Context context, String templatePath, TagInfo tagInfo, IProject project)
{
- super();
+ super(tagInfo, templatePath);
this.context = context;
- this.templatePath = templatePath;
- this.tagInfo = tagInfo;
this.project = project;
}
@@ -75,22 +71,6 @@ public class WsProjectAction implements IWorkspaceAction
}
/* (non-Javadoc)
- * @see org.eclipse.jet.internal.taglib.workspace.IWorkspaceAction#getTagInfo()
- */
- public TagInfo getTagInfo()
- {
- return tagInfo;
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.jet.internal.taglib.workspace.IWorkspaceAction#getTemplatePath()
- */
- public String getTemplatePath()
- {
- return templatePath;
- }
-
- /* (non-Javadoc)
* @see org.eclipse.jet.internal.taglib.workspace.IWorkspaceAction#requiresValidateEdit()
*/
public boolean requiresValidateEdit()
@@ -101,36 +81,12 @@ public class WsProjectAction implements IWorkspaceAction
/* (non-Javadoc)
* @see org.eclipse.jet.internal.taglib.workspace.IWorkspaceAction#performAction(org.eclipse.core.runtime.IProgressMonitor)
*/
- public void performAction(IProgressMonitor monitor) throws CoreException
+ public void performAction(IProgressMonitor monitor) throws JET2TagException
{
+ ActionsUtil.createProject(project, monitor);
+
final String createMessage = MessageFormat.format(JET2Messages.WsProjectAction_CreatingProject, new Object []{ project.getName() });
- monitor.beginTask(createMessage, 2); // two ticks: create & open
- try
- {
- // create...
- if (!project.exists())
- {
- project.create(new SubProgressMonitor(monitor, 1));
- RuntimeLoggerContextExtender.log(context, createMessage, tagInfo, templatePath);
- }
- else
- {
- monitor.worked(1);
- }
- // open...
- if (!project.isOpen())
- {
- project.open(new SubProgressMonitor(monitor, 1));
- }
- else
- {
- monitor.worked(1);
- }
- }
- finally
- {
- monitor.done();
- }
+ RuntimeLoggerContextExtender.log(context, createMessage, getTagInfo(), getTemplatePath());
}
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/workspace/AbstractWorkspaceAction.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/workspace/AbstractWorkspaceAction.java
new file mode 100644
index 0000000..4edf06d
--- /dev/null
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/workspace/AbstractWorkspaceAction.java
@@ -0,0 +1,54 @@
+/**
+ * <copyright>
+ *
+ * Copyright (c) 2006 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Common Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/cpl-v10.html
+ *
+ * Contributors:
+ * IBM - Initial API and implementation
+ *
+ * </copyright>
+ *
+ * $Id$
+ */
+package org.eclipse.jet.taglib.workspace;
+
+import org.eclipse.jet.taglib.TagInfo;
+
+/**
+ * Abstract implementation of IWorkspaceAction that manages the standard
+ * execution tracing information.
+ */
+public abstract class AbstractWorkspaceAction implements IWorkspaceAction
+{
+
+ private final TagInfo tagInfo;
+ private final String templatePath;
+
+ protected AbstractWorkspaceAction(TagInfo tagInfo, String templatePath) {
+ this.tagInfo = tagInfo;
+ this.templatePath = templatePath;
+
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.taglib.workspace.IWorkspaceAction#getTagInfo()
+ */
+ public final TagInfo getTagInfo()
+ {
+ return tagInfo;
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.taglib.workspace.IWorkspaceAction#getTemplatePath()
+ */
+ public final String getTemplatePath()
+ {
+ return templatePath;
+ }
+
+
+}
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/workspace/ActionsUtil.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/workspace/ActionsUtil.java
new file mode 100644
index 0000000..7fb36ab
--- /dev/null
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/workspace/ActionsUtil.java
@@ -0,0 +1,429 @@
+/**
+ * <copyright>
+ *
+ * Copyright (c) 2006 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Common Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/cpl-v10.html
+ *
+ * Contributors:
+ * IBM - Initial API and implementation
+ *
+ * </copyright>
+ *
+ * $Id$
+ */
+package org.eclipse.jet.taglib.workspace;
+
+
+import java.io.BufferedInputStream;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.io.UnsupportedEncodingException;
+import java.net.URL;
+import java.text.MessageFormat;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IFolder;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.IWorkspace;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.SubProgressMonitor;
+import org.eclipse.jet.JET2Writer;
+import org.eclipse.jet.internal.l10n.JET2Messages;
+import org.eclipse.jet.taglib.JET2TagException;
+
+
+/**
+ * Utility class for workspace actions. Provides implementations of typical workspace operations needed by
+ * Workspace actions.
+ * <p>
+ * This class is not intended to be instantiated or subclassed by clients.
+ * </p>
+ */
+public final class ActionsUtil
+{
+
+ /**
+ *
+ */
+ private ActionsUtil()
+ {
+ super();
+ }
+
+ /**
+ * Write the file with the specified options from a JET writer.
+ * <p>
+ * This method delegates to {@link #writeTextFile(IFile, boolean, String, boolean, String, IProgressMonitor) writeTextFile}
+ * after finalizing the writer's content via {@link JET2Writer#finalizeContent(Object)}.
+ * If the content is written, then the writer is informed of this after the write via {@link JET2Writer#contentCommitted(Object)}.
+ * </p>
+ * @param file the file to write
+ * @param replace <code>true</code> if existing verions of the file should be overwritten.
+ * @param encoding the file encoding or <code>null</code> for the default encoding.
+ * @param derived <code>true</code> if the file is derived (and will be ignored by the team environment.
+ * @param writer the writer containing the file's content.
+ * @param monitor a progress monitor
+ * @return <code>true</code> if the file was written, <code>false</code> if <code>replace</code> is <code>true</code>
+ * and the file already exists..
+ * @throws JET2TagException if the file cannot be created or written, or the encoding is invalid.
+ * @see JET2Writer#finalizeContent(Object)
+ * @see JET2Writer#contentCommitted(Object)
+ */
+ public static boolean writeTextFileFromWriter(IFile file, boolean replace, String encoding, boolean derived, JET2Writer writer, IProgressMonitor monitor) throws JET2TagException
+ {
+ writer.finalizeContent(file);
+
+ boolean written = writeTextFile(file, replace, encoding, derived, writer.toString(), monitor);
+
+ if (written)
+ {
+ writer.contentCommitted(file);
+ }
+
+ return written;
+ }
+
+ /**
+ * Write the specified text file from a string.
+ * @param file the file to write
+ * @param replace <code>true</code> if existing verions of the file should be overwritten.
+ * @param encoding the file encoding or <code>null</code> for the default encoding.
+ * @param derived <code>true</code> if the file is derived (and will be ignored by the team environment.
+ * @param contents the contents to write.
+ * @param monitor a progress monitor
+ * @return <code>false</code> <code>replace</code> was <code>true</code> and the file existed; <code>true</code> otherwise.
+ * @throws JET2TagException if the file cannot be created or written, or the encoding is invalid.
+ */
+ public static boolean writeTextFile(IFile file, boolean replace, String encoding, boolean derived, final String contents, IProgressMonitor monitor) throws JET2TagException
+ {
+ try
+ {
+ final String fileMessage = MessageFormat.format(
+ JET2Messages.WsFileFromWriterAction_WritingFile,
+ new Object []{ file.getFullPath().toString() });
+
+ monitor.beginTask(fileMessage, 5);
+
+ if (file.exists() && !replace)
+ {
+ return false;
+ }
+
+ // Step 1: set the encoding...
+ if (encoding != null)
+ {
+ if (!file.exists())
+ {
+ // can't set encoding until the file exists, create the file with null-contents
+ // now, and set it's contents later.
+ file.create(new ByteArrayInputStream(new byte [0]), false, new SubProgressMonitor(monitor, 1));
+ }
+ else
+ {
+ monitor.worked(1);
+ }
+ file.setCharset(encoding.length() > 0 ? encoding : null, new SubProgressMonitor(monitor, 1));
+ }
+ else
+ {
+ monitor.worked(2);
+ }
+
+ // Step 2: convert the contents to a byte stream of the proper type
+ ByteArrayInputStream source = new ByteArrayInputStream(contents.getBytes(file.getCharset()));
+ monitor.worked(1);
+
+ // Step 3: ensure containing folder exists...
+ if (file.getParent() instanceof IFolder)
+ {
+ ensureFolderExists((IFolder)file.getParent(), new SubProgressMonitor(monitor, 1));
+ }
+ else
+ {
+ monitor.worked(1);
+ }
+
+ // Step 4: write the contents
+ if (file.exists())
+ {
+ file.setContents(source, false, true, new SubProgressMonitor(monitor, 2));
+ }
+ else
+ {
+ file.create(source, false, new SubProgressMonitor(monitor, 1));
+ }
+ // RuntimeLoggerContextExtender.log(context, fileMessage, tagInfo, templatePath);
+ // Step 5: Apply generated file properties...
+ file.setDerived(derived);
+
+ return true;
+ }
+ catch (UnsupportedEncodingException e)
+ {
+ throw new JET2TagException(e);
+ }
+ catch (CoreException e)
+ {
+ throw new JET2TagException(e);
+ }
+ finally
+ {
+ monitor.done();
+ }
+ }
+
+ /**
+ * Ensure that a folder exists by creating it and any of its parents if necessary.
+ * @param folder the folder to ensure exists.
+ * @param monitor a progress montiro
+ * @throws JET2TagException if the folder cannot be created
+ */
+ public static void ensureFolderExists(IFolder folder, IProgressMonitor monitor) throws JET2TagException
+ {
+ IProject project = folder.getProject();
+ IPath relPath = folder.getProjectRelativePath();
+ monitor.beginTask(
+ MessageFormat.format(JET2Messages.WsFolderAction_CreatingFolder, new Object []{ folder.getFullPath().toString() }),
+ relPath.segmentCount());
+ for (int i = 1; i <= relPath.segmentCount(); i++)
+ {
+ IPath subPath = relPath.uptoSegment(i);
+ IFolder iFolder = project.getFolder(subPath);
+ if (!iFolder.exists())
+ {
+ try
+ {
+ iFolder.create(false, true, new SubProgressMonitor(monitor, 1));
+ }
+ catch (CoreException e)
+ {
+ throw new JET2TagException(e);
+ }
+ }
+ else
+ {
+ monitor.worked(1);
+ }
+ }
+ }
+
+ /**
+ * Write a binary file from a byte buffer.
+ * @param file the file to write
+ * @param replace <code>true</code> if existing versions of the file should be overwritten.
+ * @param contents the contents to write.
+ * @param monitor a progress monitor
+ * @return <code>true</code> if the file was written, <code>false</code> if <code>replace</code> was <code>true</code> and the
+ * file already exists.
+ * @throws JET2TagException if the file cannot be created or updated.
+ */
+ public static boolean writeBinaryFile(IFile file, boolean replace, byte[] contents, IProgressMonitor monitor) throws JET2TagException
+ {
+ ActionsUtil.writeBinaryFile(file, replace, contents, monitor);
+ try
+ {
+ final String fileMessage = MessageFormat.format(
+ JET2Messages.WsCopyBinaryFileAction_WritingFile,
+ new Object []{ file.getFullPath().toString() });
+ monitor.beginTask(fileMessage, 2);
+
+ if (file.exists() && !replace)
+ {
+ // TODO file exists and not replace. consider marking the file as non-replacable
+ return false;
+ }
+
+ final InputStream input = new ByteArrayInputStream(contents);
+
+ // Step 1: ensure containing folder exists...
+ if (file.getParent() instanceof IFolder)
+ {
+ ensureFolderExists((IFolder)file.getParent(), new SubProgressMonitor(monitor, 1));
+ }
+ else
+ {
+ monitor.worked(1);
+ }
+
+ // Step 2: write the file
+ if (file.exists() && replace)
+ {
+ file.setContents(input, false, true, new SubProgressMonitor(monitor, 1));
+ }
+ else if (!file.exists())
+ {
+ file.create(input, false, new SubProgressMonitor(monitor, 1));
+ }
+// RuntimeLoggerContextExtender.log(context, fileMessage, tagInfo, templatePath);
+ return true;
+ }
+ catch (CoreException e)
+ {
+ throw new JET2TagException(e);
+ }
+ finally
+ {
+ monitor.done();
+ }
+ }
+
+
+ /**
+ * Return the contents of the text file at the specified URL.
+ * @return the file contents
+ * @throws JET2TagException if an IOException is thrown (the IOException is wrapped).
+ */
+ public static String readTextFile(URL url, String encoding) throws JET2TagException
+ {
+ try
+ {
+ final InputStream input = url.openStream();
+ StringBuffer inputContents = new StringBuffer();
+ Reader reader;
+ if (encoding != null)
+ {
+ reader = new InputStreamReader(input, encoding);
+ }
+ else
+ {
+ reader = new InputStreamReader(input);
+ }
+
+ char readChars[] = new char [1024];
+ for (int read = reader.read(readChars); read != -1; read = reader.read(readChars))
+ {
+ inputContents.append(readChars, 0, read);
+ }
+ reader.close();
+ input.close();
+ return inputContents.toString();
+ }
+ catch (IOException e)
+ {
+ throw new JET2TagException(e);
+ }
+ }
+
+ /**
+ * Read a binary file from the given URL.
+ * @param url the file URL
+ * @return a byte buffer containing the file's contents
+ * @throws JET2TagException if an IOException occurs (the exception is wrapped).
+ */
+ public static byte[] readBinaryFile(URL url) throws JET2TagException
+ {
+ InputStream input = null;
+ ByteArrayOutputStream output = null;
+ try
+ {
+ input = new BufferedInputStream(url.openStream());
+ output = new ByteArrayOutputStream();
+ byte buffer[] = new byte [1024];
+ for (int read = input.read(buffer); read != -1; read = input.read(buffer))
+ {
+ output.write(buffer, 0, read);
+ }
+ return output.toByteArray();
+ }
+ catch (IOException e)
+ {
+ throw new JET2TagException(e);
+ }
+ finally
+ {
+ if (input != null)
+ {
+ try
+ {
+ input.close();
+ }
+ catch (IOException e)
+ {
+ // can't do anything, cascading from a finally block is bad, so just suppress it.
+ }
+ }
+ if (output != null)
+ {
+ try
+ {
+ output.close();
+ }
+ catch (IOException e)
+ {
+ // can't do anything, cascading from a finally block is bad, so just suppress it.
+ }
+ }
+ }
+
+ }
+
+ /**
+ * Create a new project.
+ * @param project
+ * @param monitor
+ * @throws JET2TagException
+ */
+ public static void createProject(IProject project, IProgressMonitor monitor) throws JET2TagException
+ {
+ final String createMessage = MessageFormat.format(JET2Messages.WsProjectAction_CreatingProject, new Object []{ project.getName() });
+ monitor.beginTask(createMessage, 2); // two ticks: create & open
+
+ try
+ {
+ // create...
+ if (!project.exists())
+ {
+ project.create(new SubProgressMonitor(monitor, 1));
+ }
+ else
+ {
+ monitor.worked(1);
+ }
+ // open...
+ if (!project.isOpen())
+ {
+ project.open(new SubProgressMonitor(monitor, 1));
+ }
+ else
+ {
+ monitor.worked(1);
+ }
+ }
+ catch (CoreException e)
+ {
+ throw new JET2TagException(e);
+ }
+ finally
+ {
+ monitor.done();
+ }
+
+ }
+
+ /**
+ * Force the workspace to broadcast accumulated workspace change events, and optionally do a build.
+ * This method is useful if a pending workspace action depends on prior actions being processed
+ * by other actors. For example, if a Project and corresponding .project file are created that describe
+ * the project as having the Java nature, then JDT must be informed of these changes before JDT APIs will
+ * see the new project properly.
+ * <p>
+ * Generally, workspace actions authors will not need this tag.
+ * </p>
+ * @param build <code>true</code> if a workspace build should be performed after the event broadcast.
+ * @see IWorkspace#checkpoint(boolean)
+ */
+ public static void checkpointWorkspace(boolean build)
+ {
+ ResourcesPlugin.getWorkspace().checkpoint(build);
+ }
+}
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/workspace/IWorkspaceAction.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/workspace/IWorkspaceAction.java
index e781c09..2d12304 100644
--- a/plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/workspace/IWorkspaceAction.java
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/workspace/IWorkspaceAction.java
@@ -19,7 +19,6 @@ package org.eclipse.jet.taglib.workspace;
import org.eclipse.core.resources.IResource;
-import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jet.taglib.JET2TagException;
import org.eclipse.jet.taglib.TagInfo;
@@ -34,9 +33,11 @@ public interface IWorkspaceAction
/**
* Returns the workspace resource with which the action is associated.
+ * This method is only called if {@link #requiresValidateEdit()} returns <code>true</code>.
* @return an non-null Eclipse resource handle
+ * @throws JET2TagException if the resource cannot be created
*/
- public abstract IResource getResource();
+ public abstract IResource getResource() throws JET2TagException;
/**
* Returns the Tag information of the tag that originated this action.
@@ -56,15 +57,16 @@ public interface IWorkspaceAction
* If <code>true</code> is returned, then {@link #getResource()} must return an instance
* of {@link org.eclipse.core.resources.IFile}.
* @return <code>true</code> if the resource must participate in validateEdit.
+ * @throws JET2TagException if the result cannot calculated because of some error
* @see org.eclipse.core.resources.IWorkspace#validateEdit(org.eclipse.core.resources.IFile[], java.lang.Object)
*/
- public abstract boolean requiresValidateEdit();
+ public abstract boolean requiresValidateEdit() throws JET2TagException;
/**
* Perform the workspace action
* @param monitor the progress monitor to which progress information will be written.
* @throws JET2TagException if the action cannot be successfully performed.
*/
- public abstract void performAction(IProgressMonitor monitor) throws CoreException;
+ public abstract void performAction(IProgressMonitor monitor) throws JET2TagException;
}
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/workspace/WorkspaceContextExtender.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/workspace/WorkspaceContextExtender.java
index f22fcb9..b02fda0 100644
--- a/plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/workspace/WorkspaceContextExtender.java
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/workspace/WorkspaceContextExtender.java
@@ -29,7 +29,9 @@ import org.eclipse.core.resources.IWorkspaceRunnable;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.ISafeRunnable;
import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.jet.AbstractContextExtender;
import org.eclipse.jet.JET2Context;
@@ -66,14 +68,24 @@ public class WorkspaceContextExtender extends AbstractContextExtender
try
{
monitor.subTask(JET2Messages.WorkspaceContextExtender_ConfirmingTeamAccess);
- List filesToValidateEdit = new ArrayList(workspaceActions.size());
+ final List filesToValidateEdit = new ArrayList(workspaceActions.size());
for (Iterator i = workspaceActions.iterator(); i.hasNext();)
{
- IWorkspaceAction action = (IWorkspaceAction)i.next();
- if (action.requiresValidateEdit())
- {
- filesToValidateEdit.add(action.getResource());
- }
+ final IWorkspaceAction action = (IWorkspaceAction)i.next();
+ Platform.run(new ISafeRunnable() {
+
+ public void handleException(Throwable exception)
+ {
+ context.logError(action.getTemplatePath(), action.getTagInfo(), null, exception);
+ }
+
+ public void run() throws Exception
+ {
+ if (action.requiresValidateEdit())
+ {
+ filesToValidateEdit.add(action.getResource());
+ }
+ }});
}
IStatus validateOk = ResourcesPlugin.getWorkspace().validateEdit(
(IFile[])filesToValidateEdit.toArray(new IFile [filesToValidateEdit.size()]),
@@ -89,24 +101,27 @@ public class WorkspaceContextExtender extends AbstractContextExtender
ResourcesPlugin.getWorkspace().run(new IWorkspaceRunnable()
{
- public void run(IProgressMonitor submon) throws CoreException
+ public void run(final IProgressMonitor submon) throws CoreException
{
submon.beginTask(null, workspaceActions.size());
try
{
for (Iterator i = workspaceActions.iterator(); i.hasNext();)
{
- IWorkspaceAction action = (IWorkspaceAction)i.next();
+ final IWorkspaceAction action = (IWorkspaceAction)i.next();
submon.subTask(JET2Messages.WorkspaceContextExtender_Writing);
- try
- {
- action.performAction(new SubProgressMonitor(submon, 1));
- }
- catch (CoreException e)
- {
- context.logError(action.getTemplatePath(), action.getTagInfo(), null, e);
- }
+ Platform.run(new ISafeRunnable() {
+
+ public void handleException(Throwable exception)
+ {
+ context.logError(action.getTemplatePath(), action.getTagInfo(), null, exception);
+ }
+
+ public void run() throws Exception
+ {
+ action.performAction(new SubProgressMonitor(submon, 1));
+ }});
submon.worked(1);
}
}
@@ -199,4 +214,14 @@ public class WorkspaceContextExtender extends AbstractContextExtender
return (IContainer)getData().containerStack.peek();
}
+ /**
+ * Return the workspace context extender for the given JET context.
+ * @param context the JET context.
+ * @return the Workspace Context extender
+ */
+ public static WorkspaceContextExtender getInstance(JET2Context context)
+ {
+ return new WorkspaceContextExtender(context);
+ }
+
}
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/workspace/WsCopyBinaryFileAction.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/workspace/WsCopyBinaryFileAction.java
deleted file mode 100644
index f4b0559..0000000
--- a/plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/workspace/WsCopyBinaryFileAction.java
+++ /dev/null
@@ -1,198 +0,0 @@
-/**
- * <copyright>
- *
- * Copyright (c) 2006 IBM Corporation 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:
- * IBM - Initial API and implementation
- *
- * </copyright>
- *
- * $Id$
- */
-
-package org.eclipse.jet.taglib.workspace;
-
-
-import java.io.BufferedInputStream;
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.net.URL;
-import java.text.MessageFormat;
-
-import org.eclipse.core.resources.IFile;
-import org.eclipse.core.resources.IFolder;
-import org.eclipse.core.resources.IResource;
-import org.eclipse.core.runtime.CoreException;
-import org.eclipse.core.runtime.IProgressMonitor;
-import org.eclipse.core.runtime.SubProgressMonitor;
-import org.eclipse.jet.JET2Context;
-import org.eclipse.jet.internal.l10n.JET2Messages;
-import org.eclipse.jet.internal.runtime.RuntimeLoggerContextExtender;
-import org.eclipse.jet.taglib.TagInfo;
-
-
-/**
- * Copy a binary file from a source URL to a target workspace file.
- *
- */
-public class WsCopyBinaryFileAction implements IWorkspaceAction
-{
-
- private final IFile file;
-
- private final TagInfo tagInfo;
-
- private final String templatePath;
-
- private final boolean replace;
-
- private final byte[] contents;
-
- private final JET2Context context;
-
- public WsCopyBinaryFileAction(
- JET2Context context,
- String templatePath,
- TagInfo tagInfo,
- byte[] contents,
- IFile targetFile,
- boolean replaceExisting)
- {
- this.context = context;
- this.templatePath = templatePath;
- this.tagInfo = tagInfo;
- this.contents = contents;
- this.file = targetFile;
- this.replace = replaceExisting;
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.jet.internal.taglib.workspace.IWorkspaceAction#getResource()
- */
- public IResource getResource()
- {
- return file;
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.jet.internal.taglib.workspace.IWorkspaceAction#getTagInfo()
- */
- public TagInfo getTagInfo()
- {
- return tagInfo;
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.jet.internal.taglib.workspace.IWorkspaceAction#getTemplatePath()
- */
- public String getTemplatePath()
- {
- return templatePath;
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.jet.internal.taglib.workspace.IWorkspaceAction#requiresValidateEdit()
- */
- public boolean requiresValidateEdit()
- {
- return replace && file.exists() && file.isReadOnly() /* && content has changed */;
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.jet.internal.taglib.workspace.IWorkspaceAction#performAction(org.eclipse.core.runtime.IProgressMonitor)
- */
- public void performAction(IProgressMonitor monitor) throws CoreException
- {
- try
- {
- final String fileMessage = MessageFormat.format(
- JET2Messages.WsCopyBinaryFileAction_WritingFile,
- new Object []{ file.getFullPath().toString() });
- monitor.beginTask(fileMessage, 2);
-
- if (file.exists() && !replace)
- {
- // TODO file exists and not replace. consider marking the file as non-replacable
- return;
- }
-
- final InputStream input = new ByteArrayInputStream(contents);
-
- // Step 1: ensure containing folder exists...
- if (file.getParent() instanceof IFolder)
- {
- WsFolderAction.ensureFolderExists((IFolder)file.getParent(), new SubProgressMonitor(monitor, 1));
- }
- else
- {
- monitor.worked(1);
- }
-
- // Step 2: write the file
- if (file.exists() && replace)
- {
- file.setContents(input, false, true, new SubProgressMonitor(monitor, 1));
- }
- else if (!file.exists())
- {
- file.create(input, false, new SubProgressMonitor(monitor, 1));
- }
- RuntimeLoggerContextExtender.log(context, fileMessage, tagInfo, templatePath);
- }
- finally
- {
- monitor.done();
- }
-
- }
-
- public static byte[] readBinaryFile(URL url) throws IOException
- {
- InputStream input = null;
- ByteArrayOutputStream output = null;
- try
- {
- input = new BufferedInputStream(url.openStream());
- output = new ByteArrayOutputStream();
- byte buffer[] = new byte [1024];
- for (int read = input.read(buffer); read != -1; read = input.read(buffer))
- {
- output.write(buffer, 0, read);
- }
- return output.toByteArray();
- }
- finally
- {
- if (input != null)
- {
- try
- {
- input.close();
- }
- catch (IOException e)
- {
- // can't do anything, cascading from a finally block is bad, so just suppress it.
- }
- }
- if (output != null)
- {
- try
- {
- output.close();
- }
- catch (IOException e)
- {
- // can't do anything, cascading from a finally block is bad, so just suppress it.
- }
- }
- }
-
- }
-}
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/workspace/WsCopyTextFileAction.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/workspace/WsCopyTextFileAction.java
deleted file mode 100644
index 0f2d88c..0000000
--- a/plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/workspace/WsCopyTextFileAction.java
+++ /dev/null
@@ -1,229 +0,0 @@
-/**
- * <copyright>
- *
- * Copyright (c) 2006 IBM Corporation 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:
- * IBM - Initial API and implementation
- *
- * </copyright>
- *
- * $Id$
- */
-
-package org.eclipse.jet.taglib.workspace;
-
-
-import java.io.ByteArrayInputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.io.Reader;
-import java.io.UnsupportedEncodingException;
-import java.net.URL;
-import java.text.MessageFormat;
-
-import org.eclipse.core.resources.IFile;
-import org.eclipse.core.resources.IFolder;
-import org.eclipse.core.resources.IResource;
-import org.eclipse.core.runtime.CoreException;
-import org.eclipse.core.runtime.IProgressMonitor;
-import org.eclipse.core.runtime.IStatus;
-import org.eclipse.core.runtime.SubProgressMonitor;
-import org.eclipse.jet.JET2Context;
-import org.eclipse.jet.internal.InternalJET2Platform;
-import org.eclipse.jet.internal.l10n.JET2Messages;
-import org.eclipse.jet.internal.runtime.RuntimeLoggerContextExtender;
-import org.eclipse.jet.taglib.TagInfo;
-
-
-/**
- * Copy a binary file from a source URL to a target workspace file.
- *
- */
-public class WsCopyTextFileAction implements IWorkspaceAction
-{
-
- private final String templatePath;
-
- private final TagInfo tagInfo;
-
- private final IFile file;
-
- private final boolean replace;
-
- private final String targetEncoding;
-
- private final String sourceFileContents;
-
- private final JET2Context context;
-
- public WsCopyTextFileAction(
- JET2Context context,
- String templatePath,
- TagInfo td,
- String sourceFileContents,
- IFile file,
- boolean replaceExisting,
- String targetEncoding)
- {
- this.context = context;
- this.templatePath = templatePath;
- this.tagInfo = td;
- this.sourceFileContents = sourceFileContents;
- this.file = file;
- this.replace = replaceExisting;
- this.targetEncoding = targetEncoding;
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.jet.internal.taglib.workspace.IWorkspaceAction#getResource()
- */
- public IResource getResource()
- {
- return file;
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.jet.internal.taglib.workspace.IWorkspaceAction#getTagInfo()
- */
- public TagInfo getTagInfo()
- {
- return tagInfo;
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.jet.internal.taglib.workspace.IWorkspaceAction#getTemplatePath()
- */
- public String getTemplatePath()
- {
- return templatePath;
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.jet.internal.taglib.workspace.IWorkspaceAction#requiresValidateEdit()
- */
- public boolean requiresValidateEdit()
- {
- return replace && file.exists() && file.isReadOnly() /* && content has changed */;
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.jet.internal.taglib.workspace.IWorkspaceAction#performAction(org.eclipse.core.runtime.IProgressMonitor)
- */
- public void performAction(IProgressMonitor monitor) throws CoreException
- {
- try
- {
- final String fileMessage = MessageFormat.format(
- JET2Messages.WsCopyTextFileAction_WritingFile,
- new Object []{ file.getFullPath().toString() });
- monitor.beginTask(fileMessage, 5);
-
- if (file.exists() && !replace)
- {
- // TODO file exists and not replace. consider marking the file as non-replacable
- return;
- }
-
- // Step 1: set the target encoding...
- if (targetEncoding != null)
- {
- if (!file.exists())
- {
- // can't set encoding until the file exists, create the file with null-contents
- // now, and set it's contents later.
- file.create(new ByteArrayInputStream(new byte [0]), false, new SubProgressMonitor(monitor, 1));
- }
- else
- {
- monitor.worked(1);
- }
- file.setCharset(targetEncoding.length() > 0 ? targetEncoding : null, new SubProgressMonitor(monitor, 1));
- }
- else
- {
- monitor.worked(2);
- }
-
- // Step 2: convert the contents to a byte stream of the proper type
- ByteArrayInputStream source = new ByteArrayInputStream(sourceFileContents.getBytes(file.getCharset()));
- monitor.worked(1);
-
- // Step 3: ensure containing folder exists...
- if (file.getParent() instanceof IFolder)
- {
- WsFolderAction.ensureFolderExists((IFolder)file.getParent(), new SubProgressMonitor(monitor, 1));
- }
- else
- {
- monitor.worked(1);
- }
-
- // Step 4: write the contents
- if (file.exists())
- {
- file.setContents(source, false, true, new SubProgressMonitor(monitor, 1));
- }
- else
- {
- file.create(source, false, new SubProgressMonitor(monitor, 1));
- }
- RuntimeLoggerContextExtender.log(context, fileMessage, tagInfo, templatePath);
- }
- catch (IOException e)
- {
- throw new CoreException(InternalJET2Platform.newStatus(IStatus.ERROR, null, e));
- }
- finally
- {
- monitor.done();
- }
- }
-
- /**
- * Return the contents of the text file at the specified URL.
- * @return the file contents
- * @throws IOException
- * @throws UnsupportedEncodingException
- */
- public static String readTextFile(URL url, String encoding) throws CoreException
- {
- try
- {
- final InputStream input = url.openStream();
- StringBuffer inputContents = new StringBuffer();
- Reader reader;
- if (encoding != null)
- {
- reader = new InputStreamReader(input, encoding);
- }
- else
- {
- reader = new InputStreamReader(input);
- }
-
- char readChars[] = new char [1024];
- for (int read = reader.read(readChars); read != -1; read = reader.read(readChars))
- {
- inputContents.append(readChars, 0, read);
- }
- reader.close();
- input.close();
- return inputContents.toString();
- }
- catch (UnsupportedEncodingException e)
- {
- throw new CoreException(InternalJET2Platform.newStatus(IStatus.ERROR, null, e));
- }
- catch (IOException e)
- {
- throw new CoreException(InternalJET2Platform.newStatus(IStatus.ERROR, null, e));
- }
- }
-
-}
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/workspace/WsFileFromWriterAction.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/workspace/WsFileFromWriterAction.java
deleted file mode 100644
index 808f402..0000000
--- a/plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/workspace/WsFileFromWriterAction.java
+++ /dev/null
@@ -1,194 +0,0 @@
-/**
- * <copyright>
- *
- * Copyright (c) 2005 IBM Corporation 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:
- * IBM - Initial API and implementation
- *
- * </copyright>
- *
- * $Id$
- */
-
-package org.eclipse.jet.taglib.workspace;
-
-
-import java.io.ByteArrayInputStream;
-import java.io.UnsupportedEncodingException;
-import java.text.MessageFormat;
-
-import org.eclipse.core.resources.IFile;
-import org.eclipse.core.resources.IFolder;
-import org.eclipse.core.resources.IResource;
-import org.eclipse.core.runtime.CoreException;
-import org.eclipse.core.runtime.IProgressMonitor;
-import org.eclipse.core.runtime.IStatus;
-import org.eclipse.core.runtime.SubProgressMonitor;
-import org.eclipse.jet.JET2Context;
-import org.eclipse.jet.JET2Writer;
-import org.eclipse.jet.internal.InternalJET2Platform;
-import org.eclipse.jet.internal.l10n.JET2Messages;
-import org.eclipse.jet.internal.runtime.RuntimeLoggerContextExtender;
-import org.eclipse.jet.taglib.TagInfo;
-
-
-/**
- * Create or update a text file with the contents of a writer..
- *
- */
-public class WsFileFromWriterAction implements IWorkspaceAction
-{
-
- private final String templatePath;
-
- private final TagInfo tagInfo;
-
- private final IFile file;
-
- private final JET2Writer writer;
-
- private String encoding;
-
- private final boolean replace;
-
- private final boolean derived;
-
- private final JET2Context context;
-
- /**
- *
- * @param context
- * @param templatePath
- * @param tagInfo
- * @param file
- * @param writer
- * @param replace
- * @param derived
- */
- public WsFileFromWriterAction(
- JET2Context context,
- String templatePath,
- TagInfo tagInfo,
- IFile file,
- JET2Writer writer,
- boolean replace,
- boolean derived)
- {
- super();
- this.context = context;
- this.templatePath = templatePath;
- this.tagInfo = tagInfo;
- this.file = file;
- this.writer = writer;
- this.replace = replace;
- this.derived = derived;
- }
-
- public IResource getResource()
- {
- return file;
- }
-
- public TagInfo getTagInfo()
- {
- return tagInfo;
- }
-
- public String getTemplatePath()
- {
- return templatePath;
- }
-
- public boolean requiresValidateEdit()
- {
- return replace && file.exists() && file.isReadOnly() /* && content has changed */;
- }
-
- public void performAction(IProgressMonitor monitor) throws CoreException
- {
- try
- {
- final String fileMessage = MessageFormat.format(
- JET2Messages.WsFileFromWriterAction_WritingFile,
- new Object []{ file.getFullPath().toString() });
- monitor.beginTask(fileMessage, 6);
-
- if (file.exists() && !replace)
- {
- // TODO file exists and not replace. consider marking the file as non-replacable
- // System.err.println("Did not update file: " + file.getFullPath().toString());
- return;
- }
-
- // Step 1: set the encoding...
- if (encoding != null)
- {
- if (!file.exists())
- {
- // can't set encoding until the file exists, create the file with null-contents
- // now, and set it's contents later.
- file.create(new ByteArrayInputStream(new byte [0]), false, new SubProgressMonitor(monitor, 1));
- }
- else
- {
- monitor.worked(1);
- }
- file.setCharset(encoding.length() > 0 ? encoding : null, new SubProgressMonitor(monitor, 1));
- }
- else
- {
- monitor.worked(1);
- }
-
- // Step 2: convert the contents to a byte stream of the proper type
- ByteArrayInputStream source = new ByteArrayInputStream(writer.toString().getBytes(file.getCharset()));
- monitor.worked(1);
-
- // Step 3: ensure containing folder exists...
- if (file.getParent() instanceof IFolder)
- {
- WsFolderAction.ensureFolderExists((IFolder)file.getParent(), new SubProgressMonitor(monitor, 1));
- }
- else
- {
- monitor.worked(1);
- }
-
- // Step 4: write the contents
- if (file.exists())
- {
- file.setContents(source, false, true, new SubProgressMonitor(monitor, 2));
- }
- else
- {
- file.create(source, false, new SubProgressMonitor(monitor, 1));
- }
- RuntimeLoggerContextExtender.log(context, fileMessage, tagInfo, templatePath);
- // Step 5: Apply generated file properties...
- file.setDerived(derived);
-
- // Step 6: notify writer of committed content...
- writer.contentCommitted(file); // TODO Shouldn't this take a progress monitor?
-
- }
- catch (UnsupportedEncodingException e)
- {
- throw new CoreException(InternalJET2Platform.newStatus(IStatus.ERROR, null, e));
- }
- finally
- {
- monitor.done();
- }
- }
-
- public void setEncoding(String encoding)
- {
- this.encoding = encoding;
- }
-
-}
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/workspace/WsFolderAction.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/workspace/WsFolderAction.java
deleted file mode 100644
index 4c4fda9..0000000
--- a/plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/workspace/WsFolderAction.java
+++ /dev/null
@@ -1,133 +0,0 @@
-/**
- * <copyright>
- *
- * Copyright (c) 2005 IBM Corporation 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:
- * IBM - Initial API and implementation
- *
- * </copyright>
- *
- * $Id$
- */
-
-package org.eclipse.jet.taglib.workspace;
-
-
-import java.text.MessageFormat;
-
-import org.eclipse.core.resources.IFolder;
-import org.eclipse.core.resources.IProject;
-import org.eclipse.core.resources.IResource;
-import org.eclipse.core.runtime.CoreException;
-import org.eclipse.core.runtime.IPath;
-import org.eclipse.core.runtime.IProgressMonitor;
-import org.eclipse.core.runtime.SubProgressMonitor;
-import org.eclipse.jet.JET2Context;
-import org.eclipse.jet.internal.l10n.JET2Messages;
-import org.eclipse.jet.internal.runtime.RuntimeLoggerContextExtender;
-import org.eclipse.jet.taglib.TagInfo;
-
-
-/**
- * Represent a Folder action. A folder action will:
- * <bl>
- * <li>create a folder (and any uncreated parent folders), if it does not exist</li>
- * </bl>
- *
- */
-public class WsFolderAction implements IWorkspaceAction
-{
-
- private final String templatePath;
-
- private final TagInfo tagInfo;
-
- private final IFolder folder;
-
- private final JET2Context context;
-
- /**
- *
- * @param context
- * @param templatePath
- * @param tagInfo
- * @param folder
- */
- public WsFolderAction(JET2Context context, String templatePath, TagInfo tagInfo, IFolder folder)
- {
- super();
- this.context = context;
- this.templatePath = templatePath;
- this.tagInfo = tagInfo;
- this.folder = folder;
- }
-
- public IResource getResource()
- {
- return folder;
- }
-
- public TagInfo getTagInfo()
- {
- return tagInfo;
- }
-
- public String getTemplatePath()
- {
- return templatePath;
- }
-
- public boolean requiresValidateEdit()
- {
- return false;
- }
-
- public void performAction(IProgressMonitor monitor) throws CoreException
- {
- try
- {
- ensureFolderExists(folder, monitor);
- RuntimeLoggerContextExtender.log(context, MessageFormat.format(
- JET2Messages.WsFolderAction_CreatingFolder,
- new Object []{ folder.getFullPath().toString() }), tagInfo, templatePath);
- }
- finally
- {
- monitor.done();
- }
-
- }
-
- /**
- * @param folder
- * @param monitor
- * @throws CoreException
- */
- public static void ensureFolderExists(IFolder folder, IProgressMonitor monitor) throws CoreException
- {
- IProject project = folder.getProject();
- IPath relPath = folder.getProjectRelativePath();
- monitor.beginTask(
- MessageFormat.format(JET2Messages.WsFolderAction_CreatingFolder, new Object []{ folder.getFullPath().toString() }),
- relPath.segmentCount());
- for (int i = 1; i <= relPath.segmentCount(); i++)
- {
- IPath subPath = relPath.uptoSegment(i);
- IFolder iFolder = project.getFolder(subPath);
- if (!iFolder.exists())
- {
- iFolder.create(false, true, new SubProgressMonitor(monitor, 1));
- }
- else
- {
- monitor.worked(1);
- }
- }
- }
-
-}
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/transform/TransformContextExtender.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/transform/TransformContextExtender.java
index 35b11aa..0c4dafe 100644
--- a/plugins/org.eclipse.jet/src/org/eclipse/jet/transform/TransformContextExtender.java
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/transform/TransformContextExtender.java
@@ -305,4 +305,14 @@ public class TransformContextExtender extends AbstractContextExtender
return baseURL;
}
+ /**
+ * Return the TransformContextExtender for the passed context.
+ * @param context a JET context
+ * @return the TransformContextExtender
+ */
+ public static TransformContextExtender getInstance(JET2Context context)
+ {
+ return new TransformContextExtender(context);
+ }
+
}