}
}
- text = YangTextSchemaSource.delegateForByteSource(parsedId, source);
+ text = YangTextSchemaSource.delegateForCharSource(parsedId, source);
} else {
text = source;
}
import com.google.common.annotations.Beta;
import java.io.IOException;
-import java.io.InputStream;
+import java.io.Reader;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.opendaylight.yangtools.concepts.AbstractSimpleIdentifiable;
static StatementContext parseYangSource(final YangTextSchemaSource source)
throws IOException, YangSyntaxErrorException {
- try (InputStream stream = source.openStream()) {
- return parseYangSource(source.getIdentifier(), stream);
+ try (var reader = source.openStream()) {
+ return parseYangSource(source.getIdentifier(), reader);
}
}
- private static StatementContext parseYangSource(final SourceIdentifier source, final InputStream stream)
+ private static StatementContext parseYangSource(final SourceIdentifier source, final Reader stream)
throws IOException, YangSyntaxErrorException {
- final YangStatementLexer lexer = new CompactYangStatementLexer(CharStreams.fromStream(stream));
+ final YangStatementLexer lexer = new CompactYangStatementLexer(CharStreams.fromReader(stream));
final YangStatementParser parser = new YangStatementParser(new CommonTokenStream(lexer));
// disconnect from console error output
lexer.removeErrorListeners();
import static com.google.common.base.Preconditions.checkArgument;
import static java.util.Objects.requireNonNull;
-import com.google.common.io.CharSource;
-import java.io.IOException;
-import java.io.InputStream;
-import java.nio.charset.StandardCharsets;
+import java.io.StringReader;
import java.util.Optional;
import org.eclipse.jdt.annotation.NonNull;
import org.opendaylight.yangtools.yang.common.UnresolvedQName;
}
@Override
- public InputStream openStream() throws IOException {
- return CharSource.wrap(sourceString).asByteSource(StandardCharsets.UTF_8).openStream();
+ public StringReader openStream() {
+ return new StringReader(sourceString);
}
@Override
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.io.IOException;
import java.io.Reader;
-import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
for (ScannedDependency dependency : dependencies) {
for (YangTextSchemaSource s : dependency.sources()) {
- try (Reader reader = s.asCharSource(StandardCharsets.UTF_8).openStream()) {
+ try (Reader reader = s.openStream()) {
final String contents = CharStreams.toString(reader);
byContent.putIfAbsent(contents, s);
}
import com.google.common.io.ByteStreams;
import java.io.File;
import java.io.IOException;
+import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
builder.add(YangTextSchemaSource.delegateForByteSource(
entryName.substring(entryName.lastIndexOf('/') + 1),
- ByteSource.wrap(ByteStreams.toByteArray(zip.getInputStream(entry)))));
+ // FIXME: can we reasonable make this a CharSource?
+ ByteSource.wrap(ByteStreams.toByteArray(zip.getInputStream(entry))),
+ StandardCharsets.UTF_8));
}
}
import com.google.common.collect.Maps;
import java.io.File;
import java.io.IOException;
+import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
final var stateListBuilder = ImmutableList.<FileState>builderWithExpectedSize(modelsInProject.size());
for (var source : modelsInProject) {
final File file = new File(withMetaInf, source.getIdentifier().toYangFilename());
- stateListBuilder.add(FileState.ofWrittenFile(file, source::copyTo));
+ stateListBuilder.add(FileState.ofWrittenFile(file,
+ out -> source.asByteSource(StandardCharsets.UTF_8).copyTo(out)));
LOG.debug("Created file {} for {}", file, source.getIdentifier());
}
if (!astSource.getIdentifier().equals(textSource.getIdentifier())) {
// AST indicates a different source identifier, make sure we use that
- sourcesInProject.add(YangTextSchemaSource.delegateForByteSource(astSource.getIdentifier(),
+ sourcesInProject.add(YangTextSchemaSource.delegateForCharSource(astSource.getIdentifier(),
textSource));
} else {
sourcesInProject.add(textSource);
import static java.util.Objects.requireNonNull;
import com.google.common.base.MoreObjects.ToStringHelper;
-import com.google.common.io.ByteSource;
+import com.google.common.io.CharSource;
import java.io.IOException;
-import java.io.InputStream;
+import java.io.Reader;
import java.util.Optional;
import org.eclipse.jdt.annotation.NonNull;
import org.opendaylight.yangtools.concepts.Delegator;
-final class DelegatedYangTextSchemaSource extends YangTextSchemaSource implements Delegator<ByteSource> {
- private final @NonNull ByteSource delegate;
+final class DelegatedYangTextSchemaSource extends YangTextSchemaSource implements Delegator<CharSource> {
+ private final @NonNull CharSource delegate;
- DelegatedYangTextSchemaSource(final SourceIdentifier identifier, final ByteSource delegate) {
+ DelegatedYangTextSchemaSource(final SourceIdentifier identifier, final CharSource delegate) {
super(identifier);
this.delegate = requireNonNull(delegate);
}
@Override
- public ByteSource getDelegate() {
+ public CharSource getDelegate() {
return delegate;
}
@Override
- public InputStream openStream() throws IOException {
+ public Reader openStream() throws IOException {
return delegate.openStream();
}
import com.google.common.base.MoreObjects.ToStringHelper;
import java.io.IOException;
-import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
import java.net.URL;
+import java.nio.charset.Charset;
import java.util.Optional;
import org.eclipse.jdt.annotation.NonNull;
import org.opendaylight.yangtools.concepts.Delegator;
*/
final class ResourceYangTextSchemaSource extends YangTextSchemaSource implements Delegator<URL> {
private final @NonNull URL url;
+ private final @NonNull Charset charset;
- ResourceYangTextSchemaSource(final SourceIdentifier identifier, final URL url) {
+ ResourceYangTextSchemaSource(final SourceIdentifier identifier, final URL url, final Charset charset) {
super(identifier);
this.url = requireNonNull(url);
+ this.charset = requireNonNull(charset);
}
@Override
}
@Override
- public InputStream openStream() throws IOException {
- return url.openStream();
+ public Reader openStream() throws IOException {
+ return new InputStreamReader(url.openStream(), charset);
}
@Override
*/
package org.opendaylight.yangtools.yang.model.repo.api;
-import java.io.ByteArrayInputStream;
-import java.io.InputStream;
+import static java.util.Objects.requireNonNull;
+
+import java.io.Reader;
+import java.io.StringReader;
import java.util.Optional;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
/**
* A {@link YangTextSchemaSource} with content readily available.
*/
-public class ImmediateYangTextSchemaSource extends YangTextSchemaSource {
+public class StringYangTextSchemaSource extends YangTextSchemaSource {
private final @Nullable String symbolicName;
- private final byte @NonNull [] bytes;
+ private final @NonNull String content;
- public ImmediateYangTextSchemaSource(final SourceIdentifier identifier, final byte[] bytes) {
- this(identifier, bytes, null);
+ public StringYangTextSchemaSource(final SourceIdentifier identifier, final String content) {
+ this(identifier, content, null);
}
- public ImmediateYangTextSchemaSource(final SourceIdentifier identifier, final byte[] bytes,
+ public StringYangTextSchemaSource(final SourceIdentifier identifier, final String content,
final @Nullable String symbolicName) {
super(identifier);
- this.bytes = bytes.clone();
+ this.content = requireNonNull(content);
this.symbolicName = symbolicName;
}
}
@Override
- public final InputStream openStream() {
- return new ByteArrayInputStream(bytes);
+ public final Reader openStream() {
+ return new StringReader(content);
}
}
import com.google.common.base.MoreObjects.ToStringHelper;
import java.io.IOException;
-import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Optional;
*/
final class YangTextFileSchemaSource extends YangTextSchemaSource implements Delegator<Path> {
private final @NonNull Path path;
+ private final @NonNull Charset charset;
- YangTextFileSchemaSource(final SourceIdentifier identifier, final Path path) {
+ YangTextFileSchemaSource(final SourceIdentifier identifier, final Path path, final Charset charset) {
super(identifier);
this.path = requireNonNull(path);
+ this.charset = requireNonNull(charset);
}
@Override
}
@Override
- public InputStream openStream() throws IOException {
- return Files.newInputStream(path);
+ public Reader openStream() throws IOException {
+ return new InputStreamReader(Files.newInputStream(path), charset);
}
@Override
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;
import java.io.InputStream;
import java.net.URL;
+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;
* 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 {
+public abstract class YangTextSchemaSource extends CharSource implements YangSchemaSourceRepresentation {
private final @NonNull SourceIdentifier identifier;
protected YangTextSchemaSource(final SourceIdentifier identifier) {
*
* @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 YangTextSchemaSource delegateForByteSource(final SourceIdentifier identifier,
- final ByteSource delegate) {
- return new DelegatedYangTextSchemaSource(identifier, delegate);
+ final ByteSource delegate, final Charset charset) {
+ return delegateForCharSource(identifier, delegate.asCharSource(charset));
}
/**
* @throws IllegalArgumentException if the file name has invalid format
*/
public static @NonNull YangTextSchemaSource delegateForByteSource(final String fileName,
- final ByteSource delegate) {
+ 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 YangTextSchemaSource delegateForCharSource(final SourceIdentifier identifier,
+ final CharSource delegate) {
+ return new DelegatedYangTextSchemaSource(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 YangTextSchemaSource delegateForCharSource(final String fileName,
+ final CharSource delegate) {
return new DelegatedYangTextSchemaSource(identifierFromFilename(fileName), delegate);
}
* @throws IllegalArgumentException if the supplied path is not a regular file
*/
public static @NonNull YangTextSchemaSource 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 YangTextSchemaSource forPath(final Path path, final SourceIdentifier identifier,
+ final Charset charset) {
checkArgument(Files.isRegularFile(path), "Supplied path %s is not a regular file", path);
- return new YangTextFileSchemaSource(identifier, path);
+ return new YangTextFileSchemaSource(identifier, path, charset);
}
/**
* @throws IllegalArgumentException if the resource does not exist or if the name has invalid format
*/
public static @NonNull YangTextSchemaSource forResource(final Class<?> clazz, final String resourceName) {
+ return forResource(clazz, resourceName, StandardCharsets.UTF_8);
+ }
+
+ /**
+ * Create a new {@link YangTextSchemaSource} backed by a resource by a resource available on the ClassLoader
+ * which loaded the specified class.
+ *
+ * @param clazz Class reference
+ * @param resourceName Resource name
+ * @param charset Expected character set
+ * @return A new instance.
+ * @throws IllegalArgumentException if the resource does not exist or if the name has invalid format
+ */
+ public static @NonNull YangTextSchemaSource forResource(final Class<?> clazz, final String resourceName,
+ final Charset charset) {
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);
+ return new ResourceYangTextSchemaSource(identifier, url, charset);
}
+
/**
* Create a new {@link YangTextSchemaSource} backed by a URL.
*
* @throws NullPointerException if any argument is {@code null}
*/
public static @NonNull YangTextSchemaSource forURL(final URL url, final SourceIdentifier identifier) {
- return new ResourceYangTextSchemaSource(identifier, url);
+ return forURL(url, identifier, StandardCharsets.UTF_8);
}
+ /**
+ * Create a new {@link YangTextSchemaSource} backed by a URL.
+ *
+ * @param url Backing URL
+ * @param identifier Source identifier
+ * @param charset Expected character set
+ * @return A new instance.
+ * @throws NullPointerException if any argument is {@code null}
+ */
+ public static @NonNull YangTextSchemaSource forURL(final URL url, final SourceIdentifier identifier,
+ final Charset charset) {
+ return new ResourceYangTextSchemaSource(identifier, url, charset);
+ }
+
+
@Override
public final SourceIdentifier getIdentifier() {
return identifier;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
-import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
@SuppressFBWarnings(value = "RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE",
justification = "https://github.com/spotbugs/spotbugs/issues/600")
protected void storeAsType(final File file, final YangTextSchemaSource cast) {
- try (InputStream castStream = cast.openStream()) {
+ try (var castStream = cast.asByteSource(StandardCharsets.UTF_8).openStream()) {
Files.copy(castStream, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
} catch (final IOException e) {
throw new IllegalStateException("Cannot store schema source " + cast.getIdentifier() + " to " + file,
import com.google.common.io.Files;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
-import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
-import java.io.InputStream;
+import java.io.Reader;
+import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
}
@Override
- public InputStream openStream() throws IOException {
- return new ByteArrayInputStream("running".getBytes(StandardCharsets.UTF_8));
+ public Reader openStream() throws IOException {
+ return new StringReader("running");
}
@Override
import com.google.common.base.MoreObjects;
import com.google.common.collect.Collections2;
import com.google.common.util.concurrent.ListenableFuture;
-import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
-import java.io.InputStream;
-import java.nio.charset.StandardCharsets;
+import java.io.Reader;
+import java.io.StringReader;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.Collection;
}
@Override
- public InputStream openStream() throws IOException {
- return new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8));
+ public Reader openStream() throws IOException {
+ return new StringReader(content);
}
@Override
import static org.mockito.Mockito.doReturn;
import com.google.common.base.MoreObjects.ToStringHelper;
-import java.io.ByteArrayInputStream;
-import java.io.InputStream;
-import java.nio.charset.StandardCharsets;
+import java.io.Reader;
+import java.io.StringReader;
import java.util.Optional;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
}
@Override
- public InputStream openStream() {
- return new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8));
+ public Reader openStream() {
+ return new StringReader(content);
}
@Override
import static org.mockito.Mockito.doReturn;
import com.google.common.base.MoreObjects.ToStringHelper;
-import java.io.ByteArrayInputStream;
-import java.io.InputStream;
-import java.nio.charset.StandardCharsets;
+import java.io.Reader;
+import java.io.StringReader;
import java.util.Optional;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
}
@Override
- public InputStream openStream() {
- return new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8));
+ public Reader openStream() {
+ return new StringReader(content);
}
@Override