Vidyashree Rama | 468f828 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Open Networking Laboratory |
| 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 | package org.onosproject.yangutils.parser.impl.parserutils; |
| 18 | |
| 19 | import org.antlr.v4.runtime.ParserRuleContext; |
| 20 | |
| 21 | import java.text.ParseException; |
| 22 | import java.text.SimpleDateFormat; |
| 23 | import java.util.regex.Pattern; |
Vidyashree Rama | 8a6b128 | 2016-03-15 10:18:25 +0530 | [diff] [blame^] | 24 | |
| 25 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
Vidyashree Rama | 468f828 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 26 | import org.onosproject.yangutils.utils.YangConstructType; |
| 27 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 28 | |
| 29 | /** |
| 30 | * It's a utility for listener. |
| 31 | */ |
| 32 | public final class ListenerUtil { |
| 33 | private static final Pattern IDENTIFIER_PATTERN = Pattern.compile("[a-zA-Z_][a-zA-Z0-9_.-]*"); |
Vidyashree Rama | 8a6b128 | 2016-03-15 10:18:25 +0530 | [diff] [blame^] | 34 | private static final String NON_NEGATIVE_INTEGER_PATTERN = "[0-9]+"; |
Vidyashree Rama | 468f828 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 35 | private static final String PLUS = "+"; |
Vidyashree Rama | 8a6b128 | 2016-03-15 10:18:25 +0530 | [diff] [blame^] | 36 | private static final String ONE = "1"; |
| 37 | private static final String TRUE_KEYWORD = "true"; |
| 38 | private static final String FALSE_KEYWORD = "false"; |
Vidyashree Rama | 468f828 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 39 | private static final int IDENTIFIER_LENGTH = 64; |
| 40 | |
| 41 | /** |
| 42 | * Creates a new listener util. |
| 43 | */ |
| 44 | private ListenerUtil() { |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Removes doubles quotes and concatenates if string has plus symbol. |
| 49 | * |
| 50 | * @param yangStringData string from yang file |
| 51 | * @return concatenated string after removing double quotes |
| 52 | */ |
| 53 | public static String removeQuotesAndHandleConcat(String yangStringData) { |
| 54 | |
| 55 | yangStringData = yangStringData.replace("\"", ""); |
| 56 | String[] tmpData = yangStringData.split(Pattern.quote(PLUS)); |
| 57 | StringBuilder builder = new StringBuilder(); |
| 58 | for (String yangString : tmpData) { |
| 59 | builder.append(yangString); |
| 60 | } |
| 61 | return builder.toString(); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Validates identifier and returns concatenated string if string contains plus symbol. |
| 66 | * |
| 67 | * @param identifier string from yang file |
| 68 | * @param yangConstruct yang construct for creating error message |
| 69 | * @param ctx yang construct's context to get the line number and character position |
| 70 | * @return concatenated string after removing double quotes |
| 71 | */ |
| 72 | public static String getValidIdentifier(String identifier, YangConstructType yangConstruct, ParserRuleContext ctx) { |
| 73 | |
| 74 | String identifierString = removeQuotesAndHandleConcat(identifier); |
| 75 | ParserException parserException; |
| 76 | |
| 77 | if (identifierString.length() > IDENTIFIER_LENGTH) { |
| 78 | parserException = new ParserException("YANG file error : " + |
| 79 | YangConstructType.getYangConstructType(yangConstruct) + " name " + identifierString + " is " + |
| 80 | "greater than 64 characters."); |
| 81 | } else if (!IDENTIFIER_PATTERN.matcher(identifierString).matches()) { |
| 82 | parserException = new ParserException("YANG file error : " + |
| 83 | YangConstructType.getYangConstructType(yangConstruct) + " name " + identifierString + " is not " + |
| 84 | "valid."); |
| 85 | } else { |
| 86 | return identifierString; |
| 87 | } |
| 88 | |
| 89 | parserException.setLine(ctx.getStart().getLine()); |
| 90 | parserException.setCharPosition(ctx.getStart().getCharPositionInLine()); |
| 91 | throw parserException; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Validates the revision date. |
| 96 | * |
| 97 | * @param dateToValidate input revision date |
| 98 | * @return validation result, true for success, false for failure |
| 99 | */ |
| 100 | public static boolean isDateValid(String dateToValidate) { |
| 101 | |
| 102 | if (dateToValidate == null || !dateToValidate.matches("[0-9]{4}-[0-9]{2}-[0-9]{2}")) { |
| 103 | return false; |
| 104 | } |
| 105 | |
| 106 | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); |
| 107 | sdf.setLenient(false); |
| 108 | |
| 109 | try { |
| 110 | //if not valid, it will throw ParseException |
| 111 | sdf.parse(dateToValidate); |
| 112 | } catch (ParseException e) { |
| 113 | return false; |
| 114 | } |
| 115 | |
| 116 | return true; |
| 117 | } |
Vidyashree Rama | 8a6b128 | 2016-03-15 10:18:25 +0530 | [diff] [blame^] | 118 | |
| 119 | /** |
| 120 | * Validates YANG version. |
| 121 | * |
| 122 | * @param ctx version context object of the grammar rule |
| 123 | * @return valid version |
| 124 | */ |
| 125 | public static byte getValidVersion(GeneratedYangParser.YangVersionStatementContext ctx) { |
| 126 | |
| 127 | String value = removeQuotesAndHandleConcat(ctx.version().getText()); |
| 128 | if (!value.equals(ONE)) { |
| 129 | ParserException parserException = new ParserException("YANG file error: Input version not supported"); |
| 130 | parserException.setLine(ctx.getStart().getLine()); |
| 131 | parserException.setCharPosition(ctx.getStart().getCharPositionInLine()); |
| 132 | throw parserException; |
| 133 | } |
| 134 | |
| 135 | return Byte.valueOf(value); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Validates non negative integer value. |
| 140 | * |
| 141 | * @param integerValue integer to be validated |
| 142 | * @param yangConstruct yang construct for creating error message |
| 143 | * @param ctx context object of the grammar rule |
| 144 | * @return valid non negative integer value |
| 145 | */ |
| 146 | public static int getValidNonNegativeIntegerValue(String integerValue, YangConstructType yangConstruct, |
| 147 | ParserRuleContext ctx) { |
| 148 | |
| 149 | String value = removeQuotesAndHandleConcat(integerValue); |
| 150 | if (!value.matches(NON_NEGATIVE_INTEGER_PATTERN)) { |
| 151 | ParserException parserException = new ParserException("YANG file error : " + |
| 152 | YangConstructType.getYangConstructType(yangConstruct) + " value " + value + " is not " + |
| 153 | "valid."); |
| 154 | parserException.setLine(ctx.getStart().getLine()); |
| 155 | parserException.setCharPosition(ctx.getStart().getCharPositionInLine()); |
| 156 | throw parserException; |
| 157 | } |
| 158 | |
| 159 | return Integer.parseInt(value); |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Validates boolean value. |
| 164 | * |
| 165 | * @param booleanValue value to be validated |
| 166 | * @param yangConstruct yang construct for creating error message |
| 167 | * @param ctx context object of the grammar rule |
| 168 | * @return boolean value either true or false |
| 169 | */ |
| 170 | public static boolean getValidBooleanValue(String booleanValue, YangConstructType yangConstruct, |
| 171 | ParserRuleContext ctx) { |
| 172 | |
| 173 | String value = removeQuotesAndHandleConcat(booleanValue); |
| 174 | if (value.equals(TRUE_KEYWORD)) { |
| 175 | return true; |
| 176 | } else if (value.equals(FALSE_KEYWORD)) { |
| 177 | return false; |
| 178 | } else { |
| 179 | ParserException parserException = new ParserException("YANG file error : " + |
| 180 | YangConstructType.getYangConstructType(yangConstruct) + " value " + value + " is not " + |
| 181 | "valid."); |
| 182 | parserException.setLine(ctx.getStart().getLine()); |
| 183 | parserException.setCharPosition(ctx.getStart().getCharPositionInLine()); |
| 184 | throw parserException; |
| 185 | } |
| 186 | } |
Vidyashree Rama | 468f828 | 2016-03-04 19:08:35 +0530 | [diff] [blame] | 187 | } |