Fix checkstyle violations in sdc/jtosca
[sdc/sdc-tosca.git] / src / main / java / org / onap / sdc / toscaparser / api / elements / constraints / Pattern.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.sdc.toscaparser.api.elements.constraints;
22
23 import org.onap.sdc.toscaparser.api.common.JToscaValidationIssue;
24 import org.onap.sdc.toscaparser.api.utils.ThreadLocalsHolder;
25
26 import java.util.Collections;
27 import java.util.regex.Matcher;
28 import java.util.regex.PatternSyntaxException;
29
30 public class Pattern extends Constraint {
31
32     @Override
33     protected void setValues() {
34
35         setConstraintKey(PATTERN);
36
37         addValidTypes(Collections.singletonList("String"));
38
39         validPropTypes.add(Schema.STRING);
40
41     }
42
43
44     public Pattern(String name, String type, Object c) {
45         super(name, type, c);
46
47         if (!validTypes.contains(constraintValue.getClass().getSimpleName())) {
48             ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE114", "InvalidSchemaError: The property \"pattern\" expects a string"));
49         }
50     }
51
52     @Override
53     protected boolean isValid(Object value) {
54         try {
55             if (!(value instanceof String)) {
56                 ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE115", String.format("ValueError: Input value \"%s\" to \"pattern\" property \"%s\" must be a string",
57                         value.toString(), propertyName)));
58                 return false;
59             }
60             String strp = constraintValue.toString();
61             String strm = value.toString();
62             java.util.regex.Pattern pattern = java.util.regex.Pattern.compile(strp);
63             Matcher matcher = pattern.matcher(strm);
64             if (matcher.find() && matcher.end() == strm.length()) {
65                 return true;
66             }
67             return false;
68         } catch (PatternSyntaxException pse) {
69             ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE116", String.format("ValueError: Invalid regex \"%s\" in \"pattern\" property \"%s\"",
70                     constraintValue.toString(), propertyName)));
71             return false;
72         }
73     }
74
75     @Override
76     protected String errMsg(Object value) {
77         return String.format("The value \"%s\" of property \"%s\" does not match the pattern \"%s\"",
78                 value.toString(), propertyName, constraintValue.toString());
79     }
80
81 }
82
83 /*python
84
85 class Pattern(Constraint):
86     """Constraint class for "pattern"
87
88     Constrains the property or parameter to a value that is allowed by
89     the provided regular expression.
90     """
91
92     constraint_key = Constraint.PATTERN
93
94     valid_types = (str, )
95
96     valid_prop_types = (Schema.STRING, )
97
98     def __init__(self, property_name, property_type, constraint):
99         super(Pattern, self).__init__(property_name, property_type, constraint)
100         if not isinstance(self.constraint_value, self.valid_types):
101             ValidationIsshueCollector.appendException(
102                 InvalidSchemaError(message=_('The property "pattern" '
103                                              'expects a string.')))
104         self.match = re.compile(self.constraint_value).match
105
106     def _is_valid(self, value):
107         match = self.match(value)
108         return match is not None and match.end() == len(value)
109
110     def _err_msg(self, value):
111         return (_('The value "%(pvalue)s" of property "%(pname)s" does not '
112                   'match pattern "%(cvalue)s".') %
113                 dict(pname=self.property_name,
114                      pvalue=value,
115                      cvalue=self.constraint_value))
116 */