Gaurav Agrawal
Committed by Gerrit Code Review

Invalid pattern restriction check

Change-Id: I5398cb6e85f06efd7937562ba0d9a51eff23572d
(cherry picked from commit 8d0ad2fa)
......@@ -16,6 +16,8 @@
package org.onosproject.yangutils.parser.impl.listeners;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import org.onosproject.yangutils.datamodel.YangDataTypes;
import org.onosproject.yangutils.datamodel.YangDerivedInfo;
import org.onosproject.yangutils.datamodel.YangPatternRestriction;
......@@ -98,8 +100,8 @@ public final class PatternRestrictionListener {
* Sets the pattern restriction to type.
*
* @param listener listener's object
* @param type Yang type for which pattern restriction to be set
* @param ctx context object of the grammar rule
* @param type Yang type for which pattern restriction to be set
* @param ctx context object of the grammar rule
*/
private static void setPatternRestriction(TreeWalkListener listener, YangType type,
GeneratedYangParser.PatternStatementContext ctx) {
......@@ -114,7 +116,8 @@ public final class PatternRestrictionListener {
throw parserException;
}
String patternArgument = ctx.string().getText().replace("\"", EMPTY_STRING);
// Validate and get valid pattern restriction string.
String patternArgument = getValidPattern(ctx);
if (type.getDataType() == YangDataTypes.STRING) {
YangStringRestriction stringRestriction = (YangStringRestriction) type.getDataTypeExtendedInfo();
......@@ -145,10 +148,10 @@ public final class PatternRestrictionListener {
* It is called when parser exits from grammar rule (pattern).
*
* @param listener listener's object
* @param ctx context object of the grammar rule
* @param ctx context object of the grammar rule
*/
public static void processPatternRestrictionExit(TreeWalkListener listener,
GeneratedYangParser.PatternStatementContext ctx) {
GeneratedYangParser.PatternStatementContext ctx) {
// Check for stack to be non empty.
checkStackIsNotEmpty(listener, MISSING_HOLDER, PATTERN_DATA, ctx.string().getText(), EXIT);
......@@ -164,4 +167,25 @@ public final class PatternRestrictionListener {
ctx.string().getText(), EXIT));
}
}
/**
* Validates and return the valid pattern.
*
* @param ctx context object of the grammar rule
* @return validated string
*/
private static String getValidPattern(GeneratedYangParser.PatternStatementContext ctx) {
String userInputPattern = ctx.string().getText().replace("\"", EMPTY_STRING);
try {
Pattern.compile(userInputPattern);
} catch (PatternSyntaxException exception) {
ParserException parserException = new ParserException("YANG file error : " +
YangConstructType.getYangConstructType(PATTERN_DATA) + " name " + ctx.string().getText() +
" is not a valid regular expression");
parserException.setLine(ctx.getStart().getLine());
parserException.setCharPosition(ctx.getStart().getCharPositionInLine());
throw parserException;
}
return userInputPattern;
}
}
......
......@@ -18,17 +18,16 @@ package org.onosproject.yangutils.parser.impl.listeners;
import java.io.IOException;
import java.util.ListIterator;
import org.junit.Test;
import org.onosproject.yangutils.datamodel.YangNode;
import org.onosproject.yangutils.datamodel.YangNodeType;
import org.onosproject.yangutils.datamodel.YangModule;
import org.onosproject.yangutils.datamodel.YangDataTypes;
import org.onosproject.yangutils.datamodel.YangLeaf;
import org.onosproject.yangutils.datamodel.YangLeafList;
import org.onosproject.yangutils.datamodel.YangDataTypes;
import org.onosproject.yangutils.datamodel.YangTypeDef;
import org.onosproject.yangutils.datamodel.YangModule;
import org.onosproject.yangutils.datamodel.YangNode;
import org.onosproject.yangutils.datamodel.YangNodeType;
import org.onosproject.yangutils.datamodel.YangPatternRestriction;
import org.onosproject.yangutils.datamodel.YangStringRestriction;
import org.onosproject.yangutils.datamodel.YangTypeDef;
import org.onosproject.yangutils.parser.exceptions.ParserException;
import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
......@@ -194,4 +193,12 @@ public class PatternRestrictionListenerTest {
.getPatternList().listIterator();
assertThat(patternListIterator.next(), is("[a-zA-Z]"));
}
/**
* Checks invalid pattern sub-statement.
*/
@Test(expected = ParserException.class)
public void processInvalidPatternSubStatements() throws IOException, ParserException {
YangNode node = manager.getDataModel("src/test/resources/InvalidPatternSubStatements.yang");
}
}
......
module Test {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
leaf invalid-interval {
type string {
pattern "[a-zA-Z]\" {
description "pattern description";
reference "pattern reference";
}
}
}
}