Use java.nio.file.Path in YangTextFileSchemaSource 02/97702/8
authorRobert Varga <robert.varga@pantheon.tech>
Fri, 1 Oct 2021 16:33:14 +0000 (18:33 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Sun, 3 Oct 2021 18:47:41 +0000 (20:47 +0200)
Switching to NIO seems to be a better strategy, as we ned up using it to
access content anyway.

JIRA: YANGTOOLS-1342
Change-Id: I066f1494327b8e4e6007cb3f620aec30a5b94c0c
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/StmtTestUtils.java
parser/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/TestUtils.java
parser/yang-test-util/src/main/java/org/opendaylight/yangtools/yang/test/util/YangParserTestUtils.java
plugin/yang-maven-plugin/src/main/java/org/opendaylight/yangtools/yang2sources/plugin/ScannedDependency.java
plugin/yang-maven-plugin/src/main/java/org/opendaylight/yangtools/yang2sources/plugin/YangToSourcesProcessor.java
tools/yang-model-validator/src/main/java/org/opendaylight/yangtools/yang/validator/SystemTestUtils.java
yang/yang-repo-api/src/main/java/org/opendaylight/yangtools/yang/model/repo/api/YangTextFileSchemaSource.java
yang/yang-repo-api/src/main/java/org/opendaylight/yangtools/yang/model/repo/api/YangTextSchemaSource.java
yang/yang-repo-api/src/main/java/org/opendaylight/yangtools/yang/model/repo/api/YinTextFileSchemaSource.java
yang/yang-repo-api/src/main/java/org/opendaylight/yangtools/yang/model/repo/api/YinTextSchemaSource.java
yang/yang-repo-fs/src/main/java/org/opendaylight/yangtools/yang/model/repo/fs/FilesystemSchemaSourceCache.java

index 5390bc4a96807a9e63d5ef3ee6f99efd1ca967d2..c06db19a823450c8fadff87b8c3629809b477481 100644 (file)
@@ -13,6 +13,7 @@ import java.io.FileFilter;
 import java.io.IOException;
 import java.net.URISyntaxException;
 import java.net.URL;
+import java.nio.file.Path;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
@@ -80,7 +81,7 @@ public final class StmtTestUtils {
 
     public static YangStatementStreamSource sourceForResource(final String resourceName) {
         try {
-            return YangStatementStreamSource.create(YangTextSchemaSource.forFile(new File(
+            return YangStatementStreamSource.create(YangTextSchemaSource.forPath(Path.of(
                 StmtTestUtils.class.getResource(resourceName).toURI())));
         } catch (IOException | YangSyntaxErrorException | URISyntaxException e) {
             throw new IllegalArgumentException("Failed to create source", e);
@@ -154,7 +155,7 @@ public final class StmtTestUtils {
 
         final Collection<YangStatementStreamSource> sources = new ArrayList<>(files.length);
         for (File file : files) {
-            sources.add(YangStatementStreamSource.create(YangTextSchemaSource.forFile(file)));
+            sources.add(YangStatementStreamSource.create(YangTextSchemaSource.forPath(file.toPath())));
         }
 
         return parseYangSources(config, supportedFeatures, sources);
@@ -167,7 +168,7 @@ public final class StmtTestUtils {
 
     public static EffectiveModelContext parseYangSources(final Collection<File> files,
             final YangParserConfiguration config) throws ReactorException, IOException, YangSyntaxErrorException {
-        return parseYangSources(config, null, files.toArray(new File[files.size()]));
+        return parseYangSources(config, null, files.toArray(new File[0]));
     }
 
     public static EffectiveModelContext parseYangSources(final String yangSourcesDirectoryPath)
index 4a3a009c88ac3b9eb4b1ced4d60173c303d43b09..fd55a88966bd7b6eecc5c14fdee44ad15f617550 100644 (file)
@@ -54,7 +54,7 @@ public final class TestUtils {
             .listFiles(StmtTestUtils.YANG_FILE_FILTER);
         final var sources = new ArrayList<StatementStreamSource>(files.length);
         for (var file : files) {
-            sources.add(YangStatementStreamSource.create(YangTextSchemaSource.forFile(file)));
+            sources.add(YangStatementStreamSource.create(YangTextSchemaSource.forPath(file.toPath())));
         }
         return sources;
     }
@@ -66,7 +66,7 @@ public final class TestUtils {
 
         for (File file : files) {
             if (file.getName().endsWith(YangConstants.RFC6020_YANG_FILE_EXTENSION)) {
-                reactor.addSource(YangStatementStreamSource.create(YangTextSchemaSource.forFile(file)));
+                reactor.addSource(YangStatementStreamSource.create(YangTextSchemaSource.forPath(file.toPath())));
             } else {
                 LOG.info("Ignoring non-yang file {}", file);
             }
@@ -93,7 +93,7 @@ public final class TestUtils {
 
         for (File file : new File(resourceDirectory).listFiles()) {
             reactor.addSource(YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(
-                YinTextSchemaSource.forFile(file))));
+                YinTextSchemaSource.forPath(file.toPath()))));
         }
 
         return reactor.buildEffective();
@@ -170,7 +170,7 @@ public final class TestUtils {
         StatementStreamSource[] sources = new StatementStreamSource[files.length];
 
         for (int i = 0; i < files.length; i++) {
-            sources[i] = YangStatementStreamSource.create(YangTextSchemaSource.forFile(files[i]));
+            sources[i] = YangStatementStreamSource.create(YangTextSchemaSource.forPath(files[i].toPath()));
         }
 
         return parseYangSources(sources);
index 24ac401b21fb56684681f7aeafb0dbba903e180b..c1c9c6e74115da7b342a056d3a797a5827acd9d4 100644 (file)
@@ -199,10 +199,11 @@ public final class YangParserTestUtils {
      * @param files YANG files to be parsed
      * @return effective schema context
      */
+    //  FIXME: use Java.nio.file.Path
     public static EffectiveModelContext parseYangFiles(final Set<QName> supportedFeatures,
             final YangParserConfiguration config, final Collection<File> files) {
         return parseSources(config, supportedFeatures,
-            files.stream().map(YangTextSchemaSource::forFile).collect(Collectors.toList()));
+            files.stream().map(file -> YangTextSchemaSource.forPath(file.toPath())).collect(Collectors.toList()));
     }
 
     /**
index cab0d2695891536d3b96786cf44d9124faa5f8fd..19d8451184738e9bc13a14ab7e3d546f4c37e361 100644 (file)
@@ -41,7 +41,7 @@ abstract class ScannedDependency {
 
         @Override
         Collection<YangTextSchemaSource> sources() {
-            return ImmutableList.of(YangTextSchemaSource.forFile(file()));
+            return ImmutableList.of(YangTextSchemaSource.forPath(file().toPath()));
         }
     }
 
@@ -119,6 +119,7 @@ abstract class ScannedDependency {
         return entryNames.isEmpty() ? ImmutableList.of() : ImmutableList.of(new Zip(zipFile, entryNames));
     }
 
+    // FIXME: java.nio.file.Path
     final File file() {
         return file;
     }
index 7ad13fbba767f4d1b0316b0b63d7eb85a19206f1..c75590a796d9217ccc6862b697917db59275f4cd 100644 (file)
@@ -87,11 +87,11 @@ class YangToSourcesProcessor {
         this.yangFilesRootDir = requireNonNull(yangFilesRootDir, "yangFilesRootDir");
         this.excludedFiles = ImmutableSet.copyOf(excludedFiles);
         this.codeGeneratorArgs = ImmutableList.copyOf(codeGeneratorArgs);
-        this.fileGeneratorArgs = Maps.uniqueIndex(fileGeneratorsArgs, FileGeneratorArg::getIdentifier);
+        fileGeneratorArgs = Maps.uniqueIndex(fileGeneratorsArgs, FileGeneratorArg::getIdentifier);
         this.project = requireNonNull(project);
         this.inspectDependencies = inspectDependencies;
         this.yangProvider = requireNonNull(yangProvider);
-        this.parserFactory = DEFAULT_PARSER_FACTORY;
+        parserFactory = DEFAULT_PARSER_FACTORY;
     }
 
     @VisibleForTesting
@@ -178,7 +178,7 @@ class YangToSourcesProcessor {
         final Stopwatch watch = Stopwatch.createStarted();
         final List<Entry<YangTextSchemaSource, IRSchemaSource>> parsed = yangFilesInProject.parallelStream()
             .map(file -> {
-                final YangTextSchemaSource textSource = YangTextSchemaSource.forFile(file);
+                final YangTextSchemaSource textSource = YangTextSchemaSource.forPath(file.toPath());
                 try {
                     return Map.entry(textSource,TextToIRTransformer.transformText(textSource));
                 } catch (YangSyntaxErrorException | IOException e) {
index 8db35300baffffe9dae8f5a61a5cfc553ed7b7eb..d626b6d7d8bc9226bf3a91dabc3f231c6322da86 100644 (file)
@@ -97,10 +97,10 @@ final class SystemTestUtils {
         }
 
         for (File file : testFiles) {
-            parser.addSource(YangTextSchemaSource.forFile(file));
+            parser.addSource(YangTextSchemaSource.forPath(file.toPath()));
         }
         for (File file : libFiles) {
-            parser.addLibSource(YangTextSchemaSource.forFile(file));
+            parser.addLibSource(YangTextSchemaSource.forPath(file.toPath()));
         }
 
         return parser.buildEffectiveModel();
index de5f0d0a477ef0751e34f766df4254ebdfe58af7..fb795c29b14a2d5cfeffdd4cb00512f54d115b7a 100644 (file)
@@ -10,10 +10,10 @@ package org.opendaylight.yangtools.yang.model.repo.api;
 import static java.util.Objects.requireNonNull;
 
 import com.google.common.base.MoreObjects.ToStringHelper;
-import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
 import java.nio.file.Files;
+import java.nio.file.Path;
 import java.util.Optional;
 import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.yangtools.concepts.Delegator;
@@ -23,31 +23,33 @@ import org.opendaylight.yangtools.concepts.Delegator;
  *
  * @author Robert Varga
  */
-final class YangTextFileSchemaSource extends YangTextSchemaSource implements Delegator<File> {
-    private final @NonNull File file;
+final class YangTextFileSchemaSource extends YangTextSchemaSource implements Delegator<Path> {
+    private final @NonNull Path path;
 
-    YangTextFileSchemaSource(final SourceIdentifier identifier, final File file) {
+    YangTextFileSchemaSource(final SourceIdentifier identifier, final Path path) {
         super(identifier);
-        this.file = requireNonNull(file);
+        this.path = requireNonNull(path);
     }
 
     @Override
-    public File getDelegate() {
-        return file;
+    public Path getDelegate() {
+        return path;
     }
 
     @Override
     public InputStream openStream() throws IOException {
-        return Files.newInputStream(file.toPath());
+        return Files.newInputStream(path);
     }
 
     @Override
     protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
-        return toStringHelper.add("file", file);
+        return toStringHelper.add("path", path);
     }
 
     @Override
     public Optional<String> getSymbolicName() {
-        return Optional.of(file.toString());
+        // FIXME: NEXT: this is forcing internal normalization. I think this boils down to providing Path back, which
+        //        is essentially getDelegate() anyway. Perhaps expose it as PathAware?
+        return Optional.of(path.toString());
     }
 }
index 2d87b9c905c9a7aa7acf6e88933a3898f994dfbf..0e1db5f0141dbceb64e58622e882f3cfaaac3f65 100644 (file)
@@ -20,6 +20,8 @@ import com.google.common.io.Resources;
 import java.io.File;
 import java.io.InputStream;
 import java.net.URL;
+import java.nio.file.Files;
+import java.nio.file.Path;
 import java.util.Map.Entry;
 import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.yangtools.yang.common.Revision;
@@ -75,27 +77,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 {@code null}
      */
-    public static @NonNull YangTextSchemaSource forFile(final File file) {
-        return forFile(file, identifierFromFilename(file.getName()));
+    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 file Backing File
+     * @param path Backing path
      * @param identifier source identifier
      * @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 any argument is {@code null}
+     * @throws IllegalArgumentException if the supplied path is not a regular file
      */
-    public static @NonNull YangTextSchemaSource forFile(final File file, final SourceIdentifier identifier) {
-        checkArgument(file.isFile(), "Supplied file %s is not a file", file);
-        return new YangTextFileSchemaSource(identifier, 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);
     }
 
     /**
index f900b8c3de974c390325d5c31262cae12e0a69b6..13362bd24cdb37207e1ab17c93ca8ee89c9fb5ff 100644 (file)
@@ -10,10 +10,10 @@ package org.opendaylight.yangtools.yang.model.repo.api;
 import static java.util.Objects.requireNonNull;
 
 import com.google.common.base.MoreObjects.ToStringHelper;
-import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
 import java.nio.file.Files;
+import java.nio.file.Path;
 import java.util.Optional;
 import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.yangtools.concepts.Delegator;
@@ -23,31 +23,31 @@ import org.opendaylight.yangtools.concepts.Delegator;
  *
  * @author Robert Varga
  */
-final class YinTextFileSchemaSource extends YinTextSchemaSource implements Delegator<File> {
-    private final @NonNull File file;
+final class YinTextFileSchemaSource extends YinTextSchemaSource implements Delegator<Path> {
+    private final @NonNull Path path;
 
-    YinTextFileSchemaSource(final @NonNull SourceIdentifier identifier, final @NonNull File file) {
+    YinTextFileSchemaSource(final @NonNull SourceIdentifier identifier, final @NonNull Path path) {
         super(identifier);
-        this.file = requireNonNull(file);
+        this.path = requireNonNull(path);
     }
 
     @Override
-    public File getDelegate() {
-        return file;
+    public Path getDelegate() {
+        return path;
     }
 
     @Override
     public InputStream openStream() throws IOException {
-        return Files.newInputStream(file.toPath());
+        return Files.newInputStream(path);
     }
 
     @Override
     protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
-        return toStringHelper.add("file", file);
+        return toStringHelper.add("path", path);
     }
 
     @Override
     public Optional<String> getSymbolicName() {
-        return Optional.of(file.toString());
+        return Optional.of(path.toString());
     }
 }
index cead5fe4f53ac7e17a146ea4150eddb0d479e424..bddf3373e2b9f35399ab559da8c9eff730ac94b1 100644 (file)
@@ -15,8 +15,9 @@ import com.google.common.base.MoreObjects;
 import com.google.common.base.MoreObjects.ToStringHelper;
 import com.google.common.io.ByteSource;
 import com.google.common.io.Resources;
-import java.io.File;
 import java.io.InputStream;
+import java.nio.file.Files;
+import java.nio.file.Path;
 import java.util.Map.Entry;
 import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.yangtools.yang.common.Revision;
@@ -94,9 +95,9 @@ public abstract class YinTextSchemaSource extends ByteSource implements YinSchem
         return new DelegatedYinTextSchemaSource(identifier, delegate);
     }
 
-    public static @NonNull YinTextSchemaSource forFile(final File file) {
-        checkArgument(file.isFile(), "Supplied file %s is not a file", file);
-        return new YinTextFileSchemaSource(identifierFromFilename(file.getName()), file);
+    public static @NonNull YinTextSchemaSource forPath(final Path path) {
+        checkArgument(Files.isRegularFile(path), "Supplied path %s is not a regular file", path);
+        return new YinTextFileSchemaSource(identifierFromFilename(path.toFile().getName()), path);
     }
 
     public static @NonNull YinTextSchemaSource forResource(final Class<?> clazz, final String resourceName) {
index 1b069ef80f53d8c58ba68fd0e872f9fb47953e46..128be91f42570d687ed13d1cac666cc906a7763e 100644 (file)
@@ -218,16 +218,17 @@ public final class FilesystemSchemaSourceCache<T extends SchemaSourceRepresentat
             storeAsType(file, supportedType.cast(schemaSourceRepresentation));
         }
 
+        // FIXME: use java.nio.filePath
         protected abstract void storeAsType(File file, T cast);
 
-        public T restore(final SourceIdentifier sourceIdentifier, final File cachedSource) {
+        T restore(final SourceIdentifier sourceIdentifier, final File cachedSource) {
             checkArgument(cachedSource.isFile());
             checkArgument(cachedSource.exists());
             checkArgument(cachedSource.canRead());
             return restoreAsType(sourceIdentifier, cachedSource);
         }
 
-        protected abstract T restoreAsType(SourceIdentifier sourceIdentifier, File cachedSource);
+        abstract T restoreAsType(SourceIdentifier sourceIdentifier, File cachedSource);
     }
 
     private static final class YangTextSchemaStorageAdapter extends StorageAdapter<YangTextSchemaSource> {
@@ -249,8 +250,8 @@ public final class FilesystemSchemaSourceCache<T extends SchemaSourceRepresentat
         }
 
         @Override
-        public YangTextSchemaSource restoreAsType(final SourceIdentifier sourceIdentifier, final File cachedSource) {
-            return YangTextSchemaSource.forFile(cachedSource, sourceIdentifier);
+        YangTextSchemaSource restoreAsType(final SourceIdentifier sourceIdentifier, final File cachedSource) {
+            return YangTextSchemaSource.forPath(cachedSource.toPath(), sourceIdentifier);
         }
     }