26884d9ff40f6e64d661571231180ca334f917a1
[yangtools.git] / yang / yang-model-api / src / main / java / org / opendaylight / yangtools / yang / model / repo / api / YangTextSchemaSource.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.model.repo.api;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11
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 javax.annotation.Nonnull;
22 import org.opendaylight.yangtools.concepts.Delegator;
23 import org.opendaylight.yangtools.yang.common.YangConstants;
24 import org.opendaylight.yangtools.yang.common.YangNames;
25
26 /**
27  * YANG text schema source representation. Exposes an RFC6020 text representation
28  * as an {@link InputStream}.
29  */
30 @Beta
31 public abstract class YangTextSchemaSource extends ByteSource implements YangSchemaSourceRepresentation {
32     private final SourceIdentifier identifier;
33
34     protected YangTextSchemaSource(final SourceIdentifier identifier) {
35         this.identifier = Preconditions.checkNotNull(identifier);
36     }
37
38     public static SourceIdentifier identifierFromFilename(final String name) {
39         checkArgument(name.endsWith(YangConstants.RFC6020_YANG_FILE_EXTENSION), "Filename %s does not have a .yang extension",
40             name);
41
42         final String baseName = name.substring(0, name.length() - YangConstants.RFC6020_YANG_FILE_EXTENSION.length());
43         final Entry<String, String> parsed = YangNames.parseFilename(baseName);
44         return RevisionSourceIdentifier.create(parsed.getKey(), Optional.fromNullable(parsed.getValue()));
45     }
46
47     /**
48      * {@inheritDoc}
49      */
50     @Override
51     public final SourceIdentifier getIdentifier() {
52         return identifier;
53     }
54
55     /**
56      * {@inheritDoc}
57      */
58     @Nonnull
59     @Override
60     public Class<? extends YangTextSchemaSource> getType() {
61         return YangTextSchemaSource.class;
62     }
63
64     @Override
65     public final String toString() {
66         return addToStringAttributes(MoreObjects.toStringHelper(this).add("identifier", identifier)).toString();
67     }
68
69     /**
70      * Add subclass-specific attributes to the output {@link #toString()} output. Since
71      * subclasses are prevented from overriding {@link #toString()} for consistency
72      * reasons, they can add their specific attributes to the resulting string by attaching
73      * attributes to the supplied {@link ToStringHelper}.
74      *
75      * @param toStringHelper ToStringHelper onto the attributes can be added
76      * @return ToStringHelper supplied as input argument.
77      */
78     protected abstract ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper);
79
80     /**
81      * Create a new YangTextSchemaSource with a specific source identifier and backed
82      * by ByteSource, which provides the actual InputStreams.
83      *
84      * @param identifier SourceIdentifier of the resulting schema source
85      * @param delegate Backing ByteSource instance
86      * @return A new YangTextSchemaSource
87      */
88     public static YangTextSchemaSource delegateForByteSource(final SourceIdentifier identifier, final ByteSource delegate) {
89         return new DelegatedYangTextSchemaSource(identifier, delegate);
90     }
91
92     private static final class DelegatedYangTextSchemaSource extends YangTextSchemaSource implements Delegator<ByteSource> {
93         private final ByteSource delegate;
94
95         private DelegatedYangTextSchemaSource(final SourceIdentifier identifier, final ByteSource delegate) {
96             super(identifier);
97             this.delegate = Preconditions.checkNotNull(delegate);
98         }
99
100         @Override
101         public ByteSource getDelegate() {
102             return delegate;
103         }
104
105         @Override
106         public InputStream openStream() throws IOException {
107             return delegate.openStream();
108         }
109
110         @Override
111         protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
112             return toStringHelper.add("delegate", delegate);
113         }
114     }
115 }