BUG-7052: introduce YangStatementStreamSource
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / util / TextToASTTransformer.java
index aed627fd54aa0cadaa89535e8f14da77d4c6a9f1..b9b48ac2dade1feacbd0f04006c59795d71cc37b 100644 (file)
@@ -9,11 +9,11 @@
 package org.opendaylight.yangtools.yang.parser.util;
 
 import com.google.common.annotations.Beta;
-import com.google.common.base.Charsets;
 import com.google.common.util.concurrent.CheckedFuture;
 import com.google.common.util.concurrent.Futures;
 import java.io.IOException;
-import java.io.InputStream;
+import java.util.Optional;
+import javax.annotation.Nonnull;
 import org.antlr.v4.runtime.ParserRuleContext;
 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
 import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
@@ -21,7 +21,7 @@ import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
 import org.opendaylight.yangtools.yang.model.repo.util.SchemaSourceTransformer;
-import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
+import org.opendaylight.yangtools.yang.parser.rfc6020.repo.YangStatementStreamSource;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -32,24 +32,36 @@ import org.slf4j.LoggerFactory;
 @Beta
 public final class TextToASTTransformer extends SchemaSourceTransformer<YangTextSchemaSource, ASTSchemaSource> {
 
+    /**
+     * @deprecated Use {@link TextToASTTransformer#transformText(YangTextSchemaSource)} instead.
+     */
+    @Deprecated
     public static final class TextToASTTransformation implements Transformation<YangTextSchemaSource, ASTSchemaSource> {
         @Override
-        public CheckedFuture<ASTSchemaSource, SchemaSourceException> apply(final YangTextSchemaSource input) throws IOException, YangSyntaxErrorException {
-            try (InputStream is = input.openStream()) {
-                final ParserRuleContext ctx = new YangStatementSourceImpl(is).getYangAST();
-                LOG.debug("Model {} parsed successfully", input);
+        public CheckedFuture<ASTSchemaSource, SchemaSourceException> apply(@Nonnull final YangTextSchemaSource input)
+                throws IOException, YangSyntaxErrorException {
+            final YangStatementStreamSource src = YangStatementStreamSource.create(input);
 
-                //:TODO missing validation (YangModelBasicValidationListener should be re-implemented to new parser)
+            final ParserRuleContext ctx = src.getYangAST();
+            LOG.debug("Model {} parsed successfully", input);
 
-                // Backwards compatibility
-                final String text = input.asCharSource(Charsets.UTF_8).read();
+            //:TODO missing validation (YangModelBasicValidationListener should be re-implemented to new parser)
 
-                return Futures.immediateCheckedFuture(ASTSchemaSource.create(input.getIdentifier(), ctx, text));
-            }
+            final Optional<String> opt = input.getSymbolicName();
+            final ASTSchemaSource result = opt.isPresent()
+                    ? ASTSchemaSource.create(opt.get(), input.getIdentifier(), ctx)
+                            : ASTSchemaSource.create(input.getIdentifier(), ctx);
+
+            return Futures.immediateCheckedFuture(result);
         }
     }
 
+    /**
+     * @deprecated Use {@link TextToASTTransformer#transformText(YangTextSchemaSource)} instead.
+     */
+    @Deprecated
     public static final TextToASTTransformation TRANSFORMATION = new TextToASTTransformation();
+
     private static final Logger LOG = LoggerFactory.getLogger(TextToASTTransformer.class);
 
     private TextToASTTransformer(final SchemaRepository provider, final SchemaSourceRegistry consumer) {
@@ -59,4 +71,9 @@ public final class TextToASTTransformer extends SchemaSourceTransformer<YangText
     public static TextToASTTransformer create(final SchemaRepository provider, final SchemaSourceRegistry consumer) {
         return new TextToASTTransformer(provider, consumer);
     }
+
+    public static ASTSchemaSource transformText(final YangTextSchemaSource text) throws SchemaSourceException,
+            IOException, YangSyntaxErrorException {
+        return TRANSFORMATION.apply(text).checkedGet();
+    }
 }