From: Robert Varga Date: Wed, 5 Sep 2018 17:39:45 +0000 (+0200) Subject: Cleanup leafref path parsing X-Git-Tag: v2.0.11~17 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=daa0958e124cbc8391fcde6cccd836ce835d538f;p=yangtools.git Cleanup leafref path parsing We have a String on input and LeafRefPathParserImpl uses ANTLR's CharStreams -- hence it makes no sense to bounce input through an InputStream. Remove this atrocity, eliminating an IOException/IllegalStateException error path in process. JIRA: YANGTOOLS-892 Change-Id: I78eadf30da6a1e64621ca4c14dfa13e84524b591 Signed-off-by: Robert Varga (cherry picked from commit 23b7ff7e2e39e0d520e249ae5b256746acef5abd) --- diff --git a/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/leafref/LeafRefContext.java b/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/leafref/LeafRefContext.java index 7c05720493..4445ff3175 100644 --- a/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/leafref/LeafRefContext.java +++ b/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/leafref/LeafRefContext.java @@ -8,7 +8,6 @@ package org.opendaylight.yangtools.yang.data.impl.leafref; import com.google.common.collect.ImmutableMap; -import java.io.IOException; import java.util.Map; import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.model.api.Module; @@ -53,8 +52,6 @@ public final class LeafRefContext { return new LeafRefContextTreeBuilder(ctx).buildLeafRefContextTree(); } catch (LeafRefYangSyntaxErrorException e) { throw new IllegalArgumentException(e); - } catch (IOException e) { - throw new IllegalStateException(e); } } diff --git a/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/leafref/LeafRefContextTreeBuilder.java b/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/leafref/LeafRefContextTreeBuilder.java index 0a23636320..75151eb968 100644 --- a/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/leafref/LeafRefContextTreeBuilder.java +++ b/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/leafref/LeafRefContextTreeBuilder.java @@ -9,9 +9,6 @@ package org.opendaylight.yangtools.yang.data.impl.leafref; import static com.google.common.base.Preconditions.checkNotNull; -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.nio.charset.StandardCharsets; import java.util.Collection; import java.util.LinkedList; import java.util.List; @@ -36,7 +33,7 @@ final class LeafRefContextTreeBuilder { this.schemaContext = schemaContext; } - LeafRefContext buildLeafRefContextTree() throws IOException, LeafRefYangSyntaxErrorException { + LeafRefContext buildLeafRefContextTree() throws LeafRefYangSyntaxErrorException { final LeafRefContextBuilder rootBuilder = new LeafRefContextBuilder(schemaContext.getQName(), schemaContext.getPath(), schemaContext); @@ -68,7 +65,7 @@ final class LeafRefContextTreeBuilder { } private LeafRefContext buildLeafRefContextReferencingTree(final DataSchemaNode node, final Module currentModule) - throws IOException, LeafRefYangSyntaxErrorException { + throws LeafRefYangSyntaxErrorException { final LeafRefContextBuilder currentLeafRefContextBuilder = new LeafRefContextBuilder(node.getQName(), node.getPath(), schemaContext); @@ -97,17 +94,13 @@ final class LeafRefContextTreeBuilder { if (type instanceof LeafrefTypeDefinition) { final LeafrefTypeDefinition leafrefType = (LeafrefTypeDefinition) type; final String leafRefPathString = leafrefType.getPathStatement().toString(); - - currentLeafRefContextBuilder.setLeafRefTargetPathString(leafRefPathString); - currentLeafRefContextBuilder.setReferencing(true); - final LeafRefPathParserImpl leafRefPathParser = new LeafRefPathParserImpl(schemaContext, checkNotNull(getBaseTypeModule(leafrefType), "Unable to find base module for leafref %s", node), node); + final LeafRefPath leafRefPath = leafRefPathParser.parseLeafRefPath(leafRefPathString); - final LeafRefPath leafRefPath = leafRefPathParser.parseLeafRefPathSourceToSchemaPath( - new ByteArrayInputStream(leafRefPathString.getBytes(StandardCharsets.UTF_8))); - + currentLeafRefContextBuilder.setLeafRefTargetPathString(leafRefPathString); + currentLeafRefContextBuilder.setReferencing(true); currentLeafRefContextBuilder.setLeafRefTargetPath(leafRefPath); final LeafRefContext currentLeafRefContext = currentLeafRefContextBuilder.build(); @@ -132,7 +125,7 @@ final class LeafRefContextTreeBuilder { } private LeafRefContext buildLeafRefContextReferencedByTree(final DataSchemaNode node, final Module currentModule) - throws IOException,LeafRefYangSyntaxErrorException { + throws LeafRefYangSyntaxErrorException { final LeafRefContextBuilder currentLeafRefContextBuilder = new LeafRefContextBuilder(node.getQName(), node.getPath(), schemaContext); if (node instanceof DataNodeContainer) { diff --git a/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/leafref/LeafRefPathParserImpl.java b/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/leafref/LeafRefPathParserImpl.java index ab70aaa4cb..3c5877839a 100644 --- a/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/leafref/LeafRefPathParserImpl.java +++ b/yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/leafref/LeafRefPathParserImpl.java @@ -7,8 +7,6 @@ */ package org.opendaylight.yangtools.yang.data.impl.leafref; -import java.io.IOException; -import java.io.InputStream; import org.antlr.v4.runtime.CharStreams; import org.antlr.v4.runtime.CommonTokenStream; import org.antlr.v4.runtime.tree.ParseTreeWalker; @@ -28,9 +26,8 @@ final class LeafRefPathParserImpl { this.node = currentNode; } - public LeafRefPath parseLeafRefPathSourceToSchemaPath(final InputStream stream) throws IOException, - LeafRefYangSyntaxErrorException { - final Path_argContext pathCtx = parseLeafRefPathSource(stream); + LeafRefPath parseLeafRefPath(final String path) throws LeafRefYangSyntaxErrorException { + final Path_argContext pathCtx = parseLeafRefPathSource(path); final ParseTreeWalker walker = new ParseTreeWalker(); final LeafRefPathParserListenerImpl leafRefPathParserListenerImpl = new LeafRefPathParserListenerImpl( @@ -40,9 +37,8 @@ final class LeafRefPathParserImpl { return leafRefPathParserListenerImpl.getLeafRefPath(); } - private Path_argContext parseLeafRefPathSource(final InputStream stream) throws IOException, - LeafRefYangSyntaxErrorException { - final LeafRefPathLexer lexer = new LeafRefPathLexer(CharStreams.fromStream(stream)); + private Path_argContext parseLeafRefPathSource(final String path) throws LeafRefYangSyntaxErrorException { + final LeafRefPathLexer lexer = new LeafRefPathLexer(CharStreams.fromString(path)); final CommonTokenStream tokens = new CommonTokenStream(lexer); final LeafRefPathParser parser = new LeafRefPathParser(tokens); parser.removeErrorListeners();