/******************************************************************************* * Copyright (c) 2011-2012 Igor Fedorenko * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * Igor Fedorenko - initial API and implementation *******************************************************************************/ package org.eclipse.m2e.sourcelookup.internal.launch; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Enumeration; import java.util.List; import java.util.jar.JarEntry; import java.util.jar.JarFile; /** * Helper to find and extract information from META-INF/maven pom.properties files. */ public abstract class MetaInfMavenScanner { private static final String META_INF_MAVEN = "META-INF/maven"; public List scan(File file, String filename) { List result = new ArrayList(); if (file != null) { if (file.isDirectory()) { scanFilesystem(new File(file, META_INF_MAVEN), filename, result); } else if (file.isFile()) { try { try (JarFile jar = new JarFile(file)) { scanJar(jar, filename, result); } } catch (IOException e) { // fall through } } } return result; } private void scanJar(JarFile jar, String filename, List result) throws IOException { Enumeration entries = jar.entries(); while (entries.hasMoreElements()) { JarEntry entry = entries.nextElement(); if (!entry.isDirectory()) { String name = entry.getName(); if (name.startsWith(META_INF_MAVEN) && name.endsWith(filename)) { try { T t = visitJarEntry(jar, entry); if (t != null) { result.add(t); } } catch (IOException e) { // ignore } } } } } private void scanFilesystem(File dir, String filename, List result) { File[] files = dir.listFiles(); if (files == null) { return; } for (File file : files) { if (file.isDirectory()) { scanFilesystem(file, filename, result); } else if (file.isFile() && filename.equals(file.getName())) { try { T t = visitFile(file); if (t != null) { result.add(t); } } catch (IOException e) { // ignore } } } } protected abstract T visitFile(File file) throws IOException; protected abstract T visitJarEntry(JarFile jar, JarEntry entry) throws IOException; }