| 1 | |
package org.jtheque.core.managers.file.impl; |
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
import org.jtheque.core.managers.file.IFileManager.XmlBackupVersion; |
| 20 | |
import org.jtheque.core.managers.file.able.BackupReader; |
| 21 | |
import org.jtheque.core.managers.file.able.FileType; |
| 22 | |
import org.jtheque.core.managers.file.able.Restorer; |
| 23 | |
import org.jtheque.core.utils.file.XMLException; |
| 24 | |
import org.jtheque.core.utils.file.XMLReader; |
| 25 | |
import org.jtheque.utils.io.FileException; |
| 26 | |
import org.jtheque.utils.io.FileUtils; |
| 27 | |
|
| 28 | |
import java.io.File; |
| 29 | |
import java.util.Collection; |
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | 0 | public final class XMLRestorer implements Restorer { |
| 37 | |
@Override |
| 38 | |
public void restore(File file, Collection<BackupReader> readers) throws FileException { |
| 39 | 0 | XMLReader reader = new XMLReader(); |
| 40 | |
|
| 41 | |
try { |
| 42 | 0 | reader.openFile(file); |
| 43 | |
|
| 44 | 0 | if (reader.getRootElement() == null) { |
| 45 | 0 | throw new FileException("File corrupted (no root element)"); |
| 46 | |
} |
| 47 | |
|
| 48 | 0 | int version = reader.readInt("./header/file-version", reader.getRootElement()); |
| 49 | |
|
| 50 | 0 | if (version != XmlBackupVersion.FIRST.ordinal() && version != XmlBackupVersion.SECOND.ordinal()) { |
| 51 | 0 | throw new FileException("Unsupported version"); |
| 52 | |
} |
| 53 | |
|
| 54 | 0 | for (BackupReader r : readers) { |
| 55 | 0 | r.readBackup(reader.getRootElement()); |
| 56 | |
} |
| 57 | 0 | } catch (XMLException e) { |
| 58 | 0 | throw new FileException("File corrupted", e); |
| 59 | |
} finally { |
| 60 | 0 | FileUtils.close(reader); |
| 61 | 0 | } |
| 62 | 0 | } |
| 63 | |
|
| 64 | |
@Override |
| 65 | |
public boolean canImportFrom(FileType fileType) { |
| 66 | 0 | return fileType == FileType.XML; |
| 67 | |
} |
| 68 | |
} |