Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authormkersten2005-07-14 21:48:44 +0000
committermkersten2005-07-14 21:48:44 +0000
commit587623e4f4aee31ef3897e9d178d8d77189245cc (patch)
tree2fabe2e23caa9e494559b42cad69282956839a42 /org.eclipse.mylyn.tasks.ui
parentfd915922c4e76a819a5d25a8f39e6a2c4fd53745 (diff)
downloadorg.eclipse.mylyn.tasks-587623e4f4aee31ef3897e9d178d8d77189245cc.tar.gz
org.eclipse.mylyn.tasks-587623e4f4aee31ef3897e9d178d8d77189245cc.tar.xz
org.eclipse.mylyn.tasks-587623e4f4aee31ef3897e9d178d8d77189245cc.zip
Bugzilla Bug 103107
bugzilla fails to load corrupted offline reports
Diffstat (limited to 'org.eclipse.mylyn.tasks.ui')
-rw-r--r--org.eclipse.mylyn.tasks.ui/src/org/eclipse/mylyn/tasks/ITaskListExternalizer.java5
-rw-r--r--org.eclipse.mylyn.tasks.ui/src/org/eclipse/mylyn/tasks/internal/DefaultTaskListExternalizer.java51
-rw-r--r--org.eclipse.mylyn.tasks.ui/src/org/eclipse/mylyn/tasks/internal/MylarExternalizerException.java30
-rw-r--r--org.eclipse.mylyn.tasks.ui/src/org/eclipse/mylyn/tasks/internal/TaskListExternalizer.java125
4 files changed, 162 insertions, 49 deletions
diff --git a/org.eclipse.mylyn.tasks.ui/src/org/eclipse/mylyn/tasks/ITaskListExternalizer.java b/org.eclipse.mylyn.tasks.ui/src/org/eclipse/mylyn/tasks/ITaskListExternalizer.java
index 8bafc5a22..a83cbbd00 100644
--- a/org.eclipse.mylyn.tasks.ui/src/org/eclipse/mylyn/tasks/ITaskListExternalizer.java
+++ b/org.eclipse.mylyn.tasks.ui/src/org/eclipse/mylyn/tasks/ITaskListExternalizer.java
@@ -11,6 +11,7 @@
package org.eclipse.mylar.tasks;
+import org.eclipse.mylar.tasks.internal.MylarExternalizerException;
import org.eclipse.mylar.tasks.internal.TaskList;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
@@ -49,9 +50,9 @@ public interface ITaskListExternalizer {
public abstract boolean canReadCategory(Node node);
- public abstract void readCategory(Node node, TaskList tlist);
+ public abstract void readCategory(Node node, TaskList tlist) throws MylarExternalizerException;
public abstract boolean canReadTask(Node node);
- public abstract ITask readTask(Node node, TaskList tlist, AbstractCategory category, ITask parent);
+ public abstract ITask readTask(Node node, TaskList tlist, AbstractCategory category, ITask parent) throws MylarExternalizerException;
}
diff --git a/org.eclipse.mylyn.tasks.ui/src/org/eclipse/mylyn/tasks/internal/DefaultTaskListExternalizer.java b/org.eclipse.mylyn.tasks.ui/src/org/eclipse/mylyn/tasks/internal/DefaultTaskListExternalizer.java
index efd77010e..96109eb91 100644
--- a/org.eclipse.mylyn.tasks.ui/src/org/eclipse/mylyn/tasks/internal/DefaultTaskListExternalizer.java
+++ b/org.eclipse.mylyn.tasks.ui/src/org/eclipse/mylyn/tasks/internal/DefaultTaskListExternalizer.java
@@ -122,7 +122,8 @@ public class DefaultTaskListExternalizer implements ITaskListExternalizer {
return node.getNodeName().equals(getCategoryTagName());
}
- public void readCategory(Node node, TaskList tlist) {
+ public void readCategory(Node node, TaskList tlist) throws MylarExternalizerException {
+ boolean hasCaughtException = false;
Element element = (Element) node;
TaskCategory category = new TaskCategory(element.getAttribute("Name"));
tlist.addCategory(category);
@@ -130,32 +131,58 @@ public class DefaultTaskListExternalizer implements ITaskListExternalizer {
for (int i = 0; i < list.getLength(); i++) {
Node child = list.item(i);
boolean read = false;
- for (ITaskListExternalizer externalizer : externalizers) {
- if (externalizer.canReadTask(child)) {
- category.addTask(externalizer.readTask(child, tlist, category, null));
- read = true;
+ try {
+ for (ITaskListExternalizer externalizer : externalizers) {
+ if (externalizer.canReadTask(child)) {
+ category.addTask(externalizer.readTask(child, tlist,
+ category, null));
+ read = true;
+ }
+ }
+ if (!read && canReadTask(child)) {
+ category.addTask(readTask(child, tlist, category, null));
}
+ } catch (MylarExternalizerException e) {
+ hasCaughtException = true;
}
- if (!read) category.addTask(readTask(child, tlist, category, null));
}
+ if (hasCaughtException) throw new MylarExternalizerException("Failed to load all tasks");
}
public boolean canReadTask(Node node) {
return node.getNodeName().equals(getTaskTagName());
}
- public ITask readTask(Node node, TaskList tlist, AbstractCategory category, ITask parent) {
+ public ITask readTask(Node node, TaskList tlist, AbstractCategory category, ITask parent) throws MylarExternalizerException {
Element element = (Element) node;
- String handle = element.getAttribute(HANDLE);
- String label = element.getAttribute(LABEL);
+ String handle;
+ String label;
+ if (element.hasAttribute(HANDLE)) {
+ handle = element.getAttribute(HANDLE);
+ } else {
+ throw new MylarExternalizerException("Handle not stored for task");
+ }
+ if (element.hasAttribute(LABEL)) {
+ label = element.getAttribute(LABEL);
+ } else {
+ label = "Description was corrupted in stored tasklist";
+ }
Task task = new Task(handle, label);
readTaskInfo(task, tlist, element, category, parent);
return task;
}
- protected void readTaskInfo(ITask task, TaskList tlist, Element element, AbstractCategory category, ITask parent) {
- task.setPriority(element.getAttribute(PRIORITY));
- task.setPath(element.getAttribute(PATH));
+ protected void readTaskInfo(ITask task, TaskList tlist, Element element, AbstractCategory category, ITask parent) throws MylarExternalizerException{
+ if (element.hasAttribute(PRIORITY)) {
+ task.setPriority(element.getAttribute(PRIORITY));
+ } else {
+ task.setPriority("P3");
+ }
+ if (element.hasAttribute(PATH)) {
+ task.setPath(element.getAttribute(PATH));
+ } else {
+ task.setPath(task.getHandle());
+ }
if (element.getAttribute(ACTIVE).compareTo(TRUE) == 0) {
task.setActive(true);
diff --git a/org.eclipse.mylyn.tasks.ui/src/org/eclipse/mylyn/tasks/internal/MylarExternalizerException.java b/org.eclipse.mylyn.tasks.ui/src/org/eclipse/mylyn/tasks/internal/MylarExternalizerException.java
new file mode 100644
index 000000000..427a7ce70
--- /dev/null
+++ b/org.eclipse.mylyn.tasks.ui/src/org/eclipse/mylyn/tasks/internal/MylarExternalizerException.java
@@ -0,0 +1,30 @@
+/*******************************************************************************
+ * Copyright (c) 2004 - 2005 University Of British Columbia 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:
+ * University Of British Columbia - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.mylar.tasks.internal;
+
+/**
+ * @author Mik Kersten and Ken Sueda
+ */
+public class MylarExternalizerException extends Exception {
+
+ /**
+ *
+ */
+ private static final long serialVersionUID = 5804522104992031907L;
+
+ public MylarExternalizerException() {
+ super();
+ }
+
+ public MylarExternalizerException(String detailMessage) {
+ super(detailMessage);
+ }
+}
diff --git a/org.eclipse.mylyn.tasks.ui/src/org/eclipse/mylyn/tasks/internal/TaskListExternalizer.java b/org.eclipse.mylyn.tasks.ui/src/org/eclipse/mylyn/tasks/internal/TaskListExternalizer.java
index 5f6aea23e..050948a8a 100644
--- a/org.eclipse.mylyn.tasks.ui/src/org/eclipse/mylyn/tasks/internal/TaskListExternalizer.java
+++ b/org.eclipse.mylyn.tasks.ui/src/org/eclipse/mylyn/tasks/internal/TaskListExternalizer.java
@@ -1,4 +1,3 @@
-
/*******************************************************************************
* Copyright (c) 2004 - 2005 University Of British Columbia and others.
* All rights reserved. This program and the accompanying materials
@@ -12,8 +11,10 @@
package org.eclipse.mylar.tasks.internal;
import java.io.File;
+import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
+import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
@@ -52,6 +53,7 @@ public class TaskListExternalizer {
private DefaultTaskListExternalizer defaultExternalizer = new DefaultTaskListExternalizer();
private String readVersion = "";
+ private boolean hasCaughtException = false;
public void addExternalizer(ITaskListExternalizer externalizer) {
externalizers.add(externalizer);
@@ -184,11 +186,17 @@ public class TaskListExternalizer {
// }
public void readTaskList(TaskList tlist, File inFile) {
+ hasCaughtException = false;
try {
// parse file
//
+ if (!inFile.exists())
+ return;
Document doc = openAsDOM(inFile);
-
+ if (doc == null) {
+ handleException(inFile, null, new MylarExternalizerException("Tasklist was not well formed XML"));
+ return;
+ }
// read root node to get version number
//
Element root = doc.getDocumentElement();
@@ -207,46 +215,53 @@ public class TaskListExternalizer {
for (int i = 0; i < list.getLength(); i++) {
Node child = list.item(i);
boolean wasRead = false;
- if (child.getNodeName().endsWith(DefaultTaskListExternalizer.TAG_CATEGORY)) {
- for (ITaskListExternalizer externalizer : externalizers) {
- if (externalizer.canReadCategory(child)) {
- externalizer.readCategory(child, tlist);
- wasRead = true;
- break;
+ try {
+ if (child.getNodeName().endsWith(DefaultTaskListExternalizer.TAG_CATEGORY)) {
+ for (ITaskListExternalizer externalizer : externalizers) {
+ if (externalizer.canReadCategory(child)) {
+ externalizer.readCategory(child, tlist);
+ wasRead = true;
+ break;
+ }
}
- }
- if (!wasRead && defaultExternalizer.canReadCategory(child)) {
- defaultExternalizer.readCategory(child, tlist);
+ if (!wasRead && defaultExternalizer.canReadCategory(child)) {
+ defaultExternalizer.readCategory(child, tlist);
+ } else {
+ // MylarPlugin.log("Did not read: " +
+ // child.getNodeName(), this);
+ }
} else {
-// MylarPlugin.log("Did not read: " + child.getNodeName(), this);
- }
- } else {
- for (ITaskListExternalizer externalizer : externalizers) {
- if (externalizer.canReadTask(child)) {
- // TODO add the tasks properly
- ITask newTask = externalizer.readTask(child, tlist, null, null);
- if(MylarTasksPlugin.getDefault().getContributor() != null && MylarTasksPlugin.getDefault().getContributor().acceptsItem(newTask)){
- newTask = MylarTasksPlugin.getDefault().getContributor().taskAdded(newTask);
- }
- tlist.addRootTask(newTask);
-
- wasRead = true;
- break;
+ for (ITaskListExternalizer externalizer : externalizers) {
+ if (externalizer.canReadTask(child)) {
+ // TODO add the tasks properly
+ ITask newTask = externalizer.readTask(child, tlist, null, null);
+ if(MylarTasksPlugin.getDefault().getContributor() != null && MylarTasksPlugin.getDefault().getContributor().acceptsItem(newTask)){
+ newTask = MylarTasksPlugin.getDefault().getContributor().taskAdded(newTask);
+ }
+ tlist.addRootTask(newTask);
+
+ wasRead = true;
+ break;
+ }
+ }
+ if (!wasRead && defaultExternalizer.canReadTask(child)) {
+ tlist.addRootTask(defaultExternalizer.readTask(child, tlist, null, null));
+ } else {
+ // MylarPlugin.log("Did not read: " + child.getNodeName(), this);
}
}
- if (!wasRead && defaultExternalizer.canReadTask(child)) {
- tlist.addRootTask(defaultExternalizer.readTask(child, tlist, null, null));
- } else {
-// MylarPlugin.log("Did not read: " + child.getNodeName(), this);
- }
+ } catch (Exception e) {
+ handleException(inFile, child, e);
}
}
}
} catch (Exception e) {
- String name = inFile.getAbsolutePath();
- name = name.substring(0, name.lastIndexOf('.')) + "-save.xml";
- inFile.renameTo(new File(name));
- MylarPlugin.log(e, "Could not read task list");
+ handleException(inFile, null, e);
+ }
+ if (hasCaughtException) {
+ // if exception was caught, write out the new task file, so that it doesn't happen again.
+ // this is OK, since the original (corrupt) tasklist is saved.
+ writeTaskList(tlist, inFile);
}
}
@@ -287,8 +302,48 @@ public class TaskListExternalizer {
}
return document;
}
+
+ private void handleException(File inFile, Node child, Exception e) {
+ hasCaughtException = true;
+ String name = inFile.getAbsolutePath();
+ name = name.substring(0, name.lastIndexOf('.')) + "-save1.xml";
+ File save = new File(name);
+ int i = 2;
+ while(save.exists()) {
+ name = name.substring(0, name.lastIndexOf('.')-1) + i + ".xml";
+ save = new File(name);
+ i++;
+ }
+ if (!copy(inFile, save)) {
+ inFile.renameTo(new File(name));
+ }
+ if (child == null) {
+ MylarPlugin.log(e, "Could not read task list");
+ } else {
+ MylarPlugin.log(e, "Tasks may have been lost from " + child.getNodeName());
+ }
+ }
+ private boolean copy(File src, File dst) {
+ try {
+ InputStream in = new FileInputStream(src);
+ OutputStream out = new FileOutputStream(dst);
+
+ // Transfer bytes from in to out
+ byte[] buf = new byte[1024];
+ int len;
+ while ((len = in.read(buf)) > 0) {
+ out.write(buf, 0, len);
+ }
+ in.close();
+ out.close();
+ return true;
+ } catch (IOException ioe) {
+ return false;
+ }
+ }
-// private static ITask readTaskAndSubTasks(Node node, ITask root, TaskList tlist) {
+// private static ITask readTaskAndSubTasks(Node node, ITask root, TaskList
+// tlist) {
// //extract node and create new sub task
// //
// Element e = (Element) node;

Back to the top