Remove RevisionSourceIdentifier
[yangtools.git] / yang / yang-repo-api / src / main / java / org / opendaylight / yangtools / yang / model / repo / api / YangTextSchemaSource.java
index 0e521561aa6927ae7c8225e215a7903da39b961a..8cd7de30be4b7f00b918948c5f51c782add7c379 100644 (file)
@@ -20,9 +20,9 @@ import com.google.common.io.Resources;
 import java.io.File;
 import java.io.InputStream;
 import java.net.URL;
-import java.util.Map.Entry;
+import java.nio.file.Files;
+import java.nio.file.Path;
 import org.eclipse.jdt.annotation.NonNull;
-import org.opendaylight.yangtools.yang.common.Revision;
 
 /**
  * YANG text schema source representation. Exposes an RFC6020 or RFC7950 text representation as an {@link InputStream}.
@@ -36,12 +36,12 @@ public abstract class YangTextSchemaSource extends ByteSource implements YangSch
     }
 
     public static @NonNull SourceIdentifier identifierFromFilename(final String name) {
-        checkArgument(name.endsWith(RFC6020_YANG_FILE_EXTENSION), "Filename %s does not end with '%s'",
-            RFC6020_YANG_FILE_EXTENSION, name);
+        checkArgument(name.endsWith(RFC6020_YANG_FILE_EXTENSION), "Filename '%s' does not end with '%s'", name,
+            RFC6020_YANG_FILE_EXTENSION);
 
         final String baseName = name.substring(0, name.length() - RFC6020_YANG_FILE_EXTENSION.length());
-        final Entry<String, String> parsed = parseFilename(baseName);
-        return RevisionSourceIdentifier.create(parsed.getKey(), Revision.ofNullable(parsed.getValue()));
+        final var parsed = parseFilename(baseName);
+        return new SourceIdentifier(parsed.getKey(), parsed.getValue());
     }
 
     /**
@@ -75,14 +75,27 @@ public abstract class YangTextSchemaSource extends ByteSource implements YangSch
      * Create a new YangTextSchemaSource backed by a {@link File} with {@link SourceIdentifier} derived from the file
      * name.
      *
-     * @param file Backing File
+     * @param path Backing path
      * @return A new YangTextSchemaSource
      * @throws IllegalArgumentException if the file name has invalid format or if the supplied File is not a file
-     * @throws NullPointerException if file is null
+     * @throws NullPointerException if file is {@code null}
      */
-    public static @NonNull YangTextSchemaSource forFile(final File file) {
-        checkArgument(file.isFile(), "Supplied file %s is not a file", file);
-        return new YangTextFileSchemaSource(identifierFromFilename(file.getName()), file);
+    public static @NonNull YangTextSchemaSource forPath(final Path path) {
+        return forPath(path, identifierFromFilename(path.toFile().getName()));
+    }
+
+    /**
+     * Create a new YangTextSchemaSource backed by a {@link File} and specified {@link SourceIdentifier}.
+     *
+     * @param path Backing path
+     * @param identifier source identifier
+     * @return A new YangTextSchemaSource
+     * @throws NullPointerException if any argument is {@code null}
+     * @throws IllegalArgumentException if the supplied path is not a regular file
+     */
+    public static @NonNull YangTextSchemaSource forPath(final Path path, final SourceIdentifier identifier) {
+        checkArgument(Files.isRegularFile(path), "Supplied path %s is not a regular file", path);
+        return new YangTextFileSchemaSource(identifier, path);
     }
 
     /**
@@ -113,6 +126,18 @@ public abstract class YangTextSchemaSource extends ByteSource implements YangSch
         return new ResourceYangTextSchemaSource(identifier, url);
     }
 
+    /**
+     * Create a new {@link YangTextSchemaSource} backed by a URL.
+     *
+     * @param url Backing URL
+     * @param identifier Source identifier
+     * @return A new instance.
+     * @throws NullPointerException if any argument is {@code null}
+     */
+    public static @NonNull YangTextSchemaSource forURL(final URL url, final SourceIdentifier identifier) {
+        return new ResourceYangTextSchemaSource(identifier, url);
+    }
+
     @Override
     public final SourceIdentifier getIdentifier() {
         return identifier;