Promote SchemaSourceRepresentation
[yangtools.git] / model / yang-model-spi / src / main / java / org / opendaylight / yangtools / yang / model / spi / source / ResourceYangTextSource.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. 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.net.URL;
17 import java.nio.charset.Charset;
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.opendaylight.yangtools.concepts.Delegator;
20 import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
21
22 /**
23  * A resource-backed {@link YangTextSource}.
24  */
25 final class ResourceYangTextSource extends YangTextSource implements Delegator<URL> {
26     private final @NonNull URL url;
27     private final @NonNull Charset charset;
28
29     ResourceYangTextSource(final SourceIdentifier sourceId, final URL url, final Charset charset) {
30         super(sourceId);
31         this.url = requireNonNull(url);
32         this.charset = requireNonNull(charset);
33     }
34
35     @Override
36     public URL getDelegate() {
37         return url;
38     }
39
40     @Override
41     protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
42         return super.addToStringAttributes(toStringHelper).add("url", url);
43     }
44
45     @Override
46     public Reader openStream() throws IOException {
47         return new InputStreamReader(url.openStream(), charset);
48     }
49
50     @Override
51     public String symbolicName() {
52         return url.toString();
53     }
54 }