Expose URLYangTextSource
[yangtools.git] / model / yang-model-spi / src / main / java / org / opendaylight / yangtools / yang / model / spi / source / YangTextSource.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.spi.source;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.base.MoreObjects;
13 import com.google.common.base.MoreObjects.ToStringHelper;
14 import com.google.common.io.CharSource;
15 import java.io.File;
16 import java.io.InputStream;
17 import java.nio.charset.Charset;
18 import java.nio.charset.StandardCharsets;
19 import java.nio.file.Files;
20 import java.nio.file.Path;
21 import org.eclipse.jdt.annotation.NonNull;
22 import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
23 import org.opendaylight.yangtools.yang.model.api.source.YangSourceRepresentation;
24
25 /**
26  * YANG text schema source representation. Exposes an RFC6020 or RFC7950 text representation as an {@link InputStream}.
27  */
28 public abstract class YangTextSource extends CharSource implements YangSourceRepresentation {
29     private final @NonNull SourceIdentifier sourceId;
30
31     protected YangTextSource(final SourceIdentifier sourceId) {
32         this.sourceId = requireNonNull(sourceId);
33     }
34
35     /**
36      * Create a new YangTextSchemaSource backed by a {@link File} with {@link SourceIdentifier} derived from the file
37      * name.
38      *
39      * @param path Backing path
40      * @return A new YangTextSchemaSource
41      * @throws IllegalArgumentException if the file name has invalid format or if the supplied File is not a file
42      * @throws NullPointerException if file is {@code null}
43      */
44     public static @NonNull YangTextSource forPath(final Path path) {
45         // FIXME: do not use .toFile() here
46         return forPath(path, SourceIdentifier.ofYangFileName(path.toFile().getName()));
47     }
48
49     /**
50      * Create a new YangTextSchemaSource backed by a {@link File} and specified {@link SourceIdentifier}.
51      *
52      * @param path Backing path
53      * @param identifier source identifier
54      * @return A new YangTextSchemaSource
55      * @throws NullPointerException if any argument is {@code null}
56      * @throws IllegalArgumentException if the supplied path is not a regular file
57      */
58     public static @NonNull YangTextSource forPath(final Path path, final SourceIdentifier identifier) {
59         return forPath(path, identifier, StandardCharsets.UTF_8);
60     }
61
62     /**
63      * Create a new YangTextSchemaSource backed by a {@link File} and specified {@link SourceIdentifier}.
64      *
65      * @param path Backing path
66      * @param identifier Source identifier
67      * @param charset expected stream character set
68      * @return A new YangTextSchemaSource
69      * @throws NullPointerException if any argument is {@code null}
70      * @throws IllegalArgumentException if the supplied path is not a regular file
71      */
72     public static @NonNull YangTextSource forPath(final Path path, final SourceIdentifier identifier,
73             final Charset charset) {
74         if (Files.isRegularFile(path)) {
75             return new YangTextFileSource(identifier, path, charset);
76         }
77         throw new IllegalArgumentException("Supplied path " + path + " is not a regular file");
78     }
79
80     @Override
81     public final Class<YangTextSource> getType() {
82         return YangTextSource.class;
83     }
84
85     @Override
86     public final SourceIdentifier sourceId() {
87         return sourceId;
88     }
89
90     @Override
91     public final String toString() {
92         return addToStringAttributes(MoreObjects.toStringHelper(this).omitNullValues()).toString();
93     }
94
95     /**
96      * Add subclass-specific attributes to the output {@link #toString()} output. Since
97      * subclasses are prevented from overriding {@link #toString()} for consistency
98      * reasons, they can add their specific attributes to the resulting string by attaching
99      * attributes to the supplied {@link ToStringHelper}.
100      *
101      * @param toStringHelper ToStringHelper onto the attributes can be added
102      * @return ToStringHelper supplied as input argument.
103      */
104     protected ToStringHelper addToStringAttributes(final @NonNull ToStringHelper toStringHelper) {
105         return toStringHelper.add("identifier", sourceId);
106     }
107 }