1b424c8714e2908c186fcf2394a4145259e3df0a
[yangtools.git] / yang / yang-model-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
12 import com.google.common.annotations.Beta;
13 import com.google.common.base.MoreObjects;
14 import com.google.common.base.MoreObjects.ToStringHelper;
15 import com.google.common.base.Optional;
16 import com.google.common.base.Preconditions;
17 import com.google.common.io.ByteSource;
18 import com.google.common.io.Resources;
19 import java.io.File;
20 import java.io.InputStream;
21 import java.net.URL;
22 import java.util.Map.Entry;
23 import javax.annotation.Nonnull;
24 import org.opendaylight.yangtools.yang.common.YangConstants;
25 import org.opendaylight.yangtools.yang.common.YangNames;
26
27 /**
28  * YANG text schema source representation. Exposes an RFC6020 or RFC7950 text representation
29  * as an {@link InputStream}.
30  */
31 @Beta
32 public abstract class YangTextSchemaSource extends ByteSource implements YangSchemaSourceRepresentation {
33     private final SourceIdentifier identifier;
34
35     protected YangTextSchemaSource(final SourceIdentifier identifier) {
36         this.identifier = Preconditions.checkNotNull(identifier);
37     }
38
39     public static SourceIdentifier identifierFromFilename(final String name) {
40         checkArgument(name.endsWith(YangConstants.RFC6020_YANG_FILE_EXTENSION),
41             "Filename %s does not have a .yang extension", name);
42
43         final String baseName = name.substring(0, name.length() - YangConstants.RFC6020_YANG_FILE_EXTENSION.length());
44         final Entry<String, String> parsed = YangNames.parseFilename(baseName);
45         return RevisionSourceIdentifier.create(parsed.getKey(), Optional.fromNullable(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 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 YangTextSchemaSource delegateForByteSource(final String fileName, 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 null
82      */
83     public static YangTextSchemaSource forFile(final File file) {
84         Preconditions.checkArgument(file.isFile(), "Supplied file %s is not a file");
85         return new YangTextFileSchemaSource(identifierFromFilename(file.getName()), file);
86     }
87
88     /**
89      * Create a new {@link YangTextSchemaSource} backed by a resource available in the ClassLoader where this
90      * class resides.
91      *
92      * @param resourceName Resource name
93      * @return A new instance.
94      * @throws IllegalArgumentException if the resource does not exist or if the name has invalid format
95      */
96     public static ResourceYangTextSchemaSource forResource(final String resourceName) {
97         return forResource(YangTextSchemaSource.class, resourceName);
98     }
99
100     /**
101      * Create a new {@link YangTextSchemaSource} backed by a resource by a resource available on the ClassLoader
102      * which loaded the specified class.
103      *
104      * @param clazz Class reference
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 ResourceYangTextSchemaSource forResource(final Class<?> clazz, final String resourceName) {
110         final String fileName = resourceName.substring(resourceName.lastIndexOf('/') + 1);
111         final SourceIdentifier identifier = identifierFromFilename(fileName);
112         final URL url = Resources.getResource(clazz, resourceName);
113         return new ResourceYangTextSchemaSource(identifier, url);
114     }
115
116     @Override
117     public final SourceIdentifier getIdentifier() {
118         return identifier;
119     }
120
121     @Nonnull
122     @Override
123     public Class<? extends YangTextSchemaSource> getType() {
124         return YangTextSchemaSource.class;
125     }
126
127     @Override
128     public final String toString() {
129         return addToStringAttributes(MoreObjects.toStringHelper(this).add("identifier", identifier)).toString();
130     }
131
132     /**
133      * Add subclass-specific attributes to the output {@link #toString()} output. Since
134      * subclasses are prevented from overriding {@link #toString()} for consistency
135      * reasons, they can add their specific attributes to the resulting string by attaching
136      * attributes to the supplied {@link ToStringHelper}.
137      *
138      * @param toStringHelper ToStringHelper onto the attributes can be added
139      * @return ToStringHelper supplied as input argument.
140      */
141     protected abstract ToStringHelper addToStringAttributes(ToStringHelper toStringHelper);
142 }