/**
   * Returns the CMDocument that corresponds to the DOM Node. or null if no CMDocument is
   * appropriate for the DOM Node.
   */
  public CMDocument getCorrespondingCMDocument(Node node) {
    CMDocument jcmdoc = null;
    if (node instanceof IDOMNode) {
      IDOMModel model = ((IDOMNode) node).getModel();
      String modelPath = model.getBaseLocation();
      if (modelPath != null && !IModelManager.UNMANAGED_MODEL.equals(modelPath)) {
        float version =
            DeploymentDescriptorPropertyCache.getInstance().getJSPVersion(new Path(modelPath));
        jcmdoc = JSPCMDocumentFactory.getCMDocument(version);
      }
    }
    if (jcmdoc == null) {
      jcmdoc = JSPCMDocumentFactory.getCMDocument();
    }

    CMDocument result = null;
    try {
      if (node.getNodeType() == Node.ELEMENT_NODE) {
        String elementName = node.getNodeName();

        // test to see if this node belongs to JSP's CMDocument (case
        // sensitive)
        CMElementDeclaration dec =
            (CMElementDeclaration) jcmdoc.getElements().getNamedItem(elementName);
        if (dec != null) {
          result = jcmdoc;
        }
      }

      String prefix = node.getPrefix();

      if (result == null && prefix != null && prefix.length() > 0 && node instanceof IDOMNode) {
        // check position dependent
        IDOMNode xmlNode = (IDOMNode) node;
        TLDCMDocumentManager tldmgr =
            TaglibController.getTLDCMDocumentManager(xmlNode.getStructuredDocument());
        if (tldmgr != null) {
          List documents = tldmgr.getCMDocumentTrackers(node.getPrefix(), xmlNode.getStartOffset());
          // there shouldn't be more than one cmdocument returned
          if (documents != null && documents.size() > 0) result = (CMDocument) documents.get(0);
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return result;
  }
Пример #2
0
  /**
   * Another public method for those who want to get the process resource by their own means (such
   * as the editor).
   */
  public void read(Resource processResource, IDOMModel domModel, ResourceSet resourceSet) {
    // TODO: These two lines are a workaround for
    // https://bugs.eclipse.org/bugs/show_bug.cgi?id=72565
    EcorePackage instance = EcorePackage.eINSTANCE;
    instance.eAdapters();

    this.processResource = processResource;

    // IPath extensionsPath =
    // modelFile.getFullPath().removeFileExtension().addFileExtension(IBPELUIConstants.EXTENSION_MODEL_EXTENSIONS);
    org.eclipse.core.runtime.IPath extensionsPath =
        (new org.eclipse.core.runtime.Path(domModel.getBaseLocation()))
            .removeFileExtension()
            .addFileExtension(IBPELUIConstants.EXTENSION_MODEL_EXTENSIONS);
    URI extensionsUri = URI.createPlatformResourceURI(extensionsPath.toString());
    IFile extensionsFile = ResourcesPlugin.getWorkspace().getRoot().getFile(extensionsPath);

    try {
      processResource.load(Collections.EMPTY_MAP);
      EList<EObject> contents = processResource.getContents();
      if (!contents.isEmpty()) process = (Process) contents.get(0);
    } catch (Exception e) {
      // TODO: If a file is empty Resource.load(Map) throws a java.lang.NegativeArraySizeException
      // We should investigate EMF to see if we are supposed to handle this case or if this
      // is a bug in EMF.
      BPELUIPlugin.log(e);
    }
    try {
      extensionsResource = resourceSet.getResource(extensionsUri, extensionsFile.exists());
      if (extensionsResource != null) {
        extensionMap =
            ExtensionmodelFactory.eINSTANCE.findExtensionMap(
                IBPELUIConstants.MODEL_EXTENSIONS_NAMESPACE, extensionsResource.getContents());
      }
    } catch (Exception e) {
      BPELUIPlugin.log(e);
    }
    if (extensionMap != null) extensionMap.initializeAdapter();

    if (process == null) {
      process = BPELFactory.eINSTANCE.createProcess();
      processResource.getContents().add(process);
    }
    if (extensionMap == null) {
      extensionMap =
          ExtensionmodelFactory.eINSTANCE.createExtensionMap(
              IBPELUIConstants.MODEL_EXTENSIONS_NAMESPACE);
      if (extensionsResource == null) {
        extensionsResource = resourceSet.createResource(extensionsUri);
      }
      extensionsResource.getContents().clear();
      extensionsResource.getContents().add(extensionMap);
    }

    // Make sure the Process has Variables, PartnerLinks and CorrelationSets objects.
    // They aren't strictly necessary according to the spec but make we need those in
    // order for the editor tray to work.
    if (process.getVariables() == null) {
      process.setVariables(BPELFactory.eINSTANCE.createVariables());
    }
    if (process.getPartnerLinks() == null) {
      process.setPartnerLinks(BPELFactory.eINSTANCE.createPartnerLinks());
    }
    if (process.getCorrelationSets() == null) {
      process.setCorrelationSets(BPELFactory.eINSTANCE.createCorrelationSets());
    }
    if (process.getMessageExchanges() == null) {
      process.setMessageExchanges(BPELFactory.eINSTANCE.createMessageExchanges());
    }
    // Make sure scopes have Variables.
    // They aren't strictly necessary according to the spec but make we need those in
    // order for the editor tray to work.
    for (Iterator<EObject> iter = process.eAllContents(); iter.hasNext(); ) {
      EObject object = iter.next();
      if (object instanceof Scope) {
        Scope scope = (Scope) object;
        if (scope.getVariables() == null) {
          scope.setVariables(BPELFactory.eINSTANCE.createVariables());
        }
        if (scope.getPartnerLinks() == null) {
          scope.setPartnerLinks(BPELFactory.eINSTANCE.createPartnerLinks());
        }
        if (scope.getCorrelationSets() == null) {
          scope.setCorrelationSets(BPELFactory.eINSTANCE.createCorrelationSets());
        }
        if (scope.getMessageExchanges() == null) {
          scope.setMessageExchanges(BPELFactory.eINSTANCE.createMessageExchanges());
        }
      }
    }

    // Make sure each model object has the necessary extensions!
    TreeIterator<EObject> it = process.eAllContents();
    while (it.hasNext()) {
      ModelHelper.createExtensionIfNecessary(extensionMap, it.next());
    }

    if (extensionMap.get(process) == null) {
      ModelHelper.createExtensionIfNecessary(extensionMap, process);
    }
  }