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);
}
}
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;
}
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);
}
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());
}
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);
}
}
Aggregations