import java.io.InputStreamReader;
import java.io.Reader;
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;
+import org.eclipse.jdt.annotation.NonNullByDefault;
import org.opendaylight.yangtools.concepts.Delegator;
import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
/**
* A {@link YangTextSource} backed by a file.
*/
-final class YangTextFileSource extends YangTextSource implements Delegator<Path> {
- private final @NonNull Path path;
- private final @NonNull Charset charset;
+@NonNullByDefault
+public class FileYangTextSource extends YangTextSource implements Delegator<Path> {
+ private final Path path;
+ private final Charset charset;
- YangTextFileSource(final SourceIdentifier sourceId, final Path path, final Charset charset) {
+ /**
+ * Default constructor.
+ *
+ * @param path Backing path
+ * @param sourceId source identifier
+ * @param charset expected stream character set
+ * @throws NullPointerException if any argument is {@code null}
+ * @throws IllegalArgumentException if the supplied path is not a regular file
+ */
+ public FileYangTextSource(final SourceIdentifier sourceId, final Path path, final Charset charset) {
super(sourceId);
+ if (!Files.isRegularFile(path)) {
+ throw new IllegalArgumentException("Supplied path " + path + " is not a regular file");
+ }
this.path = requireNonNull(path);
this.charset = requireNonNull(charset);
}
- @Override
- public Path getDelegate() {
- return path;
+ /**
+ * Utility constructor. Derives the {@link SourceIdentifier} from {@link Path} and assumes UTF-8 encoding.
+ *
+ * @param path backing path
+ * @throws NullPointerException if {@code path} is {@code null}
+ */
+ public FileYangTextSource(final Path path) {
+ // FIXME: do not use .toFile() here
+ this(SourceIdentifier.ofYangFileName(path.toFile().getName()), path, StandardCharsets.UTF_8);
}
@Override
- public Reader openStream() throws IOException {
- return new InputStreamReader(Files.newInputStream(path), charset);
+ public final Path getDelegate() {
+ return path;
}
@Override
- protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
- return super.addToStringAttributes(toStringHelper).add("path", path);
+ public final Reader openStream() throws IOException {
+ return new InputStreamReader(Files.newInputStream(path), charset);
}
@Override
- public String symbolicName() {
+ public final @NonNull String symbolicName() {
// FIXME: NEXT: this is forcing internal normalization. I think this boils down to providing Path back, which
// is essentially getDelegate() anyway. Perhaps expose it as PathAware?
return path.toString();
}
+
+ @Override
+ protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
+ return super.addToStringAttributes(toStringHelper).add("path", path);
+ }
}
import com.google.common.base.MoreObjects;
import com.google.common.base.MoreObjects.ToStringHelper;
import com.google.common.io.CharSource;
-import java.io.File;
import java.io.InputStream;
-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;
import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
import org.opendaylight.yangtools.yang.model.api.source.YangSourceRepresentation;
this.sourceId = requireNonNull(sourceId);
}
- /**
- * Create a new YangTextSchemaSource backed by a {@link File} with {@link SourceIdentifier} derived from the file
- * name.
- *
- * @param path Backing path
- * @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 {@code null}
- */
- public static @NonNull YangTextSource forPath(final Path path) {
- // FIXME: do not use .toFile() here
- return forPath(path, SourceIdentifier.ofYangFileName(path.toFile().getName()));
- }
-
- /**
- * Create a new YangTextSchemaSource backed by a {@link File} and specified {@link SourceIdentifier}.
- *
- * @param path Backing path
- * @param identifier source identifier
- * @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 YangTextSource 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 YangTextSource forPath(final Path path, final SourceIdentifier identifier,
- final Charset charset) {
- if (Files.isRegularFile(path)) {
- return new YangTextFileSource(identifier, path, charset);
- }
- throw new IllegalArgumentException("Supplied path " + path + " is not a regular file");
- }
-
@Override
public final Class<YangTextSource> getType() {
return YangTextSource.class;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
-import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import org.opendaylight.yangtools.yang.model.api.ModuleImport;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
import org.opendaylight.yangtools.yang.model.api.stmt.FeatureSet;
+import org.opendaylight.yangtools.yang.model.spi.source.FileYangTextSource;
import org.opendaylight.yangtools.yang.model.spi.source.FileYinTextSource;
-import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource;
+import org.opendaylight.yangtools.yang.model.spi.source.URLYangTextSource;
import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration;
import org.opendaylight.yangtools.yang.parser.api.YangSyntaxErrorException;
import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
public static YangStatementStreamSource sourceForResource(final String resourceName) {
try {
- return YangStatementStreamSource.create(YangTextSource.forPath(Path.of(
- StmtTestUtils.class.getResource(resourceName).toURI())));
- } catch (IOException | YangSyntaxErrorException | URISyntaxException e) {
+ return YangStatementStreamSource.create(new URLYangTextSource(
+ StmtTestUtils.class.getResource(resourceName)));
+ } catch (IOException | YangSyntaxErrorException e) {
throw new IllegalArgumentException("Failed to create source", e);
}
}
}
public static EffectiveModelContext parseYangSources(final YangParserConfiguration config,
- final Set<QName> supportedFeatures, final File... files) throws ReactorException, IOException,
- YangSyntaxErrorException {
-
- final Collection<YangStatementStreamSource> sources = new ArrayList<>(files.length);
- for (File file : files) {
- sources.add(YangStatementStreamSource.create(YangTextSource.forPath(file.toPath())));
+ final Set<QName> supportedFeatures, final File... files)
+ throws ReactorException, IOException, YangSyntaxErrorException {
+ final var sources = new ArrayList<YangStatementStreamSource>(files.length);
+ for (var file : files) {
+ sources.add(YangStatementStreamSource.create(new FileYangTextSource(file.toPath())));
}
-
return parseYangSources(config, supportedFeatures, sources);
}
import java.io.File;
import java.io.IOException;
import java.net.URI;
-import java.net.URISyntaxException;
-import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
import org.opendaylight.yangtools.yang.model.api.source.YinTextSource;
import org.opendaylight.yangtools.yang.model.api.stmt.FeatureSet;
+import org.opendaylight.yangtools.yang.model.spi.source.FileYangTextSource;
import org.opendaylight.yangtools.yang.model.spi.source.FileYinTextSource;
+import org.opendaylight.yangtools.yang.model.spi.source.URLYangTextSource;
import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource;
import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YangStatementStreamSource;
public static @NonNull List<StatementStreamSource> loadSources(final Class<?> cls, final String resourceDirectory)
throws Exception {
+ // FIXME: use Path instead
final var files = new File(cls.getResource(resourceDirectory).toURI())
.listFiles(StmtTestUtils.YANG_FILE_FILTER);
final var sources = new ArrayList<StatementStreamSource>(files.length);
for (var file : files) {
- sources.add(YangStatementStreamSource.create(YangTextSource.forPath(file.toPath())));
+ sources.add(YangStatementStreamSource.create(new FileYangTextSource(file.toPath())));
}
return sources;
}
}
public static YangTextSource assertSchemaSource(final String resourcePath) {
- try {
- return YangTextSource.forPath(Path.of(TestUtils.class.getResource(resourcePath).toURI()));
- } catch (URISyntaxException e) {
- throw new AssertionError(e);
- }
+ return new URLYangTextSource(TestUtils.class.getResource(resourcePath));
}
// FIXME: these remain unaudited
import java.util.Locale;
import java.util.ServiceLoader;
import java.util.Set;
-import java.util.stream.Collectors;
import org.eclipse.jdt.annotation.NonNull;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.common.YangConstants;
import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
import org.opendaylight.yangtools.yang.model.api.source.SourceRepresentation;
import org.opendaylight.yangtools.yang.model.api.stmt.FeatureSet;
+import org.opendaylight.yangtools.yang.model.spi.source.FileYangTextSource;
import org.opendaylight.yangtools.yang.model.spi.source.URLYangTextSource;
import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource;
import org.opendaylight.yangtools.yang.parser.api.YangParser;
public static EffectiveModelContext parseYangFiles(final Set<QName> supportedFeatures,
final YangParserConfiguration config, final Collection<File> files) {
return parseSources(config, supportedFeatures,
- files.stream().map(file -> YangTextSource.forPath(file.toPath())).collect(Collectors.toList()));
+ files.stream().map(file -> new FileYangTextSource(file.toPath())).toList());
}
/**
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.FileYangTextSource;
import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Override
ImmutableList<YangTextSource> sources() {
- return ImmutableList.of(YangTextSource.forPath(file().toPath()));
+ return ImmutableList.of(new FileYangTextSource(file().toPath()));
}
}
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.FileYangTextSource;
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;
final Stopwatch watch = Stopwatch.createStarted();
- final List<Entry<YangTextSource, YangIRSchemaSource>> parsed = yangFilesInProject.parallelStream()
+ final var parsed = yangFilesInProject.parallelStream()
.map(file -> {
- final var textSource = YangTextSource.forPath(file.toPath());
+ final var textSource = new FileYangTextSource(file.toPath());
try {
return Map.entry(textSource, TextToIRTransformer.transformText(textSource));
} catch (YangSyntaxErrorException | IOException e) {
@SuppressWarnings("checkstyle:illegalCatch")
private @NonNull ProcessorModuleReactor createReactor(final List<File> yangFilesInProject,
final YangParserConfiguration parserConfig, final Collection<ScannedDependency> dependencies,
- final List<Entry<YangTextSource, YangIRSchemaSource>> parsed) throws MojoExecutionException {
+ final List<Entry<FileYangTextSource, YangIRSchemaSource>> parsed) throws MojoExecutionException {
try {
final var sourcesInProject = new ArrayList<YangTextSource>(yangFilesInProject.size());
import org.opendaylight.yangtools.yang.common.YangConstants;
import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
import org.opendaylight.yangtools.yang.model.api.stmt.FeatureSet;
-import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource;
-import org.opendaylight.yangtools.yang.parser.api.YangParser;
+import org.opendaylight.yangtools.yang.model.spi.source.FileYangTextSource;
import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration;
import org.opendaylight.yangtools.yang.parser.api.YangParserException;
import org.opendaylight.yangtools.yang.parser.api.YangParserFactory;
final List<File> libFiles, final boolean warnForUnkeyedLists) throws IOException, YangParserException {
checkArgument(!testFiles.isEmpty(), "No yang sources");
- final YangParserConfiguration configuration = YangParserConfiguration.builder()
- .warnForUnkeyedLists(warnForUnkeyedLists).build();
- final YangParser parser = PARSER_FACTORY.createParser(configuration);
+ final var configuration = YangParserConfiguration.builder().warnForUnkeyedLists(warnForUnkeyedLists).build();
+ final var parser = PARSER_FACTORY.createParser(configuration);
if (supportedFeatures != null) {
parser.setSupportedFeatures(FeatureSet.of(supportedFeatures));
}
- for (File file : testFiles) {
- parser.addSource(YangTextSource.forPath(file.toPath()));
+ for (var file : testFiles) {
+ parser.addSource(new FileYangTextSource(file.toPath()));
}
- for (File file : libFiles) {
- parser.addLibSource(YangTextSource.forPath(file.toPath()));
+ for (var file : libFiles) {
+ parser.addLibSource(new FileYangTextSource(file.toPath()));
}
return parser.buildEffectiveModel();
import org.opendaylight.yangtools.yang.model.repo.spi.AbstractSchemaSourceCache;
import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource.Costs;
import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
+import org.opendaylight.yangtools.yang.model.spi.source.FileYangTextSource;
import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Override
YangTextSource restoreAsType(final SourceIdentifier sourceIdentifier, final File cachedSource) {
- return YangTextSource.forPath(cachedSource.toPath(), sourceIdentifier);
+ return new FileYangTextSource(sourceIdentifier, cachedSource.toPath(), StandardCharsets.UTF_8);
}
}