Commit 3e780e7e by 钟明宏

Update ServerDemo.java

这个方法主要是测试定界符
parent a5faf716
......@@ -5,8 +5,11 @@ import okhttp3.*;
import org.junit.Test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* 服务端集成demo
......@@ -156,4 +159,69 @@ public class ServerDemo {
String responseBody = response.body().string();
return new Headers.Builder().add("Authorization", "Bearer " + JSONObject.parseObject(responseBody).getString("access_token"));
}
@Test
public void parsePropertyTokens_test(){
System.out.println(parsePropertyTokens("Token"));
System.out.println(parsePropertyTokens("${TokenA}"));
System.out.println(parsePropertyTokens("${TokenA"));
}
String parsePropertyTokens(String s) {
final String OPEN = "${"; //$NON-NLS-1$
final String CLOSE = "}"; //$NON-NLS-1$
int currentIndex = 0;
List<String> answer = new ArrayList<>();
int markerStartIndex = s.indexOf(OPEN);
if (markerStartIndex < 0) {
// no parameter markers
answer.add(s);
currentIndex = s.length();
}
while (markerStartIndex > -1) {
if (markerStartIndex > currentIndex) {
// add the characters before the next parameter marker
answer.add(s.substring(currentIndex, markerStartIndex));
currentIndex = markerStartIndex;
}
int markerEndIndex = s.indexOf(CLOSE, currentIndex);
if (markerEndIndex < 0) {
// no closing delimiter, just move to the end of the string
answer.add(s.substring(markerStartIndex));
currentIndex = s.length();
break;
}
// we have a valid property marker...
String property = s.substring(markerStartIndex + OPEN.length(), markerEndIndex);
String propertyValue = resolveProperty(property);
if (propertyValue == null) {
// add the property marker back into the stream
answer.add(s.substring(markerStartIndex, markerEndIndex + 1));
} else {
answer.add(propertyValue);
}
currentIndex = markerEndIndex + CLOSE.length();
markerStartIndex = s.indexOf(OPEN, currentIndex);
}
if (currentIndex < s.length()) {
answer.add(s.substring(currentIndex));
}
return answer.stream().collect(Collectors.joining());
}
private String resolveProperty(String key){
System.out.println("key");
return "Hello World";
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment