View Javadoc
1   package nom.tam.fits.header.hierarch;
2   
3   import java.util.Locale;
4   import java.util.StringTokenizer;
5   
6   import nom.tam.fits.utilities.FitsLineAppender;
7   
8   /*
9    * #%L
10   * nom.tam FITS library
11   * %%
12   * Copyright (C) 1996 - 2024 nom-tam-fits
13   * %%
14   * This is free and unencumbered software released into the public domain.
15   *
16   * Anyone is free to copy, modify, publish, use, compile, sell, or
17   * distribute this software, either in source code form or as a compiled
18   * binary, for any purpose, commercial or non-commercial, and by any
19   * means.
20   *
21   * In jurisdictions that recognize copyright laws, the author or authors
22   * of this software dedicate any and all copyright interest in the
23   * software to the public domain. We make this dedication for the benefit
24   * of the public at large and to the detriment of our heirs and
25   * successors. We intend this dedication to be an overt act of
26   * relinquishment in perpetuity of all present and future rights to this
27   * software under copyright law.
28   *
29   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30   * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31   * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
32   * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
33   * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
34   * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
35   * OTHER DEALINGS IN THE SOFTWARE.
36   * #L%
37   */
38  
39  /**
40   * HIERARCH keyword formatter based on the ESO convention. This formatter writes HIERARCH keywords that conform to the
41   * ESO convention, but takes a more liberal approach by supporting the full range of ASCII characters allowed in FITS
42   * headers, including the option to preserve case. (The ESO convention is upper-case only).
43   * 
44   * @see nom.tam.fits.FitsFactory#setUseHierarch(boolean)
45   */
46  @SuppressWarnings("deprecation")
47  public class StandardIHierarchKeyFormatter implements IHierarchKeyFormatter {
48      private boolean allowMixedCase;
49  
50      /**
51       * Instantiates a new standard formatter for ESO HIERARCH style keywords.
52       */
53      public StandardIHierarchKeyFormatter() {
54      }
55  
56      @Override
57      public String toHeaderString(String key) {
58          StringBuilder formatted = new StringBuilder(key.length());
59  
60          if (!allowMixedCase) {
61              key = key.toUpperCase(Locale.US);
62          }
63  
64          StringTokenizer tokens = new StringTokenizer(key, ". ");
65          if (!tokens.hasMoreTokens()) {
66              return "";
67          }
68          formatted.append(tokens.nextToken());
69  
70          while (tokens.hasMoreTokens()) {
71              formatted.append(' ');
72              formatted.append(tokens.nextToken());
73          }
74  
75          return formatted.toString();
76      }
77  
78      @Override
79      public void append(String key, FitsLineAppender buffer) {
80          buffer.append(toHeaderString(key));
81      }
82  
83      @Override
84      public int getExtraSpaceRequired(String key) {
85          // The one extra space before '='...
86          return 0;
87      }
88  
89      @Override
90      public void setCaseSensitive(boolean value) {
91          allowMixedCase = value;
92      }
93  
94      @Override
95      public final boolean isCaseSensitive() {
96          return allowMixedCase;
97      }
98  
99  }