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<CharSource> {
- 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<CharSource> {
+ 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() + "]";
}
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;
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.
* @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);
}
}
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;
}
}
- text = YangTextSource.delegateForCharSource(parsedId, source);
+ text = new DelegatedYangTextSource(parsedId, source);
} else {
text = source;
}
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;
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;
ImmutableList<YangTextSource> sources() throws IOException {
final var builder = ImmutableList.<YangTextSource>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)));
}
}
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;
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);
}