/*
- * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
+ * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
+ *
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
+
package org.opendaylight.yangtools.yang.parser.util;
import com.google.common.annotations.Beta;
-import com.google.common.base.Charsets;
-import com.google.common.io.ByteSource;
import com.google.common.util.concurrent.CheckedFuture;
import com.google.common.util.concurrent.Futures;
-
import java.io.IOException;
-import java.io.InputStream;
-
-import org.antlr.v4.runtime.tree.ParseTreeWalker;
-import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.YangContext;
+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;
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.impl.YangModelBasicValidationListener;
-import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
+import org.opendaylight.yangtools.yang.parser.rfc6020.repo.YangStatementStreamSource;
import org.slf4j.Logger;
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 YangContext ctx = YangParserImpl.parseYangSource(is);
- LOG.debug("Model {} parsed successfully", input);
+ public CheckedFuture<ASTSchemaSource, SchemaSourceException> apply(@Nonnull final YangTextSchemaSource input)
+ throws IOException, YangSyntaxErrorException {
+ final YangStatementStreamSource src = YangStatementStreamSource.create(input);
- final ParseTreeWalker walker = new ParseTreeWalker();
- final YangModelBasicValidationListener validator = new YangModelBasicValidationListener();
- walker.walk(validator, ctx);
- LOG.debug("Model {} validated successfully", input);
+ final ParserRuleContext ctx = src.getYangAST();
+ LOG.debug("Model {} parsed successfully", input);
- // Backwards compatibility
- final String text = new ByteSource() {
- @Override
- public InputStream openStream() throws IOException {
- return input.openStream();
- }
- }.asCharSource(Charsets.UTF_8).read();
+ //:TODO missing validation (YangModelBasicValidationListener should be re-implemented to new parser)
- return Futures.immediateCheckedFuture(ASTSchemaSource.create(input.getIdentifier().getName(), 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) {
super(provider, YangTextSchemaSource.class, consumer, ASTSchemaSource.class, TRANSFORMATION);
}
- public static final TextToASTTransformer create(final SchemaRepository provider, final SchemaSourceRegistry consumer) {
+ 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();
+ }
}