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.spi.source;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.base.MoreObjects;
14 import com.google.common.base.MoreObjects.ToStringHelper;
15 import com.google.common.io.ByteSource;
16 import com.google.common.io.Resources;
17 import java.io.InputStream;
18 import java.nio.file.Files;
19 import java.nio.file.Path;
20 import org.eclipse.jdt.annotation.NonNull;
21 import org.opendaylight.yangtools.yang.common.YangConstants;
22 import org.opendaylight.yangtools.yang.common.YangNames;
23 import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
24 import org.opendaylight.yangtools.yang.model.api.source.YinSourceRepresentation;
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}.
31 public abstract class YinTextSource extends ByteSource implements YinSourceRepresentation {
32 private static final Logger LOG = LoggerFactory.getLogger(YinTextSource.class);
33 private static final String XML_EXTENSION = ".xml";
35 private final @NonNull SourceIdentifier sourceId;
37 protected YinTextSource(final SourceIdentifier sourceId) {
38 this.sourceId = requireNonNull(sourceId);
41 public static @NonNull SourceIdentifier identifierFromFilename(final String name) {
42 final String baseName;
43 if (name.endsWith(YangConstants.RFC6020_YIN_FILE_EXTENSION)) {
44 baseName = name.substring(0, name.length() - YangConstants.RFC6020_YIN_FILE_EXTENSION.length());
45 } else if (name.endsWith(XML_EXTENSION)) {
46 // FIXME: BUG-7061: remove this once we do not need it
47 LOG.warn("XML file {} being loaded as YIN", name);
48 baseName = name.substring(0, name.length() - XML_EXTENSION.length());
50 throw new IllegalArgumentException("Filename " + name + " does not have a .yin or .xml extension");
53 final var parsed = YangNames.parseFilename(baseName);
54 return new SourceIdentifier(parsed.getKey(), parsed.getValue());
58 public final SourceIdentifier sourceId() {
63 public final Class<YinTextSource> getType() {
64 return YinTextSource.class;
68 public final String toString() {
69 return addToStringAttributes(MoreObjects.toStringHelper(this).omitNullValues()).toString();
73 * Add subclass-specific attributes to the output {@link #toString()} output. Since
74 * subclasses are prevented from overriding {@link #toString()} for consistency
75 * reasons, they can add their specific attributes to the resulting string by attaching
76 * attributes to the supplied {@link ToStringHelper}.
78 * @param toStringHelper ToStringHelper onto the attributes can be added
79 * @return ToStringHelper supplied as input argument.
81 protected ToStringHelper addToStringAttributes(final @NonNull ToStringHelper toStringHelper) {
82 return toStringHelper.add("identifier", sourceId);
86 * Create a new YinTextSchemaSource with a specific source identifier and backed
87 * by ByteSource, which provides the actual InputStreams.
89 * @param identifier SourceIdentifier of the resulting schema source
90 * @param delegate Backing ByteSource instance
91 * @return A new YinTextSchemaSource
93 public static @NonNull YinTextSource delegateForByteSource(final SourceIdentifier identifier,
94 final ByteSource delegate) {
95 return new DelegatedYinTextSource(identifier, delegate);
98 public static @NonNull YinTextSource forPath(final Path path) {
99 checkArgument(Files.isRegularFile(path), "Supplied path %s is not a regular file", path);
100 return new YinTextFileSource(identifierFromFilename(path.toFile().getName()), path);
103 public static @NonNull YinTextSource forResource(final Class<?> clazz, final String resourceName) {
104 final String fileName = resourceName.substring(resourceName.lastIndexOf('/') + 1);
105 return new ResourceYinTextSource(identifierFromFilename(fileName),
106 Resources.getResource(clazz, resourceName));