View Javadoc
1   package nom.tam.fits.header.hierarch;
2   
3   import java.util.Locale;
4   
5   import nom.tam.fits.utilities.FitsLineAppender;
6   
7   /*
8    * #%L
9    * nom.tam FITS library
10   * %%
11   * Copyright (C) 1996 - 2024 nom-tam-fits
12   * %%
13   * This is free and unencumbered software released into the public domain.
14   *
15   * Anyone is free to copy, modify, publish, use, compile, sell, or
16   * distribute this software, either in source code form or as a compiled
17   * binary, for any purpose, commercial or non-commercial, and by any
18   * means.
19   *
20   * In jurisdictions that recognize copyright laws, the author or authors
21   * of this software dedicate any and all copyright interest in the
22   * software to the public domain. We make this dedication for the benefit
23   * of the public at large and to the detriment of our heirs and
24   * successors. We intend this dedication to be an overt act of
25   * relinquishment in perpetuity of all present and future rights to this
26   * software under copyright law.
27   *
28   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29   * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30   * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
31   * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
32   * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
33   * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
34   * OTHER DEALINGS IN THE SOFTWARE.
35   * #L%
36   */
37  
38  import static nom.tam.fits.header.NonStandard.HIERARCH;
39  
40  /**
41   * @deprecated Non-standard HIERARCH keyword formatter that separates hierarchical keyword component by multiple while
42   *                 spaces. Otherwise, it is similar to {@link StandardIHierarchKeyFormatter}. Its use over the more
43   *                 standard formatter is discouraged.
44   */
45  public class BlanksDotHierarchKeyFormatter implements IHierarchKeyFormatter {
46  
47      private final String blanks;
48  
49      private boolean allowMixedCase;
50  
51      /**
52       * Creates a HIERARCH keyword formatter instance with the desired number of blank spaces spearating components.
53       * 
54       * @param  count                    The number of blank spaces to separate hierarchical components (at least 1 is
55       *                                      required).
56       * 
57       * @throws IllegalArgumentException if count is less than 1
58       */
59      public BlanksDotHierarchKeyFormatter(int count) throws IllegalArgumentException {
60          if (count < 1) {
61              throw new IllegalArgumentException("HIERARCH needs at least one blank space after it.");
62          }
63  
64          StringBuilder builder = new StringBuilder();
65          for (int index = 0; index < count; index++) {
66              builder.append(' ');
67          }
68          blanks = builder.toString();
69      }
70  
71      @Override
72      public void append(String key, FitsLineAppender buffer) {
73          buffer.append(toHeaderString(key));
74      }
75  
76      @Override
77      public int getExtraSpaceRequired(String key) {
78          return blanks.length() - 1;
79      }
80  
81      @Override
82      public String toHeaderString(String key) {
83          if (!allowMixedCase) {
84              key = key.toUpperCase(Locale.US);
85          }
86  
87          return HIERARCH.key() + blanks + key.substring(HIERARCH.key().length() + 1);
88      }
89  
90      @Override
91      public void setCaseSensitive(boolean value) {
92          allowMixedCase = value;
93      }
94  
95      @Override
96      public final boolean isCaseSensitive() {
97          return allowMixedCase;
98      }
99  }