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.util;
11 import com.google.common.annotations.Beta;
12 import com.google.common.util.concurrent.CheckedFuture;
13 import com.google.common.util.concurrent.Futures;
14 import java.io.IOException;
15 import java.io.InputStream;
16 import java.nio.charset.StandardCharsets;
17 import org.antlr.v4.runtime.ParserRuleContext;
18 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
19 import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
20 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
21 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
22 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
23 import org.opendaylight.yangtools.yang.model.repo.util.SchemaSourceTransformer;
24 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
29 * A {@link SchemaSourceTransformer} which handles translation of models from
30 * {@link YangTextSchemaSource} representation into {@link ASTSchemaSource}.
33 public final class TextToASTTransformer extends SchemaSourceTransformer<YangTextSchemaSource, ASTSchemaSource> {
35 public static final class TextToASTTransformation implements Transformation<YangTextSchemaSource, ASTSchemaSource> {
37 public CheckedFuture<ASTSchemaSource, SchemaSourceException> apply(final YangTextSchemaSource input) throws IOException, YangSyntaxErrorException {
38 try (InputStream is = input.openStream()) {
39 final ParserRuleContext ctx = new YangStatementSourceImpl(is).getYangAST();
40 LOG.debug("Model {} parsed successfully", input);
42 //:TODO missing validation (YangModelBasicValidationListener should be re-implemented to new parser)
44 // Backwards compatibility
45 final String text = input.asCharSource(StandardCharsets.UTF_8).read();
47 return Futures.immediateCheckedFuture(ASTSchemaSource.create(input.getIdentifier(), ctx, text));
52 public static final TextToASTTransformation TRANSFORMATION = new TextToASTTransformation();
53 private static final Logger LOG = LoggerFactory.getLogger(TextToASTTransformer.class);
55 private TextToASTTransformer(final SchemaRepository provider, final SchemaSourceRegistry consumer) {
56 super(provider, YangTextSchemaSource.class, consumer, ASTSchemaSource.class, TRANSFORMATION);
59 public static TextToASTTransformer create(final SchemaRepository provider, final SchemaSourceRegistry consumer) {
60 return new TextToASTTransformer(provider, consumer);