From: Robert Varga Date: Sun, 7 Jan 2024 14:13:40 +0000 (+0100) Subject: Expose DelegatedYangTextSource X-Git-Tag: v13.0.0~12 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F58%2F109658%2F2;p=yangtools.git Expose DelegatedYangTextSource Eliminate YangTextSource static methods in favor of making DelegatedYangTextSource visible. This also eliminates the slew of possibilitiess down to the bare bones -- users can cope with the trivial conversions from ByteSource and file name. JIRA: YANGTOOLS-1561 Change-Id: I2321e9d936489fefe059399a750393b76730e297 Signed-off-by: Robert Varga --- diff --git a/model/yang-model-spi/src/main/java/org/opendaylight/yangtools/yang/model/spi/source/DelegatedYangTextSource.java b/model/yang-model-spi/src/main/java/org/opendaylight/yangtools/yang/model/spi/source/DelegatedYangTextSource.java index 1f89a73600..d965c8243a 100644 --- a/model/yang-model-spi/src/main/java/org/opendaylight/yangtools/yang/model/spi/source/DelegatedYangTextSource.java +++ b/model/yang-model-spi/src/main/java/org/opendaylight/yangtools/yang/model/spi/source/DelegatedYangTextSource.java @@ -14,29 +14,40 @@ import com.google.common.io.CharSource; import java.io.IOException; import java.io.Reader; 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; -final class DelegatedYangTextSource extends YangTextSource implements Delegator { - private final @NonNull CharSource delegate; - - DelegatedYangTextSource(final SourceIdentifier sourceId, final CharSource delegate) { +/** + * A {@link YangTextSource} delegating to a {@link CharSource}. + */ +@NonNullByDefault +public class DelegatedYangTextSource extends YangTextSource implements Delegator { + private final CharSource delegate; + + /** + * Default constructor. + * + * @param sourceId {@link SourceIdentifier} of the resulting schema source + * @param delegate Backing {@link CharSource} instance + */ + public DelegatedYangTextSource(final SourceIdentifier sourceId, final CharSource delegate) { super(sourceId); this.delegate = requireNonNull(delegate); } @Override - public CharSource getDelegate() { + public final CharSource getDelegate() { return delegate; } @Override - public Reader openStream() throws IOException { + public final Reader openStream() throws IOException { return delegate.openStream(); } @Override - public String symbolicName() { + public final @NonNull String symbolicName() { return "[" + delegate.toString() + "]"; } diff --git a/model/yang-model-spi/src/main/java/org/opendaylight/yangtools/yang/model/spi/source/YangTextSource.java b/model/yang-model-spi/src/main/java/org/opendaylight/yangtools/yang/model/spi/source/YangTextSource.java index 6c565eb90f..9fbdb35a83 100644 --- a/model/yang-model-spi/src/main/java/org/opendaylight/yangtools/yang/model/spi/source/YangTextSource.java +++ b/model/yang-model-spi/src/main/java/org/opendaylight/yangtools/yang/model/spi/source/YangTextSource.java @@ -11,7 +11,6 @@ import static java.util.Objects.requireNonNull; import com.google.common.base.MoreObjects; import com.google.common.base.MoreObjects.ToStringHelper; -import com.google.common.io.ByteSource; import com.google.common.io.CharSource; import com.google.common.io.Resources; import java.io.File; @@ -35,61 +34,6 @@ public abstract class YangTextSource extends CharSource implements YangSourceRep this.sourceId = requireNonNull(sourceId); } - /** - * Create a new YangTextSchemaSource with a specific source identifier and backed - * by ByteSource, which provides the actual InputStreams. - * - * @param identifier SourceIdentifier of the resulting schema source - * @param delegate Backing ByteSource instance - * @param charset Expected character set - * @return A new YangTextSchemaSource - */ - public static @NonNull YangTextSource delegateForByteSource(final SourceIdentifier identifier, - final ByteSource delegate, final Charset charset) { - return delegateForCharSource(identifier, delegate.asCharSource(charset)); - } - - /** - * Create a new YangTextSchemaSource with {@link SourceIdentifier} derived from a supplied filename and backed - * by ByteSource, which provides the actual InputStreams. - * - * @param fileName File name - * @param delegate Backing ByteSource instance - * @return A new YangTextSchemaSource - * @throws IllegalArgumentException if the file name has invalid format - */ - public static @NonNull YangTextSource delegateForByteSource(final String fileName, - final ByteSource delegate, final Charset charset) { - return delegateForCharSource(fileName, delegate.asCharSource(charset)); - } - - /** - * Create a new YangTextSchemaSource with a specific source identifier and backed - * by ByteSource, which provides the actual InputStreams. - * - * @param identifier SourceIdentifier of the resulting schema source - * @param delegate Backing CharSource instance - * @return A new YangTextSchemaSource - */ - public static @NonNull YangTextSource delegateForCharSource(final SourceIdentifier identifier, - final CharSource delegate) { - return new DelegatedYangTextSource(identifier, delegate); - } - - /** - * Create a new YangTextSchemaSource with {@link SourceIdentifier} derived from a supplied filename and backed - * by ByteSource, which provides the actual InputStreams. - * - * @param fileName File name - * @param delegate Backing CharSource instance - * @return A new YangTextSchemaSource - * @throws IllegalArgumentException if the file name has invalid format - */ - public static @NonNull YangTextSource delegateForCharSource(final String fileName, - final CharSource delegate) { - return new DelegatedYangTextSource(SourceIdentifier.ofYangFileName(fileName), delegate); - } - /** * Create a new YangTextSchemaSource backed by a {@link File} with {@link SourceIdentifier} derived from the file * name. @@ -229,7 +173,7 @@ public abstract class YangTextSource extends CharSource implements YangSourceRep * @param toStringHelper ToStringHelper onto the attributes can be added * @return ToStringHelper supplied as input argument. */ - protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) { + protected ToStringHelper addToStringAttributes(final @NonNull ToStringHelper toStringHelper) { return toStringHelper.add("identifier", sourceId); } } diff --git a/parser/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/repo/YangTextSchemaContextResolver.java b/parser/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/repo/YangTextSchemaContextResolver.java index 6667210eff..3e64b0027a 100644 --- a/parser/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/repo/YangTextSchemaContextResolver.java +++ b/parser/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/repo/YangTextSchemaContextResolver.java @@ -49,6 +49,7 @@ import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource; import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource.Costs; import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider; import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry; +import org.opendaylight.yangtools.yang.model.spi.source.DelegatedYangTextSource; 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.YangParserFactory; @@ -131,7 +132,7 @@ public final class YangTextSchemaContextResolver implements AutoCloseable, Schem } } - text = YangTextSource.delegateForCharSource(parsedId, source); + text = new DelegatedYangTextSource(parsedId, source); } else { text = source; } diff --git a/plugin/yang-maven-plugin/src/main/java/org/opendaylight/yangtools/yang2sources/plugin/ScannedDependency.java b/plugin/yang-maven-plugin/src/main/java/org/opendaylight/yangtools/yang2sources/plugin/ScannedDependency.java index efa1db1ea5..8bd3f194db 100644 --- a/plugin/yang-maven-plugin/src/main/java/org/opendaylight/yangtools/yang2sources/plugin/ScannedDependency.java +++ b/plugin/yang-maven-plugin/src/main/java/org/opendaylight/yangtools/yang2sources/plugin/ScannedDependency.java @@ -16,7 +16,6 @@ import com.google.common.annotations.VisibleForTesting; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; import com.google.common.io.ByteSource; -import com.google.common.io.ByteStreams; import java.io.File; import java.io.IOException; import java.nio.charset.StandardCharsets; @@ -28,6 +27,8 @@ import java.util.zip.ZipFile; import org.apache.maven.artifact.Artifact; 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.YangTextSource; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -57,15 +58,14 @@ abstract class ScannedDependency { ImmutableList sources() throws IOException { final var builder = ImmutableList.builderWithExpectedSize(entryNames.size()); - try (ZipFile zip = new ZipFile(file())) { - for (String entryName : entryNames) { - final ZipEntry entry = requireNonNull(zip.getEntry(entryName)); + try (var zip = new ZipFile(file())) { + for (var entryName : entryNames) { + final var entry = requireNonNull(zip.getEntry(entryName)); - builder.add(YangTextSource.delegateForByteSource( - entryName.substring(entryName.lastIndexOf('/') + 1), - // FIXME: can we reasonable make this a CharSource? - ByteSource.wrap(ByteStreams.toByteArray(zip.getInputStream(entry))), - StandardCharsets.UTF_8)); + builder.add(new DelegatedYangTextSource( + SourceIdentifier.ofYangFileName(entryName.substring(entryName.lastIndexOf('/') + 1)), + ByteSource.wrap(zip.getInputStream(entry).readAllBytes()) + .asCharSource(StandardCharsets.UTF_8))); } } diff --git a/plugin/yang-maven-plugin/src/main/java/org/opendaylight/yangtools/yang2sources/plugin/YangToSourcesProcessor.java b/plugin/yang-maven-plugin/src/main/java/org/opendaylight/yangtools/yang2sources/plugin/YangToSourcesProcessor.java index bd06dd3a6b..8297a392e2 100644 --- a/plugin/yang-maven-plugin/src/main/java/org/opendaylight/yangtools/yang2sources/plugin/YangToSourcesProcessor.java +++ b/plugin/yang-maven-plugin/src/main/java/org/opendaylight/yangtools/yang2sources/plugin/YangToSourcesProcessor.java @@ -39,6 +39,7 @@ import org.eclipse.jdt.annotation.NonNull; 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.YangIRSchemaSource; import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource; import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration; @@ -400,8 +401,7 @@ class YangToSourcesProcessor { if (!astSource.sourceId().equals(textSource.sourceId())) { // AST indicates a different source identifier, make sure we use that - sourcesInProject.add(YangTextSource.delegateForCharSource(astSource.sourceId(), - textSource)); + sourcesInProject.add(new DelegatedYangTextSource(astSource.sourceId(), textSource)); } else { sourcesInProject.add(textSource); }