Promote SchemaSourceRepresentation
[yangtools.git] / model / yang-model-spi / src / main / java / org / opendaylight / yangtools / yang / model / spi / source / YangTextFileSource.java
diff --git a/model/yang-model-spi/src/main/java/org/opendaylight/yangtools/yang/model/spi/source/YangTextFileSource.java b/model/yang-model-spi/src/main/java/org/opendaylight/yangtools/yang/model/spi/source/YangTextFileSource.java
new file mode 100644 (file)
index 0000000..0689002
--- /dev/null
@@ -0,0 +1,57 @@
+/*
+ * Copyright (c) 2017 Pantheon Technologies, s.r.o. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang.model.spi.source;
+
+import static java.util.Objects.requireNonNull;
+
+import com.google.common.base.MoreObjects.ToStringHelper;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.nio.charset.Charset;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import org.eclipse.jdt.annotation.NonNull;
+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;
+
+    YangTextFileSource(final SourceIdentifier sourceId, final Path path, final Charset charset) {
+        super(sourceId);
+        this.path = requireNonNull(path);
+        this.charset = requireNonNull(charset);
+    }
+
+    @Override
+    public Path getDelegate() {
+        return path;
+    }
+
+    @Override
+    public Reader openStream() throws IOException {
+        return new InputStreamReader(Files.newInputStream(path), charset);
+    }
+
+    @Override
+    protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
+        return super.addToStringAttributes(toStringHelper).add("path", path);
+    }
+
+    @Override
+    public 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();
+    }
+}