Search in sources :

Example 1 with StringInputStream

use of org.eclipse.xtext.util.StringInputStream in project applause by applause.

the class ApplauseEclipseResourceFileSystemAccess2 method generateFile.

public void generateFile(String fileName, String outputName, CharSequence contents) {
    if (monitor.isCanceled())
        throw new OperationCanceledException();
    OutputConfiguration outputConfig = getOutputConfig(outputName);
    if (!ensureOutputConfigurationDirectoryExists(outputConfig))
        return;
    IFile file = getFile(fileName, outputName);
    IFile traceFile = getTraceFile(file);
    try {
        String encoding = getEncoding(file);
        CharSequence postProcessedContent = postProcess(fileName, outputName, contents, encoding);
        String contentsAsString = postProcessedContent.toString();
        if (file.exists()) {
            if (outputConfig.isOverrideExistingResources()) {
                StringInputStream newContent = getInputStream(contentsAsString, encoding);
                if (hasContentsChanged(file, newContent)) {
                    // reset to offset zero allows to reuse internal byte[]
                    // no need to convert the string twice
                    newContent.reset();
                    file.setContents(newContent, true, true, monitor);
                } else {
                    file.touch(getMonitor());
                }
                if (file.isDerived() != outputConfig.isSetDerivedProperty()) {
                    setDerived(file, outputConfig.isSetDerivedProperty());
                }
                if (traceFile != null)
                    updateTraceInformation(traceFile, postProcessedContent, outputConfig.isSetDerivedProperty());
                if (callBack != null)
                    callBack.afterFileUpdate(file);
            }
        } else {
            ensureParentExists(file);
            file.create(getInputStream(contentsAsString, encoding), true, monitor);
            if (outputConfig.isSetDerivedProperty()) {
                setDerived(file, true);
            }
            if (traceFile != null)
                updateTraceInformation(traceFile, postProcessedContent, outputConfig.isSetDerivedProperty());
            if (callBack != null)
                callBack.afterFileCreation(file);
        }
    } catch (CoreException e) {
        throw new RuntimeIOException(e);
    } catch (IOException e) {
        throw new RuntimeIOException(e);
    }
}
Also used : RuntimeIOException(org.eclipse.xtext.util.RuntimeIOException) StringInputStream(org.eclipse.xtext.util.StringInputStream) IFile(org.eclipse.core.resources.IFile) CoreException(org.eclipse.core.runtime.CoreException) OutputConfiguration(org.eclipse.xtext.generator.OutputConfiguration) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) RuntimeIOException(org.eclipse.xtext.util.RuntimeIOException) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)1 IFile (org.eclipse.core.resources.IFile)1 CoreException (org.eclipse.core.runtime.CoreException)1 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)1 OutputConfiguration (org.eclipse.xtext.generator.OutputConfiguration)1 RuntimeIOException (org.eclipse.xtext.util.RuntimeIOException)1 StringInputStream (org.eclipse.xtext.util.StringInputStream)1