Fix eclipse warnings
[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.Resources;
20 import java.io.File;
21 import java.io.InputStream;
22 import java.net.URL;
23 import java.nio.file.Files;
24 import java.nio.file.Path;
25 import org.eclipse.jdt.annotation.NonNull;
26 import org.opendaylight.yangtools.yang.common.Revision;
27
28 /**
29  * YANG text schema source representation. Exposes an RFC6020 or RFC7950 text representation as an {@link InputStream}.
30  */
31 @Beta
32 public abstract class YangTextSchemaSource extends ByteSource implements YangSchemaSourceRepresentation {
33     private final @NonNull SourceIdentifier identifier;
34
35     protected YangTextSchemaSource(final SourceIdentifier identifier) {
36         this.identifier = requireNonNull(identifier);
37     }
38
39     public static @NonNull SourceIdentifier identifierFromFilename(final String name) {
40         checkArgument(name.endsWith(RFC6020_YANG_FILE_EXTENSION), "Filename '%s' does not end with '%s'", name,
41             RFC6020_YANG_FILE_EXTENSION);
42
43         final String baseName = name.substring(0, name.length() - RFC6020_YANG_FILE_EXTENSION.length());
44         final var parsed = parseFilename(baseName);
45         return RevisionSourceIdentifier.create(parsed.getKey(), Revision.ofNullable(parsed.getValue()));
46     }
47
48     /**
49      * Create a new YangTextSchemaSource with a specific source identifier and backed
50      * by ByteSource, which provides the actual InputStreams.
51      *
52      * @param identifier SourceIdentifier of the resulting schema source
53      * @param delegate Backing ByteSource instance
54      * @return A new YangTextSchemaSource
55      */
56     public static @NonNull YangTextSchemaSource delegateForByteSource(final SourceIdentifier identifier,
57             final ByteSource delegate) {
58         return new DelegatedYangTextSchemaSource(identifier, delegate);
59     }
60
61     /**
62      * Create a new YangTextSchemaSource with {@link SourceIdentifier} derived from a supplied filename and backed
63      * by ByteSource, which provides the actual InputStreams.
64      *
65      * @param fileName File name
66      * @param delegate Backing ByteSource instance
67      * @return A new YangTextSchemaSource
68      * @throws IllegalArgumentException if the file name has invalid format
69      */
70     public static @NonNull YangTextSchemaSource delegateForByteSource(final String fileName,
71             final ByteSource delegate) {
72         return new DelegatedYangTextSchemaSource(identifierFromFilename(fileName), delegate);
73     }
74
75     /**
76      * Create a new YangTextSchemaSource backed by a {@link File} with {@link SourceIdentifier} derived from the file
77      * name.
78      *
79      * @param path Backing path
80      * @return A new YangTextSchemaSource
81      * @throws IllegalArgumentException if the file name has invalid format or if the supplied File is not a file
82      * @throws NullPointerException if file is {@code null}
83      */
84     public static @NonNull YangTextSchemaSource forPath(final Path path) {
85         return forPath(path, identifierFromFilename(path.toFile().getName()));
86     }
87
88     /**
89      * Create a new YangTextSchemaSource backed by a {@link File} and specified {@link SourceIdentifier}.
90      *
91      * @param path Backing path
92      * @param identifier source identifier
93      * @return A new YangTextSchemaSource
94      * @throws NullPointerException if any argument is {@code null}
95      * @throws IllegalArgumentException if the supplied path is not a regular file
96      */
97     public static @NonNull YangTextSchemaSource forPath(final Path path, final SourceIdentifier identifier) {
98         checkArgument(Files.isRegularFile(path), "Supplied path %s is not a regular file", path);
99         return new YangTextFileSchemaSource(identifier, path);
100     }
101
102     /**
103      * Create a new {@link YangTextSchemaSource} backed by a resource available in the ClassLoader where this
104      * class resides.
105      *
106      * @param resourceName Resource name
107      * @return A new instance.
108      * @throws IllegalArgumentException if the resource does not exist or if the name has invalid format
109      */
110     public static @NonNull YangTextSchemaSource forResource(final String resourceName) {
111         return forResource(YangTextSchemaSource.class, resourceName);
112     }
113
114     /**
115      * Create a new {@link YangTextSchemaSource} backed by a resource by a resource available on the ClassLoader
116      * which loaded the specified class.
117      *
118      * @param clazz Class reference
119      * @param resourceName Resource name
120      * @return A new instance.
121      * @throws IllegalArgumentException if the resource does not exist or if the name has invalid format
122      */
123     public static @NonNull YangTextSchemaSource forResource(final Class<?> clazz, final String resourceName) {
124         final String fileName = resourceName.substring(resourceName.lastIndexOf('/') + 1);
125         final SourceIdentifier identifier = identifierFromFilename(fileName);
126         final URL url = Resources.getResource(clazz, resourceName);
127         return new ResourceYangTextSchemaSource(identifier, url);
128     }
129
130     /**
131      * Create a new {@link YangTextSchemaSource} backed by a URL.
132      *
133      * @param url Backing URL
134      * @param identifier Source identifier
135      * @return A new instance.
136      * @throws NullPointerException if any argument is {@code null}
137      */
138     public static @NonNull YangTextSchemaSource forURL(final URL url, final SourceIdentifier identifier) {
139         return new ResourceYangTextSchemaSource(identifier, url);
140     }
141
142     @Override
143     public final SourceIdentifier getIdentifier() {
144         return identifier;
145     }
146
147     @Override
148     public Class<? extends YangTextSchemaSource> getType() {
149         return YangTextSchemaSource.class;
150     }
151
152     @Override
153     public final String toString() {
154         return addToStringAttributes(MoreObjects.toStringHelper(this).add("identifier", identifier)).toString();
155     }
156
157     /**
158      * Add subclass-specific attributes to the output {@link #toString()} output. Since
159      * subclasses are prevented from overriding {@link #toString()} for consistency
160      * reasons, they can add their specific attributes to the resulting string by attaching
161      * attributes to the supplied {@link ToStringHelper}.
162      *
163      * @param toStringHelper ToStringHelper onto the attributes can be added
164      * @return ToStringHelper supplied as input argument.
165      */
166     protected abstract ToStringHelper addToStringAttributes(ToStringHelper toStringHelper);
167 }