X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=yang%2Fyang-model-api%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Fyang%2Fmodel%2Frepo%2Fapi%2FYangTextSchemaSource.java;h=2d87b9c905c9a7aa7acf6e88933a3898f994dfbf;hb=bf702d417b0dec12e8a9a23aa700ddb7c8b0defb;hp=f609dfc3e26c41668a36462afb3835eaa0e16566;hpb=4e23de902b25ea5cdd8e0e21525f51a30adc61d1;p=yangtools.git 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 f609dfc3e2..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 @@ -9,6 +9,8 @@ package org.opendaylight.yangtools.yang.model.repo.api; import static com.google.common.base.Preconditions.checkArgument; import static java.util.Objects.requireNonNull; +import static org.opendaylight.yangtools.yang.common.YangConstants.RFC6020_YANG_FILE_EXTENSION; +import static org.opendaylight.yangtools.yang.common.YangNames.parseFilename; import com.google.common.annotations.Beta; import com.google.common.base.MoreObjects; @@ -19,31 +21,27 @@ import java.io.File; import java.io.InputStream; import java.net.URL; import java.util.Map.Entry; -import javax.annotation.Nonnull; +import org.eclipse.jdt.annotation.NonNull; import org.opendaylight.yangtools.yang.common.Revision; -import org.opendaylight.yangtools.yang.common.YangConstants; -import org.opendaylight.yangtools.yang.common.YangNames; /** - * YANG text schema source representation. Exposes an RFC6020 or RFC7950 text representation - * as an {@link InputStream}. + * YANG text schema source representation. Exposes an RFC6020 or RFC7950 text representation as an {@link InputStream}. */ @Beta public abstract class YangTextSchemaSource extends ByteSource implements YangSchemaSourceRepresentation { - private final SourceIdentifier identifier; + private final @NonNull SourceIdentifier identifier; protected YangTextSchemaSource(final SourceIdentifier identifier) { this.identifier = requireNonNull(identifier); } - public static SourceIdentifier identifierFromFilename(final String name) { - checkArgument(name.endsWith(YangConstants.RFC6020_YANG_FILE_EXTENSION), - "Filename %s does not have a .yang extension", name); + 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); - final String baseName = name.substring(0, name.length() - YangConstants.RFC6020_YANG_FILE_EXTENSION.length()); - final Entry parsed = YangNames.parseFilename(baseName); - return RevisionSourceIdentifier.create(parsed.getKey(), parsed.getValue() == null ? null - : Revision.valueOf(parsed.getValue())); + final String baseName = name.substring(0, name.length() - RFC6020_YANG_FILE_EXTENSION.length()); + final Entry parsed = parseFilename(baseName); + return RevisionSourceIdentifier.create(parsed.getKey(), Revision.ofNullable(parsed.getValue())); } /** @@ -54,7 +52,7 @@ public abstract class YangTextSchemaSource extends ByteSource implements YangSch * @param delegate Backing ByteSource instance * @return A new YangTextSchemaSource */ - public static YangTextSchemaSource delegateForByteSource(final SourceIdentifier identifier, + public static @NonNull YangTextSchemaSource delegateForByteSource(final SourceIdentifier identifier, final ByteSource delegate) { return new DelegatedYangTextSchemaSource(identifier, delegate); } @@ -68,7 +66,8 @@ public abstract class YangTextSchemaSource extends ByteSource implements YangSch * @return A new YangTextSchemaSource * @throws IllegalArgumentException if the file name has invalid format */ - public static YangTextSchemaSource delegateForByteSource(final String fileName, final ByteSource delegate) { + public static @NonNull YangTextSchemaSource delegateForByteSource(final String fileName, + final ByteSource delegate) { return new DelegatedYangTextSchemaSource(identifierFromFilename(fileName), delegate); } @@ -79,11 +78,24 @@ public abstract class YangTextSchemaSource extends ByteSource implements YangSch * @param file Backing File * @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) { + return forFile(file, identifierFromFilename(file.getName())); + } + + /** + * Create a new YangTextSchemaSource backed by a {@link File} and specified {@link SourceIdentifier}. + * + * @param file Backing File + * @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} */ - public static YangTextSchemaSource forFile(final File file) { - checkArgument(file.isFile(), "Supplied file %s is not a file"); - return new YangTextFileSchemaSource(identifierFromFilename(file.getName()), 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); } /** @@ -94,7 +106,7 @@ public abstract class YangTextSchemaSource extends ByteSource implements YangSch * @return A new instance. * @throws IllegalArgumentException if the resource does not exist or if the name has invalid format */ - public static ResourceYangTextSchemaSource forResource(final String resourceName) { + public static @NonNull YangTextSchemaSource forResource(final String resourceName) { return forResource(YangTextSchemaSource.class, resourceName); } @@ -107,19 +119,30 @@ public abstract class YangTextSchemaSource extends ByteSource implements YangSch * @return A new instance. * @throws IllegalArgumentException if the resource does not exist or if the name has invalid format */ - public static ResourceYangTextSchemaSource forResource(final Class clazz, final String resourceName) { + public static @NonNull YangTextSchemaSource forResource(final Class clazz, final String resourceName) { final String fileName = resourceName.substring(resourceName.lastIndexOf('/') + 1); final SourceIdentifier identifier = identifierFromFilename(fileName); final URL url = Resources.getResource(clazz, resourceName); 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; } - @Nonnull @Override public Class getType() { return YangTextSchemaSource.class;