Java Code Examples for org.eclipse.emf.ecore.resource.Resource#unload()

The following examples show how to use org.eclipse.emf.ecore.resource.Resource#unload() . 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: XtextFakeResourceContext.java    From statecharts with Eclipse Public License 1.0 6 votes vote down vote up
public void updateFakeResourceContext(IXtextFakeContextResourcesProvider contextProvider) {

		// remove any other resources that may have been created earlier
		// unloading them (to remove all adapters)
		List<Resource> staleResources = new ArrayList<Resource>();
		for (Resource r : fakeResourceSet.getResources()) {
			if (r != fakeResource) {
				staleResources.add(r);
				r.unload();
			}
		}
		fakeResourceSet.getResources().removeAll(staleResources);

		// when populating the fake resource set, the non-existing fake resource
		// contained in the resource set may be problematic, so we temporarily
		// remove it
		fakeResourceSet.getResources().remove(fakeResource);
		contextProvider.populateFakeResourceSet(fakeResourceSet, fakeResource);
		fakeResourceSet.getResources().add(fakeResource);
	}
 
Example 2
Source File: ProcessConfigurationFileStore.java    From bonita-studio with GNU General Public License v2.0 6 votes vote down vote up
@Override
protected void doSave(final Object content) {
    final Resource resource = getEMFResource();
    if (content instanceof Configuration) {
        resource.getContents().clear();
        resource.getContents().add(EcoreUtil.copy((Configuration) content));
    }
    try {
        final Map<String, String> options = new HashMap<String, String>();
        options.put(XMLResource.OPTION_ENCODING, "UTF-8");
        options.put(XMLResource.OPTION_XML_VERSION, "1.0");
        resource.save(options);
        resource.unload();

    } catch (final IOException e) {
        BonitaStudioLog.error(e);
    }
}
 
Example 3
Source File: MonitoredClusteringBuilderState.java    From dsl-devkit with Eclipse Public License 1.0 6 votes vote down vote up
@Override
protected Resource addResource(final Resource resource, final ResourceSet resourceSet) {
  URI uri = resource.getURI();
  Resource r = resourceSet.getResource(uri, false);
  if (r == null) {
    resourceSet.getResources().add(resource);
    return resource;
  } else if (r instanceof StorageAwareResource && ((StorageAwareResource) r).isLoadedFromStorage()) {
    // make sure to not process any binary resources in builder as it could have incorrect linking
    r.unload();
    resourceSet.getResources().set(resourceSet.getResources().indexOf(r), resource);
    return resource;
  } else {
    return r;
  }
}
 
Example 4
Source File: TestImportBPMN2.java    From bonita-studio with GNU General Public License v2.0 5 votes vote down vote up
public void testImportBPMN2() throws Exception {
    URL bpmnResource = FileLocator.toFileURL(getClass().getResource("standardProcess.bpmn")); //$NON-NLS-1$
    BPMNToProc bpmnToProc = new BPMNToProc(bpmnResource.getFile());
    destFile = bpmnToProc.createDiagram(bpmnResource, new NullProgressMonitor());

    ResourceSet resourceSet = new ResourceSetImpl();
    Resource resource = resourceSet.getResource(toEMFURI(destFile), true);
    MainProcess mainProcess = (MainProcess) resource.getContents().get(0);

    checkContent(mainProcess, 4, 14, 3, 0, 1, null);
    resource.unload();
}
 
Example 5
Source File: TestJBPMImport.java    From bonita-studio with GNU General Public License v2.0 5 votes vote down vote up
public void testSimpleProcess() throws Exception {
    URL srcUrl = getClass().getResource("testSimpleProcess1/processdefinition.xml");
    File destFile = new JBPM3ToProc().createDiagram(srcUrl, new NullProgressMonitor());
    destFile.deleteOnExit();
    ResourceSet resourceSet = new ResourceSetImpl();
    Resource resource = resourceSet.getResource(toEMFURI(destFile), true);
    MainProcess mainProcess = (MainProcess) resource.getContents().get(0);

    assertNbItems(mainProcess, 1, 1, 1, 0, 2, 2, 1, 9, 2);
    Activity mailNode = findActivity(mainProcess, "mail-node1");
    assertEquals("No mail connector found", "email", mailNode.getConnectors().get(0).getDefinitionId());

    final AbstractProcess process = (AbstractProcess) mainProcess.getElements().get(0);
    final ProcessConfigurationRepositoryStore store = (ProcessConfigurationRepositoryStore) RepositoryManager
            .getInstance().getRepositoryStore(ProcessConfigurationRepositoryStore.class);
    final String fileName = ModelHelper.getEObjectID(process) + ".conf";
    ProcessConfigurationFileStore fileStore = store.getChild(fileName, true);
    if (fileStore == null) {
        fileStore = store.createRepositoryFileStore(fileName);
        fileStore.save(ConfigurationFactory.eINSTANCE.createConfiguration());
    }
    final Configuration configuration = fileStore.getContent();
    final ConfigurationSynchronizer configurationSynchronizer = new ConfigurationSynchronizer(process, configuration);
    configurationSynchronizer.synchronize();
    assertFalse("Configuration should not be valid", configurationSynchronizer.isConfigurationValid());
    resource.unload();

}
 
Example 6
Source File: ProcBuilderTests.java    From bonita-studio with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testStartErrorEvent() throws Exception {
    final String diagamFileName = "testDiagramWithStartErrorEvent.proc";
    final String diagramName = "testDiagramWithStartErrorEvent";
    final String poolName = "PoolWithStartErrorEvent";
    final String description = "Simple description";
    final List<String> categories = new ArrayList<String>(2);
    categories.add("R&D");
    categories.add("Service");
    final File diagramFile = new File(ProjectUtil.getBonitaStudioWorkFolder(), diagamFileName);
    diagramFile.deleteOnExit();
    final URI targetURI = URI.createFileURI(diagramFile.getAbsolutePath());
    initProcBuilderWithDefaultContent(diagramName, poolName, description, categories, diagramFile);

    procBuilder.addEventSubprocess("eventSubProc", "eventSubProc", new Point(100, 100), new Dimension(200, 200), false);
    procBuilder.addEvent("startError", "startError", new Point(100, 100), new Dimension(50, 50), EventType.START_ERROR);

    procBuilder.done();

    final TransactionalEditingDomain editingDomain = GMFEditingDomainFactory.INSTANCE.createEditingDomain();
    final Resource diagramResource = editingDomain.getResourceSet().createResource(targetURI);
    diagramResource.load(new HashMap<String, String>());
    assertTrue("Import as failed", diagramResource.getContents().size() > 0);
    final MainProcess proc = (MainProcess) diagramResource.getContents().get(0);
    final Pool p = (Pool) proc.getElements().get(0);

    final DeployProcessOperation deployProcessOperation = new DeployProcessOperation();
    deployProcessOperation.addProcessToDeploy(p);
    StatusAssert.assertThat(deployProcessOperation.run(Repository.NULL_PROGRESS_MONITOR)).isOK();
    diagramResource.unload();
}
 
Example 7
Source File: EMFGeneratorFragment.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected Resource createResourceForEPackages(Grammar grammar, XpandExecutionContext ctx, List<EPackage> packs,
		ResourceSet rs) {
	URI ecoreFileUri = getEcoreFileUri(grammar, ctx);
	ecoreFileUri = toPlatformResourceURI(ecoreFileUri);
	Resource existing = rs.getResource(ecoreFileUri, false);
	if (existing != null) {
		existing.unload();
		rs.getResources().remove(existing);
	}
	Resource ecoreFile = rs.createResource(ecoreFileUri, ContentHandler.UNSPECIFIED_CONTENT_TYPE);
	ecoreFile.getContents().addAll(packs);
	return ecoreFile;
}
 
Example 8
Source File: ProtocolEditor.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Handles what to do with changed resources on activation.
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
protected void handleChangedResources ()
{
    if ( !changedResources.isEmpty () && ( !isDirty () || handleDirtyConflict () ) )
    {
        if ( isDirty () )
        {
            changedResources.addAll ( editingDomain.getResourceSet ().getResources () );
        }
        editingDomain.getCommandStack ().flush ();

        updateProblemIndication = false;
        for ( Resource resource : changedResources )
        {
            if ( resource.isLoaded () )
            {
                resource.unload ();
                try
                {
                    resource.load ( Collections.EMPTY_MAP );
                }
                catch ( IOException exception )
                {
                    if ( !resourceToDiagnosticMap.containsKey ( resource ) )
                    {
                        resourceToDiagnosticMap.put ( resource, analyzeResourceProblems ( resource, exception ) );
                    }
                }
            }
        }

        if ( AdapterFactoryEditingDomain.isStale ( editorSelection ) )
        {
            setSelection ( StructuredSelection.EMPTY );
        }

        updateProblemIndication = true;
        updateProblemIndication ();
    }
}
 
Example 9
Source File: TestImportBPMN2.java    From bonita-studio with GNU General Public License v2.0 5 votes vote down vote up
public void testBug1908a() throws Exception {
    URL bpmnResource = FileLocator.toFileURL(getClass().getResource("definitionsTest2.bpmn")); //$NON-NLS-1$
    BPMNToProc bpmnToProc = new BPMNToProc(bpmnResource.getFile());
    destFile = bpmnToProc.createDiagram(bpmnResource, new NullProgressMonitor());

    ResourceSet resourceSet = new ResourceSetImpl();
    Resource resource = resourceSet.getResource(toEMFURI(destFile), true);
    MainProcess mainProcess = (MainProcess) resource.getContents().get(0);

    checkContent(mainProcess, 1, 2, 0, 0, 0, null);
    resource.unload();
}
 
Example 10
Source File: CrossflowEditor.java    From scava with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Handles what to do with changed resources on activation.
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
protected void handleChangedResources() {
	if (!changedResources.isEmpty() && (!isDirty() || handleDirtyConflict())) {
		ResourceSet resourceSet = editingDomain.getResourceSet();
		if (isDirty()) {
			changedResources.addAll(resourceSet.getResources());
		}
		editingDomain.getCommandStack().flush();

		updateProblemIndication = false;
		for (Resource resource : changedResources) {
			if (resource.isLoaded()) {
				resource.unload();
				try {
					resource.load(resourceSet.getLoadOptions());
				}
				catch (IOException exception) {
					if (!resourceToDiagnosticMap.containsKey(resource)) {
						resourceToDiagnosticMap.put(resource, analyzeResourceProblems(resource, exception));
					}
				}
			}
		}

		if (AdapterFactoryEditingDomain.isStale(editorSelection)) {
			setSelection(StructuredSelection.EMPTY);
		}

		updateProblemIndication = true;
		updateProblemIndication();
	}
}
 
Example 11
Source File: DeploymentEditor.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Handles what to do with changed resources on activation.
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
protected void handleChangedResources ()
{
    if ( !changedResources.isEmpty () && ( !isDirty () || handleDirtyConflict () ) )
    {
        if ( isDirty () )
        {
            changedResources.addAll ( editingDomain.getResourceSet ().getResources () );
        }
        editingDomain.getCommandStack ().flush ();

        updateProblemIndication = false;
        for ( Resource resource : changedResources )
        {
            if ( resource.isLoaded () )
            {
                resource.unload ();
                try
                {
                    resource.load ( Collections.EMPTY_MAP );
                }
                catch ( IOException exception )
                {
                    if ( !resourceToDiagnosticMap.containsKey ( resource ) )
                    {
                        resourceToDiagnosticMap.put ( resource, analyzeResourceProblems ( resource, exception ) );
                    }
                }
            }
        }

        if ( AdapterFactoryEditingDomain.isStale ( editorSelection ) )
        {
            setSelection ( StructuredSelection.EMPTY );
        }

        updateProblemIndication = true;
        updateProblemIndication ();
    }
}
 
Example 12
Source File: SetupEditor.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Handles what to do with changed resources on activation.
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
protected void handleChangedResources ()
{
    if ( !changedResources.isEmpty () && ( !isDirty () || handleDirtyConflict () ) )
    {
        if ( isDirty () )
        {
            changedResources.addAll ( editingDomain.getResourceSet ().getResources () );
        }
        editingDomain.getCommandStack ().flush ();

        updateProblemIndication = false;
        for ( Resource resource : changedResources )
        {
            if ( resource.isLoaded () )
            {
                resource.unload ();
                try
                {
                    resource.load ( Collections.EMPTY_MAP );
                }
                catch ( IOException exception )
                {
                    if ( !resourceToDiagnosticMap.containsKey ( resource ) )
                    {
                        resourceToDiagnosticMap.put ( resource, analyzeResourceProblems ( resource, exception ) );
                    }
                }
            }
        }

        if ( AdapterFactoryEditingDomain.isStale ( editorSelection ) )
        {
            setSelection ( StructuredSelection.EMPTY );
        }

        updateProblemIndication = true;
        updateProblemIndication ();
    }
}
 
Example 13
Source File: TestImportBPMN2.java    From bonita-studio with GNU General Public License v2.0 5 votes vote down vote up
public void testImportBPMN2WithOMG_ns() throws Exception {
    URL bpmnResource = FileLocator.toFileURL(getClass().getResource("testimport.bpmn")); //$NON-NLS-1$
    BPMNToProc bpmnToProc = new BPMNToProc(bpmnResource.getFile());
    destFile = bpmnToProc.createDiagram(bpmnResource, new NullProgressMonitor());

    ResourceSet resourceSet = new ResourceSetImpl();
    Resource resource = resourceSet.getResource(toEMFURI(destFile), true);
    MainProcess mainProcess = (MainProcess) resource.getContents().get(0);

    checkContent(mainProcess, 2, 4, 0, 0, 0, null);
    resource.unload();
}
 
Example 14
Source File: OsgiEditor.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Handles what to do with changed resources on activation.
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
protected void handleChangedResources ()
{
    if ( !changedResources.isEmpty () && ( !isDirty () || handleDirtyConflict () ) )
    {
        if ( isDirty () )
        {
            changedResources.addAll ( editingDomain.getResourceSet ().getResources () );
        }
        editingDomain.getCommandStack ().flush ();

        updateProblemIndication = false;
        for ( Resource resource : changedResources )
        {
            if ( resource.isLoaded () )
            {
                resource.unload ();
                try
                {
                    resource.load ( Collections.EMPTY_MAP );
                }
                catch ( IOException exception )
                {
                    if ( !resourceToDiagnosticMap.containsKey ( resource ) )
                    {
                        resourceToDiagnosticMap.put ( resource, analyzeResourceProblems ( resource, exception ) );
                    }
                }
            }
        }

        if ( AdapterFactoryEditingDomain.isStale ( editorSelection ) )
        {
            setSelection ( StructuredSelection.EMPTY );
        }

        updateProblemIndication = true;
        updateProblemIndication ();
    }
}
 
Example 15
Source File: EMFGeneratorFragment2.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected Resource createResourceForEPackages(final Grammar grammar, final List<EPackage> packs, final ResourceSet rs) {
  final URI ecoreFileUri = this.getEcoreFileUri(grammar);
  final Resource existing = rs.getResource(ecoreFileUri, false);
  if ((existing != null)) {
    existing.unload();
    rs.getResources().remove(existing);
  }
  final Resource ecoreFile = rs.createResource(ecoreFileUri, ContentHandler.UNSPECIFIED_CONTENT_TYPE);
  ecoreFile.getContents().addAll(packs);
  return ecoreFile;
}
 
Example 16
Source File: AbstractReferenceUpdater.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected void unloadNonTargetResources(ResourceSet resourceSet, Set<Resource> targetResources) {
	for (Resource resource : newArrayList(resourceSet.getResources())) {
		if (!targetResources.contains(resource)) {
			resource.unload();
			resourceSet.getResources().remove(resource);
		}
	}
}
 
Example 17
Source File: RecipeEditor.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Handles what to do with changed resources on activation.
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
protected void handleChangedResources ()
{
    if ( !changedResources.isEmpty () && ( !isDirty () || handleDirtyConflict () ) )
    {
        if ( isDirty () )
        {
            changedResources.addAll ( editingDomain.getResourceSet ().getResources () );
        }
        editingDomain.getCommandStack ().flush ();

        updateProblemIndication = false;
        for ( Resource resource : changedResources )
        {
            if ( resource.isLoaded () )
            {
                resource.unload ();
                try
                {
                    resource.load ( Collections.EMPTY_MAP );
                }
                catch ( IOException exception )
                {
                    if ( !resourceToDiagnosticMap.containsKey ( resource ) )
                    {
                        resourceToDiagnosticMap.put ( resource, analyzeResourceProblems ( resource, exception ) );
                    }
                }
            }
        }

        if ( AdapterFactoryEditingDomain.isStale ( editorSelection ) )
        {
            setSelection ( StructuredSelection.EMPTY );
        }

        updateProblemIndication = true;
        updateProblemIndication ();
    }
}
 
Example 18
Source File: EipEditor.java    From eip-designer with Apache License 2.0 5 votes vote down vote up
/**
   * Handles what to do with changed resources on activation.
   * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
   * @generated
   */
protected void handleChangedResources() {
     if (!changedResources.isEmpty() && (!isDirty() || handleDirtyConflict())) {
        if (isDirty()) {
           changedResources.addAll(editingDomain.getResourceSet().getResources());
        }
        editingDomain.getCommandStack().flush();

        updateProblemIndication = false;
        for (Resource resource : changedResources) {
           if (resource.isLoaded()) {
              resource.unload();
              try {
                 resource.load(Collections.EMPTY_MAP);
              }
              catch (IOException exception) {
                 if (!resourceToDiagnosticMap.containsKey(resource)) {
                    resourceToDiagnosticMap.put(resource, analyzeResourceProblems(resource, exception));
                 }
              }
           }
        }

        if (AdapterFactoryEditingDomain.isStale(editorSelection)) {
           setSelection(StructuredSelection.EMPTY);
        }

        updateProblemIndication = true;
        updateProblemIndication();
     }
  }
 
Example 19
Source File: IncrementalBuilder.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
protected void unloadResource(URI uri) {
	XtextResourceSet resourceSet = request.getResourceSet();
	Resource resource = resourceSet.getResource(uri, false);
	if (resource != null) {
		resourceSet.getResources().remove(resource);
		// proxify
		resource.unload();
	}
}
 
Example 20
Source File: CheckMarkerUpdateJob.java    From dsl-devkit with Eclipse Public License 1.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override
protected IStatus run(final IProgressMonitor monitor) {

  // Let's start (number of task = number of resource * 2 (loading + validating))
  monitor.beginTask("", 2 * this.uris.size()); //$NON-NLS-1$

  for (final URI uri : this.uris) {
    // Last chance to cancel before next validation
    if (monitor.isCanceled()) {
      return Status.CANCEL_STATUS;
    }

    final IResourceServiceProvider serviceProvider = serviceProviderRegistry.getResourceServiceProvider(uri);
    if (serviceProvider == null) {
      // This may happen for non-Xtext resources in ice entities
      if (LOGGER.isDebugEnabled()) {
        LOGGER.debug(MessageFormat.format("Could not validate {0}: no resource service provider found", uri.toString())); //$NON-NLS-1$
      }
      continue; // Skip to next URI
    }

    final IResourceValidator resourceValidator = serviceProvider.getResourceValidator();
    final IStorage2UriMapper uriMapper = serviceProvider.get(IStorage2UriMapper.class);
    final MarkerCreator markerCreator = serviceProvider.get(MarkerCreator.class);

    // Get the file; only local files will be re-validated, derived files are ignored
    final IFile iFile = getFileFromStorageMapper(uriMapper, uri);
    if (iFile == null) {
      continue; // no storage mapping found for this URI
    }

    if (resourceValidator == null) {
      LOGGER.error(MessageFormat.format("Could not validate {0}: no resource validator found", iFile.getName())); //$NON-NLS-1$
    } else if (iFile != null) {
      monitor.subTask("loading " + iFile.getName()); //$NON-NLS-1$

      // Don't try to evaluate resource set before it has been checked that the storage provider contains a mapping
      // for current uri
      final ResourceSet resourceSet = getResourceSet(uriMapper, uri);

      // Load the corresponding resource
      boolean loaded = false;
      Resource eResource = null;
      try {
        eResource = resourceSet.getResource(uri, false);
        if ((eResource == null) || (eResource != null && !eResource.isLoaded())) {
          // if the resource does not exist in the resource set, or is not loaded yet
          // load it.
          eResource = resourceSet.getResource(uri, true);
          loaded = true;
        }
        monitor.worked(1);
        // CHECKSTYLE:OFF
      } catch (final RuntimeException e) {
        // CHECKSTYLE:ON
        LOGGER.error(MessageFormat.format("{0} could not be validated.", iFile.getName()), e); //$NON-NLS-1$
      } finally {
        if (eResource != null) {
          validateAndCreateMarkers(resourceValidator, markerCreator, iFile, eResource, monitor);
          LOGGER.debug("Validated " + uri); //$NON-NLS-1$
          if (loaded) { // NOPMD
            // unload any resource that was previously loaded as part of this loop.
            eResource.unload();
          }
        }
      }
    }
  }

  monitor.done();

  return Status.OK_STATUS;
}