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 org.opendaylight.yangtools.concepts.Delegator;
22 import org.opendaylight.yangtools.yang.common.YangConstants;
23 import org.opendaylight.yangtools.yang.common.YangNames;
26 * YANG text schema source representation. Exposes an RFC6020 text representation
27 * as an {@link InputStream}.
30 public abstract class YangTextSchemaSource extends ByteSource implements YangSchemaSourceRepresentation {
31 private final SourceIdentifier identifier;
33 protected YangTextSchemaSource(final SourceIdentifier identifier) {
34 this.identifier = Preconditions.checkNotNull(identifier);
37 public static SourceIdentifier identifierFromFilename(final String name) {
38 checkArgument(name.endsWith(YangConstants.RFC6020_YANG_FILE_EXTENSION), "Filename %s does not have a .yang extension",
41 final String baseName = name.substring(0, name.length() - YangConstants.RFC6020_YANG_FILE_EXTENSION.length());
42 final Entry<String, String> parsed = YangNames.parseFilename(baseName);
43 return RevisionSourceIdentifier.create(parsed.getKey(), Optional.fromNullable(parsed.getValue()));
50 public final SourceIdentifier getIdentifier() {
58 public Class<? extends YangTextSchemaSource> getType() {
59 return YangTextSchemaSource.class;
63 public final String toString() {
64 return addToStringAttributes(MoreObjects.toStringHelper(this).add("identifier", identifier)).toString();
68 * Add subclass-specific attributes to the output {@link #toString()} output. Since
69 * subclasses are prevented from overriding {@link #toString()} for consistency
70 * reasons, they can add their specific attributes to the resulting string by attaching
71 * attributes to the supplied {@link ToStringHelper}.
73 * @param toStringHelper ToStringHelper onto the attributes can be added
74 * @return ToStringHelper supplied as input argument.
76 protected abstract ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper);
79 * Create a new YangTextSchemaSource with a specific source identifier and backed
80 * by ByteSource, which provides the actual InputStreams.
82 * @param identifier SourceIdentifier of the resulting schema source
83 * @param delegate Backing ByteSource instance
84 * @return A new YangTextSchemaSource
86 public static YangTextSchemaSource delegateForByteSource(final SourceIdentifier identifier, final ByteSource delegate) {
87 return new DelegatedYangTextSchemaSource(identifier, delegate);
90 private static final class DelegatedYangTextSchemaSource extends YangTextSchemaSource implements Delegator<ByteSource> {
91 private final ByteSource delegate;
93 private DelegatedYangTextSchemaSource(final SourceIdentifier identifier, final ByteSource delegate) {
95 this.delegate = Preconditions.checkNotNull(delegate);
99 public ByteSource getDelegate() {
104 public InputStream openStream() throws IOException {
105 return delegate.openStream();
109 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
110 return toStringHelper.add("delegate", delegate);