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

aspectran / aspectran / #3758

11 Oct 2024 08:30PM CUT coverage: 34.054% (-0.003%) from 34.057%
#3758

Pull #724

github

web-flow
Bump jakarta.servlet.jsp:jakarta.servlet.jsp-api from 3.1.1 to 4.0.0

Bumps [jakarta.servlet.jsp:jakarta.servlet.jsp-api](https://github.com/eclipse-ee4j/jsp-api) from 3.1.1 to 4.0.0.
- [Release notes](https://github.com/eclipse-ee4j/jsp-api/releases)
- [Commits](https://github.com/eclipse-ee4j/jsp-api/compare/3.1.1-RELEASE...4.0.0-RELEASE)

---
updated-dependencies:
- dependency-name: jakarta.servlet.jsp:jakarta.servlet.jsp-api
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
Pull Request #724: Bump jakarta.servlet.jsp:jakarta.servlet.jsp-api from 3.1.1 to 4.0.0

13336 of 39161 relevant lines covered (34.05%)

0.34 hits per line

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

62.5
/core/src/main/java/com/aspectran/core/context/expr/ExpressionEvaluation.java
1
/*
2
 * Copyright (c) 2008-2024 The Aspectran Project
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
package com.aspectran.core.context.expr;
17

18
import com.aspectran.core.activity.Activity;
19
import com.aspectran.core.activity.ActivityData;
20
import com.aspectran.core.context.expr.ognl.OgnlSupport;
21
import com.aspectran.core.context.expr.token.Token;
22
import com.aspectran.core.context.expr.token.TokenParser;
23
import com.aspectran.core.context.rule.type.TokenType;
24
import com.aspectran.utils.StringUtils;
25
import com.aspectran.utils.annotation.jsr305.NonNull;
26
import com.aspectran.utils.annotation.jsr305.Nullable;
27
import ognl.Ognl;
28
import ognl.OgnlContext;
29
import ognl.OgnlException;
30

31
import java.util.LinkedHashSet;
32
import java.util.Set;
33

34
/**
35
 * ExpressionEvaluator implementation that evaluates expressions written in
36
 * OGNL-based Aspectran expression language.
37
 *
38
 * <p>Created: 2021/01/31</p>
39
 *
40
 * @since 6.11.0
41
 */
42
public class ExpressionEvaluation implements ExpressionEvaluator {
43

44
    private static final String TOKEN_VAR_NAME_PREFIX = "__";
45

46
    private static final String TOKEN_VAR_NAME_SUFFIX = TOKEN_VAR_NAME_PREFIX;
47

48
    private static final String TOKEN_VAR_REF_SYMBOL = "#";
49

50
    private static final String TOKEN_VAR_REF_NAME_PREFIX = TOKEN_VAR_REF_SYMBOL + TOKEN_VAR_NAME_PREFIX;
51

52
    private final String expression;
53

54
    private Object represented;
55

56
    private Token[] tokens;
57

58
    public ExpressionEvaluation(String expression) throws ExpressionParserException {
1✔
59
        this.expression = expression;
1✔
60
        parseExpression(expression);
1✔
61
    }
1✔
62

63
    public String getExpression() {
64
        return expression;
1✔
65
    }
66

67
    public Token[] getTokens() {
68
        return tokens;
1✔
69
    }
70

71
    @Override
72
    @SuppressWarnings("unchecked")
73
    public <V> V evaluate(Activity activity, Class<V> resultType) {
74
        if (activity == null) {
1✔
75
            throw new IllegalArgumentException("activity must not be null");
×
76
        }
77
        if (represented == null) {
1✔
78
            return null;
×
79
        }
80
        try {
81
            ActivityData activityData;
82
            if (activity.getTranslet() != null) {
1✔
83
                activityData = activity.getTranslet().getActivityData();
×
84
            } else {
85
                activityData = new ActivityData(activity);
1✔
86
            }
87
            OgnlContext ognlContext = OgnlSupport.createDefaultContext();
1✔
88
            String[] tokenVarNames = null;
1✔
89
            if (tokens != null && tokens.length > 0) {
1✔
90
                TokenEvaluator tokenEvaluator = new TokenEvaluation(activity);
1✔
91
                tokenVarNames = putTokenVariables(ognlContext, tokenEvaluator, tokens);
1✔
92
            }
93
            Object result = Ognl.getValue(represented, ognlContext, activityData, resultType);
1✔
94
            if (tokenVarNames != null && result instanceof String) {
1✔
95
                for (String tokenVarName : tokenVarNames) {
1✔
96
                    String tokenVarRefName = TOKEN_VAR_REF_SYMBOL + tokenVarName;
1✔
97
                    String str = (String)result;
1✔
98
                    if (str.contains(tokenVarRefName)) {
1✔
99
                        Object value = ognlContext.get(tokenVarName);
1✔
100
                        if (value != null) {
1✔
101
                            result = str.replace(tokenVarRefName, value.toString());
×
102
                        } else {
103
                            result = str.replace(tokenVarRefName, StringUtils.EMPTY);
1✔
104
                        }
105
                    }
106
                }
107
            }
108
            return (V)result;
1✔
109
        } catch (OgnlException e) {
×
110
            throw new ExpressionEvaluationException(expression, e);
×
111
        }
112
    }
113

114
    @Override
115
    @SuppressWarnings("unchecked")
116
    public <V> V evaluate(TokenEvaluator tokenEvaluator, Class<V> resultType) {
117
        if (tokenEvaluator == null) {
×
118
            throw new IllegalArgumentException("tokenEvaluator must not be null");
×
119
        }
120
        if (represented == null) {
×
121
            return null;
×
122
        }
123
        try {
124
            Activity activity = tokenEvaluator.getActivity();
×
125
            ActivityData activityData;
126
            if (activity.getTranslet() != null) {
×
127
                activityData = activity.getTranslet().getActivityData();
×
128
            } else {
129
                activityData = new ActivityData(activity);
×
130
            }
131
            OgnlContext ognlContext = OgnlSupport.createDefaultContext();
×
132
            String[] tokenVarNames = null;
×
133
            if (tokens != null && tokens.length > 0) {
×
134
                tokenVarNames = putTokenVariables(ognlContext, tokenEvaluator, tokens);
×
135
            }
136
            Object result = Ognl.getValue(represented, ognlContext, activityData, resultType);
×
137
            if (tokenVarNames != null && result instanceof String) {
×
138
                for (String tokenVarName : tokenVarNames) {
×
139
                    String tokenVarRefName = TOKEN_VAR_REF_NAME_PREFIX + tokenVarName;
×
140
                    String str = (String)result;
×
141
                    if (str.contains(tokenVarRefName)) {
×
142
                        Object value = ognlContext.get(tokenVarName);
×
143
                        if (value != null) {
×
144
                            result = str.replace(tokenVarRefName, value.toString());
×
145
                        } else {
146
                            result = str.replace(tokenVarRefName, StringUtils.EMPTY);
×
147
                        }
148
                    }
149
                }
150
            }
151
            return (V)result;
×
152
        } catch (OgnlException e) {
×
153
            throw new ExpressionEvaluationException(expression, e);
×
154
        }
155
    }
156

157
    @Nullable
158
    private String[] putTokenVariables(OgnlContext ognlContext, TokenEvaluator tokenEvaluator,
159
                                       @NonNull Token[] tokens) {
160
        Set<String> tokenVarNames = new LinkedHashSet<>();
1✔
161
        for (Token token : tokens) {
1✔
162
            if (token.getType() != TokenType.TEXT) {
1✔
163
                String name = makeTokenVarName(token);
1✔
164
                Object value = tokenEvaluator.evaluate(token);
1✔
165
                ognlContext.put(name, value);
1✔
166
                tokenVarNames.add(name);
1✔
167
            }
168
        }
169
        return (tokenVarNames.isEmpty() ? null : tokenVarNames.toArray(new String[0]));
1✔
170
    }
171

172
    private void parseExpression(String expression) throws ExpressionParserException {
173
        tokens = TokenParser.makeTokens(expression, true);
1✔
174
        if (tokens != null && tokens.length > 0) {
1✔
175
            if (tokens.length == 1) {
1✔
176
                Token token = tokens[0];
1✔
177
                if (token.getType() == TokenType.TEXT) {
1✔
178
                    represented = OgnlSupport.parseExpression(token.getDefaultValue());
1✔
179
                } else {
180
                    String ognlVariableName = makeTokenVarRefName(token);
1✔
181
                    represented = OgnlSupport.parseExpression(ognlVariableName);
1✔
182
                }
183
            } else {
1✔
184
                StringBuilder sb = new StringBuilder();
1✔
185
                for (Token token : tokens) {
1✔
186
                    if (token.getType() == TokenType.TEXT) {
1✔
187
                        sb.append(token.getDefaultValue());
1✔
188
                    } else {
189
                        sb.append(makeTokenVarRefName(token));
1✔
190
                    }
191
                }
192
                represented = OgnlSupport.parseExpression(sb.toString());
1✔
193
            }
1✔
194
        } else {
195
            represented = OgnlSupport.parseExpression(expression);
×
196
        }
197
    }
1✔
198

199
    @NonNull
200
    private String makeTokenVarName(Token token) {
201
        return TOKEN_VAR_NAME_PREFIX + makeTokenName(token) + TOKEN_VAR_NAME_SUFFIX;
1✔
202
    }
203

204
    @NonNull
205
    private String makeTokenVarRefName(Token token) {
206
        return TOKEN_VAR_REF_NAME_PREFIX + makeTokenName(token) + TOKEN_VAR_NAME_SUFFIX;
1✔
207
    }
208

209
    @NonNull
210
    private String makeTokenName(@NonNull Token token) {
211
        int hashCode = token.hashCode();
1✔
212
        if (hashCode >= 0) {
1✔
213
            return Long.toString(hashCode, 32);
1✔
214
        } else {
215
            return Long.toString(hashCode & 0x7fffffff, 32);
×
216
        }
217
    }
218

219
}
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