Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpelder2012-05-16 11:24:47 -0400
committerpelder2012-05-16 11:24:47 -0400
commit12ef64d582a841b367257e983e258d988226205c (patch)
tree8f62c9bfe0f93153927dfd1a4b894da5ab389e70
parent515333d558bc95507832f22367711390b45cf218 (diff)
downloadorg.eclipse.jet-12ef64d582a841b367257e983e258d988226205c.tar.gz
org.eclipse.jet-12ef64d582a841b367257e983e258d988226205c.tar.xz
org.eclipse.jet-12ef64d582a841b367257e983e258d988226205c.zip
[377257] When multiple actions to write to a single file, ensure they see prior actions work.
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/workspace/ActionsUtil.java47
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/taglib/workspace/WorkspaceContextExtender.java47
2 files changed, 63 insertions, 31 deletions
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 910fe7d..b929721 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, 2009 IBM Corporation and others.
+ * Copyright (c) 2006, 2012 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
@@ -240,7 +240,7 @@ public final class ActionsUtil
}
// Step 4: Apply generated file properties...
- file.setDerived(derived);
+ file.setDerived(derived, new NullProgressMonitor());
return true;
}
@@ -683,30 +683,49 @@ 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
+ * Return the text content of an existing file
+ * @param file a file handle or <code>null</code>
+ * @return the existing text content or <code>null</code>
+ * @throws JET2TagException if the file exists, but cannot be read.
*/
- static boolean finalizeAndTestForChange(IFile file, BufferedJET2Writer writer, InputStream existingContents)
+ static String getExistingFileText(IFile file)
{
- String existingText = null;
- if(existingContents != null) {
- try
+ if (file != null && file.exists())
+ {
+ try
{
- existingText = readTextStream(existingContents, file.getCharset());
+ final InputStream existingContents = file.getContents();
+ return readTextStream(existingContents, file.getCharset());
+
}
- catch (IOException e)
+ catch (CoreException e)
{
throw new JET2TagException(e);
}
- catch (CoreException e)
+ catch (IOException e)
{
throw new JET2TagException(e);
}
}
+ else
+ {
+ return null;
+ }
+
+ }
+
+ /**
+ * Finalize the writer, and then test it for change against <code>existingText</code>
+ * @param writer the writer to finalize
+ * @param existingText the existing file content or <code>null</code> if no content could be found
+ * @param file the file handle to be written.
+ * Passed to {@link IWriterListener#finalizeContent(JET2Writer, Object)} or {@link IWriterListenerExtension#finalizeContent(JET2Writer, Object, String)}
+ * @return <code>true</code> if the content has changed, or if <code>existingText</code> is <code>null</code>
+ * @throws JET2TagException if an error occurs during content finalization.
+ */
+ static boolean finalizeAndTestForChange(BufferedJET2Writer writer, String existingText, IFile file) throws JET2TagException
+ {
finalizeContent(writer, file, existingText);
if(existingText != null) {
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 562419b..ae7a60f 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, 2009 IBM Corporation and others.
+ * Copyright (c) 2005, 2012 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,12 +18,15 @@ 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.HashMap;
+import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
+import java.util.Map;
+import java.util.Set;
import java.util.Stack;
import org.eclipse.core.resources.IContainer;
@@ -157,6 +160,12 @@ public final class WorkspaceContextExtender
public final Stack containerStack = new Stack();
public final List finalWorkspaceActions = new ArrayList();
+
+ /**
+ * Used by {@link #commit(JET2Context, IProgressMonitor)} and helps to accumulate
+ * independent changes to the same file.
+ */
+ private Map filesAndActionsToWrite;
public ContextData()
{
@@ -175,6 +184,7 @@ public final class WorkspaceContextExtender
final List actionsToExecute = new ArrayList(actionsToExecuteCount);
actionsToExecute.addAll(workspaceActions);
actionsToExecute.addAll(finalWorkspaceActions);
+ filesAndActionsToWrite = new HashMap(actionsToExecuteCount);
TransformContextExtender tce = TransformContextExtender.getInstance(context);
monitor.beginTask(JET2Messages.WorkspaceContextExtender_Commiting, 1 + actionsToExecuteCount);
@@ -182,7 +192,7 @@ public final class WorkspaceContextExtender
try
{
monitor.subTask(JET2Messages.WorkspaceContextExtender_ConfirmingTeamAccess);
- final List filesToValidateEdit = new ArrayList(actionsToExecute.size());
+ final Set filesToValidateEdit = new HashSet(actionsToExecute.size());
for (Iterator i = actionsToExecute.iterator(); i.hasNext();)
{
checkCanceled(monitor);
@@ -238,6 +248,8 @@ public final class WorkspaceContextExtender
finally
{
monitor.done();
+ filesAndActionsToWrite.clear();
+ filesAndActionsToWrite = null;
}
}
@@ -253,22 +265,23 @@ public final class WorkspaceContextExtender
return true;
}
// attempt to read the existing contents....
- IWorkspaceActionExtension ext = (IWorkspaceActionExtension)action;
+ IWorkspaceActionExtension extendedAction = (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...
- }
+ final String existingFileContent;
+ if(file != null) {
+ final IWorkspaceActionExtension priorAction = (IWorkspaceActionExtension)filesAndActionsToWrite.get(file.getFullPath());
+
+ existingFileContent = priorAction != null
+ ? priorAction.getContentWriter().getContent()
+ : ActionsUtil.getExistingFileText(file);
+
+ // remember the current action as the most recent action on the file
+ filesAndActionsToWrite.put(file.getFullPath(), extendedAction);
+ } else {
+ existingFileContent = null;
}
- return ActionsUtil.finalizeAndTestForChange(file, ext.getContentWriter(), existingContents);
+ return ActionsUtil.finalizeAndTestForChange(extendedAction.getContentWriter(), existingFileContent, file);
}
private IResource getActionResource(IWorkspaceAction action)
@@ -290,7 +303,7 @@ public final class WorkspaceContextExtender
* @param action
* @param filesToValidateEdit
*/
- protected void doActionRequiresValidateEdit(final JET2Context context, final IWorkspaceAction action, final List filesToValidateEdit)
+ protected void doActionRequiresValidateEdit(final JET2Context context, final IWorkspaceAction action, final Set filesToValidateEdit)
{
SafeRunner.run(new ISafeRunnable() {

Back to the top