blob: 513427c2b0955604afae4c6ac1c2f9437d75e23e [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2014 Bosch Software Innovations GmbH and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
* The Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* Contributors:
* Bosch Software Innovations GmbH - Please refer to git log
*
*******************************************************************************/
package org.eclipse.vorto.codegen.internal;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFileState;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.vorto.codegen.api.IFileWritingStrategy;
import org.eclipse.vorto.codegen.api.tasks.Generated;
public class WriteGenOverWriteStrategy implements IFileWritingStrategy {
private static final String FILENAME_SUFFIX = "_gen";
@Override
public void writeFile(Generated generated, IFile existingFile)
throws CoreException, IOException {
String fileName = generated.getFileName();
IProject project = existingFile.getProject();
if (!existingFile.exists()) {
write(generated.getContent(), existingFile);
/*
* Clearing the file history of the newly created file is because
* file history is not deleted when the file is being deleted. So if
* the file is not found whether deleted by the system or user , it
* will clear the history after creating the file to avoid false
* detection of modification.
*/
existingFile.clearHistory(null);
} else {
if (isModified(existingFile)) {
IFile replacefile = getFile(fileName + FILENAME_SUFFIX,
generated.getFolderPath(), project);
if (replacefile.exists())
replacefile.delete(true, null);
write(generated.getContent(), replacefile);
} else {
existingFile.delete(true, null);
write(generated.getContent(), existingFile);
}
}
}
private boolean isModified(IFile existingFile) {
IFileState[] history = null;
try {
history = existingFile.getHistory(null);
} catch (CoreException e) {
return true;
}
return history.length != 0;
}
private IFile getFile(String fileName, String folderPath, IProject project) {
IFile file = null;
if (StringUtils
.equalsIgnoreCase(folderPath, Generated.ROOT_FOLDER_PATH)) {
file = project.getFile(fileName);
} else {
file = project.getFolder(folderPath).getFile(fileName);
}
return file;
}
private void write(String contentStr, IFile existingFile)
throws CoreException, IOException {
InputStream content = IOUtils.toInputStream(contentStr);
existingFile.create(content, false, null);
content.close();
}
}