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.Resources;
21 import java.io.InputStream;
23 import java.nio.file.Files;
24 import java.nio.file.Path;
25 import java.util.Map.Entry;
26 import org.eclipse.jdt.annotation.NonNull;
27 import org.opendaylight.yangtools.yang.common.Revision;
30 * YANG text schema source representation. Exposes an RFC6020 or RFC7950 text representation as an {@link InputStream}.
33 public abstract class YangTextSchemaSource extends ByteSource implements YangSchemaSourceRepresentation {
34 private final @NonNull SourceIdentifier identifier;
36 protected YangTextSchemaSource(final SourceIdentifier identifier) {
37 this.identifier = requireNonNull(identifier);
40 public static @NonNull SourceIdentifier identifierFromFilename(final String name) {
41 checkArgument(name.endsWith(RFC6020_YANG_FILE_EXTENSION), "Filename %s does not end with '%s'",
42 RFC6020_YANG_FILE_EXTENSION, name);
44 final String baseName = name.substring(0, name.length() - RFC6020_YANG_FILE_EXTENSION.length());
45 final Entry<String, String> parsed = parseFilename(baseName);
46 return RevisionSourceIdentifier.create(parsed.getKey(), Revision.ofNullable(parsed.getValue()));
50 * Create a new YangTextSchemaSource with a specific source identifier and backed
51 * by ByteSource, which provides the actual InputStreams.
53 * @param identifier SourceIdentifier of the resulting schema source
54 * @param delegate Backing ByteSource instance
55 * @return A new YangTextSchemaSource
57 public static @NonNull YangTextSchemaSource delegateForByteSource(final SourceIdentifier identifier,
58 final ByteSource delegate) {
59 return new DelegatedYangTextSchemaSource(identifier, delegate);
63 * Create a new YangTextSchemaSource with {@link SourceIdentifier} derived from a supplied filename and backed
64 * by ByteSource, which provides the actual InputStreams.
66 * @param fileName File name
67 * @param delegate Backing ByteSource instance
68 * @return A new YangTextSchemaSource
69 * @throws IllegalArgumentException if the file name has invalid format
71 public static @NonNull YangTextSchemaSource delegateForByteSource(final String fileName,
72 final ByteSource delegate) {
73 return new DelegatedYangTextSchemaSource(identifierFromFilename(fileName), delegate);
77 * Create a new YangTextSchemaSource backed by a {@link File} with {@link SourceIdentifier} derived from the file
80 * @param path Backing path
81 * @return A new YangTextSchemaSource
82 * @throws IllegalArgumentException if the file name has invalid format or if the supplied File is not a file
83 * @throws NullPointerException if file is {@code null}
85 public static @NonNull YangTextSchemaSource forPath(final Path path) {
86 return forPath(path, identifierFromFilename(path.toFile().getName()));
90 * Create a new YangTextSchemaSource backed by a {@link File} and specified {@link SourceIdentifier}.
92 * @param path Backing path
93 * @param identifier source identifier
94 * @return A new YangTextSchemaSource
95 * @throws NullPointerException if any argument is {@code null}
96 * @throws IllegalArgumentException if the supplied path is not a regular file
98 public static @NonNull YangTextSchemaSource forPath(final Path path, final SourceIdentifier identifier) {
99 checkArgument(Files.isRegularFile(path), "Supplied path %s is not a regular file", path);
100 return new YangTextFileSchemaSource(identifier, path);
104 * Create a new {@link YangTextSchemaSource} backed by a resource available in the ClassLoader where this
107 * @param resourceName Resource name
108 * @return A new instance.
109 * @throws IllegalArgumentException if the resource does not exist or if the name has invalid format
111 public static @NonNull YangTextSchemaSource forResource(final String resourceName) {
112 return forResource(YangTextSchemaSource.class, resourceName);
116 * Create a new {@link YangTextSchemaSource} backed by a resource by a resource available on the ClassLoader
117 * which loaded the specified class.
119 * @param clazz Class reference
120 * @param resourceName Resource name
121 * @return A new instance.
122 * @throws IllegalArgumentException if the resource does not exist or if the name has invalid format
124 public static @NonNull YangTextSchemaSource forResource(final Class<?> clazz, final String resourceName) {
125 final String fileName = resourceName.substring(resourceName.lastIndexOf('/') + 1);
126 final SourceIdentifier identifier = identifierFromFilename(fileName);
127 final URL url = Resources.getResource(clazz, resourceName);
128 return new ResourceYangTextSchemaSource(identifier, url);
132 * Create a new {@link YangTextSchemaSource} backed by a URL.
134 * @param url Backing URL
135 * @param identifier Source identifier
136 * @return A new instance.
137 * @throws NullPointerException if any argument is {@code null}
139 public static @NonNull YangTextSchemaSource forURL(final URL url, final SourceIdentifier identifier) {
140 return new ResourceYangTextSchemaSource(identifier, url);
144 public final SourceIdentifier getIdentifier() {
149 public Class<? extends YangTextSchemaSource> getType() {
150 return YangTextSchemaSource.class;
154 public final String toString() {
155 return addToStringAttributes(MoreObjects.toStringHelper(this).add("identifier", identifier)).toString();
159 * Add subclass-specific attributes to the output {@link #toString()} output. Since
160 * subclasses are prevented from overriding {@link #toString()} for consistency
161 * reasons, they can add their specific attributes to the resulting string by attaching
162 * attributes to the supplied {@link ToStringHelper}.
164 * @param toStringHelper ToStringHelper onto the attributes can be added
165 * @return ToStringHelper supplied as input argument.
167 protected abstract ToStringHelper addToStringAttributes(ToStringHelper toStringHelper);