Skip to content

Commit

Permalink
Merge pull request #2 from SPIRIT-21/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
Daniel Simon authored Jun 21, 2017
2 parents 1dda19d + 4f36bb1 commit 63f8ed6
Show file tree
Hide file tree
Showing 17 changed files with 148 additions and 110 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
target/*
*.classpath
*.project
.settings/
/target/
54 changes: 11 additions & 43 deletions src/main/java/com/spirit21/swagger/converter/Regex.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,49 +6,17 @@
*
*/
public class Regex {
private String javadocRegex = "\\/\\*\\*((?!\\*\\/).)*\\*\\/";
private String annotationRegex = "@[a-zA-Z]+(\\([^@)]*\\))?";
private String methodRegex = "(public|protected|private|static|\\s) +[\\w\\<\\>\\[\\]]+\\s+(\\w+) *\\([^{]*\\{";
private String importRegex = "import [^;]*;";
private String apiJavadocRegex = "\\/\\*\\*(?=.*@apiTitle).+\\*\\/";
private String classRegex = "public class \\w+";
private String pathRegex = "@path [^\\n ]*";
private String descriptionRegex = "([^@{]|\\{@|\\{)*";
private String httpMethodRegex = "(@GET|@POST|@PUT|@DELETE|@PUT)";

public String getHttpMethodRegex() {
return httpMethodRegex;
}
public final static String PACKAGE = "package [^;]*;";
public final static String JAVADOC = "\\/\\*\\*((?!\\*\\/).)*\\*\\/";
public final static String ANNOTATION = "@[a-zA-Z]+(\\([^@)]*\\))?";
public final static String METHOD = "(public|protected|private|static|\\s) +[\\w\\<\\>\\[\\]]+\\s+(\\w+) *\\([^{]*\\{";
public final static String IMPORT = "import [^;]*;";
public final static String API_JAVADOC = "\\/\\*\\*(?=.*@apiTitle).+\\*\\/";
public final static String CLASS = "public class \\w+";
public final static String PATH = "@path [^\\n ]*";
public final static String DESCRIPTION = "([^@{]|\\{@|\\{)*";
public final static String HTTP_METHOD = "(@GET|@POST|@PUT|@DELETE|@PUT)";
public final static String IGNORE_JAVAFILE = "@swagger:ignore_javafile";

public String getJavadocRegex() {
return javadocRegex;
}

public String getAnnotationRegex() {
return annotationRegex;
}

public String getMethodRegex() {
return methodRegex;
}

public String getImportRegex() {
return importRegex;
}

public String getApiJavadocRegex() {
return apiJavadocRegex;
}

public String getClassRegex() {
return classRegex;
}

public String getPathRegex() {
return pathRegex;
}

public String getDescriptionRegex() {
return descriptionRegex;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@

import org.apache.maven.plugin.logging.Log;

import com.spirit21.swagger.converter.Regex;

/**
*
* @author dsimon
*
*/
public abstract class AbstractLoader {
protected Log log;
protected Regex regexes = new Regex();

public AbstractLoader(Log log) {
this.log = log;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import org.apache.maven.plugin.logging.Log;

import com.spirit21.swagger.converter.Regex;

/**
*
* @author dsimon
Expand All @@ -23,8 +25,7 @@ public ApiJavadocLoader(Log log) {
* @return javadoc
*/
public String getApiJavadocFromJavaFile(String fileString) {
String apiJavadocRegex = regexes.getApiJavadocRegex();
Pattern pattern = Pattern.compile(apiJavadocRegex);
Pattern pattern = Pattern.compile(Regex.API_JAVADOC);
Matcher matcher = pattern.matcher(fileString);
while (matcher.find()) {
return fileString.substring(matcher.start(), matcher.end());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import org.apache.maven.plugin.logging.Log;

import com.spirit21.swagger.converter.Regex;

/**
*
* @author dsimon
Expand All @@ -26,13 +28,12 @@ public ClassAnnotationLoader(Log log) {
*/
public List<String> getClassAnnotationsFromJavaFile(String fileString) {
List<String> annotations = new ArrayList<>();
String annotationRegex = regexes.getAnnotationRegex();
String reg = "(" + annotationRegex + "[\\s]*)+" + regexes.getClassRegex();
String reg = "(" + Regex.ANNOTATION + "[\\s]*)+" + Regex.CLASS;
Pattern pattern = Pattern.compile(reg);
Matcher matcher = pattern.matcher(fileString);
while (matcher.find()) {
String section = fileString.substring(matcher.start(), matcher.end());
Pattern annotationPattern = Pattern.compile(annotationRegex);
Pattern annotationPattern = Pattern.compile(Regex.ANNOTATION);
Matcher annotationMatcher = annotationPattern.matcher(section);
while (annotationMatcher.find()) {
annotations.add(section.substring(annotationMatcher.start(), annotationMatcher.end()).trim());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import org.apache.maven.plugin.logging.Log;

import com.spirit21.swagger.converter.Regex;

/**
*
* @author dsimon
Expand All @@ -24,14 +26,12 @@ public ClassJavadocLoader(Log log) {
* @return Javadoc section as String
*/
public String getClassJavadocFromJavaFile(String fileString) {
String javadocRegex = regexes.getJavadocRegex();
String regex = javadocRegex + "[\\s]*(" + regexes.getAnnotationRegex() + "[\\s]*)+[\\s]*"
+ regexes.getClassRegex();
String regex = Regex.JAVADOC + "[\\s]*(" + Regex.ANNOTATION + "[\\s]*)+[\\s]*" + Regex.CLASS;
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(fileString);
while (matcher.find()) {
String section = fileString.substring(matcher.start(), matcher.end());
Pattern javadocPattern = Pattern.compile(javadocRegex);
Pattern javadocPattern = Pattern.compile(Regex.JAVADOC);
Matcher javadocMatcher = javadocPattern.matcher(section);
while (javadocMatcher.find()) {
return section.substring(javadocMatcher.start(), javadocMatcher.end());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import org.apache.maven.plugin.logging.Log;

import com.spirit21.swagger.converter.Regex;

/**
*
* @author dsimon
Expand All @@ -26,8 +28,7 @@ public ImportLoader(Log log) {
* @return List of string
*/
public List<String> getImportsFromFile(String file) {
String regex = regexes.getImportRegex();
Pattern pattern = Pattern.compile(regex);
Pattern pattern = Pattern.compile(Regex.IMPORT);
List<String> sections = new ArrayList<>();
Matcher matcher = pattern.matcher(file);
while (matcher.find()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.project.MavenProject;

import com.spirit21.swagger.converter.Regex;
import com.spirit21.swagger.converter.models.JavaFile;
import com.spirit21.swagger.converter.models.Method;

Expand Down Expand Up @@ -101,10 +102,14 @@ private List<JavaFile> getInformationFromJavaFiles(List<Path> files) throws IOEx
List<JavaFile> javaFiles = new ArrayList<>();
for (Path file : files) {
String fileString = fileAsString(Files.readAllLines(file));

if (ignoreJavaFile(fileString)) {
continue;
}

JavaFile javaFile = new JavaFile();
String packageName = getPackageNameFromFile(fileString);
List<String> imports = importLoader.getImportsFromFile(fileString);
imports.add(packageName);
List<Method> methods = methodLoader.getMethodsFromJavaFile(fileString);
String apiJavadoc = apiJavadocLoader.getApiJavadocFromJavaFile(fileString);
List<String> classAnnotations = classAnnotationLoader.getClassAnnotationsFromJavaFile(fileString);
Expand All @@ -129,6 +134,22 @@ private List<JavaFile> getInformationFromJavaFiles(List<Path> files) throws IOEx
return javaFiles;
}

/**
* Returns true, if the given fileString contains the regex for ignoring
* javafile
*
* @param fileString
* the class as file string
* @return true, if the given fileString contains the regex for ignoring
* javafile
*/
private boolean ignoreJavaFile(String fileString) {

Pattern pattern = Pattern.compile(Regex.IGNORE_JAVAFILE);
Matcher matcher = pattern.matcher(fileString);
return matcher.find();
}

/**
* Gets the package name of a java file
*
Expand All @@ -137,7 +158,7 @@ private List<JavaFile> getInformationFromJavaFiles(List<Path> files) throws IOEx
* @return package or null
*/
private String getPackageNameFromFile(String fileString) {
Pattern pattern = Pattern.compile("package [^;]*;");
Pattern pattern = Pattern.compile(Regex.PACKAGE);
Matcher matcher = pattern.matcher(fileString);
if (matcher.find()) {
return fileString.substring(matcher.start() + 8, matcher.end() - 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import org.apache.maven.plugin.logging.Log;

import com.spirit21.swagger.converter.Regex;
import com.spirit21.swagger.converter.models.Method;

/**
Expand All @@ -30,32 +31,21 @@ public MethodLoader(Log log) {
*/
public List<Method> getMethodsFromJavaFile(String file) {
List<Method> methods = new ArrayList<>();
String javadocRegex = regexes.getJavadocRegex();
String annotationRegex = regexes.getAnnotationRegex();
String methodRegex = regexes.getMethodRegex();
String httpMethodRegex = regexes.getHttpMethodRegex();
String sectionRegex = javadocRegex + "[\\s]*" + httpMethodRegex + "([\\s]*" + annotationRegex + ")*[\\s]*"
+ methodRegex;
String sectionRegex = Regex.JAVADOC + "[\\s]*" + Regex.HTTP_METHOD + "([\\s]*" + Regex.ANNOTATION + ")*[\\s]*"
+ Regex.METHOD;
Pattern pattern = Pattern.compile(sectionRegex, Pattern.DOTALL);
Matcher matcher = pattern.matcher(file);
while (matcher.find()) {
String section = file.substring(matcher.start(), matcher.end());
String javadocSection = findJavadocSectionByRegexInSection(section, javadocRegex);
List<String> annotations = findAnnotationsByRegexInSection(section, annotationRegex);
String header = findMethodByRegexInSection(section, methodRegex);
String javadocSection = findJavadocSectionByRegexInSection(section, Regex.JAVADOC);
List<String> annotations = findAnnotationsByRegexInSection(section, Regex.ANNOTATION);
String header = findMethodByRegexInSection(section, Regex.METHOD);
String httpMethod = findHttpMethodInAnnotations(annotations);
methods.add(new Method(removeJavadocCharactersFromString(javadocSection), httpMethod, header));
}
return methods;
}

/**
* Finds HTTP methods in the annotation list
*
* @param annotations
* Annotations as string
* @return formatted annotation or null
*/
private String findHttpMethodInAnnotations(List<String> annotations) {
for (String annotation : annotations) {
switch (annotation) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
public class Resource implements SwaggerModel {

private String path;
private Parameter pathParameter;
private List<Parameter> pathParameters;
private List<Operation> operations;

public String getPath() {
Expand All @@ -29,11 +29,11 @@ public void setOperations(List<Operation> operations) {
this.operations = operations;
}

public Parameter getPathParameter() {
return pathParameter;
public List<Parameter> getPathParameters() {
return pathParameters;
}

public void setPathParameter(Parameter pathParameter) {
this.pathParameter = pathParameter;
public void setPathParameters(List<Parameter> pathParameters) {
this.pathParameters = pathParameters;
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.spirit21.swagger.converter.parsers;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.maven.plugin.logging.Log;

import com.spirit21.swagger.converter.Regex;
import com.spirit21.swagger.converter.loader.ClassLoader;
import com.spirit21.swagger.converter.models.Definition;
import com.spirit21.swagger.converter.models.Tag;
Expand All @@ -22,7 +22,6 @@ public abstract class AbstractParser {
protected Log log;
protected ClassLoader loader;
protected List<Tag> tags;
protected Regex regexes = new Regex();
protected List<Definition> definitions;

public AbstractParser(Log log, ClassLoader loader, List<Tag> tags, List<Definition> definitions) {
Expand Down Expand Up @@ -55,6 +54,35 @@ protected String findStringInSectionByRegex(String regex, int offset, String sec
return null;
}

/**
* Finds all char sequences in a string by a regular expression. Returns the
* list of char sequences with an offset.
*
* @param regex
* regular expression
* @param offset
* number of characters to cut the found string at the beginning
* @param section
* string in where to search for matches
* @return list of String
*/
protected List<String> findStringsInSectionByRegex(String regex, int offset, String section) {
Pattern pattern = Pattern.compile(regex, Pattern.DOTALL);
Matcher matcher = pattern.matcher(section);

List<String> findings = new ArrayList<String>();

while (matcher.find()) {
String ret = section.substring(matcher.start() + offset, matcher.end());
ret = ret.trim().replace("\n", "");
if (ret.isEmpty()) {
ret = null;
}
findings.add(ret);
}
return findings;
}

/**
* Removes link tags and code blocks from string
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import javax.validation.constraints.NotNull;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.maven.plugin.logging.Log;

import com.fasterxml.jackson.annotation.JsonBackReference;
Expand Down Expand Up @@ -120,7 +121,19 @@ public Definition createDefinitionByClassName(String className, List<String> imp

private Definition getDefinitionByClass(Class<?> cls, String className, Definition rootDefinition)
throws ParserException {

Field[] fields = cls.getDeclaredFields();

// if cls is enum, remove the values field
if (cls.isEnum()) {
for (int i = fields.length - 1; i >= 0; i--) {
if (!fields[i].isEnumConstant()) {
fields = ArrayUtils.removeElement(fields, fields[i]);
break;
}
}
}

Definition definition = new Definition();
definition.setClassName(className);
if (rootDefinition == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public List<Operation> findOperationsInJavaFile(JavaFile javaFile, String path,
Operation operation = new Operation();
operation.setMethod(httpMethod);
operation.setDescription(removeUnwantedCharactersFromJavadocDescription(
findStringInSectionByRegex(regex.getDescriptionRegex(), 0, section)));
findStringInSectionByRegex(Regex.DESCRIPTION, 0, section)));
operation.setSummary(findStringInSectionByRegex("@summary [^@]+", 9, section));
operation.setConsumes(findMediaTypesInAnnotations(classMethod, ResourceAcceptType.CONSUMES));
operation.setProduces(findMediaTypesInAnnotations(classMethod, ResourceAcceptType.PRODUCES));
Expand Down
Loading

0 comments on commit 63f8ed6

Please sign in to comment.