private void configureParam(IParameterModel model, AbstractParam param) {
   if (model.hasAnnotation("NotNull")) { // $NON-NLS-1$
     param.setRequired(true);
   }
   if (model.hasAnnotation("Pattern")) { // $NON-NLS-1$
     IAnnotationModel annotation = model.getAnnotation("Pattern"); // $NON-NLS-1$
     String pattern = annotation.getValue("regexp"); // $NON-NLS-1$
     param.setPattern(pattern);
   }
   if (model.hasAnnotation("Min")) { // $NON-NLS-1$
     String min = model.getAnnotationValue("Min"); // $NON-NLS-1$
     param.setMinimum(BigDecimal.valueOf(Double.parseDouble(min)));
   }
   if (model.hasAnnotation("DecimalMin")) { // $NON-NLS-1$
     String min = model.getAnnotationValue("DecimalMin"); // $NON-NLS-1$
     param.setMinimum(BigDecimal.valueOf(Double.parseDouble(min)));
   }
   if (model.hasAnnotation("Max")) { // $NON-NLS-1$
     String max = model.getAnnotationValue("Max"); // $NON-NLS-1$
     param.setMaximum(BigDecimal.valueOf(Double.parseDouble(max)));
   }
   if (model.hasAnnotation("DecimalMax")) { // $NON-NLS-1$
     String max = model.getAnnotationValue("DecimalMax"); // $NON-NLS-1$
     param.setMaximum(BigDecimal.valueOf(Double.parseDouble(max)));
   }
 }
  private String[] extractMediaTypes(
      IMethodModel m,
      String annotationName,
      String[] defaultValues,
      boolean useDefault,
      ActionType at) {

    useDefault = at != ActionType.GET;

    IAnnotationModel apiOperation1 = m.getAnnotation(API_OPERATION);
    String[] values = null;

    if (apiOperation1 != null) {
      String value = apiOperation1.getValue(annotationName.toLowerCase());
      if (value != null) {
        values = value.split(",");
        for (int i = 0; i < values.length; i++) {
          values[i] = values[i].trim();
        }
      }
    }
    if (values == null) {
      values = m.getAnnotationValues(annotationName);
    }
    if (values != null) {
      return values;
    }
    if (!useDefault) {
      return null;
    }
    if (values == null) {
      values = defaultValues;
    }
    return values;
  }
  /**
   * visit.
   *
   * @param t a {@link com.mulesoft.jaxrs.raml.annotation.model.ITypeModel} object.
   */
  public void visit(ITypeModel t) {
    consumedTypes.add(t);

    IAnnotationModel apiAnn = t.getAnnotation("Api");
    if (apiAnn != null) {

      String baseUri = apiAnn.getValue("basePath");
      if (baseUri != null && !baseUri.trim().isEmpty()) {
        spec.getCoreRaml().setBaseUri(baseUri);
      }

      String description = apiAnn.getValue("description");
      if (description != null && !description.trim().isEmpty()) {
        DocumentationItem di = new DocumentationItem();
        di.setContent(description);
        di.setTitle("description");
        spec.getCoreRaml().setDocumentation(new ArrayList<DocumentationItem>(Arrays.asList(di)));
      }

      String producesString = apiAnn.getValue(PRODUCES.toLowerCase());
      if (producesString != null && !producesString.isEmpty()) {
        classProduces = producesString.split(",");
        for (int i = 0; i < classProduces.length; i++) {
          classProduces[i] = classProduces[i].trim();
        }
      }

      String consumesString = apiAnn.getValue(CONSUMES.toLowerCase());
      if (consumesString != null && !consumesString.isEmpty()) {
        classConsumes = consumesString.split(",");
        for (int i = 0; i < classConsumes.length; i++) {
          classConsumes[i] = classConsumes[i].trim();
        }
      }
    }
    if (classConsumes == null || classConsumes.length == 0) {
      classConsumes = t.getAnnotationValues(CONSUMES);
    }
    if (classProduces == null || classProduces.length == 0) {
      classProduces = t.getAnnotationValues(PRODUCES);
    }

    String annotationValue = t.getAnnotationValue(PATH);
    if (basePath != null) {
      if (annotationValue == null) {
        annotationValue = ""; // $NON-NLS-1$
      }
      annotationValue = basePath + annotationValue;
    }
    if (annotationValue != null) {
      if (!annotationValue.endsWith("/")) { // $NON-NLS-1$
        annotationValue = annotationValue + "/"; // $NON-NLS-1$
      }
      IMethodModel[] methods = t.getMethods();
      for (IMethodModel m : methods) {
        visit(m, annotationValue);
      }
    }
  }
  private IDocInfo getDocumentation(IMethodModel m) {

    final IDocInfo basicDocInfo = m.getBasicDocInfo();

    String docString = basicDocInfo.getDocumentation();

    if (docString != null && !docString.trim().isEmpty()) {
      return basicDocInfo;
    }

    IAnnotationModel apiOperation = m.getAnnotation(API_OPERATION);
    if (apiOperation != null) {
      StringBuilder bld = new StringBuilder();

      String summary = apiOperation.getValue("value");
      if (summary != null) {
        bld.append(summary.trim());
      }

      String notes = apiOperation.getValue("notes");
      if (notes != null) {
        if (bld.length() != 0) {
          bld.append("\r\n");
        }
        bld.append(notes);
      }
      docString = bld.toString();
    }

    if (docString != null && !docString.isEmpty()) {
      final String finalDocumentation = docString;
      return new IDocInfo() {

        @Override
        public String getReturnInfo() {
          return basicDocInfo.getReturnInfo();
        }

        @Override
        public String getDocumentation(String pName) {
          return basicDocInfo.getDocumentation(pName);
        }

        @Override
        public String getDocumentation() {
          return finalDocumentation;
        }
      };
    }
    return basicDocInfo;
  }
Beispiel #5
0
  /*
   * @see ISourceViewer#getRangeIndication()
   */
  public IRegion getRangeIndication() {
    if (fRangeIndicator != null && fVisualAnnotationModel != null) {
      Position position = fVisualAnnotationModel.getPosition(fRangeIndicator);
      if (position != null) return new Region(position.getOffset(), position.getLength());
    }

    return null;
  }
Beispiel #6
0
  /**
   * Disposes the visual annotation model.
   *
   * @since 3.1
   */
  protected void disposeVisualAnnotationModel() {
    if (fVisualAnnotationModel != null) {
      if (getDocument() != null) fVisualAnnotationModel.disconnect(getDocument());

      if (fVisualAnnotationModel instanceof IAnnotationModelExtension)
        ((IAnnotationModelExtension) fVisualAnnotationModel)
            .removeAnnotationModel(MODEL_ANNOTATION_MODEL);

      fVisualAnnotationModel = null;
    }
  }
  private ActionType adjustActionType(IMethodModel m, ActionType actionType) {

    IAnnotationModel a = m.getAnnotation(API_OPERATION);
    if (a == null) {
      return actionType;
    }

    String atString = a.getValue("httpMethod");
    if (atString == null || atString.trim().isEmpty()) {
      return actionType;
    }

    ActionType adjustedActionType = null;
    try {
      adjustedActionType = Enum.valueOf(ActionType.class, atString);
    } catch (Exception e) {
      return actionType;
    }
    return adjustedActionType;
  }
Beispiel #8
0
  /*
   * @see ISourceViewer#setDocument(IDocument, IAnnotationModel, int, int)
   */
  public void setDocument(
      IDocument document,
      IAnnotationModel annotationModel,
      int modelRangeOffset,
      int modelRangeLength) {
    disposeVisualAnnotationModel();

    if (annotationModel != null && document != null) {
      fVisualAnnotationModel = createVisualAnnotationModel(annotationModel);
      fVisualAnnotationModel.connect(document);
    }

    if (modelRangeOffset == -1 && modelRangeLength == -1) super.setDocument(document);
    else super.setDocument(document, modelRangeOffset, modelRangeLength);

    if (fVerticalRuler != null) fVerticalRuler.setModel(fVisualAnnotationModel);

    if (fOverviewRuler != null) fOverviewRuler.setModel(fVisualAnnotationModel);
  }
    private void findAnnotation() {
      mPosition = null;
      mCanFix = false;

      IDocumentProvider provider = mTextEditor.getDocumentProvider();
      IAnnotationModel model = provider.getAnnotationModel(mTextEditor.getEditorInput());
      IAnnotationAccessExtension annotationAccess = getAnnotationAccessExtension();

      IDocument document = getDocument();
      if (model == null) {
        return;
      }

      Iterator<?> iter = model.getAnnotationIterator();
      int layer = Integer.MIN_VALUE;

      while (iter.hasNext()) {
        Annotation annotation = (Annotation) iter.next();
        if (annotation.isMarkedDeleted()) {
          continue;
        }

        int annotationLayer = Integer.MAX_VALUE;
        if (annotationAccess != null) {
          annotationLayer = annotationAccess.getLayer(annotation);
          if (annotationLayer < layer) {
            continue;
          }
        }

        Position position = model.getPosition(annotation);
        if (!includesRulerLine(position, document)) {
          continue;
        }

        boolean isReadOnly =
            mTextEditor instanceof ITextEditorExtension
                && ((ITextEditorExtension) mTextEditor).isEditorInputReadOnly();
        if (!isReadOnly
            && annotation instanceof INIProblemAnnotation
            && ((INIProblemAnnotation) annotation).isQuickFixable()) {
          mPosition = position;
          mCanFix = true;
          layer = annotationLayer;
          continue;
        } else {
          AnnotationPreference preference =
              mAnnotationPreferenceLookup.getAnnotationPreference(annotation);
          if (preference == null) {
            continue;
          }

          String key = preference.getVerticalRulerPreferenceKey();
          if (key == null) {
            continue;
          }

          if (mStore.getBoolean(key)) {
            mPosition = position;
            mCanFix = false;
            layer = annotationLayer;
          }
        }
      }
    }
  private void addMethod(
      ActionType action,
      Resource res,
      IMethodModel m,
      IDocInfo documentation,
      String returnName,
      String parameterName) {
    Action value = new Action();

    value.setType(action);
    res.getActions().put(action, value);
    IParameterModel[] parameters = m.getParameters();
    String[] responseCodes = new String[] {ResourceVisitor.DEFAULT_RESPONSE};
    String[] responseDescriptions = new String[] {null};
    if (config != null) {
      responseCodes = new String[] {config.getResponseCode(action)};
    }
    IAnnotationModel annotation = m.getAnnotation(ResourceVisitor.API_RESPONSE);
    if (annotation != null) {
      responseCodes = new String[] {annotation.getValue(ResourceVisitor.CODE)};
      responseDescriptions = new String[] {annotation.getValue(ResourceVisitor.MESSAGE)};
    }
    annotation = m.getAnnotation(ResourceVisitor.API_RESPONSES);
    if (annotation != null) {
      IAnnotationModel[] subAnnotations = annotation.getSubAnnotations("value");
      responseCodes = new String[subAnnotations.length];
      responseDescriptions = new String[subAnnotations.length];
      int a = 0;
      for (IAnnotationModel mq : subAnnotations) {
        responseCodes[a++] = mq.getValue(ResourceVisitor.CODE);
        responseDescriptions[a - 1] = mq.getValue(ResourceVisitor.MESSAGE);
      }
    }
    for (IParameterModel pm : parameters) {
      if (pm.hasAnnotation(QUERY_PARAM)) {
        String annotationValue = pm.getAnnotationValue(QUERY_PARAM);
        String type = pm.getType();
        QueryParameter value2 = new QueryParameter();
        configureParam(pm, value2);
        proceedType(type, value2, pm);
        String text = documentation.getDocumentation(pm.getName());
        if (!"".equals(text)) { // $NON-NLS-1$
          value2.setDescription(text);
        }
        value.getQueryParameters().put(annotationValue, value2);
      }
    }
    for (IParameterModel pm : parameters) {
      if (pm.hasAnnotation(HEADER_PARAM)) {
        String annotationValue = pm.getAnnotationValue(HEADER_PARAM);
        Header value2 = new Header();
        configureParam(pm, value2);
        proceedType(pm.getType(), value2, pm);
        String text = documentation.getDocumentation(pm.getName());
        if (!"".equals(text)) { // $NON-NLS-1$
          value2.setDescription(text);
        }
        value.getHeaders().put(annotationValue, value2);
      }
    }
    for (IParameterModel pm : parameters) {
      if (pm.hasAnnotation(PATH_PARAM)) {
        String annotationValue = pm.getAnnotationValue(PATH_PARAM);
        UriParameter value2 = new UriParameter();
        configureParam(pm, value2);
        String text = documentation.getDocumentation(pm.getName());
        if (!"".equals(text)) { // $NON-NLS-1$
          value2.setDescription(text);
        }
        proceedType(pm.getType(), value2, pm);
        res.getUriParameters().put(annotationValue, value2);
      }
    }

    String[] consumesValue = m.getAnnotationValues(CONSUMES);
    if (consumesValue == null) {
      consumesValue = classConsumes;
    }
    if (consumesValue != null) {
      for (String s : consumesValue) {
        s = sanitizeMediaType(s);
        MimeType bodyType = new MimeType();
        if (s.contains(XML)) {
          bodyType.setSchema(parameterName);
          if (parameterName != null) {
            bodyType.setExample(EXAMPLES_PREFFIX + parameterName + XML_FILE_EXT);
            bodyType.setExampleOrigin(EXAMPLES_PREFFIX + parameterName + XML_FILE_EXT);
          }
        }
        if (s.contains(JSON)) {
          if (parameterName != null) {
            bodyType.setSchema(parameterName + ResourceVisitor.JSONSCHEMA); // $NON-NLS-1$
            bodyType.setExample(EXAMPLES_PREFFIX + parameterName + JSON_FILE_EXT);
            bodyType.setExampleOrigin(EXAMPLES_PREFFIX + parameterName + JSON_FILE_EXT);
          }
        }
        bodyType.setType(s);
        if (s.contains(FORM)) {
          for (IParameterModel pm : parameters) {
            if (pm.hasAnnotation(FORM_PARAM)) {
              String annotationValue = pm.getAnnotationValue(FORM_PARAM);
              FormParameter vl = new FormParameter();
              configureParam(pm, vl);
              String text = documentation.getDocumentation(pm.getName());
              if (!"".equals(text)) { // $NON-NLS-1$
                vl.setDescription(text);
              }
              proceedType(pm.getType(), vl, pm);
              ArrayList<FormParameter> arrayList = new ArrayList<FormParameter>();
              arrayList.add(vl);
              if (bodyType.getFormParameters() == null) {
                bodyType.setFormParameters(new HashMap<String, java.util.List<FormParameter>>());
              }
              bodyType.getFormParameters().put(annotationValue, arrayList);
            }
          }
        }
        value.getBody().put(s, bodyType);
      }
    }
    String[] producesValue = m.getAnnotationValues(PRODUCES);
    if (producesValue == null) {
      producesValue = classProduces;
    }
    int a = 0;
    for (String responseCode : responseCodes) {
      if (producesValue != null) {
        Response value2 = new Response();
        String text = documentation.getReturnInfo();
        String respDesc = responseDescriptions[a];
        if (respDesc != null && respDesc.length() > 0) {
          text = respDesc;
        }
        a++;
        if (!"".equals(text)) { // $NON-NLS-1$
          value2.setDescription(text);
        }
        for (String s : producesValue) {
          s = sanitizeMediaType(s);
          MimeType mimeType = new MimeType();
          if (returnName != null) {
            if (s.contains(XML)) {
              mimeType.setSchema(returnName);
              if (returnName != null) {
                mimeType.setExample(EXAMPLES_PREFFIX + returnName + XML_FILE_EXT);
                mimeType.setExampleOrigin(EXAMPLES_PREFFIX + returnName + XML_FILE_EXT);
              }
            }
            if (s.contains(JSON)) {
              if (returnName != null) {
                mimeType.setSchema(returnName + ResourceVisitor.JSONSCHEMA); // $NON-NLS-1$
                mimeType.setExample(EXAMPLES_PREFFIX + returnName + JSON_FILE_EXT);
                mimeType.setExampleOrigin(EXAMPLES_PREFFIX + returnName + JSON_FILE_EXT);
              }
            }
          }
          mimeType.setType(s);
          value2.getBody().put(s, mimeType);
        }
        value.getResponses().put(responseCode, value2); // $NON-NLS-1$
      } else {
        Response value2 = new Response();
        String text = documentation.getReturnInfo();
        if (!"".equals(text)) { // $NON-NLS-1$
          value2.setDescription(text);
        }
        value.getResponses().put(responseCode, value2); // $NON-NLS-1$
      }
    }
  }
Beispiel #11
0
 /*
  * @see ISourceViewer#removeRangeIndication()
  */
 public void removeRangeIndication() {
   if (fRangeIndicator != null && fVisualAnnotationModel != null)
     fVisualAnnotationModel.removeAnnotation(fRangeIndicator);
 }
  private String configureParam(
      IParameterModel model,
      AbstractParam param,
      IDocInfo documentation,
      IAnnotationModel paramAnnotation) {

    String paramName = paramAnnotation.getValue("value");

    String type = model.getType();
    proceedType(type, param, model);
    String text = documentation.getDocumentation(model.getName());
    if (!"".equals(text)) { // $NON-NLS-1$
      param.setDescription(text);
    }

    if (model.hasAnnotation("NotNull")) { // $NON-NLS-1$
      param.setRequired(true);
    }
    if (model.hasAnnotation("Pattern")) { // $NON-NLS-1$
      IAnnotationModel annotation = model.getAnnotation("Pattern"); // $NON-NLS-1$
      String pattern = annotation.getValue("regexp"); // $NON-NLS-1$
      param.setPattern(pattern);
    }
    if (model.hasAnnotation("Min")) { // $NON-NLS-1$
      String min = model.getAnnotationValue("Min"); // $NON-NLS-1$
      param.setMinimum(BigDecimal.valueOf(Double.parseDouble(min)));
    }
    if (model.hasAnnotation("DecimalMin")) { // $NON-NLS-1$
      String min = model.getAnnotationValue("DecimalMin"); // $NON-NLS-1$
      param.setMinimum(BigDecimal.valueOf(Double.parseDouble(min)));
    }
    if (model.hasAnnotation("Max")) { // $NON-NLS-1$
      String max = model.getAnnotationValue("Max"); // $NON-NLS-1$
      param.setMaximum(BigDecimal.valueOf(Double.parseDouble(max)));
    }
    if (model.hasAnnotation("DecimalMax")) { // $NON-NLS-1$
      String max = model.getAnnotationValue("DecimalMax"); // $NON-NLS-1$
      param.setMaximum(BigDecimal.valueOf(Double.parseDouble(max)));
    }
    if (model.hasAnnotation("ApiParam")) {
      IAnnotationModel ann = model.getAnnotation("ApiParam");
      String allowableValues = ann.getValue("allowableValues");
      if (allowableValues != null && !allowableValues.trim().isEmpty()) {
        int start = 0;
        int end = allowableValues.length();
        if (allowableValues.startsWith("[")) {
          start++;
        }
        if (allowableValues.endsWith("]")) {
          end--;
        }
        allowableValues = allowableValues.substring(start, end);
        String[] split = allowableValues.split(",");
        ArrayList<String> list = new ArrayList<String>();
        for (String s : split) {
          list.add(s.trim());
        }
        param.setEnumeration(list);
      }
      String allowMultiple = ann.getValue("allowMultiple");
      if (allowMultiple != null) {
        boolean boolValue = Boolean.parseBoolean(allowMultiple);
        param.setRepeat(boolValue);
      }
      String defaultValue = ann.getValue("defaultValue");
      if (defaultValue != null && !defaultValue.trim().isEmpty()) {
        param.setDefaultValue(defaultValue.trim());
      }
      String required = ann.getValue("required");
      if (required != null && !required.trim().isEmpty()) {
        boolean boolValue = Boolean.parseBoolean(required);
        param.setRequired(boolValue);
      }
      String description = ann.getValue("value");
      if (description != null && !description.trim().isEmpty()) {
        param.setDescription(description);
      }
      String overridenName = ann.getValue("name");
      if (overridenName != null && !overridenName.trim().isEmpty()) {
        paramName = overridenName;
      }
    }
    return paramName;
  }
  private void processResponses(
      IMethodModel m, Action action, IDocInfo documentation, String returnName) {

    HashMap<String, ResponseModel> responses = new HashMap<String, ResponseModel>();
    String mainResponseCode = DEFAULT_RESPONSE;
    if (config != null) {
      ActionType actionType = action.getType();
      mainResponseCode = config.getResponseCode(actionType);
    }

    ResponseModel mainResponse = new ResponseModel(mainResponseCode, null, returnName);
    responses.put(mainResponseCode, mainResponse);

    IAnnotationModel apiResponse = m.getAnnotation(ResourceVisitor.API_RESPONSE);
    if (apiResponse != null) {
      String code = apiResponse.getValue(ResourceVisitor.CODE);
      String message = apiResponse.getValue(ResourceVisitor.MESSAGE);
      ResponseModel response = new ResponseModel(code, message, returnName);
      responses.put(code, response);
    }

    IAnnotationModel apiResponses = m.getAnnotation(ResourceVisitor.API_RESPONSES);
    if (apiResponses != null) {
      IAnnotationModel[] subAnnotations = apiResponses.getSubAnnotations("value");
      if (subAnnotations != null) {
        for (IAnnotationModel subAnn : subAnnotations) {
          String code = subAnn.getValue(ResourceVisitor.CODE);
          String message = subAnn.getValue(ResourceVisitor.MESSAGE);

          String adjustedReturnName = returnName;
          String responseQualifiedName = subAnn.getValue(RESPONSE);
          if (responseQualifiedName != null) {
            try {
              Class<?> responseClass = classLoader.loadClass(responseQualifiedName);
              ReflectionType rt = new ReflectionType(responseClass);
              generateXMLSchema(rt, null);
            } catch (ClassNotFoundException e) {
              e.printStackTrace();
            }
            adjustedReturnName = firstLetterToLowerCase(getSimpleName(responseQualifiedName));
          }
          ResponseModel response = responses.get(code);
          if (response == null) {
            response = new ResponseModel(code, message, adjustedReturnName);
            responses.put(code, response);
          } else {
            response.setMessage(message);
            response.setReturnTypeName(adjustedReturnName);
          }
        }
      }
    }

    String[] producesValues = new String[0];
    ITypeModel returnType = m.getReturnedType();
    if (returnType != null) {
      boolean returnsValue = !returnType.getName().toLowerCase().equals("void");
      producesValues = extractMediaTypes(m, PRODUCES, classProduces, returnsValue, null);
      if (producesValues != null) {
        for (ResponseModel responseModel : responses.values()) {
          responseModel.setProduces(producesValues);
        }
      }
    }
    IAnnotationModel apiOperation = m.getAnnotation(API_OPERATION);
    if (apiOperation != null) {
      String responseContainer = apiOperation.getValue("responseContainer");
      StructureType st = StructureType.COMMON;
      if (responseContainer != null) {
        responseContainer = responseContainer.toLowerCase();
        if (responseContainer.equals("set") || responseContainer.equals("list")) {
          st = StructureType.COLLECTION;
        } else if (responseContainer.equals("map")) {
          st = StructureType.MAP;
        }
      }

      String responseQualifiedName = apiOperation.getValue(RESPONSE);
      if (responseQualifiedName != null) {
        try {
          Class<?> responseClass = classLoader.loadClass(responseQualifiedName);
          ReflectionType rt = new ReflectionType(responseClass);
          generateXMLSchema(rt, st);
        } catch (ClassNotFoundException e) {
          e.printStackTrace();
        }
        String adjustedReturnType = firstLetterToLowerCase(getSimpleName(responseQualifiedName));
        mainResponse.setReturnTypeName(adjustedReturnType);
      }
    }

    for (ResponseModel rm : responses.values()) {

      Response response = new Response();

      String description = rm.getMessage();
      if (description == null || description.trim().isEmpty()) {
        description = documentation.getReturnInfo();
      }
      if (description != null && !description.trim().isEmpty()) {
        response.setDescription(description);
      }

      String[] produces = rm.getProduces();
      if (produces != null) {

        String returnTypeName = rm.getReturnTypeName();
        for (String mediaType : producesValues) {
          mediaType = sanitizeMediaType(mediaType);
          MimeType mimeType = new MimeType();
          tryAppendSchemesAndExamples(mimeType, mediaType, returnTypeName);
          mimeType.setType(mediaType);
          response.getBody().put(mediaType, mimeType);
        }
      }

      String code = rm.getCode();
      action.getResponses().put(code, response);
    }
  }