Merge "BUG-1143: binding-generator-impl: tests refactoring."
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / builder / impl / BuilderUtils.java
index 19daf45e8822afe39d6026c5f99ded4743cf7a92..5ca8f83120380af67c4f38453f3d8f9d2f29f24b 100644 (file)
@@ -7,6 +7,13 @@
  */
 package org.opendaylight.yangtools.yang.parser.builder.impl;
 
+import com.google.common.base.Function;
+import com.google.common.base.Optional;
+import com.google.common.base.Preconditions;
+import com.google.common.base.Splitter;
+import com.google.common.collect.Collections2;
+import com.google.common.io.ByteSource;
+
 import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileNotFoundException;
@@ -14,7 +21,6 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.net.URI;
 import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.Collection;
 import java.util.Date;
 import java.util.HashSet;
@@ -23,7 +29,6 @@ import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import java.util.TreeMap;
-
 import org.apache.commons.io.IOUtils;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.model.api.AnyXmlSchemaNode;
@@ -59,13 +64,6 @@ import org.opendaylight.yangtools.yang.parser.util.YangParseException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.google.common.base.Function;
-import com.google.common.base.Optional;
-import com.google.common.base.Preconditions;
-import com.google.common.base.Splitter;
-import com.google.common.collect.Collections2;
-import com.google.common.io.ByteSource;
-
 public final class BuilderUtils {
 
     private static final Logger LOG = LoggerFactory.getLogger(BuilderUtils.class);
@@ -109,40 +107,6 @@ public final class BuilderUtils {
         });
     }
 
-    /**
-     * Set string representation of source to ModuleBuilder.
-     *
-     * @param sourceToBuilder
-     *            source to module mapping
-     */
-    public static void setSourceToBuilder(final Map<ByteSource, ModuleBuilder> sourceToBuilder) throws IOException {
-        for (Map.Entry<ByteSource, ModuleBuilder> entry : sourceToBuilder.entrySet()) {
-            ModuleBuilder builder = entry.getValue();
-            ByteSource source = entry.getKey();
-
-            String content = null;
-            InputStream stream = null;
-            try {
-                stream = source.openStream();
-                content = IOUtils.toString(stream);
-            } finally {
-                if (stream != null) {
-                    try {
-                        stream.close();
-                    } catch (IOException e) {
-                        /*
-                         * Failed stream close does not prevent us from
-                         * continuing to work correctly, we just report that and
-                         * continue.
-                         */
-                        LOG.warn("Failed to close stream {}. Leaving stream unclosed.", stream, e);
-                    }
-                }
-            }
-            builder.setSource(content);
-        }
-    }
-
     /**
      * Create new SchemaPath from given path and qname.
      *
@@ -156,9 +120,7 @@ public final class BuilderUtils {
      */
     @Deprecated
     public static SchemaPath createSchemaPath(final SchemaPath schemaPath, final QName... qname) {
-        List<QName> path = new ArrayList<>(schemaPath.getPath());
-        path.addAll(Arrays.asList(qname));
-        return SchemaPath.create(path, schemaPath.isAbsolute());
+        return schemaPath.createChild(qname);
     }
 
     /**
@@ -170,7 +132,7 @@ public final class BuilderUtils {
      *            prefix associated with import
      * @return ModuleImport based on given prefix
      */
-    public static ModuleImport getModuleImport(final ModuleBuilder builder, final String prefix) {
+    private static ModuleImport getModuleImport(final ModuleBuilder builder, final String prefix) {
         for (ModuleImport mi : builder.getModuleImports()) {
             if (mi.getPrefix().equals(prefix)) {
                 return mi;
@@ -195,8 +157,8 @@ public final class BuilderUtils {
      */
     public static ModuleBuilder findModuleFromBuilders(final Map<String, TreeMap<Date, ModuleBuilder>> modules,
             final ModuleBuilder module, final String prefix, final int line) {
-        ModuleBuilder dependentModule = null;
-        Date dependentModuleRevision = null;
+        ModuleBuilder dependentModule;
+        Date dependentModuleRevision;
 
         if (prefix == null) {
             dependentModule = module;
@@ -261,7 +223,7 @@ public final class BuilderUtils {
             }
         }
 
-        Module result = null;
+        Module result;
         if (dependentModuleRevision == null) {
             result = modulesByRevision.get(modulesByRevision.firstKey());
         } else {
@@ -280,7 +242,7 @@ public final class BuilderUtils {
     public static SchemaPath parseXPathString(final String xpathString) {
         final boolean absolute = xpathString.indexOf('/') == 0;
 
-        final List<QName> path = new ArrayList<QName>();
+        final List<QName> path = new ArrayList<>();
         for (String pathElement : SLASH_SPLITTER.split(xpathString)) {
             if (pathElement.length() > 0) {
                 final Iterator<String> it = COLON_SPLITTER.split(pathElement).iterator();
@@ -418,7 +380,7 @@ public final class BuilderUtils {
      * @param config
      *            new config value
      */
-    public static void setNodeConfig(final DataSchemaNodeBuilder node, final Boolean config) {
+    private static void setNodeConfig(final DataSchemaNodeBuilder node, final Boolean config) {
         if (node instanceof ContainerSchemaNodeBuilder || node instanceof LeafSchemaNodeBuilder
                 || node instanceof LeafListSchemaNodeBuilder || node instanceof ListSchemaNodeBuilder
                 || node instanceof ChoiceBuilder || node instanceof AnyXmlBuilder) {
@@ -735,7 +697,6 @@ public final class BuilderUtils {
         while (!(parent instanceof ModuleBuilder)) {
             parent = parent.getParent();
         }
-        Preconditions.checkState(parent instanceof ModuleBuilder);
         ModuleBuilder parentModule = (ModuleBuilder) parent;
         if (parentModule.isSubmodule()) {
             parentModule = parentModule.getParent();
@@ -800,9 +761,7 @@ public final class BuilderUtils {
         Set<TypeDefinitionBuilder> result = new HashSet<>();
         for (TypeDefinition<?> node : nodes) {
             QName qname = new QName(ns, rev, pref, node.getQName().getLocalName());
-            List<QName> path = new ArrayList<>(parentPath.getPath());
-            path.add(qname);
-            SchemaPath schemaPath = SchemaPath.create(path, parentPath.isAbsolute());
+            SchemaPath schemaPath = parentPath.createChild(qname);
             result.add(new TypeDefinitionBuilderImpl(moduleName, line, qname, schemaPath, ((ExtendedType) node)));
         }
         return result;
@@ -814,9 +773,7 @@ public final class BuilderUtils {
         List<UnknownSchemaNodeBuilderImpl> result = new ArrayList<>();
         for (UnknownSchemaNode node : nodes) {
             QName qname = new QName(ns, rev, pref, node.getQName().getLocalName());
-            List<QName> path = new ArrayList<>(parentPath.getPath());
-            path.add(qname);
-            SchemaPath schemaPath = SchemaPath.create(path, parentPath.isAbsolute());
+            SchemaPath schemaPath = parentPath.createChild(qname);
             result.add(new UnknownSchemaNodeBuilderImpl(moduleName, line, qname, schemaPath, node));
         }
         return result;