2 * Copyright (c) 2017 Pantheon Technologies, s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang.model.spi.source;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.base.MoreObjects.ToStringHelper;
13 import java.io.IOException;
14 import java.io.InputStreamReader;
15 import java.io.Reader;
16 import java.nio.charset.Charset;
17 import java.nio.file.Files;
18 import java.nio.file.Path;
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.opendaylight.yangtools.concepts.Delegator;
21 import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
24 * A {@link YangTextSource} backed by a file.
26 final class YangTextFileSource extends YangTextSource implements Delegator<Path> {
27 private final @NonNull Path path;
28 private final @NonNull Charset charset;
30 YangTextFileSource(final SourceIdentifier sourceId, final Path path, final Charset charset) {
32 this.path = requireNonNull(path);
33 this.charset = requireNonNull(charset);
37 public Path getDelegate() {
42 public Reader openStream() throws IOException {
43 return new InputStreamReader(Files.newInputStream(path), charset);
47 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
48 return super.addToStringAttributes(toStringHelper).add("path", path);
52 public String symbolicName() {
53 // FIXME: NEXT: this is forcing internal normalization. I think this boils down to providing Path back, which
54 // is essentially getDelegate() anyway. Perhaps expose it as PathAware?
55 return path.toString();