Do not provide SourceIdentifiers from extensions
[yangtools.git] / yang / yang-repo-api / src / main / java / org / opendaylight / yangtools / yang / model / repo / api / YangTextSchemaSource.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.repo.api;
9
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;
14
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;
21 import java.io.File;
22 import java.io.InputStream;
23 import java.net.URL;
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;
29
30 /**
31  * YANG text schema source representation. Exposes an RFC6020 or RFC7950 text representation as an {@link InputStream}.
32  */
33 @Beta
34 public abstract class YangTextSchemaSource extends CharSource implements YangSchemaSourceRepresentation {
35     private final @NonNull SourceIdentifier identifier;
36
37     protected YangTextSchemaSource(final SourceIdentifier identifier) {
38         this.identifier = requireNonNull(identifier);
39     }
40
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);
44
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());
48     }
49
50     /**
51      * Create a new YangTextSchemaSource with a specific source identifier and backed
52      * by ByteSource, which provides the actual InputStreams.
53      *
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
58      */
59     public static @NonNull YangTextSchemaSource delegateForByteSource(final SourceIdentifier identifier,
60             final ByteSource delegate, final Charset charset) {
61         return delegateForCharSource(identifier, delegate.asCharSource(charset));
62     }
63
64     /**
65      * Create a new YangTextSchemaSource with {@link SourceIdentifier} derived from a supplied filename and backed
66      * by ByteSource, which provides the actual InputStreams.
67      *
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
72      */
73     public static @NonNull YangTextSchemaSource delegateForByteSource(final String fileName,
74             final ByteSource delegate, final Charset charset) {
75         return delegateForCharSource(fileName, delegate.asCharSource(charset));
76     }
77
78     /**
79      * Create a new YangTextSchemaSource with a specific source identifier and backed
80      * by ByteSource, which provides the actual InputStreams.
81      *
82      * @param identifier SourceIdentifier of the resulting schema source
83      * @param delegate Backing CharSource instance
84      * @return A new YangTextSchemaSource
85      */
86     public static @NonNull YangTextSchemaSource delegateForCharSource(final SourceIdentifier identifier,
87             final CharSource delegate) {
88         return new DelegatedYangTextSchemaSource(identifier, delegate);
89     }
90
91     /**
92      * Create a new YangTextSchemaSource with {@link SourceIdentifier} derived from a supplied filename and backed
93      * by ByteSource, which provides the actual InputStreams.
94      *
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
99      */
100     public static @NonNull YangTextSchemaSource delegateForCharSource(final String fileName,
101             final CharSource delegate) {
102         return new DelegatedYangTextSchemaSource(identifierFromFilename(fileName), delegate);
103     }
104
105     /**
106      * Create a new YangTextSchemaSource backed by a {@link File} with {@link SourceIdentifier} derived from the file
107      * name.
108      *
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}
113      */
114     public static @NonNull YangTextSchemaSource forPath(final Path path) {
115         return forPath(path, identifierFromFilename(path.toFile().getName()));
116     }
117
118     /**
119      * Create a new YangTextSchemaSource backed by a {@link File} and specified {@link SourceIdentifier}.
120      *
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
126      */
127     public static @NonNull YangTextSchemaSource forPath(final Path path, final SourceIdentifier identifier) {
128         return forPath(path, identifier, StandardCharsets.UTF_8);
129     }
130
131     /**
132      * Create a new YangTextSchemaSource backed by a {@link File} and specified {@link SourceIdentifier}.
133      *
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
140      */
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);
145     }
146
147     /**
148      * Create a new {@link YangTextSchemaSource} backed by a resource available in the ClassLoader where this
149      * class resides.
150      *
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
154      */
155     public static @NonNull YangTextSchemaSource forResource(final String resourceName) {
156         return forResource(YangTextSchemaSource.class, resourceName);
157     }
158
159     /**
160      * Create a new {@link YangTextSchemaSource} backed by a resource by a resource available on the ClassLoader
161      * which loaded the specified class.
162      *
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
167      */
168     public static @NonNull YangTextSchemaSource forResource(final Class<?> clazz, final String resourceName) {
169         return forResource(clazz, resourceName, StandardCharsets.UTF_8);
170     }
171
172     /**
173      * Create a new {@link YangTextSchemaSource} backed by a resource by a resource available on the ClassLoader
174      * which loaded the specified class.
175      *
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
181      */
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);
188     }
189
190
191     /**
192      * Create a new {@link YangTextSchemaSource} backed by a URL.
193      *
194      * @param url Backing URL
195      * @param identifier Source identifier
196      * @return A new instance.
197      * @throws NullPointerException if any argument is {@code null}
198      */
199     public static @NonNull YangTextSchemaSource forURL(final URL url, final SourceIdentifier identifier) {
200         return forURL(url, identifier, StandardCharsets.UTF_8);
201     }
202
203     /**
204      * Create a new {@link YangTextSchemaSource} backed by a URL.
205      *
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}
211      */
212     public static @NonNull YangTextSchemaSource forURL(final URL url, final SourceIdentifier identifier,
213             final Charset charset) {
214         return new ResourceYangTextSchemaSource(identifier, url, charset);
215     }
216
217
218     @Override
219     public final SourceIdentifier getIdentifier() {
220         return identifier;
221     }
222
223     @Override
224     public final Class<YangTextSchemaSource> getType() {
225         return YangTextSchemaSource.class;
226     }
227
228     @Override
229     public final String toString() {
230         return addToStringAttributes(MoreObjects.toStringHelper(this).omitNullValues()).toString();
231     }
232
233     /**
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}.
238      *
239      * @param toStringHelper ToStringHelper onto the attributes can be added
240      * @return ToStringHelper supplied as input argument.
241      */
242     protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
243         return toStringHelper.add("identifier", identifier);
244     }
245 }