* if history.xml is invalid xml for some reason just ignore and override the next time it's written to file
This commit is contained in:
parent
19f69c9fbc
commit
983c65fc58
|
@ -1,11 +1,8 @@
|
||||||
|
|
||||||
package net.sourceforge.filebot;
|
package net.sourceforge.filebot;
|
||||||
|
|
||||||
|
|
||||||
import static java.util.Collections.*;
|
import static java.util.Collections.*;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
@ -13,33 +10,30 @@ import java.util.Arrays;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import javax.xml.bind.JAXBContext;
|
import javax.xml.bind.JAXBContext;
|
||||||
import javax.xml.bind.JAXBException;
|
|
||||||
import javax.xml.bind.Marshaller;
|
import javax.xml.bind.Marshaller;
|
||||||
import javax.xml.bind.Unmarshaller;
|
import javax.xml.bind.Unmarshaller;
|
||||||
import javax.xml.bind.annotation.XmlAttribute;
|
import javax.xml.bind.annotation.XmlAttribute;
|
||||||
import javax.xml.bind.annotation.XmlElement;
|
import javax.xml.bind.annotation.XmlElement;
|
||||||
import javax.xml.bind.annotation.XmlRootElement;
|
import javax.xml.bind.annotation.XmlRootElement;
|
||||||
|
|
||||||
|
|
||||||
@XmlRootElement(name = "history")
|
@XmlRootElement(name = "history")
|
||||||
public class History {
|
public class History {
|
||||||
|
|
||||||
@XmlElement(name = "sequence")
|
@XmlElement(name = "sequence")
|
||||||
private List<Sequence> sequences;
|
private List<Sequence> sequences;
|
||||||
|
|
||||||
|
|
||||||
public History() {
|
public History() {
|
||||||
this.sequences = new ArrayList<Sequence>();
|
this.sequences = new ArrayList<Sequence>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public History(Collection<Sequence> sequences) {
|
public History(Collection<Sequence> sequences) {
|
||||||
this.sequences = new ArrayList<Sequence>(sequences);
|
this.sequences = new ArrayList<Sequence>(sequences);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static class Sequence {
|
public static class Sequence {
|
||||||
|
|
||||||
@XmlAttribute(name = "date", required = true)
|
@XmlAttribute(name = "date", required = true)
|
||||||
|
@ -48,17 +42,14 @@ public class History {
|
||||||
@XmlElement(name = "rename", required = true)
|
@XmlElement(name = "rename", required = true)
|
||||||
private List<Element> elements;
|
private List<Element> elements;
|
||||||
|
|
||||||
|
|
||||||
private Sequence() {
|
private Sequence() {
|
||||||
// hide constructor
|
// hide constructor
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public Date date() {
|
public Date date() {
|
||||||
return date;
|
return date;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public List<Element> elements() {
|
public List<Element> elements() {
|
||||||
if (elements == null)
|
if (elements == null)
|
||||||
return emptyList();
|
return emptyList();
|
||||||
|
@ -66,7 +57,6 @@ public class History {
|
||||||
return unmodifiableList(elements);
|
return unmodifiableList(elements);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object obj) {
|
public boolean equals(Object obj) {
|
||||||
if (obj instanceof Sequence) {
|
if (obj instanceof Sequence) {
|
||||||
|
@ -77,14 +67,12 @@ public class History {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
return Arrays.hashCode(new Object[] { elements, date });
|
return Arrays.hashCode(new Object[] { elements, date });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static class Element {
|
public static class Element {
|
||||||
|
|
||||||
@XmlAttribute(name = "dir", required = true)
|
@XmlAttribute(name = "dir", required = true)
|
||||||
|
@ -96,34 +84,28 @@ public class History {
|
||||||
@XmlAttribute(name = "to", required = true)
|
@XmlAttribute(name = "to", required = true)
|
||||||
private String to;
|
private String to;
|
||||||
|
|
||||||
|
|
||||||
public Element() {
|
public Element() {
|
||||||
// used by JAXB
|
// used by JAXB
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public Element(String from, String to, File dir) {
|
public Element(String from, String to, File dir) {
|
||||||
this.from = from;
|
this.from = from;
|
||||||
this.to = to;
|
this.to = to;
|
||||||
this.dir = dir;
|
this.dir = dir;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public File dir() {
|
public File dir() {
|
||||||
return dir;
|
return dir;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public String from() {
|
public String from() {
|
||||||
return from;
|
return from;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public String to() {
|
public String to() {
|
||||||
return to;
|
return to;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object obj) {
|
public boolean equals(Object obj) {
|
||||||
if (obj instanceof Element) {
|
if (obj instanceof Element) {
|
||||||
|
@ -134,19 +116,16 @@ public class History {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
return Arrays.hashCode(new Object[] { to, from, dir });
|
return Arrays.hashCode(new Object[] { to, from, dir });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public List<Sequence> sequences() {
|
public List<Sequence> sequences() {
|
||||||
return unmodifiableList(sequences);
|
return unmodifiableList(sequences);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void add(Collection<Element> elements) {
|
public void add(Collection<Element> elements) {
|
||||||
Sequence sequence = new Sequence();
|
Sequence sequence = new Sequence();
|
||||||
sequence.date = new Date();
|
sequence.date = new Date();
|
||||||
|
@ -155,17 +134,14 @@ public class History {
|
||||||
add(sequence);
|
add(sequence);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void add(Sequence sequence) {
|
public void add(Sequence sequence) {
|
||||||
this.sequences.add(sequence);
|
this.sequences.add(sequence);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void addAll(Collection<Sequence> sequences) {
|
public void addAll(Collection<Sequence> sequences) {
|
||||||
this.sequences.addAll(sequences);
|
this.sequences.addAll(sequences);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void merge(History history) {
|
public void merge(History history) {
|
||||||
for (Sequence sequence : history.sequences()) {
|
for (Sequence sequence : history.sequences()) {
|
||||||
if (!sequences.contains(sequence)) {
|
if (!sequences.contains(sequence)) {
|
||||||
|
@ -174,7 +150,6 @@ public class History {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public int totalSize() {
|
public int totalSize() {
|
||||||
int i = 0;
|
int i = 0;
|
||||||
for (Sequence it : sequences()) {
|
for (Sequence it : sequences()) {
|
||||||
|
@ -183,12 +158,10 @@ public class History {
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void clear() {
|
public void clear() {
|
||||||
sequences.clear();
|
sequences.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object obj) {
|
public boolean equals(Object obj) {
|
||||||
if (obj instanceof History) {
|
if (obj instanceof History) {
|
||||||
|
@ -199,30 +172,30 @@ public class History {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
return sequences.hashCode();
|
return sequences.hashCode();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void exportHistory(History history, OutputStream output) {
|
||||||
public static void exportHistory(History history, OutputStream output) throws IOException {
|
|
||||||
try {
|
try {
|
||||||
Marshaller marshaller = JAXBContext.newInstance(History.class).createMarshaller();
|
Marshaller marshaller = JAXBContext.newInstance(History.class).createMarshaller();
|
||||||
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
|
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
|
||||||
marshaller.marshal(history, output);
|
marshaller.marshal(history, output);
|
||||||
} catch (JAXBException e) {
|
} catch (Exception e) {
|
||||||
throw new IOException(e);
|
Logger.getLogger(History.class.getName()).log(Level.SEVERE, "Failed to write history", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static History importHistory(InputStream stream) {
|
||||||
public static History importHistory(InputStream stream) throws IOException {
|
|
||||||
try {
|
try {
|
||||||
Unmarshaller unmarshaller = JAXBContext.newInstance(History.class).createUnmarshaller();
|
Unmarshaller unmarshaller = JAXBContext.newInstance(History.class).createUnmarshaller();
|
||||||
return ((History) unmarshaller.unmarshal(stream));
|
return ((History) unmarshaller.unmarshal(stream));
|
||||||
} catch (JAXBException e) {
|
} catch (Exception e) {
|
||||||
throw new IOException(e);
|
Logger.getLogger(History.class.getName()).log(Level.SEVERE, "Failed to read history", e);
|
||||||
|
|
||||||
|
// fail-safe => default to empty history
|
||||||
|
return new History();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue