2 * Copyright (c) 2014 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.spi.source;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.base.MoreObjects;
13 import com.google.common.base.MoreObjects.ToStringHelper;
14 import com.google.common.io.CharSource;
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;
26 * YANG text schema source representation. Exposes an RFC6020 or RFC7950 text representation as an {@link InputStream}.
28 public abstract class YangTextSource extends CharSource implements YangSourceRepresentation {
29 private final @NonNull SourceIdentifier sourceId;
31 protected YangTextSource(final SourceIdentifier sourceId) {
32 this.sourceId = requireNonNull(sourceId);
36 * Create a new YangTextSchemaSource backed by a {@link File} with {@link SourceIdentifier} derived from the file
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}
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()));
50 * Create a new YangTextSchemaSource backed by a {@link File} and specified {@link SourceIdentifier}.
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
58 public static @NonNull YangTextSource forPath(final Path path, final SourceIdentifier identifier) {
59 return forPath(path, identifier, StandardCharsets.UTF_8);
63 * Create a new YangTextSchemaSource backed by a {@link File} and specified {@link SourceIdentifier}.
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
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);
77 throw new IllegalArgumentException("Supplied path " + path + " is not a regular file");
81 public final Class<YangTextSource> getType() {
82 return YangTextSource.class;
86 public final SourceIdentifier sourceId() {
91 public final String toString() {
92 return addToStringAttributes(MoreObjects.toStringHelper(this).omitNullValues()).toString();
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}.
101 * @param toStringHelper ToStringHelper onto the attributes can be added
102 * @return ToStringHelper supplied as input argument.
104 protected ToStringHelper addToStringAttributes(final @NonNull ToStringHelper toStringHelper) {
105 return toStringHelper.add("identifier", sourceId);