2 * Copyright (c) 2015 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;
13 import com.google.common.annotations.Beta;
14 import com.google.common.base.MoreObjects;
15 import com.google.common.base.MoreObjects.ToStringHelper;
16 import com.google.common.io.ByteSource;
17 import com.google.common.io.Resources;
19 import java.io.InputStream;
20 import java.util.Map.Entry;
21 import org.eclipse.jdt.annotation.NonNull;
22 import org.opendaylight.yangtools.yang.common.Revision;
23 import org.opendaylight.yangtools.yang.common.YangConstants;
24 import org.opendaylight.yangtools.yang.common.YangNames;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
29 * YIN text schema source representation. Exposes an RFC6020 or RFC7950 XML representation as an {@link InputStream}.
32 public abstract class YinTextSchemaSource extends ByteSource implements YinSchemaSourceRepresentation {
33 private static final Logger LOG = LoggerFactory.getLogger(YinTextSchemaSource.class);
34 private static final String XML_EXTENSION = ".xml";
36 private final @NonNull SourceIdentifier identifier;
38 protected YinTextSchemaSource(final SourceIdentifier identifier) {
39 this.identifier = requireNonNull(identifier);
42 public static @NonNull SourceIdentifier identifierFromFilename(final String name) {
43 final String baseName;
44 if (name.endsWith(YangConstants.RFC6020_YIN_FILE_EXTENSION)) {
45 baseName = name.substring(0, name.length() - YangConstants.RFC6020_YIN_FILE_EXTENSION.length());
46 } else if (name.endsWith(XML_EXTENSION)) {
47 // FIXME: BUG-7061: remove this once we do not need it
48 LOG.warn("XML file {} being loaded as YIN", name);
49 baseName = name.substring(0, name.length() - XML_EXTENSION.length());
51 throw new IllegalArgumentException("Filename " + name + " does not have a .yin or .xml extension");
54 final Entry<String, String> parsed = YangNames.parseFilename(baseName);
55 return RevisionSourceIdentifier.create(parsed.getKey(), Revision.ofNullable(parsed.getValue()));
59 public final SourceIdentifier getIdentifier() {
64 public Class<? extends YinTextSchemaSource> getType() {
65 return YinTextSchemaSource.class;
69 public final String toString() {
70 return addToStringAttributes(MoreObjects.toStringHelper(this).add("identifier", identifier)).toString();
74 * Add subclass-specific attributes to the output {@link #toString()} output. Since
75 * subclasses are prevented from overriding {@link #toString()} for consistency
76 * reasons, they can add their specific attributes to the resulting string by attaching
77 * attributes to the supplied {@link ToStringHelper}.
79 * @param toStringHelper ToStringHelper onto the attributes can be added
80 * @return ToStringHelper supplied as input argument.
82 protected abstract ToStringHelper addToStringAttributes(@NonNull ToStringHelper toStringHelper);
85 * Create a new YinTextSchemaSource with a specific source identifier and backed
86 * by ByteSource, which provides the actual InputStreams.
88 * @param identifier SourceIdentifier of the resulting schema source
89 * @param delegate Backing ByteSource instance
90 * @return A new YinTextSchemaSource
92 public static @NonNull YinTextSchemaSource delegateForByteSource(final SourceIdentifier identifier,
93 final ByteSource delegate) {
94 return new DelegatedYinTextSchemaSource(identifier, delegate);
97 public static @NonNull YinTextSchemaSource forFile(final File file) {
98 checkArgument(file.isFile(), "Supplied file %s is not a file", file);
99 return new YinTextFileSchemaSource(identifierFromFilename(file.getName()), file);
102 public static @NonNull YinTextSchemaSource forResource(final Class<?> clazz, final String resourceName) {
103 final String fileName = resourceName.substring(resourceName.lastIndexOf('/') + 1);
104 return new ResourceYinTextSchemaSource(identifierFromFilename(fileName),
105 Resources.getResource(clazz, resourceName));