/**
  * Line endings can be an issue when running tests on different OSs. This function standardizes
  * the line endings to use <code>\n</code>
  *
  * <p>It will get the text from the given editor, change the line endings, and then save the
  * editor
  *
  * @param editor standardize the line endings of the text presented in this editor.
  */
 private static void standardizeLineEndings(StructuredTextEditor editor) {
   IDocument doc = editor.getTextViewer().getDocument();
   String contents = doc.get();
   contents = StringUtils.replace(contents, "\r\n", "\n");
   contents = StringUtils.replace(contents, "\r", "\n");
   doc.set(contents);
 }
  private void formatAndAssertEquals(
      String beforePath, String afterPath, XMLFormattingPreferences prefs)
      throws UnsupportedEncodingException, IOException, CoreException {
    IStructuredModel beforeModel = null, afterModel = null;
    try {
      beforeModel = getModelForEdit(beforePath);
      assertNotNull("could not retrieve structured model for : " + beforePath, beforeModel);

      afterModel = getModelForEdit(afterPath);
      assertNotNull("could not retrieve structured model for : " + afterPath, afterModel);

      IStructuredDocument document = beforeModel.getStructuredDocument();

      String normalizedContents = document.get();
      normalizedContents = StringUtils.replace(normalizedContents, "\r\n", "\n");
      normalizedContents = StringUtils.replace(normalizedContents, "\r", "\n");
      document.set(normalizedContents);

      if (prefs == null) prefs = new XMLFormattingPreferences();
      TextEdit edit = partitionFormatter.format(beforeModel, 0, document.getLength(), prefs);
      try {
        edit.apply(document);
      } catch (MalformedTreeException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } catch (BadLocationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }

      ByteArrayOutputStream formattedBytes = new ByteArrayOutputStream();
      beforeModel.save(formattedBytes); // "beforeModel" should now be
      // after the formatter

      ByteArrayOutputStream afterBytes = new ByteArrayOutputStream();
      afterModel.save(afterBytes);

      String expectedContents = new String(afterBytes.toByteArray(), UTF_8);
      String actualContents = new String(formattedBytes.toByteArray(), UTF_8);

      /* Make some adjustments to ignore cross platform line delimiter issues */
      expectedContents = StringUtils.replace(expectedContents, "\r\n", "\n");
      expectedContents = StringUtils.replace(expectedContents, "\r", "\n");
      actualContents = StringUtils.replace(actualContents, "\r\n", "\n");
      actualContents = StringUtils.replace(actualContents, "\r", "\n");

      assertTrue(
          "Formatted document differs from the expected.\nExpected Contents:\n"
              + expectedContents
              + "\nActual Contents:\n"
              + actualContents,
          fStringCompareUtil.equalsIgnoreLineSeperator(expectedContents, actualContents));
    } finally {
      if (beforeModel != null) beforeModel.releaseFromEdit();
      if (afterModel != null) afterModel.releaseFromEdit();
    }
  }
Beispiel #3
0
  /*
   * (non-Javadoc)
   *
   * @seeorg.eclipse.ui.navigator.ILinkHelper#activateEditor(org.eclipse.ui.
   * IWorkbenchPage, org.eclipse.jface.viewers.IStructuredSelection)
   */
  @Override
  public void activateEditor(IWorkbenchPage aPage, IStructuredSelection aSelection) {

    // System.out.println("activateEditor: "+isDragActive);

    if (aSelection == null || aSelection.isEmpty()) {
      return;
    }

    Object obj = aSelection.getFirstElement();
    if (obj instanceof IOfsModelResource) {
      IOfsModelResource ofsResource = (IOfsModelResource) obj;
      URI uri = ofsResource.getURI();
      String extension = uri.fileExtension();
      if (extension != null
          && StringUtils.contains(PageConstants.PAGE_DESIGNER_FILE_EXTENSIONS, extension, true)) {
        // check if an editor is alread open for the given uri.
        String filename = uri.lastSegment();
        String id =
            PlatformUI.getWorkbench().getEditorRegistry().getDefaultEditor(filename).getId();
        for (IEditorReference editor : aPage.getEditorReferences()) {
          if (editor.getId().equals(id)) {
            try {
              IEditorInput eInput = editor.getEditorInput();
              if (eInput instanceof FileEditorInput) {
                FileEditorInput fInput = (FileEditorInput) eInput;
                if (filename.equals(fInput.getName())) {
                  if (isDragActive) {
                    // an editor is already open, simply return
                    // to avoid the activation of this editor.
                    isDragActive = false;
                    return;
                  }
                }
              }
            } catch (PartInitException ex) {
              // silently ignore
            }
          }
        }
      }
    }

    // editor not found
    super.activateEditor(aPage, aSelection);
  }
  protected void addAttributeValueProposals(
      ContentAssistRequest contentAssistRequest, CompletionProposalInvocationContext context) {
    IDOMNode node = (IDOMNode) contentAssistRequest.getNode();
    String tagName = node.getNodeName();
    IStructuredDocumentRegion open = node.getFirstStructuredDocumentRegion();
    ITextRegionList openRegions = open.getRegions();
    int i = openRegions.indexOf(contentAssistRequest.getRegion());
    if (i < 0) return;
    ITextRegion nameRegion = null;
    while (i >= 0) {
      nameRegion = openRegions.get(i--);
      if (nameRegion.getType() == DOMRegionContext.XML_TAG_ATTRIBUTE_NAME) break;
    }

    // get the attribute in question (first attr name to the left of the cursor)
    String attributeName = null;
    if (nameRegion != null) attributeName = open.getText(nameRegion);

    ProposalType proposalType = resolveProposalType(tagName, attributeName);
    if (ProposalType.None.equals(proposalType)) {
      return;
    }

    String currentValue = null;
    if (contentAssistRequest.getRegion().getType() == DOMRegionContext.XML_TAG_ATTRIBUTE_VALUE)
      currentValue = contentAssistRequest.getText();
    else currentValue = "";

    String matchString = null;
    int matchStrLen = contentAssistRequest.getMatchString().length();
    int start = contentAssistRequest.getReplacementBeginPosition();
    int length = contentAssistRequest.getReplacementLength();
    if (currentValue.length() > StringUtils.strip(currentValue).length()
        && (currentValue.startsWith("\"") || currentValue.startsWith("'"))
        && matchStrLen > 0) {
      // Value is surrounded by (double) quotes.
      matchString = currentValue.substring(1, matchStrLen);
      start++;
      length = currentValue.length() - 2;
      currentValue = currentValue.substring(1, length + 1);
    } else {
      matchString = currentValue.substring(0, matchStrLen);
    }

    IJavaProject project = getJavaProject(contentAssistRequest);
    try {
      switch (proposalType) {
        case Package:
          proposePackage(contentAssistRequest, project, matchString, start, length);
          break;
        case TypeAlias:
          addProposals(
              contentAssistRequest,
              ProposalComputorHelper.proposeJavaType(project, start, length, false, matchString));
          break;
        case ResultType:
          addProposals(
              contentAssistRequest,
              ProposalComputorHelper.proposeJavaType(project, start, length, true, matchString));
          break;
        case ResultProperty:
          proposeProperty(contentAssistRequest, matchString, start, length, node);
          break;
        case TypeHandlerType:
          addProposals(
              contentAssistRequest,
              ProposalComputorHelper.proposeTypeHandler(project, start, length, matchString));
          break;
        case CacheType:
          addProposals(
              contentAssistRequest,
              ProposalComputorHelper.proposeCacheType(project, start, length, matchString));
          break;
        case SettingName:
          addProposals(
              contentAssistRequest,
              ProposalComputorHelper.proposeSettingName(start, length, matchString));
          break;
        case ObjectFactory:
          addProposals(
              contentAssistRequest,
              ProposalComputorHelper.proposeObjectFactory(project, start, length, matchString));
          break;
        case ObjectWrapperFactory:
          addProposals(
              contentAssistRequest,
              ProposalComputorHelper.proposeObjectWrapperFactory(
                  project, start, length, matchString));
          break;
        case StatementId:
          proposeStatementId(contentAssistRequest, project, matchString, start, length, node);
          break;
        case MapperNamespace:
          proposeMapperNamespace(contentAssistRequest, project, start, length);
          break;
        case ResultMap:
          String ownId =
              "resultMap".equals(tagName) && "extends".equals(attributeName)
                  ? XpathUtil.xpathString(node, "@id")
                  : null;
          addProposals(
              contentAssistRequest,
              proposeResultMapReference(
                  project,
                  node.getOwnerDocument(),
                  start,
                  currentValue,
                  matchString.length(),
                  ownId));
          break;
        case Include:
          addProposals(
              contentAssistRequest,
              ProposalComputorHelper.proposeReference(
                  project, node.getOwnerDocument(), matchString, start, length, "sql", null));
          break;
        case SelectId:
          // TODO: include mapper methods with @Select.
          addProposals(
              contentAssistRequest,
              ProposalComputorHelper.proposeReference(
                  project, node.getOwnerDocument(), matchString, start, length, "select", null));
          break;
        case KeyProperty:
          String nodeName = node.getNodeName();
          Node statementNode =
              "update".equals(nodeName) || "insert".equals(nodeName)
                  ? node
                  : MybatipseXmlUtil.findEnclosingStatementNode(node.getParentNode());
          addProposals(
              contentAssistRequest,
              proposeParameter(project, start, length, statementNode, false, matchString));
          break;
        case ParamProperty:
          addProposals(
              contentAssistRequest,
              proposeParameter(
                  project,
                  start,
                  length,
                  MybatipseXmlUtil.findEnclosingStatementNode(node),
                  true,
                  matchString));
          break;
        case ParamPropertyPartial:
          AttrTextParser parser = new AttrTextParser(currentValue, matchString.length());
          addProposals(
              contentAssistRequest,
              proposeParameter(
                  project,
                  start + parser.getMatchStringStart(),
                  parser.getReplacementLength(),
                  MybatipseXmlUtil.findEnclosingStatementNode(node.getParentNode()),
                  true,
                  parser.getMatchString()));
          break;
        default:
          break;
      }
    } catch (Exception e) {
      Activator.log(Status.ERROR, e.getMessage(), e);
    }
  }
 public String getAdditionalProposalInfo() {
   String additionalInfo = super.getAdditionalProposalInfo();
   return StringUtils.convertToHTMLContent(additionalInfo);
 }