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;
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());
}
}
if (excluded) {
- if(log != null) {
+ if (log != null) {
log.info(Util.message("%s file excluded %s", YangToSourcesProcessor.LOG_PREFIX,
Util.YANG_SUFFIX.toUpperCase(), f));
}
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;
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;
}
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) {
is.close();
}
}
+
+ public List<InputStream> getYangStreams() {
+ return this.yangStreams;
+ }
}
static YangsInZipsResult findYangFilesInDependenciesAsStream(Log log, MavenProject project)
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);
}
}
}
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;