package de.grapheditor.ProjectManager;
import research.Drawing;
import research.store.StorableInput;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipFile;
import java.util.zip.ZipException;
import java.util.zip.ZipEntry;
import de.grapheditor.store.AdaptedStandardStorageFormat;
/**
* Created by IntelliJ IDEA.
* User: zhangwei
* Date: 2004-7-4
* Time: 0:24:16
* To change this template use File | Settings | File Templates.
*/
public class ProjectReader {
protected String[] viewNameArray = null;
Drawing[] drawingArray = null;
protected String errorInfo = null;
protected File file = null;
protected ZipFile zipFile = null;
public ProjectReader(File projectFile){
this.file = projectFile;
}
public ProjectReader(ZipFile projectFile){
this.zipFile = projectFile;
}
public String[] getViewNameArray(){
return (String[]) viewNameArray.clone();
}
public Drawing[] getDrawingArray(){
return (Drawing[]) drawingArray.clone();
}
public String getErrorInfo() {
return errorInfo;
}
public boolean readProject() {
if (file != null){
try {
zipFile = new ZipFile(file);
} catch (ZipException zipException) {
errorInfo = "�ļ���ʽ����";
return false;
} catch (IOException ioException) {
errorInfo = "�ļ�IO����";
return false;
}
}
ZipEntry zipEntry = null;
//begin ��ȡ��Ŀ�����ļ�
try {
zipEntry = zipFile.getEntry("index.txt");
InputStream inputStream = zipFile.getInputStream(zipEntry);
StorableInput indexInput = new StorableInput(inputStream);
int viewNum = indexInput.readInt();
if (viewNum > 0) {
viewNameArray = new String[viewNum];
for (int i = 0; i < viewNum; i++) {
viewNameArray[i] = indexInput.readString();
}
} else {
viewNameArray = new String[0];
}
} catch (Exception exception) {
errorInfo = "Exception occurs when read project index file.\n" + exception;
return false;
}
//end ��ȡ��Ŀ�����ļ�
AdaptedStandardStorageFormat reader = new AdaptedStandardStorageFormat();
if (viewNameArray.length > 0) {
//begin ��ȡͼԪ��Ϣ�ļ�
try {
drawingArray = new Drawing[viewNameArray.length];
for (int i = 0; i < viewNameArray.length; i++) {
zipEntry = zipFile.getEntry(viewNameArray[i] + ".draw");
InputStream inputStream = zipFile.getInputStream(zipEntry);
drawingArray[i] = reader.restore(inputStream);
}
} catch (Exception exception) {
errorInfo = "Exception occurs when read project graphic file.\n" + exception;
return false;
}
//end ��ȡͼԪ��Ϣ�ļ�
} else {
drawingArray = new Drawing[0];
}
errorInfo = null;
return true;
}
}