All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.eclipse.xtext.mwe.PathTraverser Maven / Gradle / Ivy

/*******************************************************************************
 * Copyright (c) 2010 itemis AG (http://www.itemis.eu) and others.
 * 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
 *******************************************************************************/
package org.eclipse.xtext.mwe;

import java.io.File;
import java.util.Enumeration;
import java.util.List;
import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import org.apache.log4j.Logger;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.common.util.WrappedException;

import com.google.common.base.Predicate;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import com.google.common.collect.Sets;

/**
 * @author Sven Efftinge - Initial contribution and API
 */
public class PathTraverser {
	private final static Logger LOG = Logger.getLogger(PathTraverser.class);
	
	public Multimap resolvePathes(List pathes, Predicate isValidPredicate) {
		Multimap uris = HashMultimap.create();
		for (String path : pathes) {
			Set resourceUris = findAllResourceUris(path, isValidPredicate);
			uris.putAll(path, resourceUris);
		}
		return uris;
	}
	
	public Set findAllResourceUris(String path, Predicate isValidPredicate) {
		File file = new File(path);
		if(!file.exists()) {
			LOG.warn("File under : " + path + " doesn't exist.");
			return Sets.newHashSet();
		} else if (file.isDirectory()) {
			return traverseDir(file, isValidPredicate);
		} else if (file.isFile()) {
			return traverseArchive(file, isValidPredicate);
		}
		throw new IllegalArgumentException("Unsupported path : " + path + " (only folders and archives are supported).");
	}

	protected Set traverseArchive(File file, Predicate isValidPredicate) {
		try {
			Set result = Sets.newHashSet();
			ZipFile zipFile = new ZipFile(file);
			try {
				Enumeration entries = zipFile.entries();
				while (entries.hasMoreElements()) {
					ZipEntry entry = entries.nextElement();
					URI uri = getUri(file, entry);
					if (uri != null && isValidPredicate.apply(uri)) {
						result.add(uri);
					}
				}
				return result;
			} finally {
				zipFile.close();
			}
		} catch (Exception e) {
			throw new WrappedException(e);
		}
	}

	protected URI getUri(File file, ZipEntry entry) {
		URI fileToArchive = URI.createFileURI(file.getAbsolutePath());
		return URI.createURI("archive:" + fileToArchive + "!/" + entry.getName());
	}

	protected Set traverseDir(File file, final Predicate isValidPredicate) {
		Set result = Sets.newHashSet();
		File[] files = file.listFiles();
		if (files == null)
			return result;
		for (File f : files) {
			if (f.isDirectory()) {
				result.addAll(traverseDir(f, isValidPredicate));
			} else {
				URI uri = URI.createFileURI(f.getAbsolutePath());
				if (isValidPredicate.apply(uri)) {
					result.add(uri);
				}
			}
		}
		return result;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy