private static BundleDefinition parseBundleClause()

in dmServer/src/main/java/com/intellij/dmserver/libraries/LibraryDefinition.java [84:150]


  private static BundleDefinition parseBundleClause(Clause bundleClause) {
    HeaderValuePart bundleNamePart = PsiTreeUtil.getChildOfType(bundleClause, HeaderValuePart.class);
    if (bundleNamePart == null) {
      return null; // error
    }

    Attribute[] bundleAttributes = PsiTreeUtil.getChildrenOfType(bundleClause, Attribute.class);
    String bundleVersion = null;
    for (Attribute bundleAttribute : bundleAttributes) {
      if ("version".equals(bundleAttribute.getNameElement().getUnwrappedText())) {
        boolean hasOpeningBracket = false;
        boolean hasClosingBracket = false;
        String leftBound = null;
        String rightBound = null;

        for (ManifestToken token : PsiTreeUtil.getChildrenOfType(bundleAttribute.getValueElement(), ManifestToken.class)) {
          ManifestTokenType tokenType = token.getTokenType();
          if (tokenType == ManifestTokenType.OPENING_BRACKET_TOKEN) {
            if ("[".equals(token.getText()) && leftBound == null) {
              hasOpeningBracket = true;
            }
          }
          else if (tokenType == ManifestTokenType.CLOSING_BRACKET_TOKEN) {
            if ("]".equals(token.getText()) && rightBound != null) {
              hasClosingBracket = true;
            }
          }
          else if (tokenType == ManifestTokenType.HEADER_VALUE_PART) {
            if (leftBound == null) {
              leftBound = token.getText().trim();
              if (leftBound.startsWith("[")) {
                leftBound = leftBound.substring(1);
                hasOpeningBracket = true;
              }
            }
            else if (rightBound == null) {
              rightBound = token.getText().trim();
              if (rightBound.endsWith("]")) {
                rightBound = rightBound.substring(0, rightBound.length() - 1);
                hasClosingBracket = true;
              }
            }
            else {
              return null; // error
            }
          }
        }
        if (!hasOpeningBracket || !hasClosingBracket) {
          return null; // unsupported
        }
        if (leftBound == null || rightBound == null) {
          return null; // error
        }
        if (!leftBound.equals(rightBound)) {
          return null; // unsupported
        }
        bundleVersion = leftBound;

        break;
      }
    }
    if (bundleVersion == null) {
      return null; // error
    }

    return new BundleDefinition(bundleNamePart.getUnwrappedText(), bundleVersion);
  }