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}.
}
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());
}
/**
* 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);
}
/**
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;