Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpelder2008-04-02 11:31:14 -0400
committerpelder2008-04-02 11:31:14 -0400
commit5c461635fd958549cf836f18cac09620d64218ef (patch)
tree6034388ca38b27783058a93d8d8441b3605b9e13
parent5dbc639b904884dd88c04b724ce6471f69651e15 (diff)
downloadorg.eclipse.jet-5c461635fd958549cf836f18cac09620d64218ef.tar.gz
org.eclipse.jet-5c461635fd958549cf836f18cac09620d64218ef.tar.xz
org.eclipse.jet-5c461635fd958549cf836f18cac09620d64218ef.zip
[211517] Performance: Improve performance of template result writing
-rw-r--r--plugins/org.eclipse.jet.core/src/org/eclipse/jet/IWriterListenerExtension.java41
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/l10n/JET2Messages.properties6
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/java/JavaFileAction.java23
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/java/JavaPackageAction.java22
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/java/MergeTag.java74
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/workspace/CopyFileTag.java66
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/workspace/FileTag.java49
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/workspace/FolderTag.java8
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/workspace/ProjectTag.java8
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/workspace/WsCopyBinaryFileAction.java26
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/workspace/WsCopyTextFileAction.java26
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/workspace/WsFileFromWriterAction.java37
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/workspace/WsFolderAction.java26
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/workspace/WsProjectAction.java26
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/UserRegionHelper.java137
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/java/JavaActionsUtil.java3
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/workspace/AbstractWorkspaceAction2.java62
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/workspace/ActionsUtil.java138
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/workspace/IWorkspaceAction.java18
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/workspace/IWorkspaceAction2.java49
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/workspace/IWorkspaceActionExtension.java36
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/workspace/WorkspaceContextExtender.java190
22 files changed, 757 insertions, 314 deletions
diff --git a/plugins/org.eclipse.jet.core/src/org/eclipse/jet/IWriterListenerExtension.java b/plugins/org.eclipse.jet.core/src/org/eclipse/jet/IWriterListenerExtension.java
new file mode 100644
index 0000000..9e652b6
--- /dev/null
+++ b/plugins/org.eclipse.jet.core/src/org/eclipse/jet/IWriterListenerExtension.java
@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) 2008 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 Corporation - initial API and implementation
+ */
+package org.eclipse.jet;
+
+import org.eclipse.jet.taglib.JET2TagException;
+
+/**
+ * Extension interface to {@link IWriterListener} allowing finalizeContent
+ * handlers to receive existing file contents
+ * @since 0.9
+ */
+public interface IWriterListenerExtension {
+
+ /**
+ * Perform any finalization of the content in the writer. If the file represented by
+ * fileObject does not exist, then {@link IWriterListener#finalizeContent(JET2Writer, Object)}
+ * is called.
+ *
+ * @param writer
+ * the writer in the process of being finalized
+ * @param file
+ * a handle to object to which the content will ultimately be
+ * written. The standard JET2 Workspace tags pass an
+ * org.eclipse.core.resources.IFile, but other tags may pass
+ * objects of other types.
+ * @param existingContent the existing file content
+ * @throws JET2TagException
+ * if the method cannot complete normally
+ */
+ void finalizeContent(JET2Writer writer, Object fileObject,
+ String existingContent) throws JET2TagException;
+
+}
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/l10n/JET2Messages.properties b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/l10n/JET2Messages.properties
index 3e0f5b5..653de2d 100644
--- a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/l10n/JET2Messages.properties
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/l10n/JET2Messages.properties
@@ -1,5 +1,5 @@
###############################################################################
-# Copyright (c) 2005, 2007 IBM Corporation and others.
+# Copyright (c) 2005, 2008 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
@@ -128,8 +128,8 @@ WorkspaceContextExtender_NoParent=No parent 'folder' or 'project' tag available
WsCopyBinaryFileAction_WritingFile=Writing file: {0}
WsFileFromWriterAction_WritingFile=Writing file: {0}
WsCopyTextFileAction_WritingFile=Writing file: {0}
-WsFolderAction_CreatingFolder=Workspace Folder {0}
-WsProjectAction_CreatingProject=Workspace Project: {0}
+WsFolderAction_CreatingFolder=Creating folder {0}
+WsProjectAction_CreatingProject=Creating project: {0}
XPathTokenScanner_UnterminatedStringLiteral=Unterminated literal String
DOMInspector_InconsistentElementName=New element's name must be the same as the source element.
DOMInspector_NotDOMObject=Source element is not a DOM object.
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/java/JavaFileAction.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/java/JavaFileAction.java
index 10819e5..e314f0c 100644
--- a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/java/JavaFileAction.java
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/java/JavaFileAction.java
@@ -1,7 +1,7 @@
/**
* <copyright>
*
- * Copyright (c) 2006 IBM Corporation and others.
+ * Copyright (c) 2006, 2008 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
@@ -21,17 +21,19 @@ import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jet.BodyContentWriter;
+import org.eclipse.jet.BufferedJET2Writer;
import org.eclipse.jet.taglib.JET2TagException;
import org.eclipse.jet.taglib.TagInfo;
import org.eclipse.jet.taglib.java.JavaActionsUtil;
-import org.eclipse.jet.taglib.workspace.AbstractWorkspaceAction;
+import org.eclipse.jet.taglib.workspace.AbstractWorkspaceAction2;
import org.eclipse.jet.taglib.workspace.ActionsUtil;
-import org.eclipse.jet.taglib.workspace.IWorkspaceAction;
+import org.eclipse.jet.taglib.workspace.IWorkspaceAction2;
+import org.eclipse.jet.taglib.workspace.IWorkspaceActionExtension;
/**
* Action for creating files in a Java context
*/
-public final class JavaFileAction extends AbstractWorkspaceAction implements IWorkspaceAction
+public final class JavaFileAction extends AbstractWorkspaceAction2 implements IWorkspaceAction2, IWorkspaceActionExtension
{
protected final IPath srcFolderPath;
@@ -78,11 +80,10 @@ public final class JavaFileAction extends AbstractWorkspaceAction implements IWo
/* (non-Javadoc)
* @see org.eclipse.jet.taglib.workspace.IWorkspaceAction#performAction(org.eclipse.core.runtime.IProgressMonitor)
*/
- public final void performAction(IProgressMonitor monitor) throws JET2TagException
+ public final boolean performActionIfRequired(IProgressMonitor monitor) throws JET2TagException
{
IFile targetFile = getUnderlyingFile();
-
- ActionsUtil.writeTextFileFromWriter(targetFile, replace, encoding, derived , content, monitor);
+ return ActionsUtil.writeTextFile(targetFile, replace, encoding, derived, content.getContent(), monitor);
}
/* (non-Javadoc)
@@ -105,6 +106,14 @@ public final class JavaFileAction extends AbstractWorkspaceAction implements IWo
}
}
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.taglib.workspace.IWorkspaceActionExtension#getFileContent()
+ */
+ public BufferedJET2Writer getContentWriter()
+ {
+ return content;
+ }
+
}
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/java/JavaPackageAction.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/java/JavaPackageAction.java
index 2300b29..1f1da08 100644
--- a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/java/JavaPackageAction.java
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/java/JavaPackageAction.java
@@ -1,7 +1,7 @@
/**
* <copyright>
*
- * Copyright (c) 2006 IBM Corporation and others.
+ * Copyright (c) 2006, 2008 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
@@ -24,18 +24,19 @@ import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jet.taglib.JET2TagException;
import org.eclipse.jet.taglib.TagInfo;
import org.eclipse.jet.taglib.java.JavaActionsUtil;
-import org.eclipse.jet.taglib.workspace.AbstractWorkspaceAction;
+import org.eclipse.jet.taglib.workspace.AbstractWorkspaceAction2;
import org.eclipse.jet.taglib.workspace.ActionsUtil;
-import org.eclipse.jet.taglib.workspace.IWorkspaceAction;
+import org.eclipse.jet.taglib.workspace.IWorkspaceAction2;
/**
- * @author pelder
+ * Create a Java package
*/
-public class JavaPackageAction extends AbstractWorkspaceAction implements IWorkspaceAction
+public class JavaPackageAction extends AbstractWorkspaceAction2 implements IWorkspaceAction2
{
private final IPath srcFolderPath;
private final String packageName;
+ private IFolder underlyingFolder;
public JavaPackageAction(IPath srcFolderPath, String packageName, TagInfo td, String templatePath)
{
@@ -49,21 +50,22 @@ public class JavaPackageAction extends AbstractWorkspaceAction implements IWorks
*/
public IResource getResource() throws JET2TagException
{
- // spec for this method says its only called ir requiresValidateEdit
- // is true. Assert the correctness of this
- throw new IllegalStateException();
+ return underlyingFolder;
}
/* (non-Javadoc)
* @see org.eclipse.jet.taglib.workspace.IWorkspaceAction#performAction(org.eclipse.core.runtime.IProgressMonitor)
*/
- public void performAction(IProgressMonitor monitor) throws JET2TagException
+ public boolean performActionIfRequired(IProgressMonitor monitor) throws JET2TagException
{
IContainer container = JavaActionsUtil.getContainerForPackage(srcFolderPath, packageName);
if(container instanceof IFolder)
{
- ActionsUtil.ensureFolderExists((IFolder)container, monitor);
+ underlyingFolder = (IFolder)container;
+ return ActionsUtil.ensureFolderExists(underlyingFolder, monitor);
+ } else {
+ return false; // container was a project - did not create it...
}
}
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/java/MergeTag.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/java/MergeTag.java
index 27b52c1..5c9633d 100644
--- a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/java/MergeTag.java
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/java/MergeTag.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2006, 2007 IBM Corporation and others.
+ * Copyright (c) 2006, 2008 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
@@ -31,6 +31,7 @@ import org.eclipse.emf.codegen.util.CodeGenUtil;
import org.eclipse.emf.common.util.WrappedException;
import org.eclipse.jet.BufferedJET2Writer;
import org.eclipse.jet.IWriterListener;
+import org.eclipse.jet.IWriterListenerExtension;
import org.eclipse.jet.JET2Context;
import org.eclipse.jet.JET2Writer;
import org.eclipse.jet.internal.l10n.JET2Messages;
@@ -89,7 +90,7 @@ public class MergeTag extends AbstractEmptyTag
/**
* Process a JMergeTag on the template content.
*/
- public static class JMergeProcessor implements IWriterListener
+ public static class JMergeProcessor implements IWriterListener, IWriterListenerExtension
{
/**
@@ -123,20 +124,30 @@ public class MergeTag extends AbstractEmptyTag
}
}
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.IWriterListenerExtension#finalizeContent(org.eclipse.jet.JET2Writer, java.lang.Object, java.lang.String)
+ */
+ public void finalizeContent(JET2Writer writer, Object fileObject, String existingContent) throws JET2TagException
+ {
+ if(fileObject instanceof IFile && existingContent != null)
+ {
+ IFile iFile = (IFile)fileObject;
+ if("java".equals(iFile.getFileExtension())) //$NON-NLS-1$
+ {
+ jmerge(writer, iFile, existingContent);
+ }
+ else if("properties".equals(iFile.getFileExtension())) //$NON-NLS-1$
+ {
+ propmerge(writer, iFile, existingContent);
+ }
+ }
+ }
+
private void propmerge(JET2Writer writer, IFile file) throws JET2TagException
{
- PropertyMerger merger = new PropertyMerger();
-
try
{
- String targetPropertiesString = TagUtil.getContents(file.getLocation());
-
- merger.setSourceProperties(((BufferedJET2Writer)writer).getContent());
- merger.setTargetProperties(targetPropertiesString);
-
- merger.merge();
-
- ((BufferedJET2Writer)writer).setContent(merger.getTargetProperties());
+ propmerge(writer, file, TagUtil.getContents(file.getLocation()));
}
catch (CoreException e)
{
@@ -145,6 +156,19 @@ public class MergeTag extends AbstractEmptyTag
}
}
+
+ private void propmerge(JET2Writer writer, IFile file, String targetPropertiesString) throws JET2TagException
+ {
+ PropertyMerger merger = new PropertyMerger();
+
+ merger.setSourceProperties(((BufferedJET2Writer)writer).getContent());
+ merger.setTargetProperties(targetPropertiesString);
+
+ merger.merge();
+
+ ((BufferedJET2Writer)writer).setContent(merger.getTargetProperties());
+ }
+
/**
* @param writer
* @param iFile
@@ -152,6 +176,25 @@ public class MergeTag extends AbstractEmptyTag
*/
private void jmerge(JET2Writer writer, IFile iFile) throws JET2TagException
{
+ try
+ {
+ jmerge(writer, iFile, TagUtil.getContents(iFile.getLocation()));
+ }
+ catch (CoreException e1)
+ {
+ final String msg = JET2Messages.MergeTag_CouldNotRead;
+ throw new JET2TagException(MessageFormat.format(msg, new Object []{ iFile.getFullPath() }));
+ }
+ }
+
+ /**
+ * @param writer
+ * @param iFile
+ * @param targetCUSource
+ * @throws JET2TagException
+ */
+ private void jmerge(JET2Writer writer, IFile iFile, String targetCUSource) throws JET2TagException
+ {
// cast with impunity - we know the following will work because the writer was set
// up this way. If we're wrong, the runtime exception is as good as any.
final IDocument document = (IDocument)((BufferedJET2Writer)writer).getAdapter(IDocument.class);
@@ -159,8 +202,6 @@ public class MergeTag extends AbstractEmptyTag
{
final Position[] positions = document.getPositions(JMERGE_POSITION_CATEGORY);
- String targetCUSource = TagUtil.getContents(iFile.getLocation());
-
MergePosition mergePosition = (MergePosition)positions[0];
FacadeHelper facadeHelper = mergePosition.getFacadeHelper();
JControlModel controlModel = new JControlModel();
@@ -177,11 +218,6 @@ public class MergeTag extends AbstractEmptyTag
merger.merge();
((BufferedJET2Writer)writer).setContent(targetCU.getContents());
}
- catch (CoreException e)
- {
- final String msg = JET2Messages.MergeTag_CouldNotRead;
- throw new JET2TagException(MessageFormat.format(msg, new Object []{ iFile.getFullPath() }));
- }
catch (BadPositionCategoryException e)
{
// This should not happen. Wrap in a runtime exception
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 fa66529..9fc837d 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
@@ -1,7 +1,7 @@
/**
* <copyright>
*
- * Copyright (c) 2006 IBM Corporation and others.
+ * Copyright (c) 2006, 2008 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
@@ -75,7 +75,42 @@ public class CopyFileTag extends AbstractEmptyTag
replaceExisting = false;
}
- TransformContextExtender tce = new TransformContextExtender(context);
+ WorkspaceContextExtender wsExtender = WorkspaceContextExtender.getInstance(context);
+ IFile file = getTargetFile(path, wsExtender);
+
+
+ if(file.exists() && !replaceExisting) {
+ // don't do anything, we're not going to write anyhow...
+ return;
+ }
+
+ URL sourceURL = getSourceURL(url, urlContext, context);
+
+ IWorkspaceAction action;
+ if (textCopy)
+ {
+ final String sourceFileContents = ActionsUtil.readTextFile(sourceURL, srcEncoding);
+ action = new WsCopyTextFileAction(context.getTemplatePath(), td, sourceFileContents, file, replaceExisting, targetEncoding);
+ }
+ else
+ {
+ final byte[] contents = ActionsUtil.readBinaryFile(sourceURL);
+ action = new WsCopyBinaryFileAction(context.getTemplatePath(), td, contents, file, replaceExisting);
+ }
+ wsExtender.addAction(action);
+
+ }
+
+ /**
+ * @param url
+ * @param urlContext
+ * @param tce
+ * @return
+ * @throws JET2TagException
+ */
+ private URL getSourceURL(String url, String urlContext, JET2Context context) throws JET2TagException
+ {
+ TransformContextExtender tce = TransformContextExtender.getInstance(context);
final URL baseURL = tce.getBaseURL(urlContext);
@@ -92,9 +127,17 @@ public class CopyFileTag extends AbstractEmptyTag
{
throw new JET2TagException(e);
}
+ return sourceURL;
+ }
- WorkspaceContextExtender wsExtender = WorkspaceContextExtender.getInstance(context);
-
+ /**
+ * @param path
+ * @param wsExtender
+ * @return
+ * @throws JET2TagException
+ */
+ private IFile getTargetFile(IPath path, WorkspaceContextExtender wsExtender) throws JET2TagException
+ {
IFile file;
if (path.isAbsolute() || !wsExtender.existsContainer())
{
@@ -111,20 +154,7 @@ public class CopyFileTag extends AbstractEmptyTag
{
file = wsExtender.getContainer().getFile(path);
}
-
- IWorkspaceAction action;
- if (textCopy)
- {
- final String sourceFileContents = ActionsUtil.readTextFile(sourceURL, srcEncoding);
- action = new WsCopyTextFileAction(context, tce.getTemplatePath(), td, sourceFileContents, file, replaceExisting, targetEncoding);
- }
- else
- {
- final byte[] contents = ActionsUtil.readBinaryFile(sourceURL);
- action = new WsCopyBinaryFileAction(context, tce.getTemplatePath(), td, contents, file, replaceExisting);
- }
- wsExtender.addAction(action);
-
+ return file;
}
}
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 f44dd99..7ffd993 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
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2005, 2006 IBM Corporation and others.
+ * Copyright (c) 2005, 2008 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
@@ -78,22 +78,7 @@ public class FileTag extends AbstractEmptyTag
WorkspaceContextExtender wsExtender = WorkspaceContextExtender.getInstance(context);
- IFile file;
- if (path.isAbsolute() || !wsExtender.existsContainer())
- {
- try
- {
- file = ResourcesPlugin.getWorkspace().getRoot().getFile(path);
- }
- catch (IllegalArgumentException e)
- {
- throw new JET2TagException(e.getLocalizedMessage(), e);
- }
- }
- else
- {
- file = wsExtender.getContainer().getFile(path);
- }
+ IFile file = getTargetFile(path, wsExtender);
if(replace == false && file.exists()) {
// don't need to run template or create action...
@@ -106,8 +91,7 @@ public class FileTag extends AbstractEmptyTag
tce.execute(templatePath, contentWriter);
WsFileFromWriterAction fileAction = new WsFileFromWriterAction(
- context,
- tce.getTemplatePath(),
+ context.getTemplatePath(),
td,
file,
contentWriter,
@@ -121,4 +105,31 @@ public class FileTag extends AbstractEmptyTag
wsExtender.addAction(fileAction);
}
+ /**
+ * @param path
+ * @param wsExtender
+ * @return
+ * @throws JET2TagException
+ */
+ private IFile getTargetFile(IPath path, WorkspaceContextExtender wsExtender) throws JET2TagException
+ {
+ IFile file;
+ if (path.isAbsolute() || !wsExtender.existsContainer())
+ {
+ try
+ {
+ file = ResourcesPlugin.getWorkspace().getRoot().getFile(path);
+ }
+ catch (IllegalArgumentException e)
+ {
+ throw new JET2TagException(e.getLocalizedMessage(), e);
+ }
+ }
+ else
+ {
+ file = wsExtender.getContainer().getFile(path);
+ }
+ return file;
+ }
+
}
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 0932bcb..fcaba65 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
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2005, 2006 IBM Corporation and others.
+ * Copyright (c) 2005, 2008 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
@@ -27,7 +27,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.transform.TransformContextExtender;
/**
@@ -75,8 +74,7 @@ public class FolderTag extends AbstractContainerTag
{
folder = wsExtender.getContainer().getFolder(path);
}
- TransformContextExtender tce = new TransformContextExtender(context);
- wsExtender.addAction(new WsFolderAction(context, tce.getTemplatePath(), td, folder));
+ wsExtender.addAction(new WsFolderAction(context.getTemplatePath(), td, folder));
wsExtender.pushContainer(folder);
containerPushed = true;
@@ -87,7 +85,7 @@ public class FolderTag extends AbstractContainerTag
*/
public void doAfterBody(TagInfo td, JET2Context context, JET2Writer out) throws JET2TagException
{
- WorkspaceContextExtender wsExtender = new WorkspaceContextExtender(context);
+ WorkspaceContextExtender wsExtender = WorkspaceContextExtender.getInstance(context);
if (containerPushed)
{
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 306a5b7..b44cea5 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
@@ -27,7 +27,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.transform.TransformContextExtender;
/**
@@ -57,7 +56,7 @@ public class ProjectTag extends AbstractContainerTag
String name = getAttribute(NAME__ATTR);
String location = getAttribute("location"); //$NON-NLS-1$
- WorkspaceContextExtender wsExtender = new WorkspaceContextExtender(context);
+ WorkspaceContextExtender wsExtender = WorkspaceContextExtender.getInstance(context);
IProject project;
try
@@ -75,8 +74,7 @@ public class ProjectTag extends AbstractContainerTag
projectDescription.setLocation(new Path(location));
}
- TransformContextExtender tce = new TransformContextExtender(context);
- wsExtender.addAction(new WsProjectAction(context, tce.getTemplatePath(), td, project, projectDescription));
+ wsExtender.addAction(new WsProjectAction(context.getTemplatePath(), td, project, projectDescription));
wsExtender.pushContainer(project);
containerPushed = true;
@@ -87,7 +85,7 @@ public class ProjectTag extends AbstractContainerTag
*/
public void doAfterBody(TagInfo td, JET2Context context, JET2Writer out) throws JET2TagException
{
- WorkspaceContextExtender wsExtender = new WorkspaceContextExtender(context);
+ WorkspaceContextExtender wsExtender = WorkspaceContextExtender.getInstance(context);
if (containerPushed)
{
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
index 4589d47..3f8bb5e 100644
--- 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
@@ -1,7 +1,7 @@
/**
* <copyright>
*
- * Copyright (c) 2006 IBM Corporation and others.
+ * Copyright (c) 2006, 2008 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
@@ -18,26 +18,21 @@
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.AbstractWorkspaceAction2;
import org.eclipse.jet.taglib.workspace.ActionsUtil;
-import org.eclipse.jet.taglib.workspace.IWorkspaceAction;
+import org.eclipse.jet.taglib.workspace.IWorkspaceAction2;
/**
* Copy a binary file from a source URL to a target workspace file.
*
*/
-public class WsCopyBinaryFileAction extends AbstractWorkspaceAction implements IWorkspaceAction
+public class WsCopyBinaryFileAction extends AbstractWorkspaceAction2 implements IWorkspaceAction2
{
private final IFile file;
@@ -46,10 +41,7 @@ public class WsCopyBinaryFileAction extends AbstractWorkspaceAction implements I
private final byte[] contents;
- private final JET2Context context;
-
public WsCopyBinaryFileAction(
- JET2Context context,
String templatePath,
TagInfo tagInfo,
byte[] contents,
@@ -57,7 +49,6 @@ public class WsCopyBinaryFileAction extends AbstractWorkspaceAction implements I
boolean replaceExisting)
{
super(tagInfo, templatePath);
- this.context = context;
this.contents = contents;
this.file = targetFile;
this.replace = replaceExisting;
@@ -82,14 +73,9 @@ public class WsCopyBinaryFileAction extends AbstractWorkspaceAction implements I
/* (non-Javadoc)
* @see org.eclipse.jet.internal.taglib.workspace.IWorkspaceAction#performAction(org.eclipse.core.runtime.IProgressMonitor)
*/
- public void performAction(IProgressMonitor monitor) throws JET2TagException
+ public boolean performActionIfRequired(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(), RuntimeLoggerContextExtender.INFO_LEVEL);
+ return ActionsUtil.writeBinaryFile(file, replace, contents, monitor);
}
}
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
index 5060587..1cca143 100644
--- 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
@@ -1,7 +1,7 @@
/**
* <copyright>
*
- * Copyright (c) 2006 IBM Corporation and others.
+ * Copyright (c) 2006, 2008 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
@@ -18,26 +18,21 @@
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.AbstractWorkspaceAction2;
import org.eclipse.jet.taglib.workspace.ActionsUtil;
-import org.eclipse.jet.taglib.workspace.IWorkspaceAction;
+import org.eclipse.jet.taglib.workspace.IWorkspaceAction2;
/**
* Copy a binary file from a source URL to a target workspace file.
*
*/
-public class WsCopyTextFileAction extends AbstractWorkspaceAction implements IWorkspaceAction
+public class WsCopyTextFileAction extends AbstractWorkspaceAction2 implements IWorkspaceAction2
{
private final IFile file;
@@ -48,10 +43,7 @@ public class WsCopyTextFileAction extends AbstractWorkspaceAction implements IWo
private final String sourceFileContents;
- private final JET2Context context;
-
public WsCopyTextFileAction(
- JET2Context context,
String templatePath,
TagInfo td,
String sourceFileContents,
@@ -60,7 +52,6 @@ public class WsCopyTextFileAction extends AbstractWorkspaceAction implements IWo
String targetEncoding)
{
super(td, templatePath);
- this.context = context;
this.sourceFileContents = sourceFileContents;
this.file = file;
this.replace = replaceExisting;
@@ -86,14 +77,9 @@ public class WsCopyTextFileAction extends AbstractWorkspaceAction implements IWo
/* (non-Javadoc)
* @see org.eclipse.jet.internal.taglib.workspace.IWorkspaceAction#performAction(org.eclipse.core.runtime.IProgressMonitor)
*/
- public void performAction(IProgressMonitor monitor) throws JET2TagException
+ public boolean performActionIfRequired(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(), RuntimeLoggerContextExtender.INFO_LEVEL);
+ return ActionsUtil.writeTextFile(file, replace, targetEncoding, false, sourceFileContents, monitor);
}
}
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
index ef087ba..3cce002 100644
--- 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
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2005, 2006 IBM Corporation and others.
+ * Copyright (c) 2005, 2008 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
@@ -17,32 +17,28 @@
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.BufferedJET2Writer;
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.AbstractWorkspaceAction2;
import org.eclipse.jet.taglib.workspace.ActionsUtil;
-import org.eclipse.jet.taglib.workspace.IWorkspaceAction;
+import org.eclipse.jet.taglib.workspace.IWorkspaceAction2;
+import org.eclipse.jet.taglib.workspace.IWorkspaceActionExtension;
/**
* Create or update a text file with the contents of a writer..
*
*/
-public class WsFileFromWriterAction extends AbstractWorkspaceAction implements IWorkspaceAction
+public class WsFileFromWriterAction extends AbstractWorkspaceAction2 implements IWorkspaceAction2, IWorkspaceActionExtension
{
private final IFile file;
- private final JET2Writer writer;
+ private final BufferedJET2Writer writer;
private String encoding;
@@ -50,8 +46,6 @@ public class WsFileFromWriterAction extends AbstractWorkspaceAction implements I
private final boolean derived;
- private final JET2Context context;
-
/**
*
* @param context
@@ -63,16 +57,14 @@ public class WsFileFromWriterAction extends AbstractWorkspaceAction implements I
* @param derived
*/
public WsFileFromWriterAction(
- JET2Context context,
String templatePath,
TagInfo tagInfo,
IFile file,
- JET2Writer writer,
+ BufferedJET2Writer writer,
boolean replace,
boolean derived)
{
super(tagInfo, templatePath);
- this.context = context;
this.file = file;
this.writer = writer;
this.replace = replace;
@@ -89,13 +81,9 @@ public class WsFileFromWriterAction extends AbstractWorkspaceAction implements I
return replace && file.exists() && file.isReadOnly() /* && content has changed */;
}
- public void performAction(IProgressMonitor monitor) throws JET2TagException
+ public boolean performActionIfRequired(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(), RuntimeLoggerContextExtender.INFO_LEVEL);
+ return ActionsUtil.writeTextFile(file, replace, encoding, derived, writer.getContent(), monitor);
}
public void setEncoding(String encoding)
@@ -103,4 +91,9 @@ public class WsFileFromWriterAction extends AbstractWorkspaceAction implements I
this.encoding = encoding;
}
+ public BufferedJET2Writer getContentWriter()
+ {
+ return writer;
+ }
+
}
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
index c9fe392..678a867 100644
--- 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
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2005, 2006 IBM Corporation and others.
+ * Copyright (c) 2005, 2008 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
@@ -17,19 +17,14 @@
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.AbstractWorkspaceAction2;
import org.eclipse.jet.taglib.workspace.ActionsUtil;
-import org.eclipse.jet.taglib.workspace.IWorkspaceAction;
+import org.eclipse.jet.taglib.workspace.IWorkspaceAction2;
/**
@@ -39,13 +34,11 @@ import org.eclipse.jet.taglib.workspace.IWorkspaceAction;
* </bl>
*
*/
-public class WsFolderAction extends AbstractWorkspaceAction implements IWorkspaceAction
+public class WsFolderAction extends AbstractWorkspaceAction2 implements IWorkspaceAction2
{
private final IFolder folder;
- private final JET2Context context;
-
/**
*
* @param context
@@ -53,10 +46,9 @@ public class WsFolderAction extends AbstractWorkspaceAction implements IWorkspac
* @param tagInfo
* @param folder
*/
- public WsFolderAction(JET2Context context, String templatePath, TagInfo tagInfo, IFolder folder)
+ public WsFolderAction(String templatePath, TagInfo tagInfo, IFolder folder)
{
super(tagInfo, templatePath);
- this.context = context;
this.folder = folder;
}
@@ -70,13 +62,9 @@ public class WsFolderAction extends AbstractWorkspaceAction implements IWorkspac
return false;
}
- public void performAction(IProgressMonitor monitor) throws JET2TagException
+ public boolean performActionIfRequired(IProgressMonitor monitor) throws JET2TagException
{
- ActionsUtil.ensureFolderExists(folder, monitor);
- RuntimeLoggerContextExtender.log(context, MessageFormat.format(
- JET2Messages.WsFolderAction_CreatingFolder,
- new Object []{ folder.getFullPath().toString() }), getTagInfo(), getTemplatePath(), RuntimeLoggerContextExtender.INFO_LEVEL);
-
+ return ActionsUtil.ensureFolderExists(folder, monitor);
}
}
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/workspace/WsProjectAction.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/workspace/WsProjectAction.java
index 0813c53..9c5e17e 100644
--- a/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/workspace/WsProjectAction.java
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/internal/taglib/workspace/WsProjectAction.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2005, 2006 IBM Corporation and others.
+ * Copyright (c) 2005, 2008 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
@@ -17,20 +17,15 @@
package org.eclipse.jet.internal.taglib.workspace;
-import java.text.MessageFormat;
-
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
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.AbstractWorkspaceAction2;
import org.eclipse.jet.taglib.workspace.ActionsUtil;
-import org.eclipse.jet.taglib.workspace.IWorkspaceAction;
+import org.eclipse.jet.taglib.workspace.IWorkspaceAction2;
/**
@@ -41,13 +36,11 @@ import org.eclipse.jet.taglib.workspace.IWorkspaceAction;
* </bl>
*
*/
-public class WsProjectAction extends AbstractWorkspaceAction implements IWorkspaceAction
+public class WsProjectAction extends AbstractWorkspaceAction2 implements IWorkspaceAction2
{
private final IProject project;
- private final JET2Context context;
-
private final IProjectDescription description;
/**
@@ -58,10 +51,9 @@ public class WsProjectAction extends AbstractWorkspaceAction implements IWorkspa
* @param project
* @param description
*/
- public WsProjectAction(JET2Context context, String templatePath, TagInfo tagInfo, IProject project, IProjectDescription description)
+ public WsProjectAction(String templatePath, TagInfo tagInfo, IProject project, IProjectDescription description)
{
super(tagInfo, templatePath);
- this.context = context;
this.project = project;
this.description = description;
}
@@ -85,12 +77,12 @@ public class WsProjectAction extends AbstractWorkspaceAction implements IWorkspa
/* (non-Javadoc)
* @see org.eclipse.jet.internal.taglib.workspace.IWorkspaceAction#performAction(org.eclipse.core.runtime.IProgressMonitor)
*/
- public void performAction(IProgressMonitor monitor) throws JET2TagException
+ public boolean performActionIfRequired(IProgressMonitor monitor) throws JET2TagException
{
- ActionsUtil.createProject(project, description, monitor);
+ return ActionsUtil.createProject(project, description, monitor);
- final String createMessage = MessageFormat.format(JET2Messages.WsProjectAction_CreatingProject, new Object []{ project.getName() });
- RuntimeLoggerContextExtender.log(context, createMessage, getTagInfo(), getTemplatePath(), RuntimeLoggerContextExtender.INFO_LEVEL);
+// final String createMessage = MessageFormat.format(JET2Messages.WsProjectAction_CreatingProject, new Object []{ project.getName() });
+// RuntimeLoggerContextExtender.log(context, createMessage, getTagInfo(), getTemplatePath(), RuntimeLoggerContextExtender.INFO_LEVEL);
}
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/UserRegionHelper.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/UserRegionHelper.java
index f95a896..995a898 100644
--- a/plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/UserRegionHelper.java
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/UserRegionHelper.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2005, 2007 IBM Corporation and others.
+ * Copyright (c) 2005, 2008 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
@@ -15,6 +15,7 @@ import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jet.BufferedJET2Writer;
import org.eclipse.jet.IWriterListener;
+import org.eclipse.jet.IWriterListenerExtension;
import org.eclipse.jet.JET2Writer;
import org.eclipse.jet.internal.taglib.DocumentHelper;
import org.eclipse.jface.text.BadLocationException;
@@ -35,7 +36,7 @@ import org.eclipse.jface.text.Position;
* </p>
*
*/
-public final class UserRegionHelper implements IWriterListener
+public final class UserRegionHelper implements IWriterListener, IWriterListenerExtension
{
/**
* Position subclass that tracks user regions that include an unmodified marker.
@@ -71,76 +72,89 @@ public final class UserRegionHelper implements IWriterListener
public void finalizeContent(JET2Writer writer, Object file)
{
- if (file instanceof IFile && writer instanceof BufferedJET2Writer)
- {
- IFile iFile = (IFile)file;
+ if(writer instanceof BufferedJET2Writer) {
BufferedJET2Writer bufferedwriter = (BufferedJET2Writer)writer;
IDocument document = (IDocument)bufferedwriter.getAdapter(IDocument.class);
- if (iFile.exists() && document != null)
+ if (file instanceof IFile)
{
- String fileContents;
- try
- {
- fileContents = TagUtil.getContents(iFile.getLocation());
- }
- catch (CoreException e)
+ IFile iFile = (IFile)file;
+ if (iFile.exists() && document != null)
{
- return;
+ String fileContents;
+ try
+ {
+ fileContents = TagUtil.getContents(iFile.getLocation());
+ }
+ catch (CoreException e)
+ {
+ return;
+ }
+
+ mergeUserRegions(bufferedwriter, document, fileContents);
+
}
+ }
+ }
- try
- {
- Position[] rtPositions = document.getPositions(UserRegionHelper.USERREGION_POSITION_CATEGORY);
- Position[] icPositions = document.getPositions(UserRegionHelper.INITIALCODE_POSITION_CATEGORY);
- for (int i = 0; i < rtPositions.length; i++)
- {
- // don't assume there will be an initial code
- // position, find it...
- final Position userRegionPosition = rtPositions[i];
- Position initialCodePosition = findContainedInitialCodePosition(userRegionPosition, icPositions);
+ }
- if (initialCodePosition == null)
- {
- continue;
- }
+ /**
+ * @param bufferedwriter
+ * @param document
+ * @param fileContents
+ * @throws RuntimeException
+ */
+ private void mergeUserRegions(BufferedJET2Writer bufferedwriter, IDocument document, String fileContents) throws RuntimeException
+ {
+ try
+ {
+ Position[] rtPositions = document.getPositions(UserRegionHelper.USERREGION_POSITION_CATEGORY);
+ Position[] icPositions = document.getPositions(UserRegionHelper.INITIALCODE_POSITION_CATEGORY);
+ for (int i = 0; i < rtPositions.length; i++)
+ {
+ // don't assume there will be an initial code
+ // position, find it...
+ final Position userRegionPosition = rtPositions[i];
+ Position initialCodePosition = findContainedInitialCodePosition(userRegionPosition, icPositions);
- String unmodifiedMarker = initialCodePosition instanceof PositionWithUnmodifiedMarker
- ? ((PositionWithUnmodifiedMarker)initialCodePosition).unmodifiedMarker : null;
+ if (initialCodePosition == null)
+ {
+ continue;
+ }
+
+ String unmodifiedMarker = initialCodePosition instanceof PositionWithUnmodifiedMarker
+ ? ((PositionWithUnmodifiedMarker)initialCodePosition).unmodifiedMarker : null;
- final Position beginMarkerPosition = new Position(userRegionPosition.offset, initialCodePosition.offset
- - userRegionPosition.offset);
- int endMarkerOffset = initialCodePosition.offset + initialCodePosition.length;
- final Position endMarkerPosition = new Position(endMarkerOffset, userRegionPosition.offset + userRegionPosition.length
- - endMarkerOffset);
+ final Position beginMarkerPosition = new Position(userRegionPosition.offset, initialCodePosition.offset
+ - userRegionPosition.offset);
+ int endMarkerOffset = initialCodePosition.offset + initialCodePosition.length;
+ final Position endMarkerPosition = new Position(endMarkerOffset, userRegionPosition.offset + userRegionPosition.length
+ - endMarkerOffset);
- final String beginMarker = bufferedwriter.getContent(beginMarkerPosition.offset, beginMarkerPosition.length);
- final String endMarker = bufferedwriter.getContent(endMarkerPosition.offset, endMarkerPosition.length);
+ final String beginMarker = bufferedwriter.getContent(beginMarkerPosition.offset, beginMarkerPosition.length);
+ final String endMarker = bufferedwriter.getContent(endMarkerPosition.offset, endMarkerPosition.length);
- final int existingRegionOffset = fileContents.indexOf(beginMarker);
- if (existingRegionOffset != -1)
+ final int existingRegionOffset = fileContents.indexOf(beginMarker);
+ if (existingRegionOffset != -1)
+ {
+ final int existingEndMarkerOffset = fileContents.indexOf(endMarker, existingRegionOffset + beginMarker.length());
+ if (existingEndMarkerOffset != -1)
+ {
+ final String existingRegion = fileContents.substring(existingRegionOffset, existingEndMarkerOffset + endMarker.length());
+ if (!existingRegion.equals(bufferedwriter.getContent(userRegionPosition.offset, userRegionPosition.length))
+ && (unmodifiedMarker == null || existingRegion.indexOf(unmodifiedMarker) == -1))
{
- final int existingEndMarkerOffset = fileContents.indexOf(endMarker, existingRegionOffset + beginMarker.length());
- if (existingEndMarkerOffset != -1)
- {
- final String existingRegion = fileContents.substring(existingRegionOffset, existingEndMarkerOffset + endMarker.length());
- if (!existingRegion.equals(bufferedwriter.getContent(userRegionPosition.offset, userRegionPosition.length))
- && (unmodifiedMarker == null || existingRegion.indexOf(unmodifiedMarker) == -1))
- {
- bufferedwriter.replaceContent(userRegionPosition.offset, userRegionPosition.length, existingRegion);
- }
- }
+ bufferedwriter.replaceContent(userRegionPosition.offset, userRegionPosition.length, existingRegion);
}
}
}
- catch (BadPositionCategoryException e)
- {
- // should not happen. Track with a Runtime exception
- throw new RuntimeException(e);
- }
-
}
}
-
+ catch (BadPositionCategoryException e)
+ {
+ // should not happen. Track with a Runtime exception
+ throw new RuntimeException(e);
+ }
}
private Position findContainedInitialCodePosition(Position userRegionPosition, Position[] icPositions)
@@ -255,4 +269,17 @@ public final class UserRegionHelper implements IWriterListener
throw new RuntimeException(e);
}
}
+
+ public void finalizeContent(JET2Writer writer, Object fileObject, String existingContent) throws JET2TagException
+ {
+ if(writer instanceof BufferedJET2Writer) {
+ BufferedJET2Writer bufferedwriter = (BufferedJET2Writer)writer;
+ IDocument document = (IDocument)bufferedwriter.getAdapter(IDocument.class);
+ if (existingContent != null)
+ {
+ mergeUserRegions(bufferedwriter, document, existingContent);
+ }
+ }
+
+ }
} \ No newline at end of file
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/java/JavaActionsUtil.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/java/JavaActionsUtil.java
index df3996c..997aec5 100644
--- a/plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/java/JavaActionsUtil.java
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/java/JavaActionsUtil.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2006, 2007 IBM Corporation and others.
+ * Copyright (c) 2006, 2008 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
@@ -45,7 +45,6 @@ import org.eclipse.jet.taglib.CustomTag;
import org.eclipse.jet.taglib.JET2TagException;
import org.eclipse.jet.taglib.workspace.ActionsUtil;
import org.eclipse.jface.text.BadPositionCategoryException;
-import org.eclipse.jface.text.DefaultPositionUpdater;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.Position;
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/workspace/AbstractWorkspaceAction2.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/workspace/AbstractWorkspaceAction2.java
new file mode 100644
index 0000000..737a301
--- /dev/null
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/workspace/AbstractWorkspaceAction2.java
@@ -0,0 +1,62 @@
+/**
+ * <copyright>
+ *
+ * Copyright (c) 2008 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: AbstractWorkspaceAction2.java,v 1.1 2008/04/02 15:31:12 pelder Exp $
+ */
+package org.eclipse.jet.taglib.workspace;
+
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.jet.taglib.JET2TagException;
+import org.eclipse.jet.taglib.TagInfo;
+
+/**
+ * Abstract implementation of IWorkspaceAction that manages the standard
+ * execution tracing information.
+ */
+public abstract class AbstractWorkspaceAction2 implements IWorkspaceAction2
+{
+
+ private final TagInfo tagInfo;
+ private final String templatePath;
+
+ protected AbstractWorkspaceAction2(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;
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jet.taglib.workspace.IWorkspaceAction#performAction(org.eclipse.core.runtime.IProgressMonitor)
+ */
+ public final void performAction(IProgressMonitor monitor) throws JET2TagException
+ {
+ performActionIfRequired(monitor);
+ }
+}
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
index 65b8234..398eb9a 100644
--- 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
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2006, 2007 IBM Corporation and others.
+ * Copyright (c) 2006, 2008 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
@@ -41,7 +41,9 @@ import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.core.runtime.content.IContentDescription;
+import org.eclipse.jet.BufferedJET2Writer;
import org.eclipse.jet.IWriterListener;
+import org.eclipse.jet.IWriterListenerExtension;
import org.eclipse.jet.JET2Writer;
import org.eclipse.jet.internal.l10n.JET2Messages;
import org.eclipse.jet.taglib.JET2TagException;
@@ -104,7 +106,7 @@ public final class ActionsUtil
JET2Writer writer,
IProgressMonitor monitor) throws JET2TagException
{
- finalizeContent(writer, file);
+ finalizeContent(writer, file, null);
boolean written = writeTextFile(file, replace, encoding, derived, writer.toString(), monitor);
@@ -116,16 +118,20 @@ public final class ActionsUtil
return written;
}
- private static final void finalizeContent(JET2Writer writer, Object committedObject) throws JET2TagException
+ private static final void finalizeContent(JET2Writer writer, Object committedObject, String existingContent) throws JET2TagException
{
final IWriterListener[] eventListeners = writer.getEventListeners();
for (int i = 0; i < eventListeners.length; i++)
{
- eventListeners[i].finalizeContent(writer, committedObject);
+ if(existingContent != null && eventListeners[i] instanceof IWriterListenerExtension) {
+ ((IWriterListenerExtension)eventListeners[i]).finalizeContent(writer, committedObject, existingContent);
+ } else {
+ eventListeners[i].finalizeContent(writer, committedObject);
+ }
}
}
- private static final void contentCommitted(JET2Writer writer, Object committedObject) throws JET2TagException
+ static final void contentCommitted(JET2Writer writer, Object committedObject) throws JET2TagException
{
final IWriterListener[] eventListeners = writer.getEventListeners();
for (int i = 0; i < eventListeners.length; i++)
@@ -239,10 +245,12 @@ public final class ActionsUtil
* 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
+ * @return <code>true</code> if folders were created...
* @throws JET2TagException if the folder cannot be created
*/
- public static void ensureFolderExists(IFolder folder, IProgressMonitor monitor) throws JET2TagException
+ public static boolean ensureFolderExists(IFolder folder, IProgressMonitor monitor) throws JET2TagException
{
+ boolean foldersCreated = false;
IProject project = folder.getProject();
IPath relPath = folder.getProjectRelativePath();
monitor.beginTask(
@@ -257,6 +265,7 @@ public final class ActionsUtil
try
{
iFolder.create(false, true, new SubProgressMonitor(monitor, 1));
+ foldersCreated = true;
}
catch (CoreException e)
{
@@ -268,6 +277,7 @@ public final class ActionsUtil
monitor.worked(1);
}
}
+ return foldersCreated;
}
/**
@@ -289,14 +299,12 @@ public final class ActionsUtil
new Object []{ file.getFullPath().toString() });
monitor.beginTask(fileMessage, 2);
- if (file.exists() && !replace)
+ final boolean fileExists = file.exists();
+ if (fileExists && !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)
{
@@ -307,18 +315,29 @@ public final class ActionsUtil
monitor.worked(1);
}
+ if (fileExists && contents.equals(readBinaryStream(file.getContents())))
+ {
+ // no content change, avoid write...
+ return false;
+ }
+
// Step 2: write the file
- if (file.exists() && replace)
+ final InputStream input = new ByteArrayInputStream(contents);
+ if (fileExists)
{
file.setContents(input, false, true, new SubProgressMonitor(monitor, 1));
}
- else if (!file.exists())
+ else if (!fileExists)
{
file.create(input, false, new SubProgressMonitor(monitor, 1));
}
// RuntimeLoggerContextExtender.log(context, fileMessage, tagInfo, templatePath);
return true;
}
+ catch (IOException e)
+ {
+ throw new JET2TagException(e);
+ }
catch (CoreException e)
{
throw new JET2TagException(e);
@@ -337,10 +356,36 @@ public final class ActionsUtil
public static String readTextFile(URL url, String encoding) throws JET2TagException
{
InputStream input = null;
- Reader reader = null;
try
{
input = url.openStream();
+ return readTextStream(input, encoding);
+ }
+ catch (IOException e)
+ {
+ throw new JET2TagException(e);
+ }
+ finally
+ {
+ // ensure stream is closed.
+ if (input != null)
+ {
+ try
+ {
+ input.close();
+ }
+ catch (IOException e)
+ {
+ // ignore
+ }
+ }
+ }
+ }
+
+ private static String readTextStream(InputStream input, String encoding) throws IOException {
+ Reader reader = null;
+ try
+ {
if (encoding != null)
{
if (encoding.equals(CHARSET_UTF_8))
@@ -360,14 +405,8 @@ public final class ActionsUtil
{
inputContents.append(readChars, 0, read);
}
- reader.close();
- input.close();
return inputContents.toString();
}
- catch (IOException e)
- {
- throw new JET2TagException(e);
- }
finally
{
// ensure stream and reader are closed.
@@ -394,8 +433,8 @@ public final class ActionsUtil
}
}
}
+
}
-
/**
* Remove the optional Byte Order Mark from UTF-8 input streams. A JRE bug means that Java doesn't do this
* automatically for UTF-8 files.
@@ -438,11 +477,21 @@ public final class ActionsUtil
*/
public static byte[] readBinaryFile(URL url) throws JET2TagException
{
- InputStream input = null;
+ try
+ {
+ return readBinaryStream(new BufferedInputStream(url.openStream()));
+ }
+ catch (IOException e)
+ {
+ throw new JET2TagException(e);
+ }
+ }
+
+ public static byte[] readBinaryStream(InputStream input) throws IOException
+ {
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))
@@ -451,10 +500,6 @@ public final class ActionsUtil
}
return output.toByteArray();
}
- catch (IOException e)
- {
- throw new JET2TagException(e);
- }
finally
{
if (input != null)
@@ -499,11 +544,13 @@ public final class ActionsUtil
* @param project the project to create
* @param description an option project description
* @param monitor a progress monitor
+ * @return <code>true</code> if the project was created, <code>false</code> if the project already existed.
* @throws JET2TagException if an error occurs
* @since 0.7.1
*/
- public static void createProject(IProject project, IProjectDescription description, IProgressMonitor monitor) throws JET2TagException
+ public static boolean createProject(IProject project, IProjectDescription description, IProgressMonitor monitor) throws JET2TagException
{
+ boolean projectCreated = false;
final String createMessage = MessageFormat.format(JET2Messages.WsProjectAction_CreatingProject, new Object []{ project.getName() });
monitor.beginTask(createMessage, 2); // two ticks: create & open
@@ -513,6 +560,7 @@ public final class ActionsUtil
if (!project.exists())
{
project.create(description, new SubProgressMonitor(monitor, 1));
+ projectCreated = true;
}
else
{
@@ -541,7 +589,7 @@ public final class ActionsUtil
{
monitor.done();
}
-
+ return projectCreated;
}
/**
@@ -599,4 +647,38 @@ public final class ActionsUtil
}
}
+ /**
+ * Finalize contents to write and determine if it must be written.
+ * @param file
+ * @param writer
+ * @param existingContents
+ * @return <code>true</code> if contents requires writing, <code>false</code> otherwise
+ */
+ static boolean finalizeAndTestForChange(IFile file, BufferedJET2Writer writer, InputStream existingContents)
+ {
+ String existingText = null;
+ if(existingContents != null) {
+ try
+ {
+ existingText = readTextStream(existingContents, file.getCharset());
+ }
+ catch (IOException e)
+ {
+ throw new JET2TagException(e);
+ }
+ catch (CoreException e)
+ {
+ throw new JET2TagException(e);
+ }
+ }
+
+ finalizeContent(writer, file, existingText);
+
+ if(existingText != null) {
+ return !existingText.equals(writer.getContent());
+ } else {
+ return true;
+ }
+ }
+
}
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 9c294ec..932aeae 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
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2005, 2006 IBM Corporation and others.
+ * Copyright (c) 2005, 2008 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
@@ -19,21 +19,31 @@ package org.eclipse.jet.taglib.workspace;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.jet.JET2Writer;
import org.eclipse.jet.taglib.JET2TagException;
import org.eclipse.jet.taglib.TagInfo;
/**
* Represents a workspace action managed by {@link WorkspaceContextExtender}.
+ * <p>
+ * Since 0.9 clients that perform expensive actions that can be avoided should implement
+ * {@link IWorkspaceAction2} in preference to {@link IWorkspaceAction} and should derive
+ * their implementations for {@link AbstractWorkspaceAction2} instead of {@link AbstractWorkspaceAction}.
+ * </p>
+ * <p>Since 0.9, the following extension interfaces may be implemented:</p>
+ * <ul>
+ * <li> {@link IWorkspaceActionExtension} to enable avoidance of unnecessary file writes on {@link JET2Writer} based actions</li>
+ * </ul>
*
*/
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
+ * Returns the workspace resource with which the action is associated. If the associated
+ * workspace resource is unknown at the time of the call, <code>null</code> may be returned.
+ * @return an Eclipse resource handle or <code>null</code>
* @throws JET2TagException if the resource cannot be created
*/
public abstract IResource getResource() throws JET2TagException;
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/workspace/IWorkspaceAction2.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/workspace/IWorkspaceAction2.java
new file mode 100644
index 0000000..b44542d
--- /dev/null
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/workspace/IWorkspaceAction2.java
@@ -0,0 +1,49 @@
+/**
+ * <copyright>
+ *
+ * Copyright (c) 2008 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: IWorkspaceAction2.java,v 1.1 2008/04/02 15:31:12 pelder Exp $
+ */
+package org.eclipse.jet.taglib.workspace;
+
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.jet.JET2Writer;
+import org.eclipse.jet.taglib.JET2TagException;
+
+/**
+ * Enhanced workspace action protocol based on {@link IWorkspaceAction} that provides feedback on whether
+ * an action IO action was performed.
+ * <p>
+ * Clients that perform expensive actions that can be avoided should implement
+ * {@link IWorkspaceAction2} in preference to {@link IWorkspaceAction} and should derive
+ * their implementations for {@link AbstractWorkspaceAction2} instead of {@link AbstractWorkspaceAction}.
+ * </p>
+ * <p>Since 0.9, the following extension interfaces may be implemented:</p>
+ * <ul>
+ * <li> {@link IWorkspaceActionExtension} to enable avoidance of unnecessary file writes on {@link JET2Writer} based actions</li>
+ * </ul>
+ */
+public interface IWorkspaceAction2 extends IWorkspaceAction
+{
+ /**
+ * Perform the workspace action if required. The method returns <code>true</code> if
+ * the action was performed and <code>false</code> otherwise. Note that implementations should
+ * throw a {@link JET2TagException} in the case of an error rather than returning <code>false</code>.
+ * The framework will call this method instead of {@link IWorkspaceAction#performAction(IProgressMonitor)}.
+ * @param monitor the progress monitor to which progress information will be written.
+ * @return <code>true</code> if files/folders were actually created/updated and <code>false</code> otherwise
+ * @throws JET2TagException if the action cannot be successfully performed.
+ */
+ public abstract boolean performActionIfRequired(IProgressMonitor monitor) throws JET2TagException;
+
+}
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/workspace/IWorkspaceActionExtension.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/workspace/IWorkspaceActionExtension.java
new file mode 100644
index 0000000..cea2890
--- /dev/null
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/workspace/IWorkspaceActionExtension.java
@@ -0,0 +1,36 @@
+/**
+ * <copyright>
+ *
+ * Copyright (c) 2008 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: IWorkspaceActionExtension.java,v 1.1 2008/04/02 15:31:12 pelder Exp $
+ */
+package org.eclipse.jet.taglib.workspace;
+
+import org.eclipse.jet.BufferedJET2Writer;
+
+/**
+ * Extension to {@link IWorkspaceAction} that permits actions to avoid
+ * unnecessary file IO if the generated contents have not changed and the
+ * contents is generated by a {@link BufferedJET2Writer}.
+ * @since 0.9
+ */
+public interface IWorkspaceActionExtension
+{
+
+ /**
+ * Return the buffered JET writer defining the contents to be written.
+ * @return a buffered JET writer instance
+ */
+ BufferedJET2Writer getContentWriter();
+
+}
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 8f3816a..2d8a591 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
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2005, 2006 IBM Corporation and others.
+ * Copyright (c) 2005, 2008 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
@@ -18,7 +18,9 @@ package org.eclipse.jet.taglib.workspace;
import java.io.IOException;
+import java.io.InputStream;
import java.net.URL;
+import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
@@ -33,12 +35,13 @@ 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.SafeRunner;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.jet.CoreJETException;
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.JET2TagException;
import org.eclipse.jet.transform.TransformContextExtender;
import org.eclipse.jet.transform.TransformContextListener;
@@ -176,27 +179,12 @@ public final class WorkspaceContextExtender
for (Iterator i = actionsToExecute.iterator(); i.hasNext();)
{
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
- {
- try
- {
- if (action.requiresValidateEdit())
- {
- filesToValidateEdit.add(action.getResource());
- }
- }
- catch (JET2TagException e)
- {
- handleException(e);
- }
- }});
+ if(isActionRequired(context, action)) {
+ doActionRequiresValidateEdit(context, action, filesToValidateEdit);
+ } else {
+ // won't execute this action, not contents have changed...
+ i.remove();
+ }
}
IStatus validateOk = ResourcesPlugin.getWorkspace().validateEdit(
(IFile[])filesToValidateEdit.toArray(new IFile [filesToValidateEdit.size()]),
@@ -223,24 +211,7 @@ public final class WorkspaceContextExtender
{
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
- {
- try
- {
- action.performAction(new SubProgressMonitor(submon, 1));
- }
- catch (JET2TagException e)
- {
- handleException(e);
- }
- }});
+ doActionPerformAction(context, action, submon);
submon.worked(1);
}
}
@@ -261,6 +232,143 @@ public final class WorkspaceContextExtender
}
}
+ protected boolean isActionRequired(JET2Context context, IWorkspaceAction action)
+ {
+ final IResource resource = action.getResource();
+ if(resource != null && !(resource instanceof IFile)) {
+ // For non-files, action is required only if resource does not exist
+ return !resource.exists();
+ }
+ if(!(action instanceof IWorkspaceActionExtension)) {
+ // Can't determine if contents are changed, must execute the action...
+ return true;
+ }
+ // attempt to read the existing contents....
+ IWorkspaceActionExtension ext = (IWorkspaceActionExtension)action;
+ final IFile file = (IFile)resource;
+ InputStream existingContents = null;
+ if (file != null && file.exists())
+ {
+ try
+ {
+ existingContents = file.getContents();
+ }
+ catch (CoreException e)
+ {
+ // exception reading existing contents, fall through...
+ }
+ }
+
+ return ActionsUtil.finalizeAndTestForChange(file, ext.getContentWriter(), existingContents);
+ }
+
+ /**
+ * @param context
+ * @param action
+ * @param filesToValidateEdit
+ */
+ protected void doActionRequiresValidateEdit(final JET2Context context, final IWorkspaceAction action, final List filesToValidateEdit)
+ {
+ SafeRunner.run(new ISafeRunnable() {
+
+ public void handleException(Throwable exception)
+ {
+ context.logError(action.getTemplatePath(), action.getTagInfo(), null, exception);
+ }
+
+ public void run() throws Exception
+ {
+ try
+ {
+ if (action.requiresValidateEdit())
+ {
+ filesToValidateEdit.add(action.getResource());
+ }
+ }
+ catch (JET2TagException e)
+ {
+ handleException(e);
+ }
+ }});
+ }
+
+ /**
+ * @param context
+ * @param action
+ * @throws JET2TagException
+ */
+ protected void doActionCommitted(final JET2Context context, final IWorkspaceAction action) throws JET2TagException
+ {
+ final IResource resource = action.getResource();
+ if(action instanceof IWorkspaceActionExtension) {
+ ActionsUtil.contentCommitted(((IWorkspaceActionExtension)action).getContentWriter(), resource);
+ }
+
+ if(resource != null) {
+ String msgPattern;
+ switch(resource.getType()) {
+ case IResource.FILE:
+ msgPattern = JET2Messages.WsFileFromWriterAction_WritingFile;
+ break;
+ case IResource.FOLDER:
+ msgPattern = JET2Messages.WsFolderAction_CreatingFolder;
+ break;
+ case IResource.PROJECT:
+ msgPattern = JET2Messages.WsProjectAction_CreatingProject;
+ break;
+ default:
+ msgPattern = null;
+ }
+ if (msgPattern != null)
+ {
+ final String fileMessage = MessageFormat.format(msgPattern,
+ new Object []{ resource.getFullPath().makeRelative().toString() });
+ RuntimeLoggerContextExtender.log(
+ context,
+ fileMessage,
+ action.getTagInfo(),
+ action.getTemplatePath(),
+ RuntimeLoggerContextExtender.INFO_LEVEL);
+ }
+ }
+ }
+
+ /**
+ * @param context
+ * @param action
+ * @param submon
+ */
+ protected void doActionPerformAction(final JET2Context context, final IWorkspaceAction action, final IProgressMonitor submon)
+ {
+ SafeRunner.run(new ISafeRunnable() {
+
+ public void handleException(Throwable exception)
+ {
+ context.logError(action.getTemplatePath(), action.getTagInfo(), null, exception);
+ }
+
+ public void run() throws Exception
+ {
+ try
+ {
+ boolean written;
+ if(action instanceof IWorkspaceAction2) {
+ written = ((IWorkspaceAction2)action).performActionIfRequired(new SubProgressMonitor(submon, 1));
+ } else {
+ action.performAction(new SubProgressMonitor(submon, 1));
+ written = true;
+ }
+ if(written) {
+ doActionCommitted(context, action);
+ }
+ }
+ catch (JET2TagException e)
+ {
+ handleException(e);
+ }
+ }});
+ }
+
}
/**

Back to the top