• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

aspectran / aspectran / #3896

18 Dec 2024 08:21PM CUT coverage: 34.945%. Remained the same
#3896

Pull #753

github

web-flow
Bump ch.qos.logback:logback-classic from 1.5.12 to 1.5.13

Bumps [ch.qos.logback:logback-classic](https://github.com/qos-ch/logback) from 1.5.12 to 1.5.13.
- [Commits](https://github.com/qos-ch/logback/commits)

---
updated-dependencies:
- dependency-name: ch.qos.logback:logback-classic
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Pull Request #753: Bump ch.qos.logback:logback-classic from 1.5.12 to 1.5.13

14070 of 40263 relevant lines covered (34.95%)

0.35 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

89.02
/core/src/main/java/com/aspectran/core/context/asel/TokenizedExpression.java
1
package com.aspectran.core.context.asel;
2

3
import com.aspectran.core.activity.Activity;
4
import com.aspectran.core.context.asel.token.Token;
5
import com.aspectran.core.context.asel.token.TokenEvaluator;
6
import com.aspectran.core.context.asel.token.TokenParser;
7
import com.aspectran.core.context.rule.type.TokenType;
8
import com.aspectran.utils.Assert;
9
import com.aspectran.utils.StringUtils;
10
import com.aspectran.utils.ToStringBuilder;
11
import com.aspectran.utils.annotation.jsr305.NonNull;
12
import com.aspectran.utils.annotation.jsr305.Nullable;
13
import ognl.Ognl;
14
import ognl.OgnlContext;
15
import ognl.OgnlException;
16

17
import java.util.Collections;
18
import java.util.HashMap;
19
import java.util.Map;
20
import java.util.Set;
21

22
/**
23
 * <p>Created: 2024. 11. 26.</p>
24
 */
25
public class TokenizedExpression implements ExpressionEvaluator {
26

27
    private static final String TOKEN_VAR_REF_SYMBOL = "#";
28

29
    private static final String TOKEN_VAR_NAME_PREFIX = "__";
30

31
    private static final String TOKEN_VAR_NAME_SUFFIX = TOKEN_VAR_NAME_PREFIX;
32

33
    private final String expression;
34

35
    private String substitutedExpression;
36

37
    private Object parsedExpression;
38

39
    private Token[] tokens;
40

41
    private Map<String, Token> tokenVars;
42

43
    private Set<String> tokenVarNames;
44

45
    public TokenizedExpression(String expression) throws ExpressionParserException {
1✔
46
        this.expression = expression;
1✔
47
        parseExpression();
1✔
48
    }
1✔
49

50
    @Override
51
    @Nullable
52
    public String getExpressionString() {
53
        return expression;
1✔
54
    }
55

56
    @Override
57
    @Nullable
58
    public String getSubstitutedExpression() {
59
        return substitutedExpression;
×
60
    }
61

62
    @Override
63
    @Nullable
64
    public Object getParsedExpression() {
65
        return parsedExpression;
1✔
66
    }
67

68
    @Override
69
    @Nullable
70
    public Token[] getTokens() {
71
        return tokens;
1✔
72
    }
73

74
    @Override
75
    @Nullable
76
    public Set<String> getTokenVarNames() {
77
        return tokenVarNames;
1✔
78
    }
79

80
    public boolean hasTokenVars() {
81
        return (tokenVarNames != null && !tokenVarNames.isEmpty());
1✔
82
    }
83

84
    private void parseExpression() throws ExpressionParserException {
85
        Token[] tokens = TokenParser.makeTokens(expression, true);
1✔
86
        Map<String, Token> tokenVars = null;
1✔
87
        String substitutedExpression = null;
1✔
88
        if (tokens != null && tokens.length > 0) {
1✔
89
            tokenVars = new HashMap<>();
1✔
90
            if (tokens.length == 1) {
1✔
91
                Token token = tokens[0];
1✔
92
                if (token.getType() == TokenType.TEXT) {
1✔
93
                    substitutedExpression = token.getDefaultValue();
1✔
94
                } else {
95
                    String tokenVarName = createTokenVarName(token);
1✔
96
                    tokenVars.putIfAbsent(tokenVarName, token);
1✔
97
                    substitutedExpression = createTokenVarRefName(tokenVarName);
1✔
98
                }
99
            } else {
1✔
100
                StringBuilder sb = new StringBuilder();
1✔
101
                for (Token token : tokens) {
1✔
102
                    if (token.getType() == TokenType.TEXT) {
1✔
103
                        sb.append(token.getDefaultValue());
1✔
104
                    } else {
105
                        String tokenVarName = createTokenVarName(token);
1✔
106
                        tokenVars.putIfAbsent(tokenVarName, token);
1✔
107
                        substitutedExpression = createTokenVarRefName(tokenVarName);
1✔
108
                        sb.append(substitutedExpression);
1✔
109
                    }
110
                }
111
                substitutedExpression = sb.toString();
1✔
112
            }
113
        }
114
        this.tokens = tokens;
1✔
115
        this.tokenVars = (tokenVars != null && !tokenVars.isEmpty() ? Collections.unmodifiableMap(tokenVars) : null);
1✔
116
        this.tokenVarNames = (tokenVars != null && !tokenVars.isEmpty() ? Collections.unmodifiableSet(tokenVars.keySet()) : null);
1✔
117
        this.substitutedExpression = substitutedExpression;
1✔
118
        if (substitutedExpression != null) {
1✔
119
            try {
120
                this.parsedExpression = Ognl.parseExpression(substitutedExpression);
1✔
121
            } catch (OgnlException e) {
×
122
                throw new ExpressionParserException(expression, e);
×
123
            }
1✔
124
        }
125
    }
1✔
126

127
    @Override
128
    public Object evaluate(Activity activity, OgnlContext ognlContext) {
129
        return evaluate(activity, ognlContext, null, null);
×
130
    }
131

132
    @Override
133
    public Object evaluate(Activity activity, OgnlContext ognlContext, Object root) {
134
        return evaluate(activity, ognlContext, root, null);
×
135
    }
136

137
    @Override
138
    public Object evaluate(Activity activity, OgnlContext ognlContext, Object root, Class<?> resultType) {
139
        Assert.notNull(activity, "activity must not be null");
1✔
140
        Assert.notNull(ognlContext, "ognlContext must not be null");
1✔
141
        if (getParsedExpression() == null) {
1✔
142
            return null;
×
143
        }
144
        try {
145
            preProcess(activity, ognlContext);
1✔
146
            Object value = Ognl.getValue(getParsedExpression(), ognlContext, root, resultType);
1✔
147
            return postProcess(activity, ognlContext, value);
1✔
148
        } catch (Exception e) {
×
149
            throw new ExpressionEvaluationException(getExpressionString(), e);
×
150
        }
151
    }
152

153
    private void preProcess(Activity activity, OgnlContext ognlContext) {
154
        if (hasTokenVars()) {
1✔
155
            TokenEvaluator tokenEvaluator = activity.getTokenEvaluator();
1✔
156
            resolveTokenVariables(ognlContext, tokenEvaluator);
1✔
157
        }
158
    }
1✔
159

160
    private Object postProcess(Activity activity, OgnlContext ognlContext, Object value) {
161
        if (getTokenVarNames() != null && value instanceof String str) {
1✔
162
            for (String tokenVarName : getTokenVarNames()) {
1✔
163
                String tokenVarRefName = createTokenVarRefName(tokenVarName);
1✔
164
                if (str.contains(tokenVarRefName)) {
1✔
165
                    Object tokenValue = ognlContext.get(tokenVarName);
1✔
166
                    String replacement;
167
                    if (tokenValue != null) {
1✔
168
                        replacement = ToStringBuilder.toString(tokenValue, activity.getStringifyContext());
1✔
169
                    } else {
170
                        replacement = StringUtils.EMPTY;
1✔
171
                    }
172
                    str = str.replace(tokenVarRefName, replacement);
1✔
173
                }
174
            }
1✔
175
            return str;
1✔
176
        } else {
177
            return value;
1✔
178
        }
179
    }
180

181
    private void resolveTokenVariables(OgnlContext ognlContext, TokenEvaluator tokenEvaluator) {
182
        for (Map.Entry<String, Token> entry : tokenVars.entrySet()) {
1✔
183
            String tokenVarName = entry.getKey();
1✔
184
            Token token = entry.getValue();
1✔
185
            Object value = tokenEvaluator.evaluate(token);
1✔
186
            ognlContext.put(tokenVarName, value);
1✔
187
        }
1✔
188
    }
1✔
189

190
    @NonNull
191
    private static String createTokenVarName(Token token) {
192
        return TOKEN_VAR_NAME_PREFIX + createTokenName(token) + TOKEN_VAR_NAME_SUFFIX;
1✔
193
    }
194

195
    @NonNull
196
    private static String createTokenVarRefName(String tokenVarName) {
197
        return TOKEN_VAR_REF_SYMBOL + tokenVarName;
1✔
198
    }
199

200
    @NonNull
201
    private static String createTokenName(@NonNull Token token) {
202
        int hashCode = token.hashCode();
1✔
203
        if (hashCode >= 0) {
1✔
204
            return Long.toString(hashCode, 32);
1✔
205
        } else {
206
            return Long.toString(hashCode & 0x7fffffff, 32);
×
207
        }
208
    }
209

210
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc