Add CopyPolicy.EXACT_REPLICA
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / ArgumentUtils.java
index 138822dc067ef408b1d12fc216ff18d09fd0fe40..8ac80fb539e55fff655051c56c5ce14325148975 100644 (file)
@@ -14,17 +14,14 @@ import java.util.ArrayList;
 import java.util.List;
 import java.util.regex.Pattern;
 import org.checkerframework.checker.regex.qual.Regex;
-import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
-import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
+import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
+import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Descendant;
 import org.opendaylight.yangtools.yang.model.api.stmt.UnresolvedNumber;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 /**
  * Utility class for dealing with arguments encountered by StatementSupport classes.
@@ -34,8 +31,6 @@ public final class ArgumentUtils {
     public static final Splitter PIPE_SPLITTER = Splitter.on('|').trimResults();
     public static final Splitter TWO_DOTS_SPLITTER = Splitter.on("..").trimResults();
 
-    private static final Logger LOG = LoggerFactory.getLogger(ArgumentUtils.class);
-
     @Regex
     private static final String PATH_ABS_STR = "/[^/].*";
     private static final Pattern PATH_ABS = Pattern.compile(PATH_ABS_STR);
@@ -47,7 +42,7 @@ public final class ArgumentUtils {
     private static final BigDecimal YANG_MAX_NUM = BigDecimal.valueOf(Double.MAX_VALUE);
 
     private ArgumentUtils() {
-        throw new UnsupportedOperationException();
+        // Hidden on purpose
     }
 
     public static int compareNumbers(final Number n1, final Number n2) {
@@ -56,51 +51,39 @@ public final class ArgumentUtils {
         return new BigDecimal(num1.toString()).compareTo(new BigDecimal(num2.toString()));
     }
 
-    public static String internBoolean(final String input) {
-        if ("true".equals(input)) {
-            return "true";
-        } else if ("false".equals(input)) {
-            return "false";
-        } else {
-            return input;
-        }
+    public static boolean isAbsoluteXPath(final String path) {
+        return PATH_ABS.matcher(path).matches();
     }
 
-    public 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 {
-            final StatementDefinition def = ctx.getPublicDefinition();
-            throw new SourceException(ctx.getStatementSourceReference(),
-                "Invalid '%s' statement %s '%s', it can be either 'true' or 'false'",
-                def.getStatementName(), def.getArgumentDefinition().get().getArgumentName(), input);
-        }
+    public static Absolute parseAbsoluteSchemaNodeIdentifier(final StmtContext<?, ?, ?> ctx, final String str) {
+        // FIXME: this does accept check for a leading slash
+        return Absolute.of(parseNodeIdentifiers(ctx, str));
     }
 
-    public static RevisionAwareXPath parseXPath(final StmtContext<?, ?, ?> ctx, final String path) {
-        return XPathSupport.parseXPath(ctx, path);
+    public static Descendant parseDescendantSchemaNodeIdentifier(final StmtContext<?, ?, ?> ctx, final String str) {
+        // FIXME: this does accept a leading slash
+        return Descendant.of(parseNodeIdentifiers(ctx, str));
     }
 
-    public static boolean isAbsoluteXPath(final String path) {
-        return PATH_ABS.matcher(path).matches();
+    public static SchemaNodeIdentifier nodeIdentifierFromPath(final StmtContext<?, ?, ?> ctx, final String path) {
+        final List<QName> qnames = parseNodeIdentifiers(ctx, path);
+        return PATH_ABS.matcher(path).matches() ? Absolute.of(qnames) : Descendant.of(qnames);
     }
 
     @SuppressWarnings("checkstyle:illegalCatch")
-    public static SchemaNodeIdentifier nodeIdentifierFromPath(final StmtContext<?, ?, ?> ctx, final String path) {
+    private static  List<QName> parseNodeIdentifiers(final StmtContext<?, ?, ?> ctx, final String path) {
         // FIXME: is the path trimming really necessary??
-        final List<QName> qNames = new ArrayList<>();
+        final List<QName> qnames = new ArrayList<>();
         for (final String nodeName : SLASH_SPLITTER.split(trimSingleLastSlashFromXPath(path))) {
             try {
-                qNames.add(StmtContextUtils.parseNodeIdentifier(ctx, nodeName));
+                qnames.add(StmtContextUtils.parseNodeIdentifier(ctx, nodeName));
             } catch (final RuntimeException e) {
-                throw new SourceException(ctx.getStatementSourceReference(), e,
-                        "Failed to parse node '%s' in path '%s'", nodeName, path);
+                throw new SourceException(ctx, e, "Failed to parse node '%s' in path '%s'", nodeName, path);
             }
         }
 
-        return SchemaNodeIdentifier.create(qNames, PATH_ABS.matcher(path).matches());
+        SourceException.throwIf(qnames.isEmpty(), ctx, "Schema node identifier must not be empty");
+        return qnames;
     }
 
     private static String trimSingleLastSlashFromXPath(final String path) {