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;
11 import com.google.common.annotations.Beta;
12 import com.google.common.base.MoreObjects;
13 import com.google.common.base.MoreObjects.ToStringHelper;
14 import com.google.common.base.Optional;
15 import com.google.common.base.Preconditions;
16 import com.google.common.io.ByteSource;
17 import java.io.IOException;
18 import java.io.InputStream;
19 import org.opendaylight.yangtools.concepts.Delegator;
22 * YANG text schema source representation. Exposes an RFC6020 text representation
23 * as an {@link InputStream}.
26 public abstract class YangTextSchemaSource extends ByteSource implements SchemaSourceRepresentation {
27 private final SourceIdentifier identifier;
29 protected YangTextSchemaSource(final SourceIdentifier identifier) {
30 this.identifier = Preconditions.checkNotNull(identifier);
33 public static SourceIdentifier identifierFromFilename(final String name) {
34 checkArgument(name.endsWith(".yang"), "Filename %s does not have a .yang extension", name);
35 // FIXME: add revision-awareness
36 return SourceIdentifier.create(name.substring(0, name.length() - 5), Optional.<String>absent());
43 public final SourceIdentifier getIdentifier() {
51 public Class<? extends YangTextSchemaSource> getType() {
52 return YangTextSchemaSource.class;
56 public final String toString() {
57 return addToStringAttributes(MoreObjects.toStringHelper(this).add("identifier", identifier)).toString();
61 * Add subclass-specific attributes to the output {@link #toString()} output. Since
62 * subclasses are prevented from overriding {@link #toString()} for consistency
63 * reasons, they can add their specific attributes to the resulting string by attaching
64 * attributes to the supplied {@link ToStringHelper}.
66 * @param toStringHelper ToStringHelper onto the attributes can be added
67 * @return ToStringHelper supplied as input argument.
69 protected abstract ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper);
72 * Create a new YangTextSchemaSource with a specific source identifier and backed
73 * by ByteSource, which provides the actual InputStreams.
75 * @param identifier SourceIdentifier of the resulting schema source
76 * @param delegate Backing ByteSource instance
77 * @return A new YangTextSchemaSource
79 public static YangTextSchemaSource delegateForByteSource(final SourceIdentifier identifier, final ByteSource delegate) {
80 return new DelegatedYangTextSchemaSource(identifier, delegate);
83 private static final class DelegatedYangTextSchemaSource extends YangTextSchemaSource implements Delegator<ByteSource> {
84 private final ByteSource delegate;
86 private DelegatedYangTextSchemaSource(final SourceIdentifier identifier, final ByteSource delegate) {
88 this.delegate = Preconditions.checkNotNull(delegate);
92 public final ByteSource getDelegate() {
97 public InputStream openStream() throws IOException {
98 return delegate.openStream();
102 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
103 return toStringHelper.add("delegate", delegate);