Expose FileYangTextSource 64/109664/4
authorRobert Varga <robert.varga@pantheon.tech>
Sun, 7 Jan 2024 16:55:03 +0000 (17:55 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Sun, 7 Jan 2024 17:50:04 +0000 (18:50 +0100)
Rather than having a plethora static factory methods, just expose
FileYangTextSource and use it as appropriate.

JIRA: YANGTOOLS-1561
Change-Id: I0efbd6bdcff32bb0079a5b974797320cbc3c88fe
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
model/yang-model-spi/src/main/java/org/opendaylight/yangtools/yang/model/spi/source/FileYangTextSource.java [moved from model/yang-model-spi/src/main/java/org/opendaylight/yangtools/yang/model/spi/source/YangTextFileSource.java with 52% similarity]
model/yang-model-spi/src/main/java/org/opendaylight/yangtools/yang/model/spi/source/YangTextSource.java
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-fs/src/main/java/org/opendaylight/yangtools/yang/model/repo/fs/FilesystemSchemaSourceCache.java

similarity index 52%
rename from model/yang-model-spi/src/main/java/org/opendaylight/yangtools/yang/model/spi/source/YangTextFileSource.java
rename to model/yang-model-spi/src/main/java/org/opendaylight/yangtools/yang/model/spi/source/FileYangTextSource.java
index 06890023dda1770619359fe0351b689841360dc4..2b7ca4b058bd46629e313dc0fdefadcee3ccbe8c 100644 (file)
@@ -14,44 +14,70 @@ import java.io.IOException;
 import java.io.InputStreamReader;
 import java.io.Reader;
 import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.opendaylight.yangtools.concepts.Delegator;
 import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
 
 /**
  * A {@link YangTextSource} backed by a file.
  */
-final class YangTextFileSource extends YangTextSource implements Delegator<Path> {
-    private final @NonNull Path path;
-    private final @NonNull Charset charset;
+@NonNullByDefault
+public class FileYangTextSource extends YangTextSource implements Delegator<Path> {
+    private final Path path;
+    private final Charset charset;
 
-    YangTextFileSource(final SourceIdentifier sourceId, final Path path, final Charset charset) {
+    /**
+     * Default constructor.
+     *
+     * @param path Backing path
+     * @param sourceId source identifier
+     * @param charset expected stream character set
+     * @throws NullPointerException if any argument is {@code null}
+     * @throws IllegalArgumentException if the supplied path is not a regular file
+     */
+    public FileYangTextSource(final SourceIdentifier sourceId, final Path path, final Charset charset) {
         super(sourceId);
+        if (!Files.isRegularFile(path)) {
+            throw new IllegalArgumentException("Supplied path " + path + " is not a regular file");
+        }
         this.path = requireNonNull(path);
         this.charset = requireNonNull(charset);
     }
 
-    @Override
-    public Path getDelegate() {
-        return path;
+    /**
+     * Utility constructor. Derives the {@link SourceIdentifier} from {@link Path} and assumes UTF-8 encoding.
+     *
+     * @param path backing path
+     * @throws NullPointerException if {@code path} is {@code null}
+     */
+    public FileYangTextSource(final Path path) {
+        // FIXME: do not use .toFile() here
+        this(SourceIdentifier.ofYangFileName(path.toFile().getName()), path, StandardCharsets.UTF_8);
     }
 
     @Override
-    public Reader openStream() throws IOException {
-        return new InputStreamReader(Files.newInputStream(path), charset);
+    public final Path getDelegate() {
+        return path;
     }
 
     @Override
-    protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
-        return super.addToStringAttributes(toStringHelper).add("path", path);
+    public final Reader openStream() throws IOException {
+        return new InputStreamReader(Files.newInputStream(path), charset);
     }
 
     @Override
-    public String symbolicName() {
+    public final @NonNull String symbolicName() {
         // 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 path.toString();
     }
+
+    @Override
+    protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
+        return super.addToStringAttributes(toStringHelper).add("path", path);
+    }
 }
index 1e52e39b8f81a8704cb211f97e272bc0dfe25c2e..785a611aa8ae7e03ccac31a30c6dd628767e5755 100644 (file)
@@ -12,12 +12,7 @@ import static java.util.Objects.requireNonNull;
 import com.google.common.base.MoreObjects;
 import com.google.common.base.MoreObjects.ToStringHelper;
 import com.google.common.io.CharSource;
-import java.io.File;
 import java.io.InputStream;
-import java.nio.charset.Charset;
-import java.nio.charset.StandardCharsets;
-import java.nio.file.Files;
-import java.nio.file.Path;
 import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
 import org.opendaylight.yangtools.yang.model.api.source.YangSourceRepresentation;
@@ -32,51 +27,6 @@ public abstract class YangTextSource extends CharSource implements YangSourceRep
         this.sourceId = requireNonNull(sourceId);
     }
 
-    /**
-     * Create a new YangTextSchemaSource backed by a {@link File} with {@link SourceIdentifier} derived from the file
-     * name.
-     *
-     * @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 YangTextSource forPath(final Path path) {
-        // FIXME: do not use .toFile() here
-        return forPath(path, SourceIdentifier.ofYangFileName(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 YangTextSource forPath(final Path path, final SourceIdentifier identifier) {
-        return forPath(path, identifier, StandardCharsets.UTF_8);
-    }
-
-    /**
-     * Create a new YangTextSchemaSource backed by a {@link File} and specified {@link SourceIdentifier}.
-     *
-     * @param path Backing path
-     * @param identifier Source identifier
-     * @param charset expected stream character set
-     * @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 YangTextSource forPath(final Path path, final SourceIdentifier identifier,
-            final Charset charset) {
-        if (Files.isRegularFile(path)) {
-            return new YangTextFileSource(identifier, path, charset);
-        }
-        throw new IllegalArgumentException("Supplied path " + path + " is not a regular file");
-    }
-
     @Override
     public final Class<YangTextSource> getType() {
         return YangTextSource.class;
index 4adf764cc8996a0ad503da1619126bb12a1cbd3a..05dbfbbd1bef9e01d5580f05cafb100c8f0e0160 100644 (file)
@@ -12,7 +12,6 @@ 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;
@@ -24,8 +23,9 @@ import org.opendaylight.yangtools.yang.model.api.Module;
 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.opendaylight.yangtools.yang.model.api.stmt.FeatureSet;
+import org.opendaylight.yangtools.yang.model.spi.source.FileYangTextSource;
 import org.opendaylight.yangtools.yang.model.spi.source.FileYinTextSource;
-import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource;
+import org.opendaylight.yangtools.yang.model.spi.source.URLYangTextSource;
 import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration;
 import org.opendaylight.yangtools.yang.parser.api.YangSyntaxErrorException;
 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
@@ -65,9 +65,9 @@ public final class StmtTestUtils {
 
     public static YangStatementStreamSource sourceForResource(final String resourceName) {
         try {
-            return YangStatementStreamSource.create(YangTextSource.forPath(Path.of(
-                StmtTestUtils.class.getResource(resourceName).toURI())));
-        } catch (IOException | YangSyntaxErrorException | URISyntaxException e) {
+            return YangStatementStreamSource.create(new URLYangTextSource(
+                StmtTestUtils.class.getResource(resourceName)));
+        } catch (IOException | YangSyntaxErrorException e) {
             throw new IllegalArgumentException("Failed to create source", e);
         }
     }
@@ -110,14 +110,12 @@ public final class StmtTestUtils {
     }
 
     public static EffectiveModelContext parseYangSources(final YangParserConfiguration config,
-            final Set<QName> supportedFeatures, final File... files) throws  ReactorException, IOException,
-            YangSyntaxErrorException {
-
-        final Collection<YangStatementStreamSource> sources = new ArrayList<>(files.length);
-        for (File file : files) {
-            sources.add(YangStatementStreamSource.create(YangTextSource.forPath(file.toPath())));
+            final Set<QName> supportedFeatures, final File... files)
+                throws ReactorException, IOException, YangSyntaxErrorException {
+        final var sources = new ArrayList<YangStatementStreamSource>(files.length);
+        for (var file : files) {
+            sources.add(YangStatementStreamSource.create(new FileYangTextSource(file.toPath())));
         }
-
         return parseYangSources(config, supportedFeatures, sources);
     }
 
index eec1dff6987733821e2004fbbcd66ca931556397..52ae77c0200d30d16a72157c8e07334b2da9664e 100644 (file)
@@ -10,8 +10,6 @@ package org.opendaylight.yangtools.yang.stmt;
 import java.io.File;
 import java.io.IOException;
 import java.net.URI;
-import java.net.URISyntaxException;
-import java.nio.file.Path;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
@@ -25,7 +23,9 @@ import org.opendaylight.yangtools.yang.model.api.ModuleImport;
 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.source.YinTextSource;
 import org.opendaylight.yangtools.yang.model.api.stmt.FeatureSet;
+import org.opendaylight.yangtools.yang.model.spi.source.FileYangTextSource;
 import org.opendaylight.yangtools.yang.model.spi.source.FileYinTextSource;
+import org.opendaylight.yangtools.yang.model.spi.source.URLYangTextSource;
 import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource;
 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YangStatementStreamSource;
@@ -48,11 +48,12 @@ public final class TestUtils {
 
     public static @NonNull List<StatementStreamSource> loadSources(final Class<?> cls, final String resourceDirectory)
             throws Exception {
+        // FIXME: use Path instead
         final var files = new File(cls.getResource(resourceDirectory).toURI())
             .listFiles(StmtTestUtils.YANG_FILE_FILTER);
         final var sources = new ArrayList<StatementStreamSource>(files.length);
         for (var file : files) {
-            sources.add(YangStatementStreamSource.create(YangTextSource.forPath(file.toPath())));
+            sources.add(YangStatementStreamSource.create(new FileYangTextSource(file.toPath())));
         }
         return sources;
     }
@@ -98,11 +99,7 @@ public final class TestUtils {
     }
 
     public static YangTextSource assertSchemaSource(final String resourcePath) {
-        try {
-            return YangTextSource.forPath(Path.of(TestUtils.class.getResource(resourcePath).toURI()));
-        } catch (URISyntaxException e) {
-            throw new AssertionError(e);
-        }
+        return new URLYangTextSource(TestUtils.class.getResource(resourcePath));
     }
 
     // FIXME: these remain unaudited
index 74fd6e2c34534c02ccd7deb2c16eb583ce5a1e1d..48e0035d55b521e641ba5c688cf77b32e6f087fa 100644 (file)
@@ -21,13 +21,13 @@ import java.util.List;
 import java.util.Locale;
 import java.util.ServiceLoader;
 import java.util.Set;
-import java.util.stream.Collectors;
 import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.YangConstants;
 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
 import org.opendaylight.yangtools.yang.model.api.source.SourceRepresentation;
 import org.opendaylight.yangtools.yang.model.api.stmt.FeatureSet;
+import org.opendaylight.yangtools.yang.model.spi.source.FileYangTextSource;
 import org.opendaylight.yangtools.yang.model.spi.source.URLYangTextSource;
 import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource;
 import org.opendaylight.yangtools.yang.parser.api.YangParser;
@@ -202,7 +202,7 @@ public final class YangParserTestUtils {
     public static EffectiveModelContext parseYangFiles(final Set<QName> supportedFeatures,
             final YangParserConfiguration config, final Collection<File> files) {
         return parseSources(config, supportedFeatures,
-            files.stream().map(file -> YangTextSource.forPath(file.toPath())).collect(Collectors.toList()));
+            files.stream().map(file -> new FileYangTextSource(file.toPath())).toList());
     }
 
     /**
index 8bd3f194dbb6b17103e7504565673b824e3386e8..6a841f2a9a3857a341affbc558fc0bf2fcdf419c 100644 (file)
@@ -29,6 +29,7 @@ import org.apache.maven.project.MavenProject;
 import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
 import org.opendaylight.yangtools.yang.model.spi.source.DelegatedYangTextSource;
+import org.opendaylight.yangtools.yang.model.spi.source.FileYangTextSource;
 import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -42,7 +43,7 @@ abstract class ScannedDependency {
 
         @Override
         ImmutableList<YangTextSource> sources() {
-            return ImmutableList.of(YangTextSource.forPath(file().toPath()));
+            return ImmutableList.of(new FileYangTextSource(file().toPath()));
         }
     }
 
index 8297a392e2e04fe863f52773ff3fa0daf20b3703..f9769f9ba8b4b4efe07160dea548606bf1b4b3dc 100644 (file)
@@ -40,6 +40,7 @@ import org.opendaylight.yangtools.plugin.generator.api.FileGeneratorException;
 import org.opendaylight.yangtools.plugin.generator.api.FileGeneratorFactory;
 import org.opendaylight.yangtools.yang.common.YangConstants;
 import org.opendaylight.yangtools.yang.model.spi.source.DelegatedYangTextSource;
+import org.opendaylight.yangtools.yang.model.spi.source.FileYangTextSource;
 import org.opendaylight.yangtools.yang.model.spi.source.YangIRSchemaSource;
 import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource;
 import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration;
@@ -245,9 +246,9 @@ class YangToSourcesProcessor {
 
         final Stopwatch watch = Stopwatch.createStarted();
 
-        final List<Entry<YangTextSource, YangIRSchemaSource>> parsed = yangFilesInProject.parallelStream()
+        final var parsed = yangFilesInProject.parallelStream()
             .map(file -> {
-                final var textSource = YangTextSource.forPath(file.toPath());
+                final var textSource = new FileYangTextSource(file.toPath());
                 try {
                     return Map.entry(textSource, TextToIRTransformer.transformText(textSource));
                 } catch (YangSyntaxErrorException | IOException e) {
@@ -389,7 +390,7 @@ class YangToSourcesProcessor {
     @SuppressWarnings("checkstyle:illegalCatch")
     private @NonNull ProcessorModuleReactor createReactor(final List<File> yangFilesInProject,
             final YangParserConfiguration parserConfig, final Collection<ScannedDependency> dependencies,
-            final List<Entry<YangTextSource, YangIRSchemaSource>> parsed) throws MojoExecutionException {
+            final List<Entry<FileYangTextSource, YangIRSchemaSource>> parsed) throws MojoExecutionException {
 
         try {
             final var sourcesInProject = new ArrayList<YangTextSource>(yangFilesInProject.size());
index b74a6287c140b501148d4d289106f549763fa789..e927855ac8946af4ea954c111a2cdba607bcb0c3 100644 (file)
@@ -33,8 +33,7 @@ import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.YangConstants;
 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
 import org.opendaylight.yangtools.yang.model.api.stmt.FeatureSet;
-import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource;
-import org.opendaylight.yangtools.yang.parser.api.YangParser;
+import org.opendaylight.yangtools.yang.model.spi.source.FileYangTextSource;
 import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration;
 import org.opendaylight.yangtools.yang.parser.api.YangParserException;
 import org.opendaylight.yangtools.yang.parser.api.YangParserFactory;
@@ -94,18 +93,17 @@ final class SystemTestUtils {
             final List<File> libFiles,  final boolean warnForUnkeyedLists) throws IOException, YangParserException {
         checkArgument(!testFiles.isEmpty(), "No yang sources");
 
-        final YangParserConfiguration configuration = YangParserConfiguration.builder()
-                .warnForUnkeyedLists(warnForUnkeyedLists).build();
-        final YangParser parser = PARSER_FACTORY.createParser(configuration);
+        final var configuration = YangParserConfiguration.builder().warnForUnkeyedLists(warnForUnkeyedLists).build();
+        final var parser = PARSER_FACTORY.createParser(configuration);
         if (supportedFeatures != null) {
             parser.setSupportedFeatures(FeatureSet.of(supportedFeatures));
         }
 
-        for (File file : testFiles) {
-            parser.addSource(YangTextSource.forPath(file.toPath()));
+        for (var file : testFiles) {
+            parser.addSource(new FileYangTextSource(file.toPath()));
         }
-        for (File file : libFiles) {
-            parser.addLibSource(YangTextSource.forPath(file.toPath()));
+        for (var file : libFiles) {
+            parser.addLibSource(new FileYangTextSource(file.toPath()));
         }
 
         return parser.buildEffectiveModel();
index 1e5a56ad9002a1a3d4967eff809120861a96b6df..570a957393832bb4229821055ab7f034fbf7051c 100644 (file)
@@ -39,6 +39,7 @@ import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceExcepti
 import org.opendaylight.yangtools.yang.model.repo.spi.AbstractSchemaSourceCache;
 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource.Costs;
 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
+import org.opendaylight.yangtools.yang.model.spi.source.FileYangTextSource;
 import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -238,7 +239,7 @@ public final class FilesystemSchemaSourceCache<T extends SourceRepresentation> e
 
         @Override
         YangTextSource restoreAsType(final SourceIdentifier sourceIdentifier, final File cachedSource) {
-            return YangTextSource.forPath(cachedSource.toPath(), sourceIdentifier);
+            return new FileYangTextSource(sourceIdentifier, cachedSource.toPath(), StandardCharsets.UTF_8);
         }
     }