1c0c4c6f0SDavid Ostrovsky /* 25c5f7c6bSMatthias Sohn * Copyright (C) 2015, David Ostrovsky <david@ostrovsky.org> and others 3c0c4c6f0SDavid Ostrovsky * 45c5f7c6bSMatthias Sohn * This program and the accompanying materials are made available under the 55c5f7c6bSMatthias Sohn * terms of the Eclipse Distribution License v. 1.0 which is available at 65c5f7c6bSMatthias Sohn * https://www.eclipse.org/org/documents/edl-v10.php. 7c0c4c6f0SDavid Ostrovsky * 85c5f7c6bSMatthias Sohn * SPDX-License-Identifier: BSD-3-Clause 9c0c4c6f0SDavid Ostrovsky */ 10c0c4c6f0SDavid Ostrovsky 11c0c4c6f0SDavid Ostrovsky package org.eclipse.jgit.archive; 12c0c4c6f0SDavid Ostrovsky 13c0c4c6f0SDavid Ostrovsky import java.beans.Statement; 14c0c4c6f0SDavid Ostrovsky import java.io.IOException; 153ac06ca6SMatthias Sohn import java.text.MessageFormat; 16c0c4c6f0SDavid Ostrovsky import java.util.Map; 17c0c4c6f0SDavid Ostrovsky 18c0c4c6f0SDavid Ostrovsky import org.apache.commons.compress.archivers.ArchiveOutputStream; 193ac06ca6SMatthias Sohn import org.eclipse.jgit.archive.internal.ArchiveText; 20c0c4c6f0SDavid Ostrovsky import org.eclipse.jgit.util.StringUtils; 21c0c4c6f0SDavid Ostrovsky 22c0c4c6f0SDavid Ostrovsky /** 23c0c4c6f0SDavid Ostrovsky * Base format class 24994b39a0SMatthias Sohn * 25994b39a0SMatthias Sohn * @since 4.0 26c0c4c6f0SDavid Ostrovsky */ 27c0c4c6f0SDavid Ostrovsky public class BaseFormat { 28*6f82690aSYoussef Elghareeb /** 29*6f82690aSYoussef Elghareeb * Compression-level for the archive file. Only values in [0-9] are allowed. 30*6f82690aSYoussef Elghareeb * @since 5.11 31*6f82690aSYoussef Elghareeb */ 32*6f82690aSYoussef Elghareeb protected static final String COMPRESSION_LEVEL = "compression-level"; //$NON-NLS-1$ 33c0c4c6f0SDavid Ostrovsky 34c0c4c6f0SDavid Ostrovsky /** 35c0c4c6f0SDavid Ostrovsky * Apply options to archive output stream 36c0c4c6f0SDavid Ostrovsky * 37c0c4c6f0SDavid Ostrovsky * @param s 38c0c4c6f0SDavid Ostrovsky * stream to apply options to 39c0c4c6f0SDavid Ostrovsky * @param o 40c0c4c6f0SDavid Ostrovsky * options map 41c0c4c6f0SDavid Ostrovsky * @return stream with option applied 42c0c4c6f0SDavid Ostrovsky * @throws IOException 43c0c4c6f0SDavid Ostrovsky */ applyFormatOptions(ArchiveOutputStream s, Map<String, Object> o)44c0c4c6f0SDavid Ostrovsky protected ArchiveOutputStream applyFormatOptions(ArchiveOutputStream s, 45c0c4c6f0SDavid Ostrovsky Map<String, Object> o) throws IOException { 46c0c4c6f0SDavid Ostrovsky for (Map.Entry<String, Object> p : o.entrySet()) { 47c0c4c6f0SDavid Ostrovsky try { 48*6f82690aSYoussef Elghareeb if (p.getKey().equals(COMPRESSION_LEVEL)) { 49*6f82690aSYoussef Elghareeb continue; 50*6f82690aSYoussef Elghareeb } 513ac06ca6SMatthias Sohn new Statement(s, "set" + StringUtils.capitalize(p.getKey()), //$NON-NLS-1$ 52c0c4c6f0SDavid Ostrovsky new Object[] { p.getValue() }).execute(); 53c0c4c6f0SDavid Ostrovsky } catch (Exception e) { 543ac06ca6SMatthias Sohn throw new IOException(MessageFormat.format( 553ac06ca6SMatthias Sohn ArchiveText.get().cannotSetOption, p.getKey()), e); 56c0c4c6f0SDavid Ostrovsky } 57c0c4c6f0SDavid Ostrovsky } 58c0c4c6f0SDavid Ostrovsky return s; 59c0c4c6f0SDavid Ostrovsky } 60*6f82690aSYoussef Elghareeb 61*6f82690aSYoussef Elghareeb /** 62*6f82690aSYoussef Elghareeb * Removes and returns the {@link #COMPRESSION_LEVEL} key from the input map 63*6f82690aSYoussef Elghareeb * parameter if it exists, or -1 if this key does not exist. 64*6f82690aSYoussef Elghareeb * 65*6f82690aSYoussef Elghareeb * @param o 66*6f82690aSYoussef Elghareeb * options map 67*6f82690aSYoussef Elghareeb * @return The compression level if it exists in the map, or -1 instead. 68*6f82690aSYoussef Elghareeb * @throws IllegalArgumentException 69*6f82690aSYoussef Elghareeb * if the {@link #COMPRESSION_LEVEL} option does not parse to an 70*6f82690aSYoussef Elghareeb * Integer. 71*6f82690aSYoussef Elghareeb * @since 5.11 72*6f82690aSYoussef Elghareeb */ getCompressionLevel(Map<String, Object> o)73*6f82690aSYoussef Elghareeb protected int getCompressionLevel(Map<String, Object> o) { 74*6f82690aSYoussef Elghareeb if (!o.containsKey(COMPRESSION_LEVEL)) { 75*6f82690aSYoussef Elghareeb return -1; 76*6f82690aSYoussef Elghareeb } 77*6f82690aSYoussef Elghareeb Object option = o.get(COMPRESSION_LEVEL); 78*6f82690aSYoussef Elghareeb try { 79*6f82690aSYoussef Elghareeb Integer compressionLevel = (Integer) option; 80*6f82690aSYoussef Elghareeb return compressionLevel.intValue(); 81*6f82690aSYoussef Elghareeb } catch (ClassCastException e) { 82*6f82690aSYoussef Elghareeb throw new IllegalArgumentException( 83*6f82690aSYoussef Elghareeb MessageFormat.format( 84*6f82690aSYoussef Elghareeb ArchiveText.get().invalidCompressionLevel, option), 85*6f82690aSYoussef Elghareeb e); 86*6f82690aSYoussef Elghareeb } 87*6f82690aSYoussef Elghareeb } 88c0c4c6f0SDavid Ostrovsky } 89