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