Skip to content

Commit

Permalink
hierarch key format now configurable #72
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchieGitHub committed Jan 16, 2016
1 parent 7552f81 commit 03a598d
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 5 deletions.
24 changes: 24 additions & 0 deletions src/main/java/nom/tam/fits/FitsFactory.java
Expand Up @@ -4,6 +4,8 @@
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

import nom.tam.fits.header.hierarch.IHierarchKeyFormatter;
import nom.tam.fits.header.hierarch.StandardIHierarchKeyFormatter;
import nom.tam.image.comp.hdu.CompressedImageData;
import nom.tam.image.comp.hdu.CompressedImageHDU;

Expand Down Expand Up @@ -60,13 +62,16 @@ private static class FitsSettings {

private boolean longStringsEnabled = false;

private IHierarchKeyFormatter hierarchKeyFormatter = new StandardIHierarchKeyFormatter();

private FitsSettings copy() {
FitsSettings settings = new FitsSettings();
settings.useAsciiTables = this.useAsciiTables;
settings.useHierarch = this.useHierarch;
settings.checkAsciiStrings = this.checkAsciiStrings;
settings.allowTerminalJunk = this.allowTerminalJunk;
settings.longStringsEnabled = this.longStringsEnabled;
settings.hierarchKeyFormatter = this.hierarchKeyFormatter;
return settings;
}

Expand Down Expand Up @@ -319,6 +324,25 @@ public static void setUseAsciiTables(boolean useAsciiTables) {
current().useAsciiTables = useAsciiTables;
}

/**
* There is not a real standard how to write hierarch keys, default we use
* the one where every key is separated by a blank. If you want or need
* another format assing the formater here.
*
* @param formatter
* the hierarch key formatter.
*/
public static void setHierarchFormater(IHierarchKeyFormatter formatter) {
current().hierarchKeyFormatter = formatter;
}

/**
* @return the formatter to use for hierarch keys.
*/
public static IHierarchKeyFormatter getHierarchFormater() {
return current().hierarchKeyFormatter;
}

/**
* Enable/Disable hierarchical keyword processing.
*
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/nom/tam/fits/HeaderCard.java
Expand Up @@ -771,7 +771,7 @@ public String toString() {
// start with the keyword, if there is one
if (this.key != null) {
if (this.key.length() > HIERARCH_WITH_BLANK_LENGTH && this.key.substring(0, HIERARCH_WITH_BLANK_LENGTH).equals(HIERARCH_WITH_DOT)) {
buf.appendRepacing(this.key, '.', ' ');
FitsFactory.getHierarchFormater().append(this.key, buf);
alignSmallString = HIERARCH_SMALL_STRING_ALIGN_POSITION;
alignPosition = HIERARCH_ALIGN_POSITION;
} else {
Expand Down
@@ -0,0 +1,39 @@
package nom.tam.fits.header.hierarch;

/*
* #%L
* nom.tam FITS library
* %%
* Copyright (C) 1996 - 2016 nom-tam-fits
* %%
* This is free and unencumbered software released into the public domain.
*
* Anyone is free to copy, modify, publish, use, compile, sell, or
* distribute this software, either in source code form or as a compiled
* binary, for any purpose, commercial or non-commercial, and by any
* means.
*
* In jurisdictions that recognize copyright laws, the author or authors
* of this software dedicate any and all copyright interest in the
* software to the public domain. We make this dedication for the benefit
* of the public at large and to the detriment of our heirs and
* successors. We intend this dedication to be an overt act of
* relinquishment in perpetuity of all present and future rights to this
* software under copyright law.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
* #L%
*/

import nom.tam.fits.utilities.FitsLineAppender;

public interface IHierarchKeyFormatter {

void append(String key, FitsLineAppender buffer);
}
@@ -0,0 +1,43 @@
package nom.tam.fits.header.hierarch;

/*
* #%L
* nom.tam FITS library
* %%
* Copyright (C) 1996 - 2016 nom-tam-fits
* %%
* This is free and unencumbered software released into the public domain.
*
* Anyone is free to copy, modify, publish, use, compile, sell, or
* distribute this software, either in source code form or as a compiled
* binary, for any purpose, commercial or non-commercial, and by any
* means.
*
* In jurisdictions that recognize copyright laws, the author or authors
* of this software dedicate any and all copyright interest in the
* software to the public domain. We make this dedication for the benefit
* of the public at large and to the detriment of our heirs and
* successors. We intend this dedication to be an overt act of
* relinquishment in perpetuity of all present and future rights to this
* software under copyright law.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
* #L%
*/

import nom.tam.fits.utilities.FitsLineAppender;

public class StandardIHierarchKeyFormatter implements IHierarchKeyFormatter {

@Override
public void append(String key, FitsLineAppender buffer) {
buffer.appendRepacing(key, '.', ' ');
}

}
6 changes: 3 additions & 3 deletions src/main/java/nom/tam/fits/utilities/FitsLineAppender.java
Expand Up @@ -49,9 +49,9 @@ public class FitsLineAppender {
private static final String FULL_CARD_AS_SPACES = " ";

/**
* the underlying StringBuffer to which the writing of fits lines happens.
* the underlying StringBuilder to which the writing of fits lines happens.
*/
private final StringBuffer buffer;
private final StringBuilder buffer;

/**
* the char current position in the line.
Expand All @@ -63,7 +63,7 @@ public class FitsLineAppender {
* line.
*/
public FitsLineAppender() {
this.buffer = new StringBuffer(HeaderCard.FITS_HEADER_CARD_SIZE);
this.buffer = new StringBuilder(HeaderCard.FITS_HEADER_CARD_SIZE);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/nom/tam/fits/utilities/FitsSubString.java
Expand Up @@ -74,7 +74,7 @@ public FitsSubString(String originalString) {
* @param buffer
* the buffer to append to.
*/
public void appendTo(StringBuffer buffer) {
public void appendTo(StringBuilder buffer) {
buffer.append(this.originalString, this.offset, this.offset + this.length);
}

Expand Down

0 comments on commit 03a598d

Please sign in to comment.