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