You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BaseFormat.java 2.5KB

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