package de.FeatureModellingTool.ProjectManager.reuse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import research.Drawing;
import research.store.StorableInput;
import component.FComponent;
import de.FeatureModellingTool.IProjectReader;
import de.FeatureModellingTool.Customize.CustomizationVersion;
import de.FeatureModellingTool.Customize.store.CmdlParser;
import de.FeatureModellingTool.Customize.store.CmdlParserPrototype;
import de.FeatureModellingTool.FeatureModel.store.FmdlParser;
import de.FeatureModellingTool.FeatureModel.store.FmdlParserPrototype;
import de.FeatureModellingTool.GraphicalEditor.store.AdaptedStandardStorageFormat;
import de.reuse.GroupMap;
import de.reuse.GroupTreeMap;
/**
* Created by IntelliJ IDEA.
* User: saturn
* Date: 2004-3-23
* Time: 22:26:01
* To change this template use File | Settings | File Templates.
*/
public class ProjectReader implements IProjectReader {
protected String domainName = null;
protected String[] viewNameArray = null;
FComponent featureModel = null;
Drawing[] drawingArray = null;
GroupTreeMap informationMap = null;
protected String errorInfo = null;
protected File file = null;
protected ZipFile zipFile = null;
public ProjectReader(){
}
public ProjectReader(File projectFile){
this.file = projectFile;
}
public String getDomainName(){
return domainName;
}
public String[] getViewNameArray(){
return (String[]) viewNameArray.clone();
}
public Drawing[] getDrawingArray(){
return (Drawing[]) drawingArray.clone();
}
public GroupMap getInformationMap(){
return (GroupMap)informationMap.clone();
}
public FComponent getFeatureModel(){
return featureModel.getInstance(null);
}
public String getErrorInfo() {
return errorInfo;
}
public boolean readProject() {
if (file != null){
try {
zipFile = new ZipFile(file);
} catch (ZipException zipException) {
errorInfo = "�ļ���ʽ����";
System.out.println(zipException);
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);
domainName = indexInput.readString();
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 ��ȡ��Ŀ�����ļ�
FmdlParser parser = new FmdlParserPrototype();
//begin ��ȡ����ģ�������ļ�
try {
zipEntry = zipFile.getEntry(domainName + ".fm");
InputStream inputStream = zipFile.getInputStream(zipEntry);
featureModel = parser.openFmdl(inputStream);
} catch (Exception exception) {
errorInfo = "Exception occurs when read feature-model file.\n" + exception;
return false;
}
//end ��ȡ����ģ�������ļ�
//begin ��ȡԼ��ģ�������ļ�
try {
zipEntry = zipFile.getEntry(domainName + ".cm");
if (zipEntry != null) {
InputStream inputStream = zipFile.getInputStream(zipEntry);
parser.openCmdl(inputStream, featureModel);
}
} catch (Exception exception) {
errorInfo = "Exception occurs when read constraint-model file.\n" + exception;
return false;
}
//end ��ȡԼ��ģ�������ļ�
//Question: *.im �洢ʲô��Ϣ��
try{
zipEntry = zipFile.getEntry(domainName + ".im");
if (zipEntry != null) {
InputStream inputStream = zipFile.getInputStream(zipEntry);
parser.openImdl(inputStream, featureModel);
}
} catch (Exception exception) {
errorInfo = "Exception occurs when read interaction-model file.\n" + exception;
return false;
}
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];
}
//begin ��ȡ������Ϣ
informationMap = new GroupTreeMap();
Enumeration enumeration = zipFile.entries();
while (enumeration.hasMoreElements()) {
ZipEntry entry = (ZipEntry) enumeration.nextElement();
String entryName = entry.getName();
if (entryName.endsWith(".draw")) {
int index = entryName.indexOf('\\');
if (index > 0) {
try {
String id = entryName.substring(0, index);
InputStream inputStream = zipFile.getInputStream(entry);
Drawing drawing = reader.restore(inputStream);
informationMap.add(id, drawing);
} catch (Exception exception) {
errorInfo = "Exception occurs when read information.\n" + exception;
exception.printStackTrace();
return false;
}
}
}
}
//end ��ȡ������Ϣ
//begin ��ȡ������Ϣ
//gh start
CmdlParser cp = new CmdlParserPrototype();
try {
zipEntry = zipFile.getEntry("customization_index.txt");
InputStream inputStream = zipFile.getInputStream(zipEntry);
for (Iterator<String> itCVId = cp.openCustomizationVersionIds(
inputStream).iterator(); itCVId.hasNext();) {
String cvId = itCVId.next();
zipEntry = zipFile.getEntry(cp.getCustomizationVersionFileName(cvId));
InputStream isCV = zipFile.getInputStream(zipEntry);
cp.openCustomizationVersion(isCV , featureModel);
}
} catch (Exception exception) {
// errorInfo = "Exception occurs when read customization file.\n"
// + exception;
// return false;
}
//gh end
// end ��ȡ������Ϣ
errorInfo = null;
return true;
}
public void setDataSource(Object src) {
if(src instanceof File)
file = (File)src;
}
}