blob: 46d749284a604751f47fe810ce8e419e21ffd917 [file] [log] [blame]
/**
********************************************************************************
* Copyright (c) 2015-2020 Robert Bosch GmbH and others.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Robert Bosch GmbH - initial API and implementation
********************************************************************************
*/
package org.eclipse.app4mc.amalthea.converters.common;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import org.eclipse.app4mc.amalthea.converters.common.utils.HelperUtil;
import org.jdom2.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class MigrationInputFile {
private static final Logger LOGGER = LoggerFactory.getLogger(MigrationInputFile.class);
private String modelVersion = "";
private boolean selectedFile;
private File file;
private File originalFile;
private String projectRelativePath;
private Document document;
private boolean zipFile;
/*- this field is used to mark ModelFiles, if they have different versions than other files */
private boolean isVersionDifferent;
public String getModelVersion() {
return this.modelVersion;
}
public void setModelVersion(String modelVersion) {
this.modelVersion = modelVersion;
}
public boolean isSelectedFile() {
return this.selectedFile;
}
public void setSelectedFile(boolean isSelectedFile) {
this.selectedFile = isSelectedFile;
}
public File getFile() {
return this.file;
}
public void setFile(File file, File project) throws IOException {
if (MigrationHelper.isZipFile(file)) {
this.zipFile = true;
this.file = MigrationHelper.temporaryUnzip(file);
} else {
this.zipFile = false;
this.file = file;
}
this.originalFile = file;
this.projectRelativePath = project.toURI().relativize(file.toURI()).getPath();
}
public String getProjectRelativePath() {
return this.projectRelativePath;
}
public Document getDocument() {
if (this.document == null) {
try {
this.document = HelperUtil.loadFile(file.getAbsolutePath());
} catch (Exception e) {
LOGGER.error("Failed to load model file {}", file.getAbsolutePath(), e);
}
}
return this.document;
}
public void setDocument(Document document) {
this.document = document;
}
public boolean isVersionDifferent() {
return this.isVersionDifferent;
}
public void setVersionDifferent(boolean isVersionDifferent) {
this.isVersionDifferent = isVersionDifferent;
}
public boolean isZipFile() {
return zipFile;
}
public File getOriginalFile() {
return originalFile;
}
public void dispose() {
if (this.zipFile && this.file.exists()) {
try {
Files.delete(Paths.get(this.file.getAbsolutePath()));
} catch (IOException e) {
LOGGER.error("Failed to delete temporary unzipped file {}", this.file.getAbsolutePath(), e);
}
}
}
}