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

org.eclipse.jgit.archive.BaseFormat Maven / Gradle / Ivy

Go to download

Support for archiving a Git tree in formats such as zip and tar. This is a separate bundle from org.eclipse.jgit to avoid a dependency by the latter on commons-compress.

There is a newer version: 7.0.0.202409031743-r
Show newest version
/*
 * Copyright (C) 2015, David Ostrovsky  and others
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Distribution License v. 1.0 which is available at
 * https://www.eclipse.org/org/documents/edl-v10.php.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

package org.eclipse.jgit.archive;

import java.beans.Statement;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.Map;

import org.apache.commons.compress.archivers.ArchiveOutputStream;
import org.eclipse.jgit.archive.internal.ArchiveText;
import org.eclipse.jgit.util.StringUtils;

/**
 * Base format class
 *
 * @since 4.0
 */
public class BaseFormat {
	/**
	 * Compression-level for the archive file. Only values in [0-9] are allowed.
	 * @since 5.11
	 */
	protected static final String COMPRESSION_LEVEL = "compression-level"; //$NON-NLS-1$

	/**
	 * Apply options to archive output stream
	 *
	 * @param s
	 *            stream to apply options to
	 * @param o
	 *            options map
	 * @return stream with option applied
	 * @throws IOException
	 */
	protected ArchiveOutputStream applyFormatOptions(ArchiveOutputStream s,
			Map o) throws IOException {
		for (Map.Entry p : o.entrySet()) {
			try {
				if (p.getKey().equals(COMPRESSION_LEVEL)) {
					continue;
				}
				new Statement(s, "set" + StringUtils.capitalize(p.getKey()), //$NON-NLS-1$
						new Object[] { p.getValue() }).execute();
			} catch (Exception e) {
				throw new IOException(MessageFormat.format(
						ArchiveText.get().cannotSetOption, p.getKey()), e);
			}
		}
		return s;
	}

	/**
	 * Removes and returns the {@link #COMPRESSION_LEVEL} key from the input map
	 * parameter if it exists, or -1 if this key does not exist.
	 *
	 * @param o
	 *            options map
	 * @return The compression level if it exists in the map, or -1 instead.
	 * @throws IllegalArgumentException
	 *             if the {@link #COMPRESSION_LEVEL} option does not parse to an
	 *             Integer.
	 * @since 5.11
	 */
	protected int getCompressionLevel(Map o) {
		if (!o.containsKey(COMPRESSION_LEVEL)) {
			return -1;
		}
		Object option = o.get(COMPRESSION_LEVEL);
		try {
			Integer compressionLevel = (Integer) option;
			return compressionLevel.intValue();
		} catch (ClassCastException e) {
			throw new IllegalArgumentException(
					MessageFormat.format(
							ArchiveText.get().invalidCompressionLevel, option),
					e);
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy