2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.so.bpmn.core.internal;
23 import java.util.Optional;
24 import java.util.regex.Matcher;
25 import java.util.regex.Pattern;
28 * Extracts variable name from expression if entire expression is just
29 * one variable, for example "${x}".
31 * Ignores all whitespaces, except inside variable name.
34 * "${x}", extracted variable name is "x"
35 * " ${\t weird_NAME }", extracted variable name is "weird_NAME"
36 * "${incorrect name}", no extracted name
37 * "${two}+${two}", no extracted name
39 public class VariableNameExtractor {
41 private static final Pattern VARIABLE_NAME_PATTERN = Pattern
42 .compile("^\\s*\\$\\s*\\{\\s*([a-zA-Z0-9_]+)\\s*\\}\\s*$");
44 private final String expression;
48 * Creates new VariableNameExtractor
49 * @param expression expression to be parsed
51 public VariableNameExtractor(String expression) {
52 this.expression = expression;
56 * Extracts variable name from expression given in constructor
57 * @return Optional of variable name, empty if expression wasn't single variable
59 public Optional<String> extract() {
60 Matcher matcher = VARIABLE_NAME_PATTERN.matcher(expression);
61 if (!matcher.matches()) {
62 return Optional.empty();
64 return Optional.of(matcher.group(1));