diff --git a/src/main/java/com/power/doc/builder/DocBuilderTemplate.java b/src/main/java/com/power/doc/builder/DocBuilderTemplate.java index 7e0059c..cd1f205 100644 --- a/src/main/java/com/power/doc/builder/DocBuilderTemplate.java +++ b/src/main/java/com/power/doc/builder/DocBuilderTemplate.java @@ -10,6 +10,7 @@ import com.power.doc.constants.TemplateVariable; import com.power.doc.model.*; import com.power.doc.utils.BeetlTemplateUtil; import com.power.doc.utils.DocUtil; +import com.power.doc.utils.EnumUtil; import org.apache.commons.codec.digest.DigestUtils; import org.beetl.core.Template; @@ -203,32 +204,15 @@ public class DocBuilderTemplate { } clzz = Class.forName(apiDataDictionary.getEnumClassName()); } + List enumDictionaryList = EnumUtil.getEnumValues(clzz,apiDataDictionary.getCodeField(), + apiDataDictionary.getDescField()); if (!clzz.isEnum()) { throw new RuntimeException(clzz.getCanonicalName() + " is not an enum class."); } - List dataDictList = new ArrayList<>(); - Object[] objects = clzz.getEnumConstants(); - String valueMethodName = "get" + StringUtil.firstToUpperCase(apiDataDictionary.getCodeField()); - String descMethodName = "get" + StringUtil.firstToUpperCase(apiDataDictionary.getDescField()); - Method valueMethod = clzz.getMethod(valueMethodName); - Method descMethod = clzz.getMethod(descMethodName); - for (Object object : objects) { - Object val = valueMethod.invoke(object); - Object desc = descMethod.invoke(object); - DataDict dataDict = new DataDict(); - if (val instanceof String) { - dataDict.setType("string"); - } else { - dataDict.setType("int32"); - } - dataDict.setDesc(String.valueOf(desc)); - dataDict.setValue(String.valueOf(val)); - dataDictList.add(dataDict); - } - apiDocDict.setDataDictList(dataDictList); + apiDocDict.setDataDictList(enumDictionaryList); apiDocDictList.add(apiDocDict); } - } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException | ClassNotFoundException e) { + } catch (ClassNotFoundException e) { e.printStackTrace(); } return apiDocDictList; @@ -253,24 +237,11 @@ public class DocBuilderTemplate { } clzz = Class.forName(dictionary.getEnumClassName()); } - if (!clzz.isEnum()) { - throw new RuntimeException(clzz.getCanonicalName() + " is not an enum class."); - } - Object[] objects = clzz.getEnumConstants(); - String valueMethodName = "get" + StringUtil.firstToUpperCase(dictionary.getCodeField()); - String descMethodName = "get" + StringUtil.firstToUpperCase(dictionary.getDescField()); - Method valueMethod = clzz.getMethod(valueMethodName); - Method descMethod = clzz.getMethod(descMethodName); - for (Object object : objects) { - Object val = valueMethod.invoke(object); - Object desc = descMethod.invoke(object); - ApiErrorCode errorCode = new ApiErrorCode(); - errorCode.setDesc(String.valueOf(desc)); - errorCode.setValue(String.valueOf(val)); - errorCodeList.add(errorCode); - } + List enumDictionaryList = EnumUtil.getEnumValues(clzz,dictionary.getCodeField(), + dictionary.getDescField()); + errorCodeList.addAll(enumDictionaryList); } - } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException | ClassNotFoundException e) { + } catch ( ClassNotFoundException e) { e.printStackTrace(); } return errorCodeList; diff --git a/src/main/java/com/power/doc/builder/SourceBuilder.java b/src/main/java/com/power/doc/builder/SourceBuilder.java index a514c9c..5b9c7de 100644 --- a/src/main/java/com/power/doc/builder/SourceBuilder.java +++ b/src/main/java/com/power/doc/builder/SourceBuilder.java @@ -543,8 +543,7 @@ public class SourceBuilder { comment = field.getComment(); } if (StringUtil.isNotEmpty(comment)) { - comment = comment.replaceAll("\r\n", "
"); - comment = comment.replaceAll("\n", "
"); + comment = DocUtil.replaceNewLineToHtmlBr(comment); } if (DocClassUtil.isPrimitive(subTypeName)) { ApiParam param = ApiParam.of().setField(pre + fieldName); @@ -560,8 +559,7 @@ public class SourceBuilder { JavaClass javaClass = builder.getClassByName(subTypeName); String enumComments = javaClass.getComment(); if (StringUtil.isNotEmpty(enumComments) && javaClass.isEnum()) { - enumComments = enumComments.replaceAll("\r\n", "
"); - enumComments = enumComments.replaceAll("\n", "
"); + enumComments = DocUtil.replaceNewLineToHtmlBr(enumComments); comment = comment + "(See: " + enumComments + ")"; } String processedType = DocClassUtil.processTypeNameForParams(typeSimpleName.toLowerCase()); diff --git a/src/main/java/com/power/doc/model/ApiErrorCode.java b/src/main/java/com/power/doc/model/ApiErrorCode.java index 7184844..57a9bda 100644 --- a/src/main/java/com/power/doc/model/ApiErrorCode.java +++ b/src/main/java/com/power/doc/model/ApiErrorCode.java @@ -6,48 +6,5 @@ package com.power.doc.model; * * @author yu 2018/06/25. */ -public class ApiErrorCode { - - /** - * error code - */ - private String value; - - /** - * error code type - */ - private String type; - - /** - * error description - */ - private String desc; - - - public String getValue() { - return value; - } - - public ApiErrorCode setValue(String value) { - this.value = value; - return this; - } - - public String getType() { - return type; - } - - public ApiErrorCode setType(String type) { - this.type = type; - return this; - } - - public String getDesc() { - return desc; - } - - public ApiErrorCode setDesc(String desc) { - this.desc = desc; - return this; - } +public class ApiErrorCode extends EnumDictionary { } diff --git a/src/main/java/com/power/doc/model/ApiErrorCodeDictionary.java b/src/main/java/com/power/doc/model/ApiErrorCodeDictionary.java index 6a29d7c..3c4bb62 100644 --- a/src/main/java/com/power/doc/model/ApiErrorCodeDictionary.java +++ b/src/main/java/com/power/doc/model/ApiErrorCodeDictionary.java @@ -1,6 +1,7 @@ package com.power.doc.model; /** + * @since 1.7.9 * @author yu 2019/12/7. */ public class ApiErrorCodeDictionary { diff --git a/src/main/java/com/power/doc/model/DataDict.java b/src/main/java/com/power/doc/model/DataDict.java index cdb2eab..fa7dfbb 100644 --- a/src/main/java/com/power/doc/model/DataDict.java +++ b/src/main/java/com/power/doc/model/DataDict.java @@ -1,24 +1,8 @@ package com.power.doc.model; -import lombok.Data; - /** * @author yu 2019/10/31. */ -@Data -public class DataDict { +public class DataDict extends EnumDictionary { - /** - * dict value - */ - private String value; - - /** - * code type - */ - private String Type; - /** - * dict desc - */ - private String desc; } diff --git a/src/main/java/com/power/doc/model/EnumDictionary.java b/src/main/java/com/power/doc/model/EnumDictionary.java new file mode 100644 index 0000000..5adc769 --- /dev/null +++ b/src/main/java/com/power/doc/model/EnumDictionary.java @@ -0,0 +1,50 @@ +package com.power.doc.model; + +/** + * @since 1.7.9 + * @author yu 2019/12/7. + */ +public class EnumDictionary { + + /** + * dict value + */ + private String value; + + /** + * code type + */ + private String type; + /** + * dict desc + */ + private String desc; + + + public String getValue() { + return value; + } + + public EnumDictionary setValue(String value) { + this.value = value; + return this; + } + + public String getType() { + return type; + } + + public EnumDictionary setType(String type) { + this.type = type; + return this; + } + + public String getDesc() { + return desc; + } + + public EnumDictionary setDesc(String desc) { + this.desc = desc; + return this; + } +} diff --git a/src/main/java/com/power/doc/utils/DocUtil.java b/src/main/java/com/power/doc/utils/DocUtil.java index 0d78496..c44073e 100644 --- a/src/main/java/com/power/doc/utils/DocUtil.java +++ b/src/main/java/com/power/doc/utils/DocUtil.java @@ -322,8 +322,7 @@ public class DocUtil { } else { value = entry.getKey() + entry.getValue(); } - value = value.replace("\r\n", "
"); - value = value.replace("\r", "
"); + value = replaceNewLineToHtmlBr(value); } } return value; @@ -343,4 +342,11 @@ public class DocUtil { return valueId.substring(length - 32, length); } } + + public static String replaceNewLineToHtmlBr(String content){ + if(StringUtil.isNotEmpty(content)){ + return content.replaceAll("(\r\n|\r|\n|\n\r)", "
"); + } + return null; + } } diff --git a/src/main/java/com/power/doc/utils/EnumUtil.java b/src/main/java/com/power/doc/utils/EnumUtil.java new file mode 100644 index 0000000..7b02180 --- /dev/null +++ b/src/main/java/com/power/doc/utils/EnumUtil.java @@ -0,0 +1,53 @@ +package com.power.doc.utils; + +import com.power.common.util.StringUtil; +import com.power.doc.model.EnumDictionary; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.List; + +/** + * @author yu 2019/12/7. + */ +public class EnumUtil { + + /** + * get enum values + * + * @param clzz class + * @param codeField code field + * @param descField desc field + * @return + */ + public static List getEnumValues(Class clzz, String codeField, String descField) { + if (!clzz.isEnum()) { + throw new RuntimeException(clzz.getCanonicalName() + " is not an enum class."); + } + Object[] objects = clzz.getEnumConstants(); + String valueMethodName = "get" + StringUtil.firstToUpperCase(codeField); + String descMethodName = "get" + StringUtil.firstToUpperCase(descField); + List enumDictionaryList = new ArrayList<>(); + try { + Method valueMethod = clzz.getMethod(valueMethodName); + Method descMethod = clzz.getMethod(descMethodName); + for (Object object : objects) { + Object val = valueMethod.invoke(object); + Object desc = descMethod.invoke(object); + EnumDictionary dataDict = new EnumDictionary(); + if (val instanceof String) { + dataDict.setType("string"); + } else { + dataDict.setType("int32"); + } + dataDict.setDesc(String.valueOf(desc)); + dataDict.setValue(String.valueOf(val)); + enumDictionaryList.add((T) dataDict); + } + } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { + e.printStackTrace(); + } + return enumDictionaryList; + } +}