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.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 * YIN text schema source representation. Exposes an RFC6020 XML representation as an {@link InputStream}.
25 public abstract class YinTextSchemaSource extends ByteSource implements YinSchemaSourceRepresentation {
26 private final SourceIdentifier identifier;
28 protected YinTextSchemaSource(final SourceIdentifier identifier) {
29 this.identifier = Preconditions.checkNotNull(identifier);
32 public static SourceIdentifier identifierFromFilename(final String name) {
33 checkArgument(name.endsWith(".yin"), "Filename %s does not have a .yin extension", name);
34 // FIXME: add revision-awareness
35 return SourceIdentifier.create(name.substring(0, name.length() - 4), Optional.<String>absent());
42 public final SourceIdentifier getIdentifier() {
50 public Class<? extends YinTextSchemaSource> getType() {
51 return YinTextSchemaSource.class;
55 public final String toString() {
56 return addToStringAttributes(MoreObjects.toStringHelper(this).add("identifier", identifier)).toString();
60 * Add subclass-specific attributes to the output {@link #toString()} output. Since
61 * subclasses are prevented from overriding {@link #toString()} for consistency
62 * reasons, they can add their specific attributes to the resulting string by attaching
63 * attributes to the supplied {@link ToStringHelper}.
65 * @param toStringHelper ToStringHelper onto the attributes can be added
66 * @return ToStringHelper supplied as input argument.
68 protected abstract ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper);
71 * Create a new YinTextSchemaSource with a specific source identifier and backed
72 * by ByteSource, which provides the actual InputStreams.
74 * @param identifier SourceIdentifier of the resulting schema source
75 * @param delegate Backing ByteSource instance
76 * @return A new YinTextSchemaSource
78 public static YinTextSchemaSource delegateForByteSource(final SourceIdentifier identifier, final ByteSource delegate) {
79 return new DelegatedYinTextSchemaSource(identifier, delegate);
82 private static final class DelegatedYinTextSchemaSource extends YinTextSchemaSource implements Delegator<ByteSource> {
83 private final ByteSource delegate;
85 private DelegatedYinTextSchemaSource(final SourceIdentifier identifier, final ByteSource delegate) {
87 this.delegate = Preconditions.checkNotNull(delegate);
91 public ByteSource getDelegate() {
96 public InputStream openStream() throws IOException {
97 return delegate.openStream();
101 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
102 return toStringHelper.add("delegate", delegate);