Search in sources :

Example 6 with TraceNotFoundException

use of org.eclipse.xtext.generator.trace.TraceNotFoundException in project xtext-core by eclipse.

the class AbstractTraceForURIProvider method getTraceToTarget.

public Trace getTraceToTarget(final SomeFile sourceFile, final AbsoluteURI absoluteSourceResource, final IProjectConfig projectConfig) {
    final List<PersistedTrace> traceFiles = findInverseTraceFiles(sourceFile);
    if (traceFiles.isEmpty() || projectConfig == null) {
        return null;
    }
    Trace result = newAbstractTrace(sourceFile);
    result.setTraceToSource(false);
    final IResourceServiceProvider serviceProvider = getServiceProvider(absoluteSourceResource);
    final ITraceURIConverter traceURIConverter = serviceProvider.get(ITraceURIConverter.class);
    result.setTraceRegionProvider(new ITraceRegionProvider() {

        @Override
        public AbstractTraceRegion getTraceRegion() {
            List<AbstractTraceRegion> result = newArrayList();
            SourceRelativeURI sourceUriForTrace = traceURIConverter.getURIForTrace(projectConfig, absoluteSourceResource);
            for (PersistedTrace traceFile : traceFiles) {
                AbsoluteURI generatedFileURI = getGeneratedLocation(traceFile);
                if (generatedFileURI == null) {
                    throw new TraceNotFoundException();
                }
                AbstractTraceRegion traceRegion = cachedTraces.getTraceRegion(traceFile);
                SourceRelativeURI generatedUriForTrace = getGeneratedUriForTrace(projectConfig, absoluteSourceResource, generatedFileURI, traceURIConverter);
                if (sourceUriForTrace != null && generatedUriForTrace != null) {
                    result.addAll(traceRegion.invertFor(sourceUriForTrace, generatedUriForTrace));
                }
            }
            if (result.isEmpty())
                throw new TraceNotFoundException();
            return AbstractTraceRegion.mergedFrom(result);
        }
    });
    return result;
}
Also used : IResourceServiceProvider(org.eclipse.xtext.resource.IResourceServiceProvider) TraceNotFoundException(org.eclipse.xtext.generator.trace.TraceNotFoundException) AbstractTraceRegion(org.eclipse.xtext.generator.trace.AbstractTraceRegion) List(java.util.List) ITraceURIConverter(org.eclipse.xtext.generator.trace.ITraceURIConverter) ITraceRegionProvider(org.eclipse.xtext.generator.trace.ITraceRegionProvider) SourceRelativeURI(org.eclipse.xtext.generator.trace.SourceRelativeURI) AbsoluteURI(org.eclipse.xtext.generator.trace.AbsoluteURI)

Example 7 with TraceNotFoundException

use of org.eclipse.xtext.generator.trace.TraceNotFoundException in project xtext-eclipse by eclipse.

the class EclipseResourceFileSystemAccess2 method updateTraceInformation.

/**
 * @throws CoreException
 *             if something unexpected happens during resource access
 * @throws IOException
 *             if serialization of the trace data fails
 * @since 2.3
 */
protected void updateTraceInformation(IFile traceFile, CharSequence contents, boolean derived) throws CoreException, IOException {
    if (contents instanceof ITraceRegionProvider) {
        try {
            AbstractTraceRegion traceRegion = ((ITraceRegionProvider) contents).getTraceRegion();
            if (sourceTraces == null) {
                sourceTraces = HashMultimap.create();
            }
            IPath tracePath = traceFile.getFullPath();
            Iterator<AbstractTraceRegion> iterator = traceRegion.treeIterator();
            while (iterator.hasNext()) {
                AbstractTraceRegion region = iterator.next();
                for (ILocationData location : region.getAssociatedLocations()) {
                    SourceRelativeURI path = location.getSrcRelativePath();
                    if (path != null) {
                        sourceTraces.put(path, tracePath);
                    }
                }
            }
            class AccessibleOutputStream extends ByteArrayOutputStream {

                byte[] internalBuffer() {
                    return buf;
                }

                int internalLength() {
                    return count;
                }
            }
            AccessibleOutputStream data = new AccessibleOutputStream();
            traceSerializer.writeTraceRegionTo(traceRegion, data);
            // avoid copying the byte array
            InputStream input = new ByteArrayInputStream(data.internalBuffer(), 0, data.internalLength());
            if (traceFile.exists()) {
                traceFile.setContents(input, true, false, monitor);
            } else {
                traceFile.create(input, true, monitor);
            }
            setDerived(traceFile, derived);
            return;
        } catch (TraceNotFoundException e) {
        // ok
        }
    }
    if (traceFile.exists()) {
        traceFile.delete(IResource.FORCE, monitor);
    }
}
Also used : IPath(org.eclipse.core.runtime.IPath) ByteArrayInputStream(java.io.ByteArrayInputStream) StringInputStream(org.eclipse.xtext.util.StringInputStream) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) TraceNotFoundException(org.eclipse.xtext.generator.trace.TraceNotFoundException) AbstractTraceRegion(org.eclipse.xtext.generator.trace.AbstractTraceRegion) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ITraceRegionProvider(org.eclipse.xtext.generator.trace.ITraceRegionProvider) SourceRelativeURI(org.eclipse.xtext.generator.trace.SourceRelativeURI) ILocationData(org.eclipse.xtext.generator.trace.ILocationData)

Aggregations

ITraceRegionProvider (org.eclipse.xtext.generator.trace.ITraceRegionProvider)7 TraceNotFoundException (org.eclipse.xtext.generator.trace.TraceNotFoundException)7 AbstractTraceRegion (org.eclipse.xtext.generator.trace.AbstractTraceRegion)5 SourceRelativeURI (org.eclipse.xtext.generator.trace.SourceRelativeURI)4 IOException (java.io.IOException)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 InputStream (java.io.InputStream)2 IPath (org.eclipse.core.runtime.IPath)2 IResourceServiceProvider (org.eclipse.xtext.resource.IResourceServiceProvider)2 RuntimeIOException (org.eclipse.xtext.util.RuntimeIOException)2 BufferedInputStream (java.io.BufferedInputStream)1 BufferedOutputStream (java.io.BufferedOutputStream)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 OutputStream (java.io.OutputStream)1 List (java.util.List)1 IProject (org.eclipse.core.resources.IProject)1 IStorage (org.eclipse.core.resources.IStorage)1