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
9 package org.opendaylight.yangtools.yang.parser.rfc6020.repo;
11 import com.google.common.annotations.Beta;
12 import com.google.common.util.concurrent.Futures;
13 import java.io.IOException;
14 import org.antlr.v4.runtime.ParserRuleContext;
15 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
16 import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
17 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
18 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
19 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
20 import org.opendaylight.yangtools.yang.model.repo.util.SchemaSourceTransformer;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
25 * A {@link SchemaSourceTransformer} which handles translation of models from
26 * {@link YangTextSchemaSource} representation into {@link ASTSchemaSource}.
29 public final class TextToASTTransformer extends SchemaSourceTransformer<YangTextSchemaSource, ASTSchemaSource> {
30 private static final Logger LOG = LoggerFactory.getLogger(TextToASTTransformer.class);
32 private TextToASTTransformer(final SchemaRepository provider, final SchemaSourceRegistry consumer) {
33 super(provider, YangTextSchemaSource.class, consumer, ASTSchemaSource.class,
34 input -> Futures.immediateFuture(transformText(input)));
37 public static TextToASTTransformer create(final SchemaRepository provider, final SchemaSourceRegistry consumer) {
38 return new TextToASTTransformer(provider, consumer);
41 public static ASTSchemaSource transformText(final YangTextSchemaSource text) throws SchemaSourceException,
42 IOException, YangSyntaxErrorException {
43 final YangStatementStreamSource src = YangStatementStreamSource.create(text);
44 final ParserRuleContext ctx = src.getYangAST();
45 LOG.debug("Model {} parsed successfully", text);
47 // TODO: missing validation (YangModelBasicValidationListener should be re-implemented to new parser)
49 return ASTSchemaSource.create(text.getIdentifier(), text.getSymbolicName().orElse(null), ctx);