Search in sources :

Example 1 with URIConverter

use of org.eclipse.emf.ecore.resource.URIConverter in project tdi-studio-se by Talend.

the class ImportItemUtil method computeProperty.

private void computeProperty(ResourcesManager manager, ItemRecord itemRecord) {
    InputStream stream = null;
    try {
        stream = manager.getStream(itemRecord.getPath());
        final Resource resource = createResource(itemRecord, itemRecord.getPath(), false);
        URIConverter uriConverter = resource.getResourceSet().getURIConverter();
        resource.getResourceSet().setURIConverter(new ExtensibleURIConverterImpl() {

            /*
                 * (non-Javadoc)
                 * 
                 * @see org.eclipse.emf.ecore.resource.impl.ExtensibleURIConverterImpl#createInputStream(org.eclipse.
                 * emf.common.util.URI, java.util.Map)
                 */
            @Override
            public InputStream createInputStream(URI uri, Map<?, ?> options) throws IOException {
                InputStream inputStream = null;
                EPackage ePackage = resource.getResourceSet().getPackageRegistry().getEPackage(uri.toString());
                if (ePackage != null || !"http".equals(uri.scheme())) {
                    inputStream = super.createInputStream(uri, options);
                } else {
                    inputStream = null;
                }
                return inputStream;
            }
        });
        EmfHelper.loadResource(resource, stream, null);
        resource.getResourceSet().setURIConverter(uriConverter);
        itemRecord.setProperty((Property) EcoreUtil.getObjectByType(resource.getContents(), PropertiesPackage.eINSTANCE.getProperty()));
    } catch (Exception e) {
    // ignore, must be one invalid or unknown item
    } finally {
        if (stream != null) {
            try {
                stream.close();
            } catch (IOException e) {
            // ignore
            }
        }
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) CwmResource(org.talend.model.emf.CwmResource) TalendXMIResource(org.talend.commons.runtime.model.emf.TalendXMIResource) ByteArrayResource(org.talend.core.model.properties.helper.ByteArrayResource) Resource(org.eclipse.emf.ecore.resource.Resource) ExtensibleURIConverterImpl(org.eclipse.emf.ecore.resource.impl.ExtensibleURIConverterImpl) IOException(java.io.IOException) URIConverter(org.eclipse.emf.ecore.resource.URIConverter) URI(org.eclipse.emf.common.util.URI) CoreException(org.eclipse.core.runtime.CoreException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) MalformedURLException(java.net.MalformedURLException) PersistenceException(org.talend.commons.exception.PersistenceException) EPackage(org.eclipse.emf.ecore.EPackage)

Example 2 with URIConverter

use of org.eclipse.emf.ecore.resource.URIConverter in project xtext-eclipse by eclipse.

the class HyperlinkHelper method createHyperlinksTo.

/**
 * Produces hyperlinks for the given {@code region} that point to the referenced {@code target}.
 */
public void createHyperlinksTo(XtextResource from, Region region, EObject target, IHyperlinkAcceptor acceptor) {
    final URIConverter uriConverter = from.getResourceSet().getURIConverter();
    final String hyperlinkText = labelProvider.getText(target);
    final URI uri = EcoreUtil.getURI(target);
    final URI normalized = uri.isPlatformResource() ? uri : uriConverter.normalize(uri);
    XtextHyperlink result = hyperlinkProvider.get();
    result.setHyperlinkRegion(region);
    result.setURI(normalized);
    result.setHyperlinkText(hyperlinkText);
    acceptor.accept(result);
}
Also used : URIConverter(org.eclipse.emf.ecore.resource.URIConverter) URI(org.eclipse.emf.common.util.URI)

Example 3 with URIConverter

use of org.eclipse.emf.ecore.resource.URIConverter in project xtext-eclipse by eclipse.

the class XtextHyperlinkHelper method createLinksToBase.

protected void createLinksToBase(ITextRegion nameLocation, AbstractRule rule, IHyperlinkAcceptor acceptor) {
    Set<AbstractRule> visited = Sets.newHashSet();
    Grammar grammar = GrammarUtil.getGrammar(rule);
    for (Grammar used : grammar.getUsedGrammars()) {
        String ruleName = rule.getName();
        AbstractRule overwritten = GrammarUtil.findRuleForName(used, ruleName);
        if (overwritten != null && visited.add(overwritten)) {
            URIConverter uriConverter = rule.eResource().getResourceSet().getURIConverter();
            String hyperlinkText = getLabelProvider().getText(rule) + " - " + GrammarUtil.getGrammar(overwritten).getName();
            URI uri = uriConverter.normalize(EcoreUtil.getURI(overwritten));
            XtextHyperlink result = getHyperlinkProvider().get();
            result.setHyperlinkRegion(toRegion(nameLocation));
            result.setURI(uri);
            result.setHyperlinkText(hyperlinkText);
            result.setTypeLabel("Go To Base");
            acceptor.accept(result);
        }
    }
}
Also used : XtextHyperlink(org.eclipse.xtext.ui.editor.hyperlinking.XtextHyperlink) Grammar(org.eclipse.xtext.Grammar) URIConverter(org.eclipse.emf.ecore.resource.URIConverter) AbstractRule(org.eclipse.xtext.AbstractRule) URI(org.eclipse.emf.common.util.URI)

Example 4 with URIConverter

use of org.eclipse.emf.ecore.resource.URIConverter in project xtext-core by eclipse.

the class XtendXtext2EcorePostProcessor method loadXtendFile.

/**
 * Expects an Xtend file named <code>MyDsl</code>PostProcessor.ext with an extension with signature
 * process(xtext::GeneratedMetamodel) in the same folder as the grammar file.
 *
 * @param metamodel
 *            the metamodel to augment
 * @return the xtendFile to execute
 */
protected Resource loadXtendFile(GeneratedMetamodel metamodel) {
    if (xtendFile == null) {
        final String extension = getExtensionName(metamodel);
        try {
            URI uri = getXtendFileLocation(metamodel);
            if (uri != null) {
                URIConverter uriConverter = metamodel.eResource().getResourceSet().getURIConverter();
                if (uriConverter.exists(uri, null)) {
                    InputStream in = uriConverter.createInputStream(uri);
                    try {
                        XtendResourceParser parser = new XtendResourceParser();
                        xtendFile = parser.parse(new InputStreamReader(in), extension + '.' + XtendFile.FILE_EXTENSION);
                        fireXtendFileLoaded();
                    } finally {
                        if (in != null)
                            in.close();
                    }
                }
            }
        } catch (ClasspathUriResolutionException ignored) {
        // no xtend file found
        } catch (Exception e) {
            logger.error("Could not parse " + extension, e);
        }
    }
    return xtendFile;
}
Also used : InputStreamReader(java.io.InputStreamReader) ClasspathUriResolutionException(org.eclipse.xtext.resource.ClasspathUriResolutionException) InputStream(java.io.InputStream) XtendResourceParser(org.eclipse.xtend.XtendResourceParser) URIConverter(org.eclipse.emf.ecore.resource.URIConverter) URI(org.eclipse.emf.common.util.URI) EvaluationException(org.eclipse.xtend.expression.EvaluationException) ClasspathUriResolutionException(org.eclipse.xtext.resource.ClasspathUriResolutionException)

Example 5 with URIConverter

use of org.eclipse.emf.ecore.resource.URIConverter in project xtext-core by eclipse.

the class FlatResourceSetBasedAllContainersState method getContainedURIs.

@Override
public Collection<URI> getContainedURIs(String containerHandle) {
    if (!HANDLE.equals(containerHandle))
        return Collections.emptySet();
    if (resourceSet instanceof XtextResourceSet) {
        ResourceDescriptionsData descriptionsData = findResourceDescriptionsData(resourceSet);
        if (descriptionsData != null) {
            return descriptionsData.getAllURIs();
        }
        return newArrayList(((XtextResourceSet) resourceSet).getNormalizationMap().values());
    }
    List<URI> uris = Lists.newArrayListWithCapacity(resourceSet.getResources().size());
    URIConverter uriConverter = resourceSet.getURIConverter();
    for (Resource r : resourceSet.getResources()) uris.add(uriConverter.normalize(r.getURI()));
    return uris;
}
Also used : ResourceDescriptionsData(org.eclipse.xtext.resource.impl.ResourceDescriptionsData) XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet) Resource(org.eclipse.emf.ecore.resource.Resource) URIConverter(org.eclipse.emf.ecore.resource.URIConverter) URI(org.eclipse.emf.common.util.URI)

Aggregations

URIConverter (org.eclipse.emf.ecore.resource.URIConverter)22 URI (org.eclipse.emf.common.util.URI)18 Resource (org.eclipse.emf.ecore.resource.Resource)9 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)6 InputStream (java.io.InputStream)4 ExtensibleURIConverterImpl (org.eclipse.emf.ecore.resource.impl.ExtensibleURIConverterImpl)4 XtextResourceSet (org.eclipse.xtext.resource.XtextResourceSet)4 IOException (java.io.IOException)3 IContainer (org.eclipse.core.resources.IContainer)2 IResource (org.eclipse.core.resources.IResource)2 CoreException (org.eclipse.core.runtime.CoreException)2 GenModel (org.eclipse.emf.codegen.ecore.genmodel.GenModel)2 EPackage (org.eclipse.emf.ecore.EPackage)2 ResourceSetImpl (org.eclipse.emf.ecore.resource.impl.ResourceSetImpl)2 Grammar (org.eclipse.xtext.Grammar)2 ClasspathUriResolutionException (org.eclipse.xtext.resource.ClasspathUriResolutionException)2 ResourceDescriptionsData (org.eclipse.xtext.resource.impl.ResourceDescriptionsData)2 XtextHyperlink (org.eclipse.xtext.ui.editor.hyperlinking.XtextHyperlink)2 Test (org.junit.Test)2 ByteArrayResource (org.talend.core.model.properties.helper.ByteArrayResource)2