Vidyashree Rama | d322132 | 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; |
| 24 | import org.onosproject.yangutils.utils.YangConstructType; |
| 25 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 26 | |
| 27 | /** |
| 28 | * It's a utility for listener. |
| 29 | */ |
| 30 | public final class ListenerUtil { |
| 31 | private static final Pattern IDENTIFIER_PATTERN = Pattern.compile("[a-zA-Z_][a-zA-Z0-9_.-]*"); |
| 32 | private static final String PLUS = "+"; |
| 33 | private static final int IDENTIFIER_LENGTH = 64; |
| 34 | |
| 35 | /** |
| 36 | * Creates a new listener util. |
| 37 | */ |
| 38 | private ListenerUtil() { |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Removes doubles quotes and concatenates if string has plus symbol. |
| 43 | * |
| 44 | * @param yangStringData string from yang file |
| 45 | * @return concatenated string after removing double quotes |
| 46 | */ |
| 47 | public static String removeQuotesAndHandleConcat(String yangStringData) { |
| 48 | |
| 49 | yangStringData = yangStringData.replace("\"", ""); |
| 50 | String[] tmpData = yangStringData.split(Pattern.quote(PLUS)); |
| 51 | StringBuilder builder = new StringBuilder(); |
| 52 | for (String yangString : tmpData) { |
| 53 | builder.append(yangString); |
| 54 | } |
| 55 | return builder.toString(); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Validates identifier and returns concatenated string if string contains plus symbol. |
| 60 | * |
| 61 | * @param identifier string from yang file |
| 62 | * @param yangConstruct yang construct for creating error message |
| 63 | * @param ctx yang construct's context to get the line number and character position |
| 64 | * @return concatenated string after removing double quotes |
| 65 | */ |
| 66 | public static String getValidIdentifier(String identifier, YangConstructType yangConstruct, ParserRuleContext ctx) { |
| 67 | |
| 68 | String identifierString = removeQuotesAndHandleConcat(identifier); |
| 69 | ParserException parserException; |
| 70 | |
| 71 | if (identifierString.length() > IDENTIFIER_LENGTH) { |
| 72 | parserException = new ParserException("YANG file error : " + |
| 73 | YangConstructType.getYangConstructType(yangConstruct) + " name " + identifierString + " is " + |
| 74 | "greater than 64 characters."); |
| 75 | } else if (!IDENTIFIER_PATTERN.matcher(identifierString).matches()) { |
| 76 | parserException = new ParserException("YANG file error : " + |
| 77 | YangConstructType.getYangConstructType(yangConstruct) + " name " + identifierString + " is not " + |
| 78 | "valid."); |
| 79 | } else { |
| 80 | return identifierString; |
| 81 | } |
| 82 | |
| 83 | parserException.setLine(ctx.getStart().getLine()); |
| 84 | parserException.setCharPosition(ctx.getStart().getCharPositionInLine()); |
| 85 | throw parserException; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Validates the revision date. |
| 90 | * |
| 91 | * @param dateToValidate input revision date |
| 92 | * @return validation result, true for success, false for failure |
| 93 | */ |
| 94 | public static boolean isDateValid(String dateToValidate) { |
| 95 | |
| 96 | if (dateToValidate == null || !dateToValidate.matches("[0-9]{4}-[0-9]{2}-[0-9]{2}")) { |
| 97 | return false; |
| 98 | } |
| 99 | |
| 100 | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); |
| 101 | sdf.setLenient(false); |
| 102 | |
| 103 | try { |
| 104 | //if not valid, it will throw ParseException |
| 105 | sdf.parse(dateToValidate); |
| 106 | } catch (ParseException e) { |
| 107 | return false; |
| 108 | } |
| 109 | |
| 110 | return true; |
| 111 | } |
| 112 | } |