blob: b02d82271751d38dd53786a42161467058fd2c7d [file] [log] [blame]
/*
* <copyright>
*
* Copyright (c) 2015 E.D.Willink and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* E.D.Willink - Initial API and implementation
*
* </copyright>
*/
package org.eclipse.ocl.examples.build.utilities;
import java.io.File;
import java.io.FileFilter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.StringTokenizer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.emf.mwe.core.WorkflowContext;
import org.eclipse.emf.mwe.core.issues.Issues;
import org.eclipse.emf.mwe.core.lib.AbstractWorkflowComponent2;
import org.eclipse.emf.mwe.core.monitor.ProgressMonitor;
import com.google.common.io.LineReader;
/**
* Convert the files in a directory tree to use Xtext 2.7 compatible references.
* Text file extensions must be included from conversion.
*/
public class BackportToXtext_2_7 extends AbstractWorkflowComponent2 {
private static final String COMPONENT_NAME = "Backport to Xtext 2.7";
private static final Log LOG = LogFactory.getLog(BackportToXtext_2_7.class);
private String directory;
private final Collection<String> textExtensions = new HashSet<String>();
private final Collection<String> defaultTextExtensions = Arrays.asList(new String[] { "java", "g" });
private boolean useDefaultTextExtensions = true;
/**
* Sets the directory.
*
* @param directory
* name of directory
*/
public void setDirectory(final String directory) {
this.directory = directory;
}
/**
* @see org.eclipse.emf.mwe.core.lib.AbstractWorkflowComponent#getLogMessage()
*/
@Override
public String getLogMessage() {
return "backporting directory '" + directory + "'";
}
@Override
protected void invokeInternal(final WorkflowContext model, final ProgressMonitor monitor, final Issues issues) {
if (directory != null) {
final StringTokenizer st = new StringTokenizer(directory, ",");
while (st.hasMoreElements()) {
final String dir = st.nextToken().trim();
final File f = new File(dir);
if (f.exists() && f.isDirectory()) {
LOG.info("Backporting " + f.getAbsolutePath());
try {
cleanFolder(f.getAbsolutePath());
}
catch (FileNotFoundException e) {
issues.addError(e.getMessage());
}
}
}
}
}
@Override
protected void checkConfigurationInternal(final Issues issues) {
if (directory == null) {
issues.addWarning("No directories specified!");
}
}
/**
* Deletes all files and subdirectories under dir. Returns true if all
* deletions were successful. If a deletion fails, the method stops
* attempting to delete and returns false.
*/
public void cleanFolder(String srcGenPath) throws FileNotFoundException {
File f = new File(srcGenPath);
if (!f.exists())
throw new FileNotFoundException(srcGenPath + " " + f.getAbsolutePath());
LOG.debug("Backporting folder " + f.getPath());
convertFolder(f, new FileFilter() {
public boolean accept(File path) {
return path.isDirectory() || isTextExtension(path);
}
}, false);
}
public boolean isTextExtension(File path) {
String name = path.getName();
int index = name.lastIndexOf('.');
String extension = index >= 0 ? name.substring(index+1) : "";
if (useDefaultTextExtensions && defaultTextExtensions.contains(extension))
return true;
return textExtensions.contains(extension);
}
public boolean convertFolder(File parentFolder, final FileFilter filter, boolean continueOnError) throws FileNotFoundException {
if (!parentFolder.exists())
throw new FileNotFoundException(parentFolder.getAbsolutePath());
FileFilter myFilter = filter;
if (myFilter == null) {
myFilter = new FileFilter() {
public boolean accept(File pathname) {
return true;
}
};
}
LOG.debug("Backporting folder " + parentFolder.toString());
final File[] contents = parentFolder.listFiles(myFilter);
for (int j = 0; contents!=null && j < contents.length; j++) {
final File file = contents[j];
if (file.isDirectory()) {
if (!convertFolder(file, myFilter, continueOnError) && !continueOnError)
return false;
}
else {
convertFile(file);
}
}
return true;
}
private void convertFile(File file) {
try {
Reader reader = new FileReader(file);
LineReader lineReader = new LineReader(reader);
StringBuilder s = new StringBuilder();
try {
for (String line; (line = lineReader.readLine()) != null; ) {
s.append(line);
s.append("\n");
}
} catch (IOException e) {
LOG.error("Failed to read '" + file + "'", e);
return;
}
try {
reader.close();
} catch (IOException e) {
LOG.error("Failed to close '" + file + "'", e);
return;
}
String s0 = s.toString();
String s1 = s0.replaceAll("org\\.eclipse\\.xtext\\.ide\\.LexerIdeBindings", "org.eclipse.xtext.ui.LexerUIBindings");
String s2 = s1.replaceAll("org\\.eclipse\\.xtext\\.ide", "org.eclipse.xtext.ui");
String s3 = s2.replaceAll("org\\.eclipse\\.xtext\\.ui\\.editor\\.contentassist\\.antlr\\.DelegatingContentAssistContextFactory", "org.eclipse.xtext.ui.editor.contentassist.antlr.ParserBasedContentAssistContextFactory");
if (!s3.equals(s0)) {
try {
Writer writer = new FileWriter(file);
try {
writer.write(s3);
writer.flush();
} catch (IOException e) {
LOG.error("Failed to write '" + file + "'", e);
return;
} finally {
writer.close();
}
} catch (IOException e) {
LOG.error("Failed to re-open '" + file + "'", e);
return;
}
LOG.info("Backported " + file);
}
} catch (FileNotFoundException e) {
LOG.error("Failed to open '" + file + "'", e);
return;
}
}
/**
* Returns if the default text extensions are used.
*/
public boolean isUseDefaultTextExtensions() {
return useDefaultTextExtensions;
}
/**
* Sets if the default text extensions are used.
*/
public void setUseDefaultTextExtensions(final boolean useDefaultTextExtensions) {
this.useDefaultTextExtensions = useDefaultTextExtensions;
}
/**
* Adds a text extension.
*/
public void addTextExtension(final String textExtension) {
textExtensions.add(textExtension);
}
/**
* @see org.eclipse.emf.mwe.core.lib.AbstractWorkflowComponent#getComponentName()
*/
@Override
public String getComponentName() {
return COMPONENT_NAME;
}
}