Search in sources :

Example 71 with URI

use of org.eclipse.emf.common.util.URI in project xtext-core by eclipse.

the class EClassifierInfos method getSuperTypeInfos.

public List<EClassInfo> getSuperTypeInfos(EClassInfo subTypeInfo) throws UnexpectedClassInfoException {
    List<EClassInfo> result = new ArrayList<EClassInfo>();
    List<EClass> superTypes = subTypeInfo.getEClass().getESuperTypes();
    for (int i = 0; i < superTypes.size(); i++) {
        EClass superType = superTypes.get(i);
        if (superType.eIsProxy()) {
            URI proxyURI = EcoreUtil.getURI(superType);
            if (proxyURI.isPlatformResource()) {
                String platformString = proxyURI.toPlatformString(true);
                URI platformPluginURI = URI.createPlatformPluginURI(platformString, true).appendFragment(proxyURI.fragment());
                EObject secondAttempt = subTypeInfo.getEClass().eResource().getResourceSet().getEObject(platformPluginURI, true);
                if (secondAttempt instanceof EClass) {
                    superType = (EClass) secondAttempt;
                    superTypes.set(i, superType);
                }
            }
        }
        EClassifierInfo info = getInfoOrNull(superType);
        if (info != null) {
            if (info instanceof EClassInfo) {
                result.add((EClassInfo) info);
            } else {
                throw new UnexpectedClassInfoException(TransformationErrorCode.InvalidSupertype, subTypeInfo, info, null);
            }
        } else {
            if (superType.eIsProxy()) {
                throw new UnexpectedClassInfoException(TransformationErrorCode.InvalidSupertype, subTypeInfo, null, "Cannot load all supertypes of  '" + subTypeInfo.getEClass().getName() + "'");
            } else if (isPackageKnown(superType.getEPackage().getNsURI())) {
                throw new UnexpectedClassInfoException(TransformationErrorCode.InvalidSupertype, subTypeInfo, null, "Cannot find info for type '" + superType.getName() + "'");
            }
        }
    }
    return result;
}
Also used : EClass(org.eclipse.emf.ecore.EClass) EClassInfo(org.eclipse.xtext.xtext.ecoreInference.EClassifierInfo.EClassInfo) EObject(org.eclipse.emf.ecore.EObject) ArrayList(java.util.ArrayList) URI(org.eclipse.emf.common.util.URI)

Example 72 with URI

use of org.eclipse.emf.common.util.URI in project xtext-core by eclipse.

the class XtendXtext2EcorePostProcessor method getXtendFileLocation.

protected URI getXtendFileLocation(GeneratedMetamodel metamodel) {
    URI uri = metamodel.eResource().getURI();
    // check if uri can be used for resolve
    if (!uri.isHierarchical() || uri.isRelative())
        return null;
    uri = URI.createURI(Strings.lastToken(getExtensionName(metamodel), "::")).appendFileExtension(XtendFile.FILE_EXTENSION).resolve(uri);
    return uri;
}
Also used : URI(org.eclipse.emf.common.util.URI)

Example 73 with URI

use of org.eclipse.emf.common.util.URI 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 74 with URI

use of org.eclipse.emf.common.util.URI in project xtext-core by eclipse.

the class EcoreUtil2Test method testExternalFormOfEReferenceNoNPE.

/**
 * This test assumes that an EPackage with indexed references is no longer available.
 */
@Test
public void testExternalFormOfEReferenceNoNPE() throws Exception {
    EReference reference = EcorePackage.Literals.EATTRIBUTE__EATTRIBUTE_TYPE;
    URI uri = EcoreUtil.getURI(reference);
    String externalForm = uri.toString();
    EReference foundReference = EcoreUtil2.getEReferenceFromExternalForm(EPackage.Registry.INSTANCE, externalForm);
    assertSame(reference, foundReference);
    String brokenExternalFrom = makeInvalid(externalForm);
    assertNull(EcoreUtil2.getEReferenceFromExternalForm(EPackage.Registry.INSTANCE, brokenExternalFrom));
    String shortExternalForm = EcoreUtil2.toExternalForm(reference);
    foundReference = EcoreUtil2.getEReferenceFromExternalForm(EPackage.Registry.INSTANCE, shortExternalForm);
    assertSame(reference, foundReference);
    String brokenShortExternalFrom = makeInvalid(shortExternalForm);
    assertNull(EcoreUtil2.getEReferenceFromExternalForm(EPackage.Registry.INSTANCE, brokenShortExternalFrom));
    brokenShortExternalFrom = shortExternalForm.replace('A', 'a');
    assertNull(EcoreUtil2.getEReferenceFromExternalForm(EPackage.Registry.INSTANCE, brokenShortExternalFrom));
}
Also used : URI(org.eclipse.emf.common.util.URI) EReference(org.eclipse.emf.ecore.EReference) Test(org.junit.Test)

Example 75 with URI

use of org.eclipse.emf.common.util.URI in project xtext-core by eclipse.

the class JavaIoFileSystemAccessTest method testEncoding.

@Test
public void testEncoding() throws Exception {
    File file = null;
    FileInputStream fileInputStream = null;
    try {
        JavaIoFileSystemAccess fileSystemAccess = new JavaIoFileSystemAccess(IResourceServiceProvider.Registry.INSTANCE, new IEncodingProvider() {

            @Override
            public String getEncoding(URI uri) {
                return "ISO-8859-1";
            }
        });
        File tmpDir = configureFileSystemAccess(fileSystemAccess);
        String contents = "g�rkenbr�d";
        fileSystemAccess.generateFile("test.txt", contents);
        file = new File(tmpDir, "test.txt");
        assertTrue(file.exists());
        fileInputStream = new FileInputStream(file);
        byte[] buffer = new byte[512];
        int read = fileInputStream.read(buffer);
        String utfString = new String(buffer, 0, read, "UTF-8");
        assertFalse(equal(contents, utfString));
        String isoString = new String(buffer, 0, read, "ISO-8859-1");
        assertEquals(contents, isoString);
    } finally {
        if (fileInputStream != null)
            fileInputStream.close();
        if (file != null)
            file.delete();
    }
}
Also used : IEncodingProvider(org.eclipse.xtext.parser.IEncodingProvider) File(java.io.File) URI(org.eclipse.emf.common.util.URI) SourceRelativeURI(org.eclipse.xtext.generator.trace.SourceRelativeURI) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Aggregations

URI (org.eclipse.emf.common.util.URI)305 Resource (org.eclipse.emf.ecore.resource.Resource)85 Test (org.junit.Test)79 EObject (org.eclipse.emf.ecore.EObject)55 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)37 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)33 XtextResource (org.eclipse.xtext.resource.XtextResource)32 XtextResourceSet (org.eclipse.xtext.resource.XtextResourceSet)26 BuildRequest (org.eclipse.xtext.build.BuildRequest)23 File (java.io.File)20 AbstractIncrementalBuilderTest (org.eclipse.xtext.testing.builder.AbstractIncrementalBuilderTest)20 ResourceSetImpl (org.eclipse.emf.ecore.resource.impl.ResourceSetImpl)19 IOException (java.io.IOException)18 InternalEObject (org.eclipse.emf.ecore.InternalEObject)18 EPackage (org.eclipse.emf.ecore.EPackage)15 IResourceDescription (org.eclipse.xtext.resource.IResourceDescription)15 InputStream (java.io.InputStream)14 ByteArrayInputStream (java.io.ByteArrayInputStream)13 ArrayList (java.util.ArrayList)13 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)13