Merge "Switch to using plexus-build-api for file output"
[yangtools.git] / yang / yang-maven-plugin / src / main / java / org / opendaylight / yangtools / yang2sources / plugin / Util.java
index 0621cb4ab6518b2e886b665b910fb30e12b797fb..f930b2c0c342dea8c4cd4a4e53fe94c54cd654c5 100644 (file)
@@ -15,6 +15,7 @@ import java.io.FilenameFilter;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collection;
 import java.util.Enumeration;
 import java.util.List;
@@ -36,22 +37,31 @@ import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
 
 final class Util {
+
+    /**
+     * It isn't desirable to create instances of this class
+     */
+    private Util() {
+    }
+
     static final String YANG_SUFFIX = "yang";
 
+    private static final int CACHE_SIZE = 10;
     // Cache for listed directories and found yang files. Typically yang files
     // are utilized twice. First: code is generated during generate-sources
     // phase Second: yang files are copied as resources during
     // generate-resources phase. This cache ensures that yang files are listed
     // only once.
-    private static Map<File, Collection<File>> cache = Maps.newHashMapWithExpectedSize(10);
+    private static Map<File, Collection<File>> cache = Maps.newHashMapWithExpectedSize(CACHE_SIZE);
 
     /**
      * List files recursively and return as array of String paths. Use cache of
      * size 1.
      */
     static Collection<File> listFiles(File root) throws FileNotFoundException {
-        if (cache.get(root) != null)
+        if (cache.get(root) != null) {
             return cache.get(root);
+        }
 
         if (!root.exists()) {
             throw new FileNotFoundException(root.toString());
@@ -78,7 +88,7 @@ final class Util {
                 }
             }
             if (excluded) {
-                if(log != null) {
+                if (log != null) {
                     log.info(Util.message("%s file excluded %s", YangToSourcesProcessor.LOG_PREFIX,
                             Util.YANG_SUFFIX.toUpperCase(), f));
                 }
@@ -90,18 +100,6 @@ final class Util {
         return result;
     }
 
-    static List<InputStream> listFilesAsStream(File rootDir, File[] excludedFiles, Log log)
-            throws FileNotFoundException {
-        List<InputStream> is = new ArrayList<InputStream>();
-
-        Collection<File> files = listFiles(rootDir, excludedFiles, log);
-        for (File f : files) {
-            is.add(new NamedFileInputStream(f));
-        }
-
-        return is;
-    }
-
     static class NamedFileInputStream extends FileInputStream {
         private final File file;
 
@@ -131,15 +129,17 @@ final class Util {
     private static Class<?> resolveClass(String codeGeneratorClass, Class<?> baseType) throws ClassNotFoundException {
         Class<?> clazz = Class.forName(codeGeneratorClass);
 
-        if (!isImplemented(baseType, clazz))
+        if (!isImplemented(baseType, clazz)) {
             throw new IllegalArgumentException("Code generator " + clazz + " has to implement " + baseType);
+        }
         return clazz;
     }
 
     private static boolean isImplemented(Class<?> expectedIface, Class<?> byClazz) {
         for (Class<?> iface : byClazz.getInterfaces()) {
-            if (iface.equals(expectedIface))
+            if (iface.equals(expectedIface)) {
                 return true;
+            }
         }
         return false;
     }
@@ -170,8 +170,8 @@ final class Util {
         return Preconditions.checkNotNull(obj, "Parameter " + paramName + " is null");
     }
 
-    final static class YangsInZipsResult implements Closeable {
-        final List<InputStream> yangStreams;
+    static final class YangsInZipsResult implements Closeable {
+        private final List<InputStream> yangStreams;
         private final List<Closeable> zipInputStreams;
 
         private YangsInZipsResult(List<InputStream> yangStreams, List<Closeable> zipInputStreams) {
@@ -188,6 +188,10 @@ final class Util {
                 is.close();
             }
         }
+
+        public List<InputStream> getYangStreams() {
+            return this.yangStreams;
+        }
     }
 
     static YangsInZipsResult findYangFilesInDependenciesAsStream(Log log, MavenProject project)
@@ -225,14 +229,13 @@ final class Util {
                         ZipEntry entry = entries.nextElement();
                         String entryName = entry.getName();
 
-                        if (entryName.startsWith(YangToSourcesProcessor.META_INF_YANG_STRING_JAR)) {
-                            if (entry.isDirectory() == false && entryName.endsWith(".yang")) {
-                                foundFilesForReporting.add(entryName);
-                                // This will be closed after all strams are
-                                // parsed.
-                                InputStream entryStream = zip.getInputStream(entry);
-                                yangsFromDependencies.add(entryStream);
-                            }
+                        if (entryName.startsWith(YangToSourcesProcessor.META_INF_YANG_STRING_JAR)
+                                && !entry.isDirectory() && entryName.endsWith(".yang")) {
+                            foundFilesForReporting.add(entryName);
+                            // This will be closed after all streams are
+                            // parsed.
+                            InputStream entryStream = zip.getInputStream(entry);
+                            yangsFromDependencies.add(entryStream);
                         }
                     }
                 }
@@ -248,7 +251,54 @@ final class Util {
         return new YangsInZipsResult(yangsFromDependencies, zips);
     }
 
-    final static class ContextHolder {
+    static Collection<File> findYangFilesInDependencies(Log log, MavenProject project) throws MojoFailureException {
+        final List<File> yangsFilesFromDependencies = new ArrayList<>();
+
+        try {
+            List<File> filesOnCp = Util.getClassPath(project);
+            log.info(Util.message("Searching for yang files in following dependencies: %s",
+                    YangToSourcesProcessor.LOG_PREFIX, filesOnCp));
+
+            for (File file : filesOnCp) {
+                // is it jar file or directory?
+                if (file.isDirectory()) {
+                    File yangDir = new File(file, YangToSourcesProcessor.META_INF_YANG_STRING);
+                    if (yangDir.exists() && yangDir.isDirectory()) {
+                        File[] yangFiles = yangDir.listFiles(new FilenameFilter() {
+                            @Override
+                            public boolean accept(File dir, String name) {
+                                return name.endsWith(".yang") && new File(dir, name).isFile();
+                            }
+                        });
+
+                        yangsFilesFromDependencies.addAll(Arrays.asList(yangFiles));
+                    }
+                } else {
+                    try (ZipFile zip = new ZipFile(file)) {
+
+                        final Enumeration<? extends ZipEntry> entries = zip.entries();
+                        while (entries.hasMoreElements()) {
+                            ZipEntry entry = entries.nextElement();
+                            String entryName = entry.getName();
+
+                            if (entryName.startsWith(YangToSourcesProcessor.META_INF_YANG_STRING_JAR)
+                                    && !entry.isDirectory() && entryName.endsWith(".yang")) {
+                                log.debug(Util.message("Found a YANG file in %s: %s", YangToSourcesProcessor.LOG_PREFIX,
+                                        file, entryName));
+                                yangsFilesFromDependencies.add(file);
+                                break;
+                            }
+                        }
+                    }
+                }
+            }
+        } catch (Exception e) {
+            throw new MojoFailureException("Failed to scan for YANG files in depedencies", e);
+        }
+        return yangsFilesFromDependencies;
+    }
+
+    static final class ContextHolder {
         private final SchemaContext context;
         private final Set<Module> yangModules;