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/eplv10.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.Objects;
14 import com.google.common.base.Objects.ToStringHelper;
15 import com.google.common.base.Optional;
16 import com.google.common.base.Preconditions;
17 import com.google.common.io.ByteSource;
19 import java.io.IOException;
20 import java.io.InputStream;
22 import org.opendaylight.yangtools.concepts.Delegator;
25 * YANG text schema source representation. Exposes an RFC6020 text representation
26 * as an {@link InputStream}.
29 public abstract class YangTextSchemaSource extends ByteSource implements SchemaSourceRepresentation {
30 private final SourceIdentifier identifier;
32 protected YangTextSchemaSource(final SourceIdentifier identifier) {
33 this.identifier = Preconditions.checkNotNull(identifier);
36 public static SourceIdentifier identifierFromFilename(final String name) {
37 checkArgument(name.endsWith(".yang"), "Filename %s does not have a .yang extension", name);
38 // FIXME: add revision-awareness
39 return SourceIdentifier.create(name.substring(0, name.length() - 5), Optional.<String>absent());
46 public final SourceIdentifier getIdentifier() {
54 public Class<? extends YangTextSchemaSource> getType() {
55 return YangTextSchemaSource.class;
59 public final String toString() {
60 return addToStringAttributes(Objects.toStringHelper(this).add("identifier", identifier)).toString();
64 * Add subclass-specific attributes to the output {@link #toString()} output. Since
65 * subclasses are prevented from overriding {@link #toString()} for consistency
66 * reasons, they can add their specific attributes to the resulting string by attaching
67 * attributes to the supplied {@link ToStringHelper}.
69 * @param toStringHelper ToStringHelper onto the attributes can be added
70 * @return ToStringHelper supplied as input argument.
72 protected abstract ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper);
75 * Create a new YangTextSchemaSource with a specific source identifier and backed
76 * by ByteSource, which provides the actual InputStreams.
78 * @param identifier SourceIdentifier of the resulting schema source
79 * @param delegate Backing ByteSource instance
80 * @return A new YangTextSchemaSource
82 public static YangTextSchemaSource delegateForByteSource(final SourceIdentifier identifier, final ByteSource delegate) {
83 return new DelegatedYangTextSchemaSource(identifier, delegate);
86 private static final class DelegatedYangTextSchemaSource extends YangTextSchemaSource implements Delegator<ByteSource> {
87 private final ByteSource delegate;
89 private DelegatedYangTextSchemaSource(final SourceIdentifier identifier, final ByteSource delegate) {
91 this.delegate = Preconditions.checkNotNull(delegate);
95 public final ByteSource getDelegate() {
100 public InputStream openStream() throws IOException {
101 return delegate.openStream();
105 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
106 return toStringHelper.add("delegate", delegate);