Skip to content

Commit

Permalink
Merge pull request #120 from fmidev/disable-linewrap-conversionhint
Browse files Browse the repository at this point in the history
ConversionHint to disable line wrap in TAC serialization
  • Loading branch information
kasarit authored Feb 14, 2023
2 parents 8fc6b69 + c7746cf commit a3c2dd3
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 30 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<dependency>
<groupId>fi.fmi.avi.converter</groupId>
<artifactId>fmi-avi-messageconverter</artifactId>
<version>6.2.0</version>
<version>6.2.1-SNAPSHOT</version>
</dependency>

<!-- For JUnit tests -->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
package fi.fmi.avi.converter.tac.bulletin;

import java.util.List;

import fi.fmi.avi.converter.ConversionHints;
import fi.fmi.avi.converter.ConversionIssue;
import fi.fmi.avi.converter.ConversionResult;
import fi.fmi.avi.converter.tac.AbstractTACSerializer;
import fi.fmi.avi.converter.tac.lexer.Lexeme;
import fi.fmi.avi.converter.tac.lexer.LexemeIdentity;
import fi.fmi.avi.converter.tac.lexer.LexemeSequence;
import fi.fmi.avi.converter.tac.lexer.LexemeSequenceBuilder;
import fi.fmi.avi.converter.tac.lexer.SerializingException;
import fi.fmi.avi.converter.tac.lexer.*;
import fi.fmi.avi.converter.tac.lexer.impl.ReconstructorContext;
import fi.fmi.avi.model.AviationWeatherMessage;
import fi.fmi.avi.model.AviationWeatherMessageOrCollection;
import fi.fmi.avi.model.bulletin.MeteorologicalBulletin;
import fi.fmi.avi.model.bulletin.MeteorologicalBulletinSpecialCharacter;
import fi.fmi.avi.model.swx.SpaceWeatherAdvisory;

import java.util.List;

public abstract class AbstractTACBulletinSerializer<S extends AviationWeatherMessage, T extends MeteorologicalBulletin<S>> extends AbstractTACSerializer<T> {

/**
Expand Down Expand Up @@ -65,6 +61,8 @@ public LexemeSequence tokenizeMessage(final AviationWeatherMessageOrCollection m

final boolean whitespacePassthrough = hints != null && ConversionHints.VALUE_WHITESPACE_SERIALIZATION_MODE_PASSTHROUGH.equals(
hints.getOrDefault(ConversionHints.KEY_WHITESPACE_SERIALIZATION_MODE, null));
final boolean disableLineWrap = hints != null && ConversionHints.VALUE_DISABLE_LINEWRAP_SERIALIZATION_MODE.equals(
hints.getOrDefault(ConversionHints.KEY_DISABLE_LINEWRAP_SERIALIZATION_MODE, null));

final List<S> messages = input.getMessages();
LexemeSequence messageSequence;
Expand Down Expand Up @@ -97,7 +95,7 @@ public LexemeSequence tokenizeMessage(final AviationWeatherMessageOrCollection m
appendWhitespace(retval, MeteorologicalBulletinSpecialCharacter.CARRIAGE_RETURN);
}
charsOnRow = 0;
} else if (charsOnRow + tokenLength > MAX_ROW_LENGTH) {
} else if (!disableLineWrap && charsOnRow + tokenLength > MAX_ROW_LENGTH) {
while (retval.getLast().isPresent() && LexemeIdentity.WHITE_SPACE.equals(retval.getLast().get().getIdentity())) {
retval.removeLast();
}
Expand All @@ -110,7 +108,7 @@ public LexemeSequence tokenizeMessage(final AviationWeatherMessageOrCollection m
}
} else {
if (!LexemeIdentity.WHITE_SPACE.equals(lexeme.getIdentity()) && !LexemeIdentity.END_TOKEN.equals(lexeme.getIdentity())) {
if (charsOnRow + tokenLength > MAX_ROW_LENGTH) {
if (!disableLineWrap && charsOnRow + tokenLength > MAX_ROW_LENGTH) {
if (retval.getLast().isPresent() && LexemeIdentity.WHITE_SPACE.equals(retval.getLast().get().getIdentity())) {
retval.removeLast();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,5 @@
package fi.fmi.avi.converter.tac.bulletin;

import static fi.fmi.avi.model.bulletin.MeteorologicalBulletinSpecialCharacter.CARRIAGE_RETURN;
import static fi.fmi.avi.model.bulletin.MeteorologicalBulletinSpecialCharacter.LINE_FEED;
import static fi.fmi.avi.model.bulletin.DataTypeDesignatorT1.UPPER_AIR_DATA;
import static junit.framework.TestCase.assertTrue;
import static org.junit.Assert.assertEquals;

import java.util.Optional;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;

import fi.fmi.avi.converter.AviMessageConverter;
import fi.fmi.avi.converter.ConversionHints;
import fi.fmi.avi.converter.ConversionResult;
Expand All @@ -29,6 +14,20 @@
import fi.fmi.avi.model.bulletin.immutable.GenericMeteorologicalBulletinImpl;
import fi.fmi.avi.model.immutable.GenericAviationWeatherMessageImpl;
import junit.framework.TestCase;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;

import java.util.Optional;

import static fi.fmi.avi.model.bulletin.DataTypeDesignatorT1.UPPER_AIR_DATA;
import static fi.fmi.avi.model.bulletin.MeteorologicalBulletinSpecialCharacter.CARRIAGE_RETURN;
import static fi.fmi.avi.model.bulletin.MeteorologicalBulletinSpecialCharacter.LINE_FEED;
import static junit.framework.TestCase.assertTrue;
import static org.junit.Assert.assertEquals;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = TACTestConfiguration.class, loader = AnnotationConfigContextLoader.class)
Expand Down Expand Up @@ -57,7 +56,7 @@ public void testSerialization() {
public void whitespacePassthrough() {
final GenericMeteorologicalBulletin msg = createGenericBulletin(
"LOW WIND EFHK 270925Z\r\n\r\n1000FT 2000FT FL050 FL100\r\n200/05 260/05 310/05 320/15=");
final ConversionResult<String> tacResult = this.converter.convertMessage(msg, TACConverter.GENERIC_BULLETIN_POJO_TO_TAC, passthroughConversionHints());
final ConversionResult<String> tacResult = this.converter.convertMessage(msg, TACConverter.GENERIC_BULLETIN_POJO_TO_TAC, passthroughHints());
assertEquals(ConversionResult.Status.SUCCESS, tacResult.getStatus());

final Optional<String> tacBulletin = tacResult.getConvertedMessage();
Expand All @@ -75,7 +74,7 @@ public void whitespacePassthrough() {
public void whitespacePassthroughConvertsLFtoCRLF() {
final GenericMeteorologicalBulletin msg = createGenericBulletin(
"LOW WIND EFHK 270925Z\n\n1000FT 2000FT FL050 FL100\n200/05 260/05 310/05 320/15=");
final ConversionResult<String> tacResult = this.converter.convertMessage(msg, TACConverter.GENERIC_BULLETIN_POJO_TO_TAC, passthroughConversionHints());
final ConversionResult<String> tacResult = this.converter.convertMessage(msg, TACConverter.GENERIC_BULLETIN_POJO_TO_TAC, passthroughHints());
assertEquals(ConversionResult.Status.SUCCESS, tacResult.getStatus());

final Optional<String> tacBulletin = tacResult.getConvertedMessage();
Expand All @@ -93,7 +92,7 @@ public void whitespacePassthroughConvertsLFtoCRLF() {
public void whitespacePassthroughWithNewlineAtMaxRowLength() {
final GenericMeteorologicalBulletin msg = createGenericBulletin(
"LOW WIND EFHK 270925Z\n\n1000FT 2000FT FL050 FL100 FOOBAR FOOBAR FOOBAR\n200/05 260/05 310/05 320/15=");
final ConversionResult<String> tacResult = this.converter.convertMessage(msg, TACConverter.GENERIC_BULLETIN_POJO_TO_TAC, passthroughConversionHints());
final ConversionResult<String> tacResult = this.converter.convertMessage(msg, TACConverter.GENERIC_BULLETIN_POJO_TO_TAC, passthroughHints());
assertEquals(ConversionResult.Status.SUCCESS, tacResult.getStatus());

final Optional<String> tacBulletin = tacResult.getConvertedMessage();
Expand All @@ -111,7 +110,7 @@ public void whitespacePassthroughWithNewlineAtMaxRowLength() {
public void whitespacePassthroughLongRow() {
final GenericMeteorologicalBulletin msg = createGenericBulletin(
"LOW WIND EFHK 270925Z\n\nFOOBAR FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR=");
final ConversionResult<String> tacResult = this.converter.convertMessage(msg, TACConverter.GENERIC_BULLETIN_POJO_TO_TAC, passthroughConversionHints());
final ConversionResult<String> tacResult = this.converter.convertMessage(msg, TACConverter.GENERIC_BULLETIN_POJO_TO_TAC, passthroughHints());
assertEquals(ConversionResult.Status.SUCCESS, tacResult.getStatus());

final Optional<String> tacBulletin = tacResult.getConvertedMessage();
Expand All @@ -125,6 +124,41 @@ public void whitespacePassthroughLongRow() {
+ "FOOBAR FOOBAR FOOBAR=", tacBulletin.get());
}

@Test
public void lineWrapDisabled() {
final GenericMeteorologicalBulletin msg = createGenericBulletin(
"LOW WIND EFHK 270925Z\n\n1000FT 2000FT FL050 FL100\n200/05 260/05 310/05 320/15=");
final ConversionResult<String> tacResult = this.converter.convertMessage(msg, TACConverter.GENERIC_BULLETIN_POJO_TO_TAC, lineWrapDisabledHints());
assertEquals(ConversionResult.Status.SUCCESS, tacResult.getStatus());

final Optional<String> tacBulletin = tacResult.getConvertedMessage();
assertTrue(tacBulletin.isPresent());
TestCase.assertEquals(//
"UXFI81 EFKL 271402"//
+ CARRIAGE_RETURN.getContent() + CARRIAGE_RETURN.getContent() + LINE_FEED.getContent()
+ "LOW WIND EFHK 270925Z 1000FT 2000FT FL050 FL100 200/05 260/05 310/05 320/15=", tacBulletin.get());
}

@Test
public void disabledLinewrapWhitespacePassthrough() {
final GenericMeteorologicalBulletin msg = createGenericBulletin(
"LOW WIND EFHK 270925Z\n\nFOOBAR FOOBAR FOOBAR FOOBAR FOOBAR\n\nFOOBAR FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR " +
"FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR=");
final ConversionResult<String> tacResult = this.converter.convertMessage(msg, TACConverter.GENERIC_BULLETIN_POJO_TO_TAC, passthroughWithLinewrapDisabledHints());
assertEquals(ConversionResult.Status.SUCCESS, tacResult.getStatus());

final Optional<String> tacBulletin = tacResult.getConvertedMessage();
assertTrue(tacBulletin.isPresent());
TestCase.assertEquals(//
"UXFI81 EFKL 271402"//
+ CARRIAGE_RETURN.getContent() + CARRIAGE_RETURN.getContent() + LINE_FEED.getContent()//
+ "LOW WIND EFHK 270925Z" + CARRIAGE_RETURN.getContent() + LINE_FEED.getContent()//
+ CARRIAGE_RETURN.getContent() + LINE_FEED.getContent()//
+ "FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR" + CARRIAGE_RETURN.getContent() + LINE_FEED.getContent()//
+ CARRIAGE_RETURN.getContent() + LINE_FEED.getContent() +//
"FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR=", tacBulletin.get());
}

private GenericMeteorologicalBulletin createGenericBulletin(final String message) {
final GenericMeteorologicalBulletinImpl.Builder builder = createBulletinBuilder();
builder.addMessages(GenericAviationWeatherMessageImpl.builder()//
Expand All @@ -148,9 +182,22 @@ private GenericMeteorologicalBulletinImpl.Builder createBulletinBuilder() {
.build());
}

private ConversionHints passthroughConversionHints() {
private static ConversionHints passthroughHints() {
final ConversionHints hints = new ConversionHints();
hints.put(ConversionHints.KEY_WHITESPACE_SERIALIZATION_MODE, ConversionHints.VALUE_WHITESPACE_SERIALIZATION_MODE_PASSTHROUGH);
return hints;
}

private static ConversionHints lineWrapDisabledHints() {
final ConversionHints hints = new ConversionHints();
hints.put(ConversionHints.KEY_DISABLE_LINEWRAP_SERIALIZATION_MODE, ConversionHints.VALUE_DISABLE_LINEWRAP_SERIALIZATION_MODE);
return hints;
}

private static ConversionHints passthroughWithLinewrapDisabledHints() {
final ConversionHints hints = new ConversionHints();
hints.put(ConversionHints.KEY_WHITESPACE_SERIALIZATION_MODE, ConversionHints.VALUE_WHITESPACE_SERIALIZATION_MODE_PASSTHROUGH);
hints.put(ConversionHints.KEY_DISABLE_LINEWRAP_SERIALIZATION_MODE, ConversionHints.VALUE_DISABLE_LINEWRAP_SERIALIZATION_MODE);
return hints;
}

Expand Down

0 comments on commit a3c2dd3

Please sign in to comment.