Search in sources :

Example 11 with URIConverter

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

the class ClasspathBasedChecks method checkFileNamingConventions.

/**
 * Verifies that a given catalog file has the same name as the name given in the model.
 * Also verifies that the given package exists and that the file is in that package.
 *
 * @param catalog
 *          a check catalog
 */
@Check
public void checkFileNamingConventions(final CheckCatalog catalog) {
    Resource resource = catalog.eResource();
    URI resourceURI = resource.getURI();
    String packageName = catalog.getPackageName();
    StringBuilder classpathURIBuilder = new StringBuilder(ClasspathUriUtil.CLASSPATH_SCHEME);
    classpathURIBuilder.append(":/");
    if (packageName != null) {
        classpathURIBuilder.append(packageName.replace(DOT, SLASH)).append(SLASH);
    }
    classpathURIBuilder.append(resourceURI.lastSegment());
    URI classpathURI = URI.createURI(classpathURIBuilder.toString());
    URIConverter uriConverter = resource.getResourceSet().getURIConverter();
    try {
        URI normalizedClasspathURI = uriConverter.normalize(classpathURI);
        URI normalizedResourceURI = uriConverter.normalize(resourceURI);
        // Just to be sure we don't break anything, leave that earlier behavior in.
        if (!normalizedResourceURI.equals(normalizedClasspathURI) && !resourceURI.equals(normalizedClasspathURI)) {
            reportInvalidPackage(catalog, packageName, null);
        }
    } catch (ClasspathUriResolutionException e) {
        reportInvalidPackage(catalog, packageName, null);
    }
    String catalogName = catalog.getName();
    if (catalogName != null && !equal(resourceURI.trimFileExtension().lastSegment(), catalogName)) {
        error("The catalog '" + (packageName != null ? notNull(packageName) + DOT : "") + catalogName + "' must be defined in its own file", catalog, CheckPackage.Literals.CHECK_CATALOG__NAME, ValidationMessageAcceptor.INSIGNIFICANT_INDEX, IssueCodes.WRONG_FILE);
    }
}
Also used : ClasspathUriResolutionException(org.eclipse.xtext.resource.ClasspathUriResolutionException) Resource(org.eclipse.emf.ecore.resource.Resource) URIConverter(org.eclipse.emf.ecore.resource.URIConverter) URI(org.eclipse.emf.common.util.URI) Check(org.eclipse.xtext.validation.Check)

Example 12 with URIConverter

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

the class FormatScopeUtil method findExtensionURI.

/**
 * Find URI of extended FormatConfiguration.
 * The following heuristic is used to find the URI: extract grammar name from the extensionName (path name) and then replace the file name segment of the
 * context's URI with the extracted grammar name.
 *
 * @param context
 *          the context
 * @param extensionName
 *          the extension name
 * @return the URI if found, otherwise null
 */
private URI findExtensionURI(final Resource context, final String extensionName) {
    URI uri = context.getURI().trimFileExtension();
    String name = convertPathNameToShortName(extensionName);
    final URI extensionURI = uri.trimSegments(1).appendSegment(name).appendFileExtension(FormatConstants.FILE_EXTENSION);
    final ResourceSet resourceSet = context.getResourceSet();
    final URIConverter uriConverter = resourceSet.getURIConverter();
    try {
        if (resourceSet.getResource(extensionURI, false) != null || uriConverter.exists(extensionURI, null)) {
            return extensionURI;
        }
    // CHECKSTYLE:OFF
    } catch (Exception e) {
    // NOPMD
    // no resource found - return null
    // CHECKSTYLE:ON
    }
    return null;
}
Also used : ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) URIConverter(org.eclipse.emf.ecore.resource.URIConverter) URI(org.eclipse.emf.common.util.URI)

Example 13 with URIConverter

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

the class ExternalContentSupportTest method testConfigureConverter.

@Test
public void testConfigureConverter() {
    URIConverter converter = new ExtensibleURIConverterImpl();
    support.configureConverter(converter, this);
    checkConverter(converter);
}
Also used : ExtensibleURIConverterImpl(org.eclipse.emf.ecore.resource.impl.ExtensibleURIConverterImpl) URIConverter(org.eclipse.emf.ecore.resource.URIConverter) Test(org.junit.Test)

Example 14 with URIConverter

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

the class Bug266082Test method testManyResolvedImports.

@Test
public void testManyResolvedImports() throws Exception {
    StringBuilder model = new StringBuilder(2000);
    int max = 100;
    for (int i = 1; i <= max; i++) {
        model.append("import '" + i + ".importuritestlanguage'\n");
    }
    model.append("type Foo extends Bar");
    final String modelAsString = model.toString();
    model = null;
    XtextResourceSet resourceSet = get(XtextResourceSet.class);
    resourceSet.setClasspathURIContext(getClass().getClassLoader());
    final URIConverter converter = resourceSet.getURIConverter();
    resourceSet.setURIConverter(new URIConverter() {

        @Override
        public Map<String, ?> contentDescription(URI uri, Map<?, ?> options) throws IOException {
            return converter.contentDescription(uri, options);
        }

        @Override
        public InputStream createInputStream(URI uri) throws IOException {
            return new StringInputStream(modelAsString);
        }

        @Override
        public InputStream createInputStream(URI uri, Map<?, ?> options) throws IOException {
            return new StringInputStream(modelAsString);
        }

        @Override
        public OutputStream createOutputStream(URI uri) throws IOException {
            return converter.createOutputStream(uri);
        }

        @Override
        public OutputStream createOutputStream(URI uri, Map<?, ?> options) throws IOException {
            return converter.createOutputStream(uri, options);
        }

        @Override
        public void delete(URI uri, Map<?, ?> options) throws IOException {
            converter.delete(uri, options);
        }

        @Override
        public boolean exists(URI uri, Map<?, ?> options) {
            return true;
        }

        @Override
        public Map<String, ?> getAttributes(URI uri, Map<?, ?> options) {
            return converter.getAttributes(uri, options);
        }

        @Override
        public EList<ContentHandler> getContentHandlers() {
            return converter.getContentHandlers();
        }

        @Override
        public URIHandler getURIHandler(URI uri) {
            return converter.getURIHandler(uri);
        }

        @Override
        public EList<URIHandler> getURIHandlers() {
            return converter.getURIHandlers();
        }

        @Override
        public Map<URI, URI> getURIMap() {
            return converter.getURIMap();
        }

        @Override
        public URI normalize(URI uri) {
            return converter.normalize(uri);
        }

        @Override
        public void setAttributes(URI uri, Map<String, ?> attributes, Map<?, ?> options) throws IOException {
            converter.setAttributes(uri, attributes, options);
        }
    });
    Resource res = resourceSet.getResource(URI.createURI("file:/1.importuritestlanguage"), true);
    EcoreUtil.resolveAll(res);
    assertEquals(res.getErrors().toString(), 1, res.getErrors().size());
}
Also used : StringInputStream(org.eclipse.xtext.util.StringInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) Resource(org.eclipse.emf.ecore.resource.Resource) IOException(java.io.IOException) URI(org.eclipse.emf.common.util.URI) StringInputStream(org.eclipse.xtext.util.StringInputStream) EList(org.eclipse.emf.common.util.EList) XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet) URIHandler(org.eclipse.emf.ecore.resource.URIHandler) URIConverter(org.eclipse.emf.ecore.resource.URIConverter) Map(java.util.Map) Test(org.junit.Test)

Example 15 with URIConverter

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

the class SerializationUtil method getCompleteContent.

public static String getCompleteContent(XtextResource xr) throws IOException, UnsupportedEncodingException {
    XtextResourceSet resourceSet = (XtextResourceSet) xr.getResourceSet();
    URIConverter uriConverter = resourceSet.getURIConverter();
    URI uri = xr.getURI();
    String encoding = xr.getEncoding();
    InputStream inputStream = null;
    try {
        inputStream = uriConverter.createInputStream(uri);
        return getCompleteContent(encoding, inputStream);
    } finally {
        tryClose(inputStream, null);
    }
}
Also used : XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet) DataInputStream(java.io.DataInputStream) InputStream(java.io.InputStream) 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