BUG-869: remove empty statements
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / util / TextToASTTransformer.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
3  * This program and the accompanying materials are made available under the
4  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
5  * and is available at http://www.eclipse.org/legal/epl-v10.html
6  */
7 package org.opendaylight.yangtools.yang.parser.util;
8
9 import com.google.common.annotations.Beta;
10 import com.google.common.base.Charsets;
11 import com.google.common.io.ByteSource;
12 import com.google.common.util.concurrent.CheckedFuture;
13 import com.google.common.util.concurrent.Futures;
14
15 import java.io.IOException;
16 import java.io.InputStream;
17
18 import org.antlr.v4.runtime.tree.ParseTreeWalker;
19 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.YangContext;
20 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
21 import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
22 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
23 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
24 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
25 import org.opendaylight.yangtools.yang.model.repo.util.SchemaSourceTransformer;
26 import org.opendaylight.yangtools.yang.parser.impl.YangModelBasicValidationListener;
27 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 /**
32  * A {@link SchemaSourceTransformer} which handles translation of models from
33  * {@link YangTextSchemaSource} representation into {@link ASTSchemaSource}.
34  */
35 @Beta
36 public final class TextToASTTransformer extends SchemaSourceTransformer<YangTextSchemaSource, ASTSchemaSource> {
37     public static final class TextToASTTransformation implements Transformation<YangTextSchemaSource, ASTSchemaSource> {
38         @Override
39         public CheckedFuture<ASTSchemaSource, SchemaSourceException> apply(final YangTextSchemaSource input) throws IOException, YangSyntaxErrorException {
40             try (InputStream is = input.openStream()) {
41                 final YangContext ctx = YangParserImpl.parseYangSource(is);
42                 LOG.debug("Model {} parsed successfully", input);
43
44                 final ParseTreeWalker walker = new ParseTreeWalker();
45                 final YangModelBasicValidationListener validator = new YangModelBasicValidationListener();
46                 walker.walk(validator, ctx);
47                 LOG.debug("Model {} validated successfully", input);
48
49                 // Backwards compatibility
50                 final String text = new ByteSource() {
51                     @Override
52                     public InputStream openStream() throws IOException {
53                         return input.openStream();
54                     }
55                 }.asCharSource(Charsets.UTF_8).read();
56
57                 return Futures.immediateCheckedFuture(ASTSchemaSource.create(input.getIdentifier().getName(), ctx, text));
58             }
59         }
60     }
61
62     public static final TextToASTTransformation TRANSFORMATION = new TextToASTTransformation();
63     private static final Logger LOG = LoggerFactory.getLogger(TextToASTTransformer.class);
64
65     private TextToASTTransformer(final SchemaRepository provider, final SchemaSourceRegistry consumer) {
66         super(provider, YangTextSchemaSource.class, consumer, ASTSchemaSource.class, TRANSFORMATION);
67     }
68
69     public static final TextToASTTransformer create(final SchemaRepository provider, final SchemaSourceRegistry consumer) {
70         return new TextToASTTransformer(provider, consumer);
71     }
72 }