2 * Copyright (c) 2014 Cisco Systems, Inc. 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.repo.api;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
12 import static org.opendaylight.yangtools.yang.common.YangConstants.RFC6020_YANG_FILE_EXTENSION;
13 import static org.opendaylight.yangtools.yang.common.YangNames.parseFilename;
15 import com.google.common.annotations.Beta;
16 import com.google.common.base.MoreObjects;
17 import com.google.common.base.MoreObjects.ToStringHelper;
18 import com.google.common.io.ByteSource;
19 import com.google.common.io.CharSource;
20 import com.google.common.io.Resources;
22 import java.io.InputStream;
24 import java.nio.charset.Charset;
25 import java.nio.charset.StandardCharsets;
26 import java.nio.file.Files;
27 import java.nio.file.Path;
28 import org.eclipse.jdt.annotation.NonNull;
31 * YANG text schema source representation. Exposes an RFC6020 or RFC7950 text representation as an {@link InputStream}.
34 public abstract class YangTextSchemaSource extends CharSource implements YangSchemaSourceRepresentation {
35 private final @NonNull SourceIdentifier identifier;
37 protected YangTextSchemaSource(final SourceIdentifier identifier) {
38 this.identifier = requireNonNull(identifier);
41 public static @NonNull SourceIdentifier identifierFromFilename(final String name) {
42 checkArgument(name.endsWith(RFC6020_YANG_FILE_EXTENSION), "Filename '%s' does not end with '%s'", name,
43 RFC6020_YANG_FILE_EXTENSION);
45 final String baseName = name.substring(0, name.length() - RFC6020_YANG_FILE_EXTENSION.length());
46 final var parsed = parseFilename(baseName);
47 return new SourceIdentifier(parsed.getKey(), parsed.getValue());
51 * Create a new YangTextSchemaSource with a specific source identifier and backed
52 * by ByteSource, which provides the actual InputStreams.
54 * @param identifier SourceIdentifier of the resulting schema source
55 * @param delegate Backing ByteSource instance
56 * @param charset Expected character set
57 * @return A new YangTextSchemaSource
59 public static @NonNull YangTextSchemaSource delegateForByteSource(final SourceIdentifier identifier,
60 final ByteSource delegate, final Charset charset) {
61 return delegateForCharSource(identifier, delegate.asCharSource(charset));
65 * Create a new YangTextSchemaSource with {@link SourceIdentifier} derived from a supplied filename and backed
66 * by ByteSource, which provides the actual InputStreams.
68 * @param fileName File name
69 * @param delegate Backing ByteSource instance
70 * @return A new YangTextSchemaSource
71 * @throws IllegalArgumentException if the file name has invalid format
73 public static @NonNull YangTextSchemaSource delegateForByteSource(final String fileName,
74 final ByteSource delegate, final Charset charset) {
75 return delegateForCharSource(fileName, delegate.asCharSource(charset));
79 * Create a new YangTextSchemaSource with a specific source identifier and backed
80 * by ByteSource, which provides the actual InputStreams.
82 * @param identifier SourceIdentifier of the resulting schema source
83 * @param delegate Backing CharSource instance
84 * @return A new YangTextSchemaSource
86 public static @NonNull YangTextSchemaSource delegateForCharSource(final SourceIdentifier identifier,
87 final CharSource delegate) {
88 return new DelegatedYangTextSchemaSource(identifier, delegate);
92 * Create a new YangTextSchemaSource with {@link SourceIdentifier} derived from a supplied filename and backed
93 * by ByteSource, which provides the actual InputStreams.
95 * @param fileName File name
96 * @param delegate Backing CharSource instance
97 * @return A new YangTextSchemaSource
98 * @throws IllegalArgumentException if the file name has invalid format
100 public static @NonNull YangTextSchemaSource delegateForCharSource(final String fileName,
101 final CharSource delegate) {
102 return new DelegatedYangTextSchemaSource(identifierFromFilename(fileName), delegate);
106 * Create a new YangTextSchemaSource backed by a {@link File} with {@link SourceIdentifier} derived from the file
109 * @param path Backing path
110 * @return A new YangTextSchemaSource
111 * @throws IllegalArgumentException if the file name has invalid format or if the supplied File is not a file
112 * @throws NullPointerException if file is {@code null}
114 public static @NonNull YangTextSchemaSource forPath(final Path path) {
115 return forPath(path, identifierFromFilename(path.toFile().getName()));
119 * Create a new YangTextSchemaSource backed by a {@link File} and specified {@link SourceIdentifier}.
121 * @param path Backing path
122 * @param identifier source identifier
123 * @return A new YangTextSchemaSource
124 * @throws NullPointerException if any argument is {@code null}
125 * @throws IllegalArgumentException if the supplied path is not a regular file
127 public static @NonNull YangTextSchemaSource forPath(final Path path, final SourceIdentifier identifier) {
128 return forPath(path, identifier, StandardCharsets.UTF_8);
132 * Create a new YangTextSchemaSource backed by a {@link File} and specified {@link SourceIdentifier}.
134 * @param path Backing path
135 * @param identifier Source identifier
136 * @param charset expected stream character set
137 * @return A new YangTextSchemaSource
138 * @throws NullPointerException if any argument is {@code null}
139 * @throws IllegalArgumentException if the supplied path is not a regular file
141 public static @NonNull YangTextSchemaSource forPath(final Path path, final SourceIdentifier identifier,
142 final Charset charset) {
143 checkArgument(Files.isRegularFile(path), "Supplied path %s is not a regular file", path);
144 return new YangTextFileSchemaSource(identifier, path, charset);
148 * Create a new {@link YangTextSchemaSource} backed by a resource available in the ClassLoader where this
151 * @param resourceName Resource name
152 * @return A new instance.
153 * @throws IllegalArgumentException if the resource does not exist or if the name has invalid format
155 public static @NonNull YangTextSchemaSource forResource(final String resourceName) {
156 return forResource(YangTextSchemaSource.class, resourceName);
160 * Create a new {@link YangTextSchemaSource} backed by a resource by a resource available on the ClassLoader
161 * which loaded the specified class.
163 * @param clazz Class reference
164 * @param resourceName Resource name
165 * @return A new instance.
166 * @throws IllegalArgumentException if the resource does not exist or if the name has invalid format
168 public static @NonNull YangTextSchemaSource forResource(final Class<?> clazz, final String resourceName) {
169 return forResource(clazz, resourceName, StandardCharsets.UTF_8);
173 * Create a new {@link YangTextSchemaSource} backed by a resource by a resource available on the ClassLoader
174 * which loaded the specified class.
176 * @param clazz Class reference
177 * @param resourceName Resource name
178 * @param charset Expected character set
179 * @return A new instance.
180 * @throws IllegalArgumentException if the resource does not exist or if the name has invalid format
182 public static @NonNull YangTextSchemaSource forResource(final Class<?> clazz, final String resourceName,
183 final Charset charset) {
184 final String fileName = resourceName.substring(resourceName.lastIndexOf('/') + 1);
185 final SourceIdentifier identifier = identifierFromFilename(fileName);
186 final URL url = Resources.getResource(clazz, resourceName);
187 return new ResourceYangTextSchemaSource(identifier, url, charset);
192 * Create a new {@link YangTextSchemaSource} backed by a URL.
194 * @param url Backing URL
195 * @param identifier Source identifier
196 * @return A new instance.
197 * @throws NullPointerException if any argument is {@code null}
199 public static @NonNull YangTextSchemaSource forURL(final URL url, final SourceIdentifier identifier) {
200 return forURL(url, identifier, StandardCharsets.UTF_8);
204 * Create a new {@link YangTextSchemaSource} backed by a URL.
206 * @param url Backing URL
207 * @param identifier Source identifier
208 * @param charset Expected character set
209 * @return A new instance.
210 * @throws NullPointerException if any argument is {@code null}
212 public static @NonNull YangTextSchemaSource forURL(final URL url, final SourceIdentifier identifier,
213 final Charset charset) {
214 return new ResourceYangTextSchemaSource(identifier, url, charset);
219 public final SourceIdentifier getIdentifier() {
224 public final Class<YangTextSchemaSource> getType() {
225 return YangTextSchemaSource.class;
229 public final String toString() {
230 return addToStringAttributes(MoreObjects.toStringHelper(this).omitNullValues()).toString();
234 * Add subclass-specific attributes to the output {@link #toString()} output. Since
235 * subclasses are prevented from overriding {@link #toString()} for consistency
236 * reasons, they can add their specific attributes to the resulting string by attaching
237 * attributes to the supplied {@link ToStringHelper}.
239 * @param toStringHelper ToStringHelper onto the attributes can be added
240 * @return ToStringHelper supplied as input argument.
242 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
243 return toStringHelper.add("identifier", identifier);