Do not provide SourceIdentifiers from extensions
[yangtools.git] / yang / yang-repo-api / src / main / java / org / opendaylight / yangtools / yang / model / repo / api / YangTextFileSchemaSource.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.repo.api;
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 java.util.Optional;
20 import org.eclipse.jdt.annotation.NonNull;
21 import org.opendaylight.yangtools.concepts.Delegator;
22
23 /**
24  * A {@link YangTextSchemaSource} backed by a file.
25  *
26  * @author Robert Varga
27  */
28 final class YangTextFileSchemaSource extends YangTextSchemaSource implements Delegator<Path> {
29     private final @NonNull Path path;
30     private final @NonNull Charset charset;
31
32     YangTextFileSchemaSource(final SourceIdentifier identifier, final Path path, final Charset charset) {
33         super(identifier);
34         this.path = requireNonNull(path);
35         this.charset = requireNonNull(charset);
36     }
37
38     @Override
39     public Path getDelegate() {
40         return path;
41     }
42
43     @Override
44     public Reader openStream() throws IOException {
45         return new InputStreamReader(Files.newInputStream(path), charset);
46     }
47
48     @Override
49     protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
50         return super.addToStringAttributes(toStringHelper).add("path", path);
51     }
52
53     @Override
54     public Optional<String> getSymbolicName() {
55         // FIXME: NEXT: this is forcing internal normalization. I think this boils down to providing Path back, which
56         //        is essentially getDelegate() anyway. Perhaps expose it as PathAware?
57         return Optional.of(path.toString());
58     }
59 }