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      @Override
51      public String toHeaderString(String key) {
52          StringBuilder formatted = new StringBuilder(key.length());
53  
54          if (!allowMixedCase) {
55              key = key.toUpperCase(Locale.US);
56          }
57  
58          StringTokenizer tokens = new StringTokenizer(key, ". ");
59          if (!tokens.hasMoreTokens()) {
60              return "";
61          }
62          formatted.append(tokens.nextToken());
63  
64          while (tokens.hasMoreTokens()) {
65              formatted.append(' ');
66              formatted.append(tokens.nextToken());
67          }
68  
69          return formatted.toString();
70      }
71  
72      @Override
73      public void append(String key, FitsLineAppender buffer) {
74          buffer.append(toHeaderString(key));
75      }
76  
77      @Override
78      public int getExtraSpaceRequired(String key) {
79          // The one extra space before '='...
80          return 0;
81      }
82  
83      @Override
84      public void setCaseSensitive(boolean value) {
85          allowMixedCase = value;
86      }
87  
88      @Override
89      public final boolean isCaseSensitive() {
90          return allowMixedCase;
91      }
92  
93  }