Skip to content

Commit

Permalink
3.0.0-44
Browse files Browse the repository at this point in the history
  • Loading branch information
topframe committed Sep 19, 2016
1 parent 768141a commit 9ed768a
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 69 deletions.
5 changes: 3 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.aspectran</groupId>
<artifactId>aspectran-parent</artifactId>
<version>4</version>
<version>5</version>
<relativePath />
</parent>

Expand All @@ -15,7 +15,8 @@

<name>aspectran</name>
<description>
Aspectran is a lightweight and extensible framework for building enterprise-ready Java Web and Console-based applications.
Aspectran is a lightweight Java framework for building Enterprise-ready Web applications.
Also, It will be able to launch as a Console-based and Embedded application.
</description>
<url>http://www.aspectran.com/</url>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public class ConsoleResponseAdapter extends BasicResponseAdapter {

/**
* Instantiates a new ConsoleResponseAdapter.
*
* @throws IOException if an I/O error has occurred
*/
public ConsoleResponseAdapter() throws IOException {
super(null);
Expand Down
19 changes: 10 additions & 9 deletions src/main/java/com/aspectran/core/context/expr/token/Token.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,21 @@ public class Token implements BeanReferenceInspectable {

private static final BeanReferrerType BEAN_REFERRER_TYPE = BeanReferrerType.TOKEN;

public static final char PARAMETER_SYMBOL = '$';
static final char PARAMETER_SYMBOL = '$';

public static final char ATTRIBUTE_SYMBOL = '@';
static final char ATTRIBUTE_SYMBOL = '@';

public static final char BEAN_SYMBOL = '#';
static final char BEAN_SYMBOL = '#';

public static final char PROPERTY_SYMBOL = '%';
static final char PROPERTY_SYMBOL = '%';

public static final char START_BRACKET = '{';
static final char START_BRACKET = '{';

public static final char END_BRACKET = '}';
static final char END_BRACKET = '}';

public static final char VALUE_SEPARATOR = ':';
static final char VALUE_SEPARATOR = ':';

public static final char GETTER_SEPARATOR = '^';
static final char GETTER_SEPARATOR = '^';

private final TokenType type;

Expand Down Expand Up @@ -132,7 +132,8 @@ public String getName() {

/**
* Returns the token's default value or bean's class name.
* If the token's type is Bean and token's name is "class" then token's value is class name of the Bean. Others that is default value.
* If the token's type is Bean and token's name is "class" then token's
* value is class name of the Bean. Others that is default value.
*
* @return the default value or bean's class name
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,40 @@
import com.aspectran.core.context.rule.type.TokenType;
import com.aspectran.core.util.StringUtils;

/**
* The Class TokenParser.
*/
public class TokenParser {

/**
* Returns an array of tokens that contains tokenized string.
*
* @param value the string to parse
* @return an array of tokens
*/
public static Token[] parse(String value) {
return parse(value, false);
}

public static Token[] parse(String value, boolean trimStringToken) {
if (value == null)

/**
* Returns an array of tokens that contains tokenized string.
*
* @param value the string to parse
* @param optimize whether to optimize tokens
* @return an array of tokens
*/
public static Token[] parse(String value, boolean optimize) {
if (value == null) {
return null;
}

Token[] tokens = null;
List<Token> tokenList = Tokenizer.tokenize(value, trimStringToken);
List<Token> tokenList = Tokenizer.tokenize(value, optimize);

if (!tokenList.isEmpty()) {
tokens = tokenList.toArray(new Token[tokenList.size()]);

if (trimStringToken) {
/**
* If you did not trim the string type token,
* first token string is left trim
* last token string is right trim
*/
if (optimize) {
tokens = Tokenizer.optimize(tokens);
}
}
Expand All @@ -53,8 +65,9 @@ public static Token[] parse(String value, boolean trimStringToken) {
}

public static List<Token[]> parseAsList(String value) {
if (value == null)
if (value == null) {
return null;
}

List<Token> tokenList = Tokenizer.tokenize(value, true);
List<Token[]> tokensList = null;
Expand All @@ -77,8 +90,9 @@ public static List<Token[]> parseAsList(String value) {
}

public static Map<String, Token[]> parseAsMap(String value) {
if (value == null)
if (value == null) {
return null;
}

List<Token> tokenList = Tokenizer.tokenize(value, true);
Map<String, Token[]> tokensMap = null;
Expand Down Expand Up @@ -124,16 +138,19 @@ public static Token[] makeTokens(String text, boolean tokenize) {
* @return the string
*/
public static String toString(Token[] tokens) {
if (tokens == null || tokens.length == 0)
if (tokens == null || tokens.length == 0) {
return StringUtils.EMPTY;
}

if (tokens.length == 1)
if (tokens.length == 1) {
return (tokens[0] == null ? StringUtils.EMPTY : tokens[0].stringify());
}

StringBuilder sb = new StringBuilder();
for (Token t : tokens) {
if (t != null)
if (t != null) {
sb.append(t.stringify());
}
}

return sb.toString();
Expand Down
95 changes: 52 additions & 43 deletions src/main/java/com/aspectran/core/context/expr/token/Tokenizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ public class Tokenizer {
private static final char LF = '\n';

/**
* Tokenize a string and returns a list of tokens.
* Returns a list of tokens that contains tokenized string.
*
* @param input the string to tokenize
* @param trimStringToken the trim string token
* @return the token list
* @param trimText whether to trim text
* @return a list of tokens
*/
public static List<Token> tokenize(CharSequence input, boolean trimStringToken) {
public static List<Token> tokenize(CharSequence input, boolean trimText) {
List<Token> tokens = new ArrayList<>();

int inputLen = input.length();
Expand Down Expand Up @@ -106,7 +106,7 @@ public static List<Token> tokenize(CharSequence input, boolean trimStringToken)
if (tokenNameBuffer.length() > 0 || defTextBuffer.length() > 0) {
// save previous non-token string
if (tokenStartOffset > 0) {
String defaultText = trimBuffer(stringBuffer, tokenStartOffset, trimStringToken);
String defaultText = trimBuffer(stringBuffer, tokenStartOffset, trimText);
Token token = new Token(TokenType.TEXT, defaultText);
tokens.add(token);
}
Expand All @@ -129,7 +129,6 @@ public static List<Token> tokenize(CharSequence input, boolean trimStringToken)
status = AT_STRING;
tokenNameBuffer.setLength(0);
}

tokenNameBuffer.append(c);
} else {
defTextBuffer.append(c);
Expand All @@ -140,7 +139,7 @@ public static List<Token> tokenize(CharSequence input, boolean trimStringToken)
}

if (stringBuffer.length() > 0) {
String defaultText = trimBuffer(stringBuffer, stringBuffer.length(), trimStringToken);
String defaultText = trimBuffer(stringBuffer, stringBuffer.length(), trimText);
Token token = new Token(TokenType.TEXT, defaultText);
tokens.add(token);
}
Expand Down Expand Up @@ -226,20 +225,21 @@ private static Token createToken(char symbol, StringBuilder tokenNameBuffer, Str
}

/**
* Returns a copy of the string, with leading and trailing whitespace omitted.
* Returns a copy of the string, with leading and trailing whitespaces stripped.
* <pre>
* " \r\n aaa \r\n bbb " ==&gt; "\naaa \n bbb"
* " aaa \r\n bbb \r\n " ==&gt; "aaa\nbbb\n"
* </pre>
*
* @param sb the sb
* @param end the end
* @param trim the trim
* @return the string
* @param sb the string builder object
* @param end the ending index, exclusive.
* @param trim whether to trim
* @return the trimmed string
*/
private static String trimBuffer(StringBuilder sb, int end, boolean trim) {
if (!trim)
if (!trim) {
return sb.substring(0, end);
}

int start = 0;
boolean leadingLF = false;
Expand All @@ -258,10 +258,11 @@ private static String trimBuffer(StringBuilder sb, int end, boolean trim) {
}
}

if (leadingLF && start == 0)
if (leadingLF && start == 0) {
return String.valueOf(LF);
}

// tailing whitespace
// trailing whitespace
for (int i = end - 1; i > start; i--) {
c = sb.charAt(i);

Expand All @@ -274,106 +275,114 @@ private static String trimBuffer(StringBuilder sb, int end, boolean trim) {
}

// restore a new line character which is leading whitespace
if (leadingLF)
if (leadingLF) {
sb.setCharAt(--start, LF);
}

// restore a new line character which is tailing whitespace
if (tailingLF)
if (tailingLF) {
sb.setCharAt(end++, LF);
}

return sb.substring(start, end);
}

/**
* Optimize tokens.
* Returns an array of tokens that is optimized.
*
* @param tokens the tokens
* @return the token[]
* @param tokens the tokens before optimizing
* @return the optimized tokens
*/
public static Token[] optimize(Token[] tokens) {
if (tokens == null)
if (tokens == null) {
return null;
}

String firstDefaultText = null;
String lastDefaultText = null;

if (tokens.length == 1) {
if (tokens[0].getType() == TokenType.TEXT)
if (tokens[0].getType() == TokenType.TEXT) {
firstDefaultText = tokens[0].getValue();
}
} else if (tokens.length > 1) {
if (tokens[0].getType() == TokenType.TEXT)
if (tokens[0].getType() == TokenType.TEXT) {
firstDefaultText = tokens[0].getValue();

if (tokens[tokens.length - 1].getType() == TokenType.TEXT)
}
if (tokens[tokens.length - 1].getType() == TokenType.TEXT) {
lastDefaultText = tokens[tokens.length - 1].getValue();
}
}

if (firstDefaultText != null) {
String text = trimLeadingWhitespace(firstDefaultText);

if (!Objects.equals(firstDefaultText, text))
if (!Objects.equals(firstDefaultText, text)) {
tokens[0] = new Token(TokenType.TEXT, text);
}
}

if (lastDefaultText != null && !lastDefaultText.isEmpty()) {
String text = trimTailingWhitespace(lastDefaultText);

if (!Objects.equals(lastDefaultText, text))
String text = trimTrailingWhitespace(lastDefaultText);
if (!Objects.equals(lastDefaultText, text)) {
tokens[tokens.length - 1] = new Token(TokenType.TEXT, text);
}
}

return tokens;
}

/**
* Trim leading whitespace.
* Returns a string that contains a copy of a specified string
* without leading whitespaces.
*
* @param string the string
* @return the string
* @param string the string to trim leading whitespaces
* @return a string with leading whitespaces trimmed
*/
private static String trimLeadingWhitespace(String string) {
if (string.isEmpty())
if (string.isEmpty()) {
return string;
}

int start = 0;
char c;

for (int i = 0; i < string.length(); i++) {
c = string.charAt(i);

if (!Character.isWhitespace(c)) {
start = i;
break;
}
}

if (start == 0)
if (start == 0) {
return string;
}

return string.substring(start);
}

/**
* Trim tailing whitespace.
*
* @param string the string
* @return the string
* Returns a string that contains a copy of a specified string
* without trailing whitespaces.
*
* @param string the string to trim trailing whitespaces
* @return a string with trailing whitespaces trimmed
*/
private static String trimTailingWhitespace(String string) {
private static String trimTrailingWhitespace(String string) {
int end = 0;
char c;

for (int i = string.length() - 1; i >= 0; i--) {
c = string.charAt(i);

if (!Character.isWhitespace(c)) {
end = i;
break;
}
}

if (end == 0)
if (end == 0) {
return string;
}

return string.substring(0, end + 1);
}
Expand Down

0 comments on commit 9ed768a

Please sign in to comment.