Be strict about boolean arguments
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / Utils.java
index bce189b6df16bcfba55e29b3868356a72e543fb7..677b9b3456c77a00a227c1023a9b48cf71c0d15b 100644 (file)
@@ -22,6 +22,7 @@ import java.util.List;
 import java.util.Set;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
+import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
 import javax.annotation.RegEx;
 import javax.xml.xpath.XPath;
@@ -272,6 +273,18 @@ public final class Utils {
         return (StatementContextBase<?, ?, ?>) rootStmtCtx.getFromNamespace(SchemaNodeIdentifierBuildNamespace.class, node);
     }
 
+    static @Nonnull Boolean parseBoolean(final StmtContext<?, ?, ?> ctx, final String input) {
+        if ("true".equals(input)) {
+            return Boolean.TRUE;
+        } else if ("false".equals(input)) {
+            return Boolean.FALSE;
+        } else {
+            throw new SourceException(ctx.getStatementSourceReference(),
+                "Invalid '%s' statement %s '%s', it can be either 'true' or 'false'",
+                ctx.getPublicDefinition().getStatementName(), ctx.getPublicDefinition().getArgumentName(), input);
+        }
+    }
+
     static String internBoolean(final String input) {
         if ("true".equals(input)) {
             return "true";