BUG-7054: support .xml for YIN files
[yangtools.git] / yang / yang-model-api / src / main / java / org / opendaylight / yangtools / yang / model / repo / api / YinTextSchemaSource.java
1 /*
2  * Copyright (c) 2015 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 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;
19
20 /**
21  * YIN text schema source representation. Exposes an RFC6020 XML representation as an {@link InputStream}.
22  */
23 @Beta
24 public abstract class YinTextSchemaSource extends ByteSource implements YinSchemaSourceRepresentation {
25     private final SourceIdentifier identifier;
26
27     protected YinTextSchemaSource(final SourceIdentifier identifier) {
28         this.identifier = Preconditions.checkNotNull(identifier);
29     }
30
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);
35         } else {
36             throw new IllegalArgumentException("Filename " + name + " does not have a .yin or .xml extension");
37         }
38
39         // FIXME: add revision-awareness
40         return SourceIdentifier.create(baseName, Optional.absent());
41     }
42
43     /**
44      * {@inheritDoc}
45      */
46     @Override
47     public final SourceIdentifier getIdentifier() {
48         return identifier;
49     }
50
51     /**
52      * {@inheritDoc}
53      */
54     @Override
55     public Class<? extends YinTextSchemaSource> getType() {
56         return YinTextSchemaSource.class;
57     }
58
59     @Override
60     public final String toString() {
61         return addToStringAttributes(MoreObjects.toStringHelper(this).add("identifier", identifier)).toString();
62     }
63
64     /**
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}.
69      *
70      * @param toStringHelper ToStringHelper onto the attributes can be added
71      * @return ToStringHelper supplied as input argument.
72      */
73     protected abstract ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper);
74
75     /**
76      * Create a new YinTextSchemaSource with a specific source identifier and backed
77      * by ByteSource, which provides the actual InputStreams.
78      *
79      * @param identifier SourceIdentifier of the resulting schema source
80      * @param delegate Backing ByteSource instance
81      * @return A new YinTextSchemaSource
82      */
83     public static YinTextSchemaSource delegateForByteSource(final SourceIdentifier identifier, final ByteSource delegate) {
84         return new DelegatedYinTextSchemaSource(identifier, delegate);
85     }
86
87     private static final class DelegatedYinTextSchemaSource extends YinTextSchemaSource implements Delegator<ByteSource> {
88         private final ByteSource delegate;
89
90         private DelegatedYinTextSchemaSource(final SourceIdentifier identifier, final ByteSource delegate) {
91             super(identifier);
92             this.delegate = Preconditions.checkNotNull(delegate);
93         }
94
95         @Override
96         public ByteSource getDelegate() {
97             return delegate;
98         }
99
100         @Override
101         public InputStream openStream() throws IOException {
102             return delegate.openStream();
103         }
104
105         @Override
106         protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
107             return toStringHelper.add("delegate", delegate);
108         }
109     }
110 }