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

bndtools.jareditor.internal.ZipTreeNode Maven / Gradle / Ivy

There is a newer version: 7.1.0
Show newest version
package bndtools.jareditor.internal;

import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.jar.JarEntry;
import java.util.zip.ZipEntry;

import org.eclipse.core.runtime.IAdaptable;

class ZipTreeNode implements IAdaptable {

	private final ZipTreeNode				parent;
	private final String					name;
	private final ZipEntry					entry;
	private final Map	children	= new LinkedHashMap<>();

	private ZipTreeNode(ZipTreeNode parent, String name, ZipEntry entry) {
		this.parent = parent;
		this.name = name;
		this.entry = entry;
	}

	public ZipTreeNode getParent() {
		return parent;
	}

	public boolean hasChildren() {
		return !children.isEmpty();
	}

	public Collection getChildren() {
		return Collections.unmodifiableCollection(children.values());
	}

	public ZipEntry getZipEntry() {
		return entry;
	}

	@Override
	public String toString() {
		return name;

	}

	public static void addEntry(Map rootMap, ZipEntry entry) {
		List path = getPath(entry);
		pushEntry(null, rootMap, path, entry);
	}

	private static void pushEntry(ZipTreeNode parent, Map map, List path, ZipEntry entry) {
		String pathPart = path.remove(0);
		ZipTreeNode node = map.get(pathPart);
		if (node == null) {
			node = new ZipTreeNode(parent, pathPart, path.isEmpty() ? entry : null);
			map.put(pathPart, node);
		}
		if (!path.isEmpty())
			pushEntry(node, node.children, path, entry);
	}

	private static List getPath(ZipEntry entry) {
		List path = new LinkedList<>();

		String name = entry.getName();
		int index = 0;
		while (index < name.length()) {
			int nextSlash = name.indexOf('/', index);

			if (nextSlash == -1) {
				path.add(name.substring(index));
				break;
			}
			path.add(name.substring(index, nextSlash + 1));
			index = nextSlash + 1;
		}

		return path;
	}

	@SuppressWarnings("unchecked")
	@Override
	public  T getAdapter(Class adapter) {
		if (adapter == JarEntry.class) {
			return (T) entry;
		}
		return null;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy