/**
   * SIMPLY COPIED;
   *
   * @param diagram
   * @return
   */
  private IProject getProjectFromDiagram(Diagram diagram) {
    IProject currentProject = null;
    Resource resource = diagram.eResource();

    URI uri = resource.getURI();
    URI uriTrimmed = uri.trimFragment();

    if (uriTrimmed.isPlatformResource()) {
      String platformString = uriTrimmed.toPlatformString(true);
      IResource fileResource = ResourcesPlugin.getWorkspace().getRoot().findMember(platformString);

      if (fileResource != null) {
        currentProject = fileResource.getProject();
      }
    } else {
      IResource fileResource =
          ResourcesPlugin.getWorkspace().getRoot().findMember(uriTrimmed.toString());

      if (fileResource != null) {
        currentProject = fileResource.getProject();
      }
    }

    return currentProject;
  }
 private void loadProfilesFromUML2Registry(org.eclipse.uml2.uml.Package package_) {
   ResourceSet resourceSet = package_.eResource().getResourceSet();
   for (URI profileURI : UMLPlugin.getEPackageNsURIToProfileLocationMap().values()) {
     try {
       resourceSet.getResource(profileURI.trimFragment(), true);
     } catch (Exception e) { // ignore
     }
   }
 }
  /** Add the specified list any external resource for this EReference feature */
  protected void processReferenceValue(
      final Resource eResource,
      final EObject eObject,
      final EReference eReference,
      final EObject value,
      final List externalResources,
      final Set unresolvedResourceURIs) {
    if (value == null) {
      return;
    }

    // Check if the object is an EMF proxy ...
    if (value.eIsProxy()) {
      if (value instanceof InternalEObject) {
        final InternalEObject iObject = (InternalEObject) value;
        final URI proxyUri = iObject.eProxyURI();
        CoreArgCheck.isNotNull(proxyUri);

        // Get the URI of the resource ...
        URI resourceUri = proxyUri.trimFragment();

        // Make the relative URI absolute if necessary
        URI baseLocationUri = eResource.getURI();
        URI proxyLocationUri = UriHelper.makeAbsoluteUri(baseLocationUri, resourceUri);
        // URI proxyLocationUri = resourceUri;
        // if (baseLocationUri.isHierarchical() && !baseLocationUri.isRelative() &&
        // proxyUri.isRelative()) {
        // proxyLocationUri = proxyLocationUri.resolve(baseLocationUri);
        // }
        Resource rsrc = findByURI(proxyLocationUri, true);

        // If the resource URI is a workspace relative path (e.g. "/project/.../model.xmi")
        if (rsrc == null && baseLocationUri.isFile() && resourceUri.toString().charAt(0) == '/') {
          String baseLocation = URI.decode(baseLocationUri.toFileString());
          String projectName = resourceUri.segment(0);
          String proxyLocation = URI.decode(resourceUri.toString());
          int index = baseLocation.indexOf(projectName);
          if (index != -1) {
            proxyLocation = baseLocation.substring(0, index - 1) + proxyLocation;
            rsrc = findByURI(URI.createFileURI(proxyLocation), true);
          }
        }

        if (rsrc != null && eResource != rsrc && !externalResources.contains(rsrc)) {
          externalResources.add(rsrc);
        } else if (rsrc == null) {
          unresolvedResourceURIs.add(resourceUri);
        }
      }
    } else {
      Resource rsrc = value.eResource();
      if (eResource != rsrc && !externalResources.contains(rsrc)) {
        externalResources.add(rsrc);
      }
    }
  }
示例#4
0
文件: Ecore2AS.java 项目: eclipse/ocl
 protected @NonNull URI createPivotURI() {
   if (ecoreURI != null) {
     return PivotUtilInternal.getASURI(ecoreURI.trimFragment());
   }
   URI uri = ecoreResource.getURI();
   if (uri == null) {
     throw new IllegalStateException("Missing resource URI");
   }
   return PivotUtilInternal.getASURI(uri);
 }
示例#5
0
 public static @NonNull URI getASURI(@NonNull URI uri) {
   if (uri.fragment() != null) {
     logger.error("Unexpected fragment ignored for '" + uri.toString() + "'");
     uri = uri.trimFragment();
   }
   URI asURI = uri.appendFileExtension(PivotConstants.OCL_AS_FILE_EXTENSION);
   if (!isASURI(asURI)) {
     asURI = uri.appendSegment(PivotConstants.DOT_OCL_AS_FILE_EXTENSION);
   }
   assert isASURI(asURI);
   return asURI;
 }
 /**
  * @see org.teiid.designer.core.container#findByEObject(org.eclipse.emf.ecore.EObject)
  * @since 4.3
  */
 @Override
 public Resource findByEObject(final EObject eObject) {
   if (eObject == null) {
     return null;
   }
   Resource result = null;
   if (eObject.eIsProxy()) {
     URI proxyURI = ((InternalEObject) eObject).eProxyURI();
     result = findByURI(proxyURI.trimFragment(), true);
   } else {
     result = eObject.eResource();
   }
   return result;
 }
示例#7
0
文件: Ecore2AS.java 项目: eclipse/ocl
  private @NonNull URI resolveImportURI(
      @NonNull URI uri, @NonNull EPackage ePackage, @Nullable URI baseURI) {
    if (baseURI == null) {
      return uri;
    }
    ProjectManager projectManager = environmentFactory.getProjectManager();
    if (!(projectManager instanceof StandaloneProjectMap)) {
      return uri;
    }
    StandaloneProjectMap projectMap = (StandaloneProjectMap) projectManager;
    /*		if (baseURI == null) {
    	IPackageDescriptor packageDescriptor = projectManager.getPackageDescriptor(URI.createURI(ePackage.getNsURI()));
    	if (packageDescriptor == null) {
    		return uri;
    	}
    	IResourceDescriptor resourceDescriptor = packageDescriptor.getResourceDescriptor();
    	baseURI = resourceDescriptor.getPlatformPluginURI();
    } */
    uri = uri.resolve(baseURI);
    if (uri.isPlatformPlugin()
        && ClassUtil.safeEquals(ePackage.getNsURI(), String.valueOf(ePackage.eResource().getURI()))
        && (uri.segmentCount() >= 1)) {
      @NonNull String projectName = uri.segment(1);
      IProjectDescriptor projectDescriptor = projectMap.getProjectDescriptor(projectName);
      if (projectDescriptor instanceof IProjectDescriptorExtension) {
        Collection<IResourceDescriptor> resourceDescriptors =
            projectDescriptor.getResourceDescriptors();
        if (resourceDescriptors != null) {
          for (IResourceDescriptor resourceDescriptor : resourceDescriptors) {
            if (ClassUtil.safeEquals(
                uri.trimFragment(), resourceDescriptor.getPlatformPluginURI())) {
              Iterable<@NonNull IPackageDescriptor> packageDescriptors =
                  ((IProjectDescriptorExtension) projectDescriptor).getPackageDescriptors();
              if (packageDescriptors != null) {
                for (IPackageDescriptor packageDescriptor : packageDescriptors) {
                  uri = packageDescriptor.getNsURI();
                  break;
                }
              }

              break;
            }
          }
        }
      }
    }
    return uri;
  }
示例#8
0
 public static void saveToModelFile(final EObject obj, final Diagram d)
     throws CoreException, IOException {
   URI uri = d.eResource().getURI();
   uri = uri.trimFragment();
   uri = uri.trimFileExtension();
   uri = uri.appendFileExtension("model");
   ResourceSet rSet = d.eResource().getResourceSet();
   final IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
   IResource file = workspaceRoot.findMember(uri.toPlatformString(true));
   if (file == null || !file.exists()) {
     Resource createResource = rSet.createResource(uri);
     createResource.save(new HashMap());
     createResource.setTrackingModification(true);
   }
   final Resource resource = rSet.getResource(uri, true);
   resource.getContents().add(obj);
 }
 /** @since 2.0 */
 public void findIndexedReferences(
     IQueryData queryData,
     URI resourceURI,
     IAcceptor<IReferenceDescription> acceptor,
     IProgressMonitor progressMonitor) {
   IResourceDescription resourceDescription =
       index.getResourceDescription(resourceURI.trimFragment());
   if (resourceDescription != null) {
     for (IReferenceDescription referenceDescription :
         resourceDescription.getReferenceDescriptions()) {
       if (queryData.getTargetURIs().contains(referenceDescription.getTargetEObjectUri())
           && (queryData.getResultFilter() == null
               || queryData.getResultFilter().apply(referenceDescription))) {
         acceptor.accept(referenceDescription);
       }
     }
   }
 }
示例#10
0
 public boolean loadResource(ModelSet modelSet, URI uri) {
   // pathmap resource are always loaded
   boolean result = !uri.isPlatform() && !uri.isFile();
   if (!result) {
     URI lastInitialURI = SashModelUtils.getInitialURI(modelSet).trimFileExtension();
     if (!lastInitialURI.equals(initialURI)) {
       clear();
       initialURI = lastInitialURI;
     }
     URI uritrimFragment = uri.trimFragment().trimFileExtension();
     Set<String> extensions = mappingURIExtensions.get(uritrimFragment);
     if (extensions == null) {
       extensions = new HashSet<String>();
       mappingURIExtensions.put(uritrimFragment, extensions);
     }
     extensions.add(uri.fileExtension());
     result = lastInitialURI.equals(uritrimFragment);
     if (!result) {
       result = alreadyValidated.contains(uritrimFragment);
       if (!result) {
         if (!alreadyGuessed.contains(uritrimFragment)) {
           String message =
               new StringBuffer("<form><p>Your model is linked to an external resource (")
                   .append(uritrimFragment.toString())
                   .append(").</p><p>Do you want to load it ?</p></form>")
                   .toString();
           NotificationBuilder builder = getNotification(message, uritrimFragment, modelSet);
           builder.run();
           alreadyGuessed.add(uritrimFragment);
           // notification
         }
       }
     }
   }
   return result;
 }
 /** @generated */
 protected void setDocumentContent(IDocument document, IEditorInput element) throws CoreException {
   IDiagramDocument diagramDocument = (IDiagramDocument) document;
   TransactionalEditingDomain domain = diagramDocument.getEditingDomain();
   if (element instanceof URIEditorInput) {
     URI uri = ((URIEditorInput) element).getURI();
     Resource resource = null;
     try {
       resource = domain.getResourceSet().getResource(uri.trimFragment(), false);
       if (resource == null) {
         resource = domain.getResourceSet().createResource(uri.trimFragment());
       }
       if (!resource.isLoaded()) {
         try {
           Map options = new HashMap(GMFResourceFactory.getDefaultLoadOptions());
           // @see 171060
           // options.put(org.eclipse.emf.ecore.xmi.XMLResource.OPTION_RECORD_UNKNOWN_FEATURE,
           // Boolean.TRUE);
           resource.load(options);
         } catch (IOException e) {
           resource.unload();
           throw e;
         }
       }
       if (uri.fragment() != null) {
         EObject rootElement = resource.getEObject(uri.fragment());
         if (rootElement instanceof Diagram) {
           document.setContent((Diagram) rootElement);
           return;
         }
       } else {
         for (Iterator it = resource.getContents().iterator(); it.hasNext(); ) {
           Object rootElement = it.next();
           if (rootElement instanceof Diagram) {
             document.setContent((Diagram) rootElement);
             return;
           }
         }
       }
       throw new RuntimeException(Messages.StateChartDocumentProvider_NoDiagramInResourceError);
     } catch (Exception e) {
       CoreException thrownExcp = null;
       if (e instanceof CoreException) {
         thrownExcp = (CoreException) e;
       } else {
         String msg = e.getLocalizedMessage();
         thrownExcp =
             new CoreException(
                 new Status(
                     IStatus.ERROR,
                     StateChartDiagramEditorPlugin.ID,
                     0,
                     msg != null ? msg : Messages.StateChartDocumentProvider_DiagramLoadingError,
                     e));
       }
       throw thrownExcp;
     }
   } else {
     throw new CoreException(
         new Status(
             IStatus.ERROR,
             StateChartDiagramEditorPlugin.ID,
             0,
             NLS.bind(
                 Messages.StateChartDocumentProvider_IncorrectInputError,
                 new Object[] {
                   element, "org.eclipse.emf.common.ui.URIEditorInput"
                 }), //$NON-NLS-1$
             null));
   }
 }