Search in sources :

Example 1 with XtendResourceParser

use of org.eclipse.xtend.XtendResourceParser 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)

Aggregations

InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 URI (org.eclipse.emf.common.util.URI)1 URIConverter (org.eclipse.emf.ecore.resource.URIConverter)1 XtendResourceParser (org.eclipse.xtend.XtendResourceParser)1 EvaluationException (org.eclipse.xtend.expression.EvaluationException)1 ClasspathUriResolutionException (org.eclipse.xtext.resource.ClasspathUriResolutionException)1