Skip to content

Commit

Permalink
fix #355,#351
Browse files Browse the repository at this point in the history
  • Loading branch information
lixingzhi committed Sep 25, 2022
1 parent 2ddafc1 commit 9e3cc7c
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 2 deletions.
28 changes: 28 additions & 0 deletions src/main/java/com/power/doc/model/SystemPlaceholders.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.power.doc.model;

/**
* @author xingzi
* Date 2022/9/25 14:52
*/
public class SystemPlaceholders {
private SystemPlaceholders(){

}
public static final String PLACEHOLDER_PREFIX = "${";

/** Suffix for system property placeholders: "}". */
public static final String PLACEHOLDER_SUFFIX = "}";

/** Value separator for system property placeholders: ":". */
public static final String VALUE_SEPARATOR = ":";

public static final String SIMPLE_PREFIX = "{";

public static boolean hasSystemProperties(String url){
return url.startsWith(PLACEHOLDER_PREFIX) && url.endsWith(PLACEHOLDER_SUFFIX) && url.contains(VALUE_SEPARATOR);
}

public static String replaceSystemProperties(String url){
return null;
}
}
102 changes: 100 additions & 2 deletions src/main/java/com/power/doc/utils/DocUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@

import static com.power.doc.constants.DocGlobalConstants.JSON_CONTENT_TYPE;
import static com.power.doc.constants.DocGlobalConstants.NO_COMMENTS_FOUND;
import static com.power.doc.model.SystemPlaceholders.*;

/**
* Description:
Expand Down Expand Up @@ -232,14 +233,17 @@ public static boolean isMatch(String packageFilters, String controllerName) {
* @return formatted string
*/
public static String formatAndRemove(String str, Map<String, String> values) {
// /detail/{id:[a-zA-Z0-9]{3}}/{name:[a-zA-Z0-9]{3}}
if(SystemPlaceholders.hasSystemProperties(str)){
str = DocUtil.delPropertiesUrl(str,new HashSet<>());
}
if (str.contains(":")) {
String[] strArr = str.split("/");
for (int i = 0; i < strArr.length; i++) {
String pathParam = strArr[i];
if (pathParam.contains(":")) {
int length = pathParam.length();
String reg = pathParam.substring(pathParam.indexOf(":") + 1, length - 1);

Generex generex = new Generex(reg);
// Generate random String
String randomStr = generex.random();
Expand Down Expand Up @@ -269,7 +273,6 @@ public static String formatAndRemove(String str, Map<String, String> values) {
}
return builder.toString();
}

/**
* // /detail/{id:[a-zA-Z0-9]{3}}/{name:[a-zA-Z0-9]{3}}
* remove pattern
Expand All @@ -278,6 +281,9 @@ public static String formatAndRemove(String str, Map<String, String> values) {
* @return String
*/
public static String formatPathUrl(String str) {
if(SystemPlaceholders.hasSystemProperties(str)){
str = DocUtil.delPropertiesUrl(str,new HashSet<>());
}
if (!str.contains(":")) {
return str;
}
Expand Down Expand Up @@ -914,4 +920,96 @@ public static String paramCommentResolve(String comment) {
}
return comment;
}

/**
* del ${server.port:/error}
* @param value url
* @param visitedPlaceholders cycle
* @return
*/
public static String delPropertiesUrl(String value,Set<String> visitedPlaceholders) {
int startIndex = value.indexOf(PLACEHOLDER_PREFIX);
if (startIndex == -1) {
return value;
}
StringBuilder result = new StringBuilder(value);
while (startIndex != -1) {
int endIndex = findPlaceholderEndIndex(result, startIndex);
if (endIndex != -1) {
String placeholder = result.substring(startIndex + PLACEHOLDER_PREFIX.length(), endIndex);
String originalPlaceholder = placeholder;
if (visitedPlaceholders == null) {
visitedPlaceholders = new HashSet<>(4);
}
if (!visitedPlaceholders.add(originalPlaceholder)) {
throw new IllegalArgumentException(
"Circular placeholder reference '" + originalPlaceholder + "' in property definitions");
}
// Recursive invocation, parsing placeholders contained in the placeholder key.
placeholder = delPropertiesUrl(placeholder, visitedPlaceholders);
String propVal = SystemPlaceholders.replaceSystemProperties(placeholder);
if (propVal == null) {
int separatorIndex = placeholder.indexOf(":");
if (separatorIndex != -1) {
String actualPlaceholder = placeholder.substring(0, separatorIndex);
String defaultValue = placeholder.substring(separatorIndex + ":".length());
propVal = SystemPlaceholders.replaceSystemProperties(actualPlaceholder);
if (propVal == null) {
propVal = defaultValue;
}
}
}
if (propVal != null) {
propVal = delPropertiesUrl(propVal, visitedPlaceholders);
result.replace(startIndex, endIndex + PLACEHOLDER_PREFIX.length(), propVal);
startIndex = result.indexOf(PLACEHOLDER_PREFIX, startIndex + propVal.length());
} else{
// Proceed with unprocessed value.
startIndex = result.indexOf(PLACEHOLDER_PREFIX, endIndex + PLACEHOLDER_PREFIX.length());
}

visitedPlaceholders.remove(originalPlaceholder);
}
else {
startIndex = -1;
}
}
return result.toString();
}

private static int findPlaceholderEndIndex(CharSequence buf, int startIndex) {
int index = startIndex +PLACEHOLDER_PREFIX.length();
int withinNestedPlaceholder = 0;
while (index < buf.length()) {
if (substringMatch(buf, index, PLACEHOLDER_SUFFIX)) {
if (withinNestedPlaceholder > 0) {
withinNestedPlaceholder--;
index = index + "}".length();
}
else {
return index;
}
}
else if (substringMatch(buf, index, SIMPLE_PREFIX)) {
withinNestedPlaceholder++;
index = index + SIMPLE_PREFIX.length();
}
else {
index++;
}
}
return -1;
}

public static boolean substringMatch(CharSequence str, int index, CharSequence substring) {
if (index + substring.length() > str.length()) {
return false;
}
for (int i = 0; i < substring.length(); i++) {
if (str.charAt(index + i) != substring.charAt(i)) {
return false;
}
}
return true;
}
}

0 comments on commit 9e3cc7c

Please sign in to comment.