Search in sources :

Example 21 with URIConverter

use of org.eclipse.emf.ecore.resource.URIConverter in project dsl-devkit by dsldevkit.

the class CheckGenModelUtil method getGenModelUsingHeuristics.

/**
 * Returns the genmodel for the given EPackage.
 *
 * @param ePackage
 *          the model element
 * @return the genmodel
 */
private static GenModel getGenModelUsingHeuristics(final EPackage ePackage) {
    final Resource res = ePackage.eResource();
    ResourceSet resourceSet = res.getResourceSet();
    if (resourceSet == null) {
        resourceSet = new ResourceSetImpl();
        resourceSet.getResources().add(res);
    }
    final URIConverter uriConverter = resourceSet.getURIConverter();
    URI uri = uriConverter.normalize(res.getURI());
    // NOPMD - We want the value before reassignment
    final URI originalURI = uri;
    URI baseURI = uri.trimFileExtension();
    uri = baseURI.appendFileExtension(GENMODEL_EXTENSION);
    uri = uriConverter.normalize(uri);
    if (uri.scheme().equals("http")) /* toString().equals(EcorePackage.eNS_URI) */
    {
        // optimization, because we are not interested in the extension for the Ecore model.
        return null;
    // otherwise getResource will go on the internet to load the model and we loose 20 seconds on each call!
    }
    GenModel result = null;
    // First try: same uri as containing ecore model, with file extension switched to genmodel
    if (uriConverter.exists(uri, null)) {
        result = loadGenModel(uri, ePackage, resourceSet);
    }
    // If the first try failed: look for any *.genmodel files in the same folder.
    if (result == null) {
        baseURI = baseURI.trimSegments(1);
        IContainer[] folders = determineContainersToCheck(baseURI);
        try {
            result = findGenModelInContainers(folders, baseURI, ePackage, resourceSet);
        } catch (CoreException e) {
        // Ignore
        }
    }
    if (result == null) {
        // $NON-NLS-1$
        throw new IllegalStateException("No genmodel found for " + originalURI);
    }
    return result;
}
Also used : ResourceSetImpl(org.eclipse.emf.ecore.resource.impl.ResourceSetImpl) CoreException(org.eclipse.core.runtime.CoreException) GenModel(org.eclipse.emf.codegen.ecore.genmodel.GenModel) IResource(org.eclipse.core.resources.IResource) Resource(org.eclipse.emf.ecore.resource.Resource) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) URIConverter(org.eclipse.emf.ecore.resource.URIConverter) IContainer(org.eclipse.core.resources.IContainer) URI(org.eclipse.emf.common.util.URI)

Example 22 with URIConverter

use of org.eclipse.emf.ecore.resource.URIConverter in project ow by vtst.

the class LessHyperlinkHelper method createHyperlinksTo.

/**
 * Create an hyper-link to the resource designated by an URI.
 * @param from  The source resource.
 * @param uri  The target URI.
 * @param acceptor  An acceptor for the hyper-link.
 */
public void createHyperlinksTo(XtextResource from, URI uri, IHyperlinkAcceptor acceptor) {
    final URIConverter uriConverter = from.getResourceSet().getURIConverter();
    final String hyperlinkText = uri.toString();
    final URI normalized = uriConverter.normalize(uri).resolve(from.getURI());
    XtextHyperlink result = getHyperlinkProvider().get();
    result.setHyperlinkRegion(new Region(1, 1));
    result.setURI(normalized);
    result.setHyperlinkText(hyperlinkText);
    acceptor.accept(result);
}
Also used : XtextHyperlink(org.eclipse.xtext.ui.editor.hyperlinking.XtextHyperlink) Region(org.eclipse.jface.text.Region) 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