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;
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 java.io.IOException;
19 import java.io.InputStream;
20 import java.util.Map.Entry;
21 import javax.annotation.Nonnull;
22 import org.opendaylight.yangtools.concepts.Delegator;
23 import org.opendaylight.yangtools.yang.common.YangConstants;
24 import org.opendaylight.yangtools.yang.common.YangNames;
27 * YANG text schema source representation. Exposes an RFC6020 text representation
28 * as an {@link InputStream}.
31 public abstract class YangTextSchemaSource extends ByteSource implements YangSchemaSourceRepresentation {
32 private final SourceIdentifier identifier;
34 protected YangTextSchemaSource(final SourceIdentifier identifier) {
35 this.identifier = Preconditions.checkNotNull(identifier);
38 public static SourceIdentifier identifierFromFilename(final String name) {
39 checkArgument(name.endsWith(YangConstants.RFC6020_YANG_FILE_EXTENSION), "Filename %s does not have a .yang extension",
42 final String baseName = name.substring(0, name.length() - YangConstants.RFC6020_YANG_FILE_EXTENSION.length());
43 final Entry<String, String> parsed = YangNames.parseFilename(baseName);
44 return RevisionSourceIdentifier.create(parsed.getKey(), Optional.fromNullable(parsed.getValue()));
51 public final SourceIdentifier getIdentifier() {
60 public Class<? extends YangTextSchemaSource> getType() {
61 return YangTextSchemaSource.class;
65 public final String toString() {
66 return addToStringAttributes(MoreObjects.toStringHelper(this).add("identifier", identifier)).toString();
70 * Add subclass-specific attributes to the output {@link #toString()} output. Since
71 * subclasses are prevented from overriding {@link #toString()} for consistency
72 * reasons, they can add their specific attributes to the resulting string by attaching
73 * attributes to the supplied {@link ToStringHelper}.
75 * @param toStringHelper ToStringHelper onto the attributes can be added
76 * @return ToStringHelper supplied as input argument.
78 protected abstract ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper);
81 * Create a new YangTextSchemaSource with a specific source identifier and backed
82 * by ByteSource, which provides the actual InputStreams.
84 * @param identifier SourceIdentifier of the resulting schema source
85 * @param delegate Backing ByteSource instance
86 * @return A new YangTextSchemaSource
88 public static YangTextSchemaSource delegateForByteSource(final SourceIdentifier identifier, final ByteSource delegate) {
89 return new DelegatedYangTextSchemaSource(identifier, delegate);
92 private static final class DelegatedYangTextSchemaSource extends YangTextSchemaSource implements Delegator<ByteSource> {
93 private final ByteSource delegate;
95 private DelegatedYangTextSchemaSource(final SourceIdentifier identifier, final ByteSource delegate) {
97 this.delegate = Preconditions.checkNotNull(delegate);
101 public ByteSource getDelegate() {
106 public InputStream openStream() throws IOException {
107 return delegate.openStream();
111 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
112 return toStringHelper.add("delegate", delegate);