/**
   * 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);
      }
    }
  }
  /**
   * doGenerateAndSave.
   *
   * @param schemaFile a {@link java.io.File} object.
   * @param parentDir a {@link java.io.File} object.
   * @param examplesDir a {@link java.io.File} object.
   * @param dummyXml a {@link java.lang.String} object.
   */
  protected void doGenerateAndSave(
      File schemaFile, File parentDir, File examplesDir, String dummyXml) {

    String jsonText = JsonUtil.convertToJSON(dummyXml, true);
    jsonText = JsonFormatter.format(jsonText);
    String fName =
        schemaFile.getName().replace(XML_FILE_EXT, ResourceVisitor.JSONSCHEMA); // $NON-NLS-1$
    fName = fName.replace(".xsd", ResourceVisitor.JSONSCHEMA);

    String generatedSchema =
        jsonText != null ? new SchemaGenerator().generateSchema(jsonText) : null;
    generatedSchema = generatedSchema != null ? JsonFormatter.format(generatedSchema) : null;
    if (generatedSchema != null) {
      spec.getCoreRaml().addGlobalSchema(fName, generatedSchema, true, false);
    }
    String name = schemaFile.getName();
    name = name.substring(0, name.lastIndexOf('.'));
    File toSave = new File(examplesDir, name + XML_FILE_EXT);
    writeString(dummyXml, toSave);
    toSave = new File(examplesDir, name + JSON_FILE_EXT);
    if (jsonText != null) {
      writeString(jsonText, toSave);
    }
    File shemas = new File(parentDir, SCHEMAS_FOLDER);
    toSave = new File(shemas, fName + JSON_FILE_EXT);
    if (generatedSchema != null) {
      writeString(generatedSchema, toSave);
    }
  }
Пример #3
0
  public String getRaml() {
    spec.optimize();
    RamlEmitterV2 emmitter = new RamlEmitterV2();
    emmitter.setSingle(false);
    final StringHolder holder = new StringHolder();
    emmitter.dump(
        new IRamlHierarchyTarget() {

          public void write(String path, String content) {}

          public void writeRoot(String content) {
            holder.content = content;
          }
        },
        spec.getCoreRaml());
    return holder.content;
  }
Пример #4
0
 protected void generateXSDForClass(Class<?> element) {
   try {
     String name = element.getSimpleName().toLowerCase();
     String fileName = name + ".xsd"; // $NON-NLS-1$
     JAXBContext jaxbContext = JAXBContext.newInstance(element);
     CustomSchemaOutputResolver sor = new CustomSchemaOutputResolver(fileName);
     jaxbContext.generateSchema(sor);
     File file = sor.getFile();
     String content = FileUtil.fileToString(file);
     generateExamle(file, content);
     spec.getCoreRaml().addGlobalSchema(name, content, false, false);
   } catch (JAXBException e) {
     e.printStackTrace();
   } catch (IOException e) {
     e.printStackTrace();
   }
 }
  /**
   * afterSchemaGen.
   *
   * @param t a {@link com.mulesoft.jaxrs.raml.annotation.model.ITypeModel} object.
   * @param st
   * @param gotXSD
   */
  protected void afterSchemaGen(ITypeModel t, StructureType st) {

    JAXBRegistry rs = new JAXBRegistry();
    JAXBType jaxbModel = rs.getJAXBModel(t);

    if (jaxbModel == null) {
      return;
    }
    ISchemaType schemaModel = null;
    try {
      schemaModel = new SchemaModelBuilder().buildSchemaModel(jaxbModel, st);
    } catch (Exception e) {
      e.printStackTrace();
    }
    if (schemaModel == null) {
      return;
    }

    try {
      if (st == StructureType.COMMON) {
        String xmlExample = new XMLModelSerializer().serialize(schemaModel);
        writeString(xmlExample, constructFileLocation(t.getName(), EXAMPLE, XML));
      }
    } catch (Exception e) {
      e.printStackTrace();
    }

    try {
      String jsonExample = new JsonModelSerializer().serialize(schemaModel);
      writeString(jsonExample, constructFileLocation(t.getName(), EXAMPLE, JSON));
    } catch (Exception e) {
      e.printStackTrace();
    }

    try {
      String jsonSchema = new JsonSchemaModelSerializer().serialize(schemaModel);
      spec.getCoreRaml()
          .addGlobalSchema(firstLetterToLowerCase(t.getName()), jsonSchema, true, true);
      writeString(jsonSchema, constructFileLocation(t.getName(), SCHEMA, JSON));
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
 /**
  * generateXSDForClass.
  *
  * @param element a {@link java.lang.Class} object.
  */
 protected String generateXSDForClass(Class<?> element) {
   try {
     String name = firstLetterToLowerCase(element.getSimpleName());
     JAXBContext jaxbContext = JAXBContext.newInstance(element);
     CustomSchemaOutputResolver sor = new CustomSchemaOutputResolver(name);
     jaxbContext.generateSchema(sor);
     File file = sor.getFile();
     if (file != null) {
       String content = FileUtil.fileToString(file);
       generateExamle(file, content);
       spec.getCoreRaml().addGlobalSchema(name + "-xml", content, false, true);
       return content;
     }
   } catch (JAXBException e) {
     e.printStackTrace();
   } catch (IOException e) {
     e.printStackTrace();
   }
   return null;
 }
Пример #7
0
 public void setPreferences(IRamlConfig preferencesConfig) {
   this.config = preferencesConfig;
   if (preferencesConfig.getTitle() != null && preferencesConfig.getTitle().length() > 0) {
     spec.getCoreRaml().setTitle(preferencesConfig.getTitle());
   }
   if (preferencesConfig.getVersion() != null && preferencesConfig.getVersion().length() > 0) {
     spec.getCoreRaml().setVersion(preferencesConfig.getVersion());
   }
   if (preferencesConfig.getBaseUrl() != null && preferencesConfig.getBaseUrl().length() > 0) {
     spec.getCoreRaml().setBaseUri(preferencesConfig.getBaseUrl());
   }
   if (preferencesConfig.getProtocols() != null) {
     ArrayList<Protocol> protocols = new ArrayList<Protocol>(preferencesConfig.getProtocols());
     Collections.sort(protocols);
     spec.getCoreRaml().setProtocols(protocols);
   }
   spec.doSort = preferencesConfig.isSorted();
   spec.extractCommonParts = preferencesConfig.doFullTree();
 }
Пример #8
0
 public void clear() {
   spec.coreRaml = new Raml2();
   spec.coreRaml.setBaseUri("http://example.com"); // $NON-NLS-1$
   spec.coreRaml.setTitle("Please type API title here"); // $NON-NLS-1$
   spec.coreRaml.setProtocols(Collections.singletonList(Protocol.HTTP));
 }
Пример #9
0
  private void visit(IMethodModel m, String path) {
    boolean hasPath = m.hasAnnotation(PATH);
    if (hasPath) {
      String localPath = m.getAnnotationValue(PATH);
      if (path.endsWith("/")) { // $NON-NLS-1$
        if (localPath.startsWith("/")) { // $NON-NLS-1$
          localPath = localPath.substring(1);
        }
      }

      path += localPath;
    }

    boolean isWs = hasPath;
    for (ActionType q : ActionType.values()) {
      boolean hasAnnotation = m.hasAnnotation(q.name());
      isWs |= hasAnnotation;
    }
    if (isWs) {
      Resource res = new Resource();
      IDocInfo documentation = m.getBasicDocInfo();
      String text = documentation.getDocumentation();
      if (!"".equals(text)) { // $NON-NLS-1$
        res.setDescription(text);
      }
      String returnName = null;
      String parameterName = null;

      ITypeModel returnedType = m.getReturnedType();

      if (returnedType != null) {
        if (returnedType.hasAnnotation(XML_ROOT_ELEMENT)) {
          generateXMLSchema(returnedType);
          returnName = returnedType.getName().toLowerCase();
        }
        if (hasPath) {
          if (consumedTypes.add(returnedType)) {
            ResourceVisitor resourceVisitor = createResourceVisitor();
            resourceVisitor.consumedTypes.addAll(this.consumedTypes);
            resourceVisitor.basePath = path;
            resourceVisitor.spec = this.spec;
            resourceVisitor.visit(returnedType);
          }
        }
      }
      ITypeModel bodyType = m.getBodyType();
      if (bodyType != null) {
        if (bodyType.hasAnnotation(XML_ROOT_ELEMENT)) {
          generateXMLSchema(bodyType);
          parameterName = bodyType.getName().toLowerCase();
        }
      }
      if (path.endsWith("/")) { // $NON-NLS-1$
        res.setRelativeUri(path.substring(0, path.length() - 1));
      } else {
        res.setRelativeUri(path);
      }
      for (ActionType q : ActionType.values()) {
        boolean hasAnnotation = m.hasAnnotation(q.name());
        if (hasAnnotation) {
          addMethod(q, res, m, documentation, returnName, parameterName);
        }
      }
      spec.addResource(res);
    }
  }