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