Cleanup leafref path parsing 95/75795/1
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 5 Sep 2018 17:39:45 +0000 (19:39 +0200)
committerRobert Varga <nite@hq.sk>
Thu, 6 Sep 2018 07:29:48 +0000 (07:29 +0000)
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 <robert.varga@pantheon.tech>
(cherry picked from commit 23b7ff7e2e39e0d520e249ae5b256746acef5abd)

yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/leafref/LeafRefContext.java
yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/leafref/LeafRefContextTreeBuilder.java
yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/leafref/LeafRefPathParserImpl.java

index 7c0572049336a30e61ad74fa7b20084a5bb0cb37..4445ff31757d8f8297d0f459e77a28adfcf9d40c 100644 (file)
@@ -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);
         }
     }
 
index 0a23636320ff0dd078c06a2844c5d1e777f83f0c..75151eb968f63f04b497446f697a534feb4265c4 100644 (file)
@@ -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) {
index ab70aaa4cb0685ab84c1a105e0e8cc34c0674e61..3c5877839a22a598b8b6229c56f0b4e4817c154d 100644 (file)
@@ -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();