From: Robert Varga Date: Wed, 21 Apr 2021 05:47:24 +0000 (+0200) Subject: Do not subclass YangTextSchemaSource in yang-parser-impl X-Git-Tag: v6.0.6~3 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=yangtools.git;a=commitdiff_plain;h=bf702d417b0dec12e8a9a23aa700ddb7c8b0defb Do not subclass YangTextSchemaSource in yang-parser-impl We have a readily-available URL-based implementation, reuse it instead of rolling our own. JIRA: YANGTOOLS-1275 Change-Id: I43a6215e8dbe678966c80d3d05dde5a44c8e7833 Signed-off-by: Robert Varga (cherry picked from commit 3310cd312a6419064b7a2850865e434970ba5a67) --- diff --git a/yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/repo/api/YangTextSchemaSource.java b/yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/repo/api/YangTextSchemaSource.java index d21fdcb021..2d87b9c905 100644 --- a/yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/repo/api/YangTextSchemaSource.java +++ b/yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/repo/api/YangTextSchemaSource.java @@ -126,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; diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/repo/YangTextSchemaContextResolver.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/repo/YangTextSchemaContextResolver.java index 3606cc3876..6cdcaac81c 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/repo/YangTextSchemaContextResolver.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/repo/YangTextSchemaContextResolver.java @@ -13,7 +13,6 @@ import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediate import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFluentFuture; import com.google.common.annotations.Beta; -import com.google.common.base.MoreObjects.ToStringHelper; import com.google.common.base.Verify; import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.ImmutableSet; @@ -21,7 +20,6 @@ import com.google.common.collect.Multimap; import com.google.common.util.concurrent.FluentFuture; import com.google.common.util.concurrent.ListenableFuture; import java.io.IOException; -import java.io.InputStream; import java.net.URL; import java.util.Collection; import java.util.Optional; @@ -177,23 +175,7 @@ public final class YangTextSchemaContextResolver implements AutoCloseable, Schem final String path = url.getPath(); final String fileName = path.substring(path.lastIndexOf('/') + 1); - final SourceIdentifier guessedId = guessSourceIdentifier(fileName); - return registerSource(new YangTextSchemaSource(guessedId) { - @Override - public InputStream openStream() throws IOException { - return url.openStream(); - } - - @Override - public Optional getSymbolicName() { - return Optional.of(url.toString()); - } - - @Override - protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) { - return toStringHelper.add("url", url); - } - }); + return registerSource(YangTextSchemaSource.forURL(url, guessSourceIdentifier(fileName))); } private static SourceIdentifier guessSourceIdentifier(final @NonNull String fileName) { @@ -275,7 +257,7 @@ public final class YangTextSchemaContextResolver implements AutoCloseable, Schem } @Override - public synchronized FluentFuture getSource( + public synchronized @NonNull FluentFuture getSource( final SourceIdentifier sourceIdentifier) { final Collection ret = texts.get(sourceIdentifier);