2 * Copyright (c) 2020 PANTHEON.tech, s.r.o. 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.parser.rfc7950.repo;
10 import com.google.common.annotations.Beta;
11 import com.google.common.util.concurrent.Futures;
12 import java.io.IOException;
13 import org.eclipse.jdt.annotation.NonNull;
14 import org.opendaylight.yangtools.yang.ir.IRStatement;
15 import org.opendaylight.yangtools.yang.ir.YangIRSchemaSource;
16 import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
17 import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
18 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
19 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceTransformer;
20 import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource;
21 import org.opendaylight.yangtools.yang.parser.api.YangSyntaxErrorException;
22 import org.opendaylight.yangtools.yang.parser.rfc7950.antlr.IRSupport;
25 public final class TextToIRTransformer extends SchemaSourceTransformer<YangTextSource, YangIRSchemaSource> {
26 private TextToIRTransformer(final SchemaRepository provider, final SchemaSourceRegistry consumer) {
27 super(provider, YangTextSource.class, consumer, YangIRSchemaSource.class,
28 input -> Futures.immediateFuture(transformText(input)));
31 public static @NonNull TextToIRTransformer create(final SchemaRepository provider,
32 final SchemaSourceRegistry consumer) {
33 return new TextToIRTransformer(provider, consumer);
36 public static @NonNull YangIRSchemaSource transformText(final YangTextSource text)
37 throws YangSyntaxErrorException, IOException {
38 final IRStatement rootStatement = IRSupport.createStatement(YangStatementStreamSource.parseYangSource(text));
39 final String name = YangModelDependencyInfo.safeStringArgument(text.sourceId(), rootStatement, "name");
40 final String latestRevision = YangModelDependencyInfo.getLatestRevision(rootStatement, text.sourceId());
41 final SourceIdentifier sourceId = new SourceIdentifier(name, latestRevision);
43 return new YangIRSchemaSource(sourceId, rootStatement, text.symbolicName());