|
| 1 | +/* |
| 2 | + * Copyright [2019] [恒宇少年 - 于起宇] |
| 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 | + */ |
| 17 | + |
| 18 | +package org.minbox.framework.api.boot.plugin.resource.load.expression; |
| 19 | + |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.List; |
| 22 | +import java.util.regex.Matcher; |
| 23 | +import java.util.regex.Pattern; |
| 24 | + |
| 25 | +/** |
| 26 | + * ApiBoot Resource Source Field Name Expression |
| 27 | + * |
| 28 | + * @author:恒宇少年 - 于起宇 |
| 29 | + * <p> |
| 30 | + * DateTime:2019-04-19 14:47 |
| 31 | + * Blog:http://blog.yuqiyu.com |
| 32 | + * WebSite:http://www.jianshu.com/u/092df3f77bca |
| 33 | + * Gitee:https://gitee.com/hengboy |
| 34 | + * GitHub:https://github.com/hengboy |
| 35 | + */ |
| 36 | +public class ResourceSourceExpression { |
| 37 | + /** |
| 38 | + * Source Name Basic Pattern |
| 39 | + */ |
| 40 | + static final Pattern SOURCE_NAME_PATTERN_BASIC = Pattern.compile("#p(\\d+)"); |
| 41 | + /** |
| 42 | + * Source Name Ognl Pattern |
| 43 | + */ |
| 44 | + static final Pattern SOURCE_NAME_PATTERN_OGNL = Pattern.compile("#p(\\d+).(\\S+)"); |
| 45 | + |
| 46 | + /** |
| 47 | + * If match basic pattern |
| 48 | + * |
| 49 | + * @param sourceFieldName ResourceField annotation source value |
| 50 | + * @return true: match , false: don't match |
| 51 | + * @see org.minbox.framework.api.boot.plugin.resource.load.annotation.ResourceField |
| 52 | + */ |
| 53 | + public static Matcher getBasicMatch(String sourceFieldName) { |
| 54 | + Matcher matcher = SOURCE_NAME_PATTERN_BASIC.matcher(sourceFieldName); |
| 55 | + return matcher; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * If match ognl pattern |
| 60 | + * |
| 61 | + * @param sourceFieldName ResourceField annotation source value |
| 62 | + * @return true: match , false: don't match |
| 63 | + * @see org.minbox.framework.api.boot.plugin.resource.load.annotation.ResourceField |
| 64 | + */ |
| 65 | + public static Matcher getOgnlMatch(String sourceFieldName) { |
| 66 | + Matcher matcher = SOURCE_NAME_PATTERN_OGNL.matcher(sourceFieldName); |
| 67 | + return matcher; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * get match content list |
| 72 | + * |
| 73 | + * @param sourceFieldName ResourceField annotation source value |
| 74 | + * @return match content list |
| 75 | + */ |
| 76 | + public static List<String> getMatchContent(String sourceFieldName) { |
| 77 | + List<String> matchContents = new ArrayList<>(); |
| 78 | + Matcher matcher = getOgnlMatch(sourceFieldName); |
| 79 | + boolean isMatch = matcher.find(); |
| 80 | + // If don't match ognl expression |
| 81 | + if (!isMatch) { |
| 82 | + // use basic expression |
| 83 | + matcher = getBasicMatch(sourceFieldName); |
| 84 | + isMatch = matcher.find(); |
| 85 | + } |
| 86 | + // If match basic expression |
| 87 | + if (isMatch) { |
| 88 | + // get all match value |
| 89 | + for (int i = 1; i <= matcher.groupCount(); i++) { |
| 90 | + matchContents.add(matcher.group(i)); |
| 91 | + } |
| 92 | + } |
| 93 | + return matchContents; |
| 94 | + } |
| 95 | +} |
0 commit comments