/**
   * Parses an annotation type definition
   *
   * @param docClass
   * @return
   */
  protected static Annotation ParseAnnotation(ClassDoc docClass) {
    AnnotationTypeDoc docAnnotation = (AnnotationTypeDoc) docClass;

    assert (docAnnotation != null);

    Annotation xmlAnnotation = new Annotation();

    xmlAnnotation.name = docClass.name();
    xmlAnnotation.qualifiedName = docClass.qualifiedName();
    xmlAnnotation.comment = docClass.commentText();
    xmlAnnotation.isIncluded = docClass.isIncluded();
    xmlAnnotation.scope = DetermineScope(docClass);

    AnnotationTypeElementDoc[] elements = docAnnotation.elements();

    if (elements != null && elements.length > 0) {
      ArrayList<AnnotationElement> elementList = new ArrayList<AnnotationElement>();

      for (AnnotationTypeElementDoc element : elements) {
        elementList.add(ParseAnnotationElement(element));
      }

      xmlAnnotation.elements = elementList.toArray(new AnnotationElement[] {});
    } else {
      log.debug("No elements in annotation: " + docClass.name());
    }

    xmlAnnotation.annotationInstances =
        ParseAnnotationInstances(docClass.annotations(), docClass.qualifiedName());
    return xmlAnnotation;
  }
  /**
   * Parses the enum type definition
   *
   * @param docClass
   * @return
   */
  protected static Enum ParseEnum(ClassDoc docClass) {
    assert (docClass != null);

    Enum xmlEnum = new Enum();

    xmlEnum.name = docClass.name();
    xmlEnum.qualifiedName = docClass.qualifiedName();
    xmlEnum.comment = docClass.commentText();
    xmlEnum.isIncluded = docClass.isIncluded();
    xmlEnum.scope = DetermineScope(docClass);
    Type superClassType = docClass.superclassType();
    if (superClassType != null) {
      xmlEnum.superClass = superClassType.qualifiedTypeName();
    }

    Type[] interfaces = docClass.interfaceTypes();

    ArrayList<String> interfaceTypeNames = new ArrayList<String>();
    if (interfaces != null && interfaces.length > 0) {
      for (Type interfaceType : interfaces) {
        interfaceTypeNames.add(interfaceType.qualifiedTypeName());
      }
    }

    xmlEnum.extendedFrom = interfaceTypeNames.toArray(new String[] {});

    FieldDoc[] fields = docClass.enumConstants();

    if (fields != null && fields.length > 0) {
      ArrayList<EnumField> fieldList = new ArrayList<EnumField>();

      for (FieldDoc field : fields) {
        fieldList.add(ParseEnumField(field));
      }

      xmlEnum.fields = fieldList.toArray(new EnumField[] {});
    }

    xmlEnum.annotationInstances =
        ParseAnnotationInstances(docClass.annotations(), docClass.qualifiedName());
    return xmlEnum;
  }
  /**
   * Parses an interface type definition
   *
   * @param docClass
   * @return
   */
  protected static Interface ParseInterface(ClassDoc docClass) {
    assert (docClass != null);

    Interface xmlInterface = new Interface();

    xmlInterface.name = docClass.name();
    xmlInterface.qualifiedName = docClass.qualifiedName();
    xmlInterface.comment = docClass.commentText();
    xmlInterface.isIncluded = docClass.isIncluded();
    xmlInterface.scope = DetermineScope(docClass);
    xmlInterface.typeVariables =
        ParseTypeVariables(docClass.typeParameters(), docClass.typeParamTags());

    Type[] interfaces = docClass.interfaceTypes();

    ArrayList<String> interfaceTypeNames = new ArrayList<String>();
    if (interfaces != null && interfaces.length > 0) {
      for (Type interfaceType : interfaces) {
        interfaceTypeNames.add(interfaceType.qualifiedTypeName());
      }

      xmlInterface.interfaces = interfaceTypeNames.toArray(new String[] {});
    }

    MethodDoc[] methods = docClass.methods();

    if (methods != null && methods.length > 0) {
      ArrayList<Method> methodList = new ArrayList<Method>();

      for (MethodDoc method : methods) {
        methodList.add(ParseMethod(method));
      }

      xmlInterface.methods = methodList.toArray(new Method[] {});
    } else {
      log.debug("No methods in interface: " + docClass.name());
    }

    xmlInterface.annotationInstances =
        ParseAnnotationInstances(docClass.annotations(), docClass.qualifiedName());
    return xmlInterface;
  }
  private static void scanClassDoc(List<ApiDoc> apiDocs, ClassDoc classDoc) {
    AnnotationDesc[] annDescs = classDoc.annotations();
    for (AnnotationDesc annDesc : annDescs) {
      if (IgnoreDocument.class.getName().equals(annDesc.annotationType().qualifiedTypeName()))
        return;
    }

    MethodDoc[] methodDocs = classDoc.methods();
    if (ArrayUtils.isNotEmpty(methodDocs)) {
      for (MethodDoc methodDoc : methodDocs) {
        ApiDoc apiDoc = scanMethodDoc(classDoc, methodDoc);
        if (apiDoc != null) apiDocs.add(apiDoc);
      }
    }

    ClassDoc[] innerClassDocs = classDoc.innerClasses();
    if (ArrayUtils.isNotEmpty(innerClassDocs)) {
      for (ClassDoc innerClassDoc : innerClassDocs) scanClassDoc(apiDocs, innerClassDoc);
    }
  }
  /**
   * Parses the data for a class type definition
   *
   * @param docClass
   * @return
   */
  protected static Class ParseClass(ClassDoc docClass) {
    assert (docClass != null);

    Class xmlClass = new Class();

    // illegal use of this class.
    assert (xmlClass != null);

    xmlClass.name = docClass.name();
    xmlClass.qualifiedName = docClass.qualifiedName();
    xmlClass.isSerializable = docClass.isSerializable();
    xmlClass.isExternalizable = docClass.isExternalizable();
    xmlClass.isAbstract = docClass.isAbstract();
    xmlClass.isException = docClass.isException();
    xmlClass.isError = docClass.isError();
    xmlClass.comment = docClass.commentText();
    xmlClass.scope = DetermineScope(docClass);
    xmlClass.isIncluded = docClass.isIncluded();
    xmlClass.typeVariables =
        ParseTypeVariables(docClass.typeParameters(), docClass.typeParamTags());
    Type superClassType = docClass.superclassType();
    if (superClassType != null) {
      xmlClass.superClass = ParseType(superClassType);
    }

    Type[] interfaces = docClass.interfaceTypes();

    ArrayList<TypeInfo> interfaceTypeNames = new ArrayList<TypeInfo>();
    if (interfaces != null && interfaces.length > 0) {
      for (Type interfaceType : interfaces) {
        interfaceTypeNames.add(ParseType(interfaceType));
      }

      xmlClass.interfaces = interfaceTypeNames.toArray(new TypeInfo[] {});
    }

    ConstructorDoc[] constructors = docClass.constructors();

    if (constructors != null && constructors.length > 0) {
      ArrayList<Constructor> constructorList = new ArrayList<Constructor>();

      for (ConstructorDoc constructor : constructors) {
        constructorList.add(ParseConstructor(constructor));
      }

      xmlClass.constructors = constructorList.toArray(new Constructor[] {});
    } else {
      log.debug("No constructors in class: " + docClass.name());
    }

    MethodDoc[] methods = docClass.methods();

    if (methods != null && methods.length > 0) {
      ArrayList<Method> methodList = new ArrayList<Method>();

      for (MethodDoc method : methods) {
        methodList.add(ParseMethod(method));
      }

      xmlClass.methods = methodList.toArray(new Method[] {});
    } else {
      log.debug("No methods in class: " + docClass.name());
    }

    FieldDoc[] fields = docClass.fields();

    if (fields != null && fields.length > 0) {
      ArrayList<Field> fieldList = new ArrayList<Field>();

      for (FieldDoc field : fields) {
        fieldList.add(ParseField(field));
      }

      xmlClass.fields = fieldList.toArray(new Field[] {});
    }

    xmlClass.annotationInstances =
        ParseAnnotationInstances(docClass.annotations(), docClass.qualifiedName());
    return xmlClass;
  }
  private static ApiDoc scanMethodDoc(ClassDoc classDoc, MethodDoc methodDoc) {
    String routePrefix = "";

    // RoutePrefix for class
    for (AnnotationDesc annDesc : classDoc.annotations()) {
      if (RoutePrefix.class.getName().equals(annDesc.annotationType().qualifiedTypeName())) {
        routePrefix = annotationToString(annDesc, "value", "");
        break;
      }
    }

    AnnotationDesc[] annDescs = methodDoc.annotations();
    if (ArrayUtils.isEmpty(annDescs)) return null;

    for (AnnotationDesc annDesc : annDescs) {
      if (IgnoreDocument.class.getName().equals(annDesc.annotationType().qualifiedTypeName()))
        return null;
    }

    // RoutePrefix for method
    for (AnnotationDesc annDesc : annDescs) {
      if (RoutePrefix.class.getName().equals(annDesc.annotationType().qualifiedTypeName())) {
        routePrefix = annotationToString(annDesc, "value", "");
        break;
      }
    }

    Class httpExamplePackageClass = null;
    String[] routes = null;
    String[] httpMethods = {"GET", "POST"};
    boolean deprecated = false;
    for (AnnotationDesc annDesc : annDescs) {
      if (Route.class.getName().equals(annDesc.annotationType().qualifiedTypeName())) {
        for (AnnotationDesc.ElementValuePair annValue : annDesc.elementValues()) {
          String name = annValue.element().name();
          if ("url".equals(name)) {
            routes = annotationToStringArray(annValue.value().value());
          } else if ("method".equals(name)) {
            httpMethods = annotationToStringArray(annValue.value().value());
          }
        }
      }

      if (Deprecated.class.getName().equals(annDesc.annotationType().qualifiedTypeName()))
        deprecated = true;

      if (HttpExamplePackage.class.getName().equals(annDesc.annotationType().qualifiedTypeName())) {
        for (AnnotationDesc.ElementValuePair annValue : annDesc.elementValues()) {
          String name = annValue.element().name();
          if ("value".equals(name)) {
            ClassDoc httpExamplePackageClassAnn = (ClassDoc) annValue.value().value();
            httpExamplePackageClass =
                ClassHelper.forName(httpExamplePackageClassAnn.qualifiedTypeName());
          }
        }
      }
    }
    if (routes == null) return null;

    if (httpExamplePackageClass == null) {
      annDescs = classDoc.annotations();
      for (AnnotationDesc annDesc : annDescs) {
        if (HttpExamplePackage.class
            .getName()
            .equals(annDesc.annotationType().qualifiedTypeName())) {
          for (AnnotationDesc.ElementValuePair annValue : annDesc.elementValues()) {
            String name = annValue.element().name();
            if ("value".equals(name)) {
              ClassDoc httpExamplePackageClassAnn = (ClassDoc) annValue.value().value();
              httpExamplePackageClass =
                  ClassHelper.forName(httpExamplePackageClassAnn.qualifiedTypeName());
            }
          }
        }
      }
    }

    ApiDoc apiDoc = new ApiDoc();
    apiDoc.group = getTag(methodDoc, GROUP_TAG, "");
    apiDoc.routes = addRoutePrefix(routes, routePrefix);
    apiDoc.httpMethods = httpMethods;
    apiDoc.description = expandText(methodDoc.commentText());
    apiDoc.login = parseBoolean(getTag(methodDoc, LOGIN_TAG, "y"));
    apiDoc.deprecated = deprecated;
    apiDoc.httpReturn = getTag(methodDoc, HTTP_RETURN, "");

    apiDoc.remark = expandText(getTag(methodDoc, REMARK_TAG, ""));
    apiDoc.className = classDoc.qualifiedTypeName();
    apiDoc.httpExamplePackageClass = httpExamplePackageClass;
    String[] httpExamples = expandTexts(getTags(methodDoc, HTTP_EXAMPLE_TAG));
    for (int i = 0; i < httpExamples.length; i++) {
      String httpExample = httpExamples[i];
      if (httpExample.startsWith("@")) {
        if (apiDoc.httpExamplePackageClass != null) {
          httpExamples[i] =
              VfsHelper.loadTextInClasspath(
                  apiDoc.httpExamplePackageClass, StringUtils.removeStart(httpExample, "@"));
        } else {
          httpExamples[i] = "";
        }
      }
    }

    apiDoc.httpExamples = httpExamples;

    ArrayList<ParamDoc> l = new ArrayList<ParamDoc>();
    for (String httpParamTag : getTags(methodDoc, HTTP_PARAM_TAG)) {
      ParamDoc httpParamDoc = parseParamDoc(httpParamTag);
      if (httpParamDoc != null) l.add(httpParamDoc);
    }
    apiDoc.httpParams = l.toArray(new ParamDoc[l.size()]);
    return apiDoc;
  }