Do not provide SourceIdentifiers from extensions
[yangtools.git] / model / yang-model-spi / src / main / java / org / opendaylight / yangtools / yang / model / spi / source / YangTextFileSource.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies, s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.model.spi.source;
9
10 import static java.util.Objects.requireNonNull;
11
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;
22
23 /**
24  * A {@link YangTextSource} backed by a file.
25  */
26 final class YangTextFileSource extends YangTextSource implements Delegator<Path> {
27     private final @NonNull Path path;
28     private final @NonNull Charset charset;
29
30     YangTextFileSource(final SourceIdentifier sourceId, final Path path, final Charset charset) {
31         super(sourceId);
32         this.path = requireNonNull(path);
33         this.charset = requireNonNull(charset);
34     }
35
36     @Override
37     public Path getDelegate() {
38         return path;
39     }
40
41     @Override
42     public Reader openStream() throws IOException {
43         return new InputStreamReader(Files.newInputStream(path), charset);
44     }
45
46     @Override
47     protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
48         return super.addToStringAttributes(toStringHelper).add("path", path);
49     }
50
51     @Override
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();
56     }
57 }