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 com.google.common.annotations.Beta;
11 import com.google.common.base.MoreObjects;
12 import com.google.common.base.MoreObjects.ToStringHelper;
13 import com.google.common.base.Optional;
14 import com.google.common.base.Preconditions;
15 import com.google.common.io.ByteSource;
16 import java.io.IOException;
17 import java.io.InputStream;
18 import org.opendaylight.yangtools.concepts.Delegator;
21 * YIN text schema source representation. Exposes an RFC6020 XML representation as an {@link InputStream}.
24 public abstract class YinTextSchemaSource extends ByteSource implements YinSchemaSourceRepresentation {
25 private final SourceIdentifier identifier;
27 protected YinTextSchemaSource(final SourceIdentifier identifier) {
28 this.identifier = Preconditions.checkNotNull(identifier);
31 public static SourceIdentifier identifierFromFilename(final String name) {
32 final String baseName;
33 if (name.endsWith(".xml") || name.endsWith(".yin")) {
34 baseName = name.substring(0, name.length() - 4);
36 throw new IllegalArgumentException("Filename " + name + " does not have a .yin or .xml extension");
39 // FIXME: add revision-awareness
40 return SourceIdentifier.create(baseName, Optional.absent());
47 public final SourceIdentifier getIdentifier() {
55 public Class<? extends YinTextSchemaSource> getType() {
56 return YinTextSchemaSource.class;
60 public final String toString() {
61 return addToStringAttributes(MoreObjects.toStringHelper(this).add("identifier", identifier)).toString();
65 * Add subclass-specific attributes to the output {@link #toString()} output. Since
66 * subclasses are prevented from overriding {@link #toString()} for consistency
67 * reasons, they can add their specific attributes to the resulting string by attaching
68 * attributes to the supplied {@link ToStringHelper}.
70 * @param toStringHelper ToStringHelper onto the attributes can be added
71 * @return ToStringHelper supplied as input argument.
73 protected abstract ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper);
76 * Create a new YinTextSchemaSource with a specific source identifier and backed
77 * by ByteSource, which provides the actual InputStreams.
79 * @param identifier SourceIdentifier of the resulting schema source
80 * @param delegate Backing ByteSource instance
81 * @return A new YinTextSchemaSource
83 public static YinTextSchemaSource delegateForByteSource(final SourceIdentifier identifier, final ByteSource delegate) {
84 return new DelegatedYinTextSchemaSource(identifier, delegate);
87 private static final class DelegatedYinTextSchemaSource extends YinTextSchemaSource implements Delegator<ByteSource> {
88 private final ByteSource delegate;
90 private DelegatedYinTextSchemaSource(final SourceIdentifier identifier, final ByteSource delegate) {
92 this.delegate = Preconditions.checkNotNull(delegate);
96 public ByteSource getDelegate() {
101 public InputStream openStream() throws IOException {
102 return delegate.openStream();
106 protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
107 return toStringHelper.add("delegate", delegate);