Java Code Examples for org.eclipse.emf.ecore.resource.Resource#setURI()
The following examples show how to use
org.eclipse.emf.ecore.resource.Resource#setURI() .
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: GrammarAccessFragment.java From xtext-extras with Eclipse Public License 2.0 | 6 votes |
public void replaceResourceURIsWithNsURIs(Grammar grammar, ResourceSet set) { for (AbstractMetamodelDeclaration metamodelDecl : grammar.getMetamodelDeclarations()) { EPackage pack = metamodelDecl.getEPackage(); Resource packResource = pack.eResource(); if (!packResource.getURI().toString().equals(pack.getNsURI())) { ResourceSet packResourceSet = packResource.getResourceSet(); if (packResourceSet != null) { EPackage topMost = pack; // we need to be aware of empty subpackages while (topMost.getESuperPackage() != null && topMost.getESuperPackage().eResource() == topMost.eResource()) topMost = topMost.getESuperPackage(); if (packResource.getContents().contains(topMost) && packResource.getContents().size() == 1) { if (!topMost.getEClassifiers().isEmpty()) packResource.setURI(URI.createURI(topMost.getNsURI())); else moveSubpackagesToNewResource(topMost, set); } if (!topMost.eResource().getURI().toString().equals(topMost.getNsURI())) movePackageToNewResource(topMost, set); } } } }
Example 2
Source File: XtextLinkingService.java From xtext-core with Eclipse Public License 2.0 | 6 votes |
private List<EObject> createPackage(GeneratedMetamodel generatedMetamodel, ILeafNode text) { final String nsURI = getMetamodelNsURI(text); final URI uri = URI.createURI(nsURI); if (uri == null || isReferencedByUsedGrammar(generatedMetamodel, nsURI)) return Collections.emptyList(); EPackage pack = ((GeneratedMetamodelImpl)generatedMetamodel).basicGetEPackage(); if (pack != null && !pack.eIsProxy()) return singletonList((EObject)pack); final EPackage generatedEPackage = EcoreFactory.eINSTANCE.createEPackage(); generatedEPackage.setName(generatedMetamodel.getName()); generatedEPackage.setNsPrefix(generatedMetamodel.getName()); generatedEPackage.setNsURI(nsURI); final Resource generatedPackageResource = new EcoreResourceFactoryImpl().createResource(uri); XtextResourceSet resourceSet = (XtextResourceSet) generatedMetamodel.eResource().getResourceSet(); if (!resourceSet.getURIResourceMap().containsKey(generatedPackageResource.getURI())) { generatedMetamodel.eResource().getResourceSet().getResources().add(generatedPackageResource); } else { generatedPackageResource.setURI(URI.createURI(nsURI+"_"+generatedMetamodel.hashCode())); generatedMetamodel.eResource().getResourceSet().getResources().add(generatedPackageResource); } generatedPackageResource.getContents().add(generatedEPackage); return Collections.<EObject>singletonList(generatedEPackage); }
Example 3
Source File: ChangeSerializerTest.java From xtext-core with Eclipse Public License 2.0 | 6 votes |
@Test public void testResourceURIChange() { final InMemoryURIHandler fs = new InMemoryURIHandler(); StringConcatenation _builder = new StringConcatenation(); _builder.append("#1 root { }"); Pair<String, String> _mappedTo = Pair.<String, String>of("inmemory:/f.pstl", _builder.toString()); this._changeSerializerTestHelper.operator_add(fs, _mappedTo); final ResourceSet rs = this._changeSerializerTestHelper.createResourceSet(fs); final Node model = this._changeSerializerTestHelper.<Node>contents(rs, "inmemory:/f.pstl", Node.class); final IChangeSerializer serializer = this._changeSerializerTestHelper.newChangeSerializer(); final IChangeSerializer.IModification<Resource> _function = (Resource it) -> { Resource _eResource = model.eResource(); _eResource.setURI(URI.createURI("inmemory:/x.pstl")); }; serializer.<Resource>addModification(model.eResource(), _function); Collection<IEmfResourceChange> _endRecordChangesToTextDocuments = this._changeSerializerTestHelper.endRecordChangesToTextDocuments(serializer); StringConcatenation _builder_1 = new StringConcatenation(); _builder_1.append("----- renamed inmemory:/f.pstl to inmemory:/x.pstl (syntax: <offset|text>) -----"); _builder_1.newLine(); _builder_1.append("(no changes)"); _builder_1.newLine(); _builder_1.append("--------------------------------------------------------------------------------"); _builder_1.newLine(); this._changeSerializerTestHelper.operator_tripleEquals(_endRecordChangesToTextDocuments, _builder_1); }
Example 4
Source File: ResourceRelocationContext.java From xtext-core with Eclipse Public License 2.0 | 6 votes |
/** * Loads and watches the respective resource and applies the relocation change. Clients may usually rather call * {@link #addModification} to register their side-effects. * * @param change * the change to execute */ protected Resource loadAndWatchResource(ResourceRelocationChange change) { if (changeType != null) { switch (changeType) { case MOVE: case RENAME: Resource original = resourceSet.getResource(change.getFromURI(), true); changeSerializer.addModification(original, (Resource it) -> original.setURI(change.getToURI())); return original; case COPY: Resource copy = resourceSet.createResource(change.getFromURI()); try { copy.load(resourceSet.getURIConverter().createInputStream(change.getFromURI()), null); } catch (IOException e) { Exceptions.sneakyThrow(e); } copy.setURI(change.getToURI()); return copy; default: return null; } } return null; }
Example 5
Source File: BackupUtils.java From bonita-studio with GNU General Public License v2.0 | 6 votes |
/** Copy a model based on a number of {@link URI}s. */ public static List<URI> copy(List<URI> sourceURIs, Metamodel metamodel, URIMapper mapper) throws IOException { final List<URI> targetURIs = new ArrayList<URI>(); final ResourceSet model = ResourceUtils.loadResourceSet(sourceURIs, metamodel .getEPackages()); for (final Resource resource : model.getResources()) { if (resource.getURI() == null || resource.getURI().isPlatformPlugin()) { continue; } final URI targetURI = mapper.map(resource.getURI()); if (targetURI != null) { resource.setURI(targetURI); targetURIs.add(targetURI); } } ResourceUtils.saveResourceSet(model, null); return targetURIs; }
Example 6
Source File: GrammarAccessFragment.java From xtext-extras with Eclipse Public License 2.0 | 5 votes |
/** * @since 2.4 */ protected void movePackageToNewResource(EPackage pack, ResourceSet set) { Resource resource = set.createResource( URI.createURI("___temp___." + FragmentFakingEcoreResourceFactoryImpl.ECORE_SUFFIX), ContentHandler.UNSPECIFIED_CONTENT_TYPE); resource.setURI(URI.createURI(pack.getNsURI())); resource.getContents().add(pack); }
Example 7
Source File: GrammarAccessFragment2.java From xtext-core with Eclipse Public License 2.0 | 5 votes |
protected void movePackageToNewResource(final EPackage pack, final ResourceSet set) { final Resource resource = set.createResource( URI.createURI(("___temp___." + FragmentFakingEcoreResource.FactoryImpl.ECORE_SUFFIX)), ContentHandler.UNSPECIFIED_CONTENT_TYPE); resource.setURI(URI.createURI(pack.getNsURI())); resource.getContents().add(pack); }
Example 8
Source File: StextResourceFactory.java From statecharts with Eclipse Public License 1.0 | 5 votes |
@Override public Resource createResource(URI uri) { String domainID = DomainRegistry.determineDomainID(uri); if (domainID == null || DomainRegistry.getDomain(domainID) == null) { return new XMIResourceImpl(uri); } IDomain domain = DomainRegistry.getDomain(domainID); Injector injector = getInjector(domain); Resource resource = injector.getInstance(Resource.class); ResourceSet set = new ResourceSetImpl(); set.getResources().add(resource); resource.setURI(uri); return resource; }
Example 9
Source File: GrammarAccessFragment2.java From xtext-core with Eclipse Public License 2.0 | 4 votes |
protected void addAllGrammarsToResource(final Resource resource, final Grammar grammar, final Set<Grammar> visitedGrammars) { boolean _add = visitedGrammars.add(grammar); boolean _not = (!_add); if (_not) { return; } resource.getContents().add(grammar); EList<AbstractMetamodelDeclaration> _metamodelDeclarations = grammar.getMetamodelDeclarations(); for (final AbstractMetamodelDeclaration metamodelDecl : _metamodelDeclarations) { { final EPackage pack = metamodelDecl.getEPackage(); final Resource packResource = pack.eResource(); String _string = packResource.getURI().toString(); String _nsURI = pack.getNsURI(); boolean _notEquals = (!Objects.equal(_string, _nsURI)); if (_notEquals) { final ResourceSet packResourceSet = packResource.getResourceSet(); if ((packResourceSet != null)) { EPackage topMost = pack; while (((topMost.getESuperPackage() != null) && (topMost.getESuperPackage().eResource() == topMost.eResource()))) { topMost = topMost.getESuperPackage(); } if ((packResource.getContents().contains(topMost) && (packResource.getContents().size() == 1))) { boolean _isEmpty = topMost.getEClassifiers().isEmpty(); boolean _not_1 = (!_isEmpty); if (_not_1) { packResource.setURI(URI.createURI(topMost.getNsURI())); } else { this.moveSubpackagesToNewResource(topMost, resource.getResourceSet()); } } boolean _equals = topMost.eResource().getURI().toString().equals(topMost.getNsURI()); boolean _not_2 = (!_equals); if (_not_2) { this.movePackageToNewResource(topMost, resource.getResourceSet()); } } } } } EList<Grammar> _usedGrammars = grammar.getUsedGrammars(); for (final Grammar usedGrammar : _usedGrammars) { this.addAllGrammarsToResource(resource, usedGrammar, visitedGrammars); } }