Coverage Report - org.jtheque.core.managers.file.impl.XMLRestorer
 
Classes in this File Line Coverage Branch Coverage Complexity
XMLRestorer
0 %
0/16
0 %
0/10
5
 
 1  
 package org.jtheque.core.managers.file.impl;
 2  
 
 3  
 /*
 4  
  * This file is part of JTheque.
 5  
  *
 6  
  * JTheque is free software: you can redistribute it and/or modify
 7  
  * it under the terms of the GNU General Public License as published by
 8  
  * the Free Software Foundation, either version 3 of the License.
 9  
  *
 10  
  * JTheque is distributed in the hope that it will be useful,
 11  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13  
  * GNU General Public License for more details.
 14  
  *
 15  
  * You should have received a copy of the GNU General Public License
 16  
  * along with JTheque.  If not, see <http://www.gnu.org/licenses/>.
 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  
  * A Restorer for the XML format.
 33  
  *
 34  
  * @author Baptiste Wicht
 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  
 }