Java Code Examples for org.eclipse.emf.ecore.resource.URIConverter#exists()

The following examples show how to use org.eclipse.emf.ecore.resource.URIConverter#exists() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EcoreUtil2.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * checks whether the given URI can be loaded given the context. I.e. there's a resource set with a corresponding
 * resource factory and the physical resource exists.
 */
public static boolean isValidUri(Resource resource, URI uri) {
	if (uri == null || uri.isEmpty()) {
		return false;
	}
	URI newURI = getResolvedImportUri(resource, uri);
	try {
		ResourceSet resourceSet = resource.getResourceSet();
		if (resourceSet.getResource(uri, false) != null)
			return true;
		URIConverter uriConverter = resourceSet.getURIConverter();
		URI normalized = uriConverter.normalize(newURI);
		if (normalized != null)
			// fix for https://bugs.eclipse.org/bugs/show_bug.cgi?id=326760
			if("platform".equals(normalized.scheme()) && !normalized.isPlatform()) 
				return false;
			return uriConverter.exists(normalized, Collections.emptyMap());
	} catch (RuntimeException e) { // thrown by org.eclipse.emf.ecore.resource.ResourceSet#getResource(URI, boolean)
		log.trace("Cannot load resource: " + newURI, e);
	}
	return false;
}
 
Example 2
Source File: EMFGeneratorFragment2.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
private void registerUsedGenModel(final URIConverter converter, final Grammar grammar) {
  final URI genModelUri = this.getGenModelUri(grammar);
  boolean _exists = converter.exists(genModelUri, null);
  if (_exists) {
    try {
      GenModelHelper _genModelHelper = new GenModelHelper();
      XtextResourceSet _xtextResourceSet = new XtextResourceSet();
      _genModelHelper.registerGenModel(_xtextResourceSet, genModelUri);
    } catch (final Throwable _t) {
      if (_t instanceof Exception) {
        final Exception e = (Exception)_t;
        EMFGeneratorFragment2.LOG.error("Failed to register GenModel", e);
      } else {
        throw Exceptions.sneakyThrow(_t);
      }
    }
  }
}
 
Example 3
Source File: FormatScopeUtil.java    From dsl-devkit with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 4
Source File: EMFGeneratorFragment.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
private void registerUsedGenModel(URIConverter converter) {
	if (genModel == null)
		return;
	URI genModelUri = URI.createURI(genModel);
	genModelUri = toPlatformResourceURI(genModelUri);
	if (converter.exists(genModelUri, null)) {
		try {
			new GenModelHelper().registerGenModel(new XtextResourceSet(), genModelUri);
		} catch (ConfigurationException ce) {
			throw ce;
		} catch (Exception e) {
			log.error(e, e);
		}
	}
}
 
Example 5
Source File: UserContentManager.java    From M2Doc with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Constructor.
 * 
 * @param uriConverter
 *            the {@link URIConverter uri converter} to use.
 * @param sourceURI
 *            the source {@link URI}
 * @param destinationURI
 *            the destination {@link URI}
 */
public UserContentManager(URIConverter uriConverter, URI sourceURI, URI destinationURI) {
    this.uriConverter = uriConverter;
    this.sourceURI = sourceURI;
    this.destinationURI = destinationURI;
    if (uriConverter != null && destinationURI != null
        && uriConverter.exists(destinationURI, Collections.EMPTY_MAP)) {
        // Copy file
        uriConverter.getURIHandlers().add(0, uriHandler);
        memoryCopy = memoryCopy(destinationURI);
    } else {
        memoryCopy = null;
    }
}
 
Example 6
Source File: XtendXtext2EcorePostProcessor.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 7
Source File: GenconfUtils.java    From M2Doc with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Launch the documentation generation.
 * 
 * @param generation
 *            the generation configuration object
 * @param classProvider
 *            the {@link IClassProvider}
 * @param templateURI
 *            the template {@link URI}
 * @param generatedURI
 *            the generated docx {@link URI}
 * @param validationURI
 *            the validation docx {@link URI}
 * @param monitor
 *            used to track the progress will generating.
 * @return generated file and validation file if exists
 * @throws IOException
 *             if an I/O problem occurs
 * @throws DocumentParserException
 *             if the document coulnd'nt be parsed.
 * @throws DocumentGenerationException
 *             if the document couldn't be generated
 */
private static List<URI> generate(Generation generation, IClassProvider classProvider, URI templateURI,
        URI generatedURI, URI validationURI, Monitor monitor)
        throws IOException, DocumentParserException, DocumentGenerationException {

    final List<Exception> exceptions = new ArrayList<Exception>();
    final Map<String, String> options = getOptions(generation);
    final ResourceSet resourceSetForModels = M2DocUtils.createResourceSetForModels(exceptions, generation,
            new ResourceSetImpl(), options);
    final URIConverter uriConverter = resourceSetForModels.getURIConverter();
    final IQueryEnvironment queryEnvironment = GenconfUtils.getQueryEnvironment(resourceSetForModels, generation);

    if (!uriConverter.exists(templateURI, Collections.EMPTY_MAP)) {
        throw new DocumentGenerationException("The template doest not exist " + templateURI);
    }

    // create generated file
    try (DocumentTemplate documentTemplate = M2DocUtils.parse(uriConverter, templateURI, queryEnvironment,
            classProvider, monitor)) {

        // create definitions
        Map<String, Object> definitions = GenconfUtils.getVariables(generation, resourceSetForModels);

        // validate template
        final URI resultValidationURI = validate(uriConverter, generatedURI, validationURI, documentTemplate,
                queryEnvironment, monitor);

        // launch generation
        final boolean updateFields = Boolean.valueOf(options.get(M2DocUtils.UPDATE_FIELDS_OPTION));
        M2DocUtils.generate(documentTemplate, queryEnvironment, definitions, resourceSetForModels, generatedURI,
                updateFields, monitor);

        List<URI> generatedURIs = new ArrayList<URI>();
        generatedURIs.add(generatedURI);
        if (resultValidationURI != null) {
            generatedURIs.add(resultValidationURI);
        }

        M2DocUtils.cleanResourceSetForModels(generation, resourceSetForModels);

        return generatedURIs;
    }
}
 
Example 8
Source File: ProjectDescriptionLoader.java    From n4js with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * Checks whether {@code uri} points to a resource that actually exists on the file system.
 *
 * @param uriConverter
 *            The uri converter to use
 * @param uri
 *            The uri to check.
 */
private boolean exists(URIConverter uriConverter, URI uri) {
	return uriConverter.exists(uri, null);
}