From 983c65fc5846ab50960e229bbafc586c6336c4ff Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Wed, 9 Oct 2013 21:25:21 +0000 Subject: [PATCH] * if history.xml is invalid xml for some reason just ignore and override the next time it's written to file --- source/net/sourceforge/filebot/History.java | 127 ++++++++------------ 1 file changed, 50 insertions(+), 77 deletions(-) diff --git a/source/net/sourceforge/filebot/History.java b/source/net/sourceforge/filebot/History.java index 830952df..972c64e0 100644 --- a/source/net/sourceforge/filebot/History.java +++ b/source/net/sourceforge/filebot/History.java @@ -1,11 +1,8 @@ - package net.sourceforge.filebot; - import static java.util.Collections.*; import java.io.File; -import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; @@ -13,159 +10,138 @@ import java.util.Arrays; import java.util.Collection; import java.util.Date; import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; import javax.xml.bind.JAXBContext; -import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; - @XmlRootElement(name = "history") public class History { - + @XmlElement(name = "sequence") private List sequences; - - + public History() { this.sequences = new ArrayList(); } - - + public History(Collection sequences) { this.sequences = new ArrayList(sequences); } - - + public static class Sequence { - + @XmlAttribute(name = "date", required = true) private Date date; - + @XmlElement(name = "rename", required = true) private List elements; - - + private Sequence() { // hide constructor } - - + public Date date() { return date; } - - + public List elements() { if (elements == null) return emptyList(); - + return unmodifiableList(elements); } - - + @Override public boolean equals(Object obj) { if (obj instanceof Sequence) { Sequence other = (Sequence) obj; return date.equals(other.date) && elements.equals(other.elements); } - + return false; } - - + @Override public int hashCode() { return Arrays.hashCode(new Object[] { elements, date }); } } - - + public static class Element { - + @XmlAttribute(name = "dir", required = true) private File dir; - + @XmlAttribute(name = "from", required = true) private String from; - + @XmlAttribute(name = "to", required = true) private String to; - - + public Element() { // used by JAXB } - - + public Element(String from, String to, File dir) { this.from = from; this.to = to; this.dir = dir; } - - + public File dir() { return dir; } - - + public String from() { return from; } - - + public String to() { return to; } - - + @Override public boolean equals(Object obj) { if (obj instanceof Element) { Element element = (Element) obj; return to.equals(element.to) && from.equals(element.from) && dir.getPath().equals(element.dir.getPath()); } - + return false; } - - + @Override public int hashCode() { return Arrays.hashCode(new Object[] { to, from, dir }); } } - - + public List sequences() { return unmodifiableList(sequences); } - - + public void add(Collection elements) { Sequence sequence = new Sequence(); sequence.date = new Date(); sequence.elements = new ArrayList(elements); - + add(sequence); } - - + public void add(Sequence sequence) { this.sequences.add(sequence); } - - + public void addAll(Collection sequences) { this.sequences.addAll(sequences); } - - + public void merge(History history) { for (Sequence sequence : history.sequences()) { if (!sequences.contains(sequence)) { @@ -173,8 +149,7 @@ public class History { } } } - - + public int totalSize() { int i = 0; for (Sequence it : sequences()) { @@ -182,48 +157,46 @@ public class History { } return i; } - - + public void clear() { sequences.clear(); } - - + @Override public boolean equals(Object obj) { if (obj instanceof History) { History other = (History) obj; return sequences.equals(other.sequences); } - + return false; } - - + @Override public int hashCode() { return sequences.hashCode(); } - - - public static void exportHistory(History history, OutputStream output) throws IOException { + + public static void exportHistory(History history, OutputStream output) { try { Marshaller marshaller = JAXBContext.newInstance(History.class).createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); marshaller.marshal(history, output); - } catch (JAXBException e) { - throw new IOException(e); + } catch (Exception e) { + Logger.getLogger(History.class.getName()).log(Level.SEVERE, "Failed to write history", e); } } - - - public static History importHistory(InputStream stream) throws IOException { + + public static History importHistory(InputStream stream) { try { Unmarshaller unmarshaller = JAXBContext.newInstance(History.class).createUnmarshaller(); return ((History) unmarshaller.unmarshal(stream)); - } catch (JAXBException e) { - throw new IOException(e); + } catch (Exception e) { + Logger.getLogger(History.class.getName()).log(Level.SEVERE, "Failed to read history", e); + + // fail-safe => default to empty history + return new History(); } } - + }