-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from imsweb/x12_string_from_loop-33
X12 string from loop (#33)
- Loading branch information
Showing
7 changed files
with
554 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Copyright (C) 2020 Information Management Services, Inc. | ||
*/ | ||
package com.imsweb.x12.writer; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import com.imsweb.x12.LineBreak; | ||
import com.imsweb.x12.Loop; | ||
import com.imsweb.x12.Separators; | ||
import com.imsweb.x12.mapping.TransactionDefinition; | ||
import com.imsweb.x12.reader.X12Reader; | ||
import com.imsweb.x12.reader.X12Reader.FileType; | ||
|
||
public class X12Writer { | ||
|
||
private final List<Loop> _dataLoops; | ||
private final Separators _separators; | ||
private final TransactionDefinition _definition; | ||
|
||
public X12Writer(FileType fileType, List<Loop> loops, Separators separators) { | ||
_dataLoops = loops; | ||
_definition = fileType.getDefinition(); | ||
_separators = separators; | ||
} | ||
|
||
public X12Writer(FileType fileType, List<Loop> loops) { | ||
_dataLoops = loops; | ||
_definition = fileType.getDefinition(); | ||
_separators = new Separators(); | ||
} | ||
|
||
public X12Writer(X12Reader reader) { | ||
_dataLoops = reader.getLoops(); | ||
_definition = reader.getDefinition(); | ||
_separators = reader.getSeparators(); | ||
} | ||
|
||
|
||
/** | ||
* Gets an X12 formatted string representing this X12 reader. Will use no line | ||
* breaks after separators. | ||
* | ||
* @return X12 formatted string representing this X12 reader. | ||
*/ | ||
public String toX12String() { | ||
return toX12String(LineBreak.NONE); | ||
} | ||
|
||
/** | ||
* Gets an X12 formatted string representing this X12 reader. | ||
* | ||
* @param lineBreak Line break to use for separators. | ||
* @return X12 formatted string representing this X12 reader. | ||
*/ | ||
public String toX12String(LineBreak lineBreak) { | ||
_separators.setLineBreak(lineBreak); | ||
return toX12StringImpl(); | ||
} | ||
|
||
private String toX12StringImpl() { | ||
StringBuilder builder = new StringBuilder(); | ||
for (Loop loop : _dataLoops) { | ||
builder.append(loop.toX12String(_definition.getLoop())); | ||
builder.append(_separators.getLineBreak().getLineBreakString()); | ||
} | ||
return builder.toString(); | ||
} | ||
|
||
/** | ||
* To HTML string will create an HTML segment from this X12 file. | ||
* | ||
* @return Human readable html segment representation of the X12 file. | ||
*/ | ||
public String toHtml() { | ||
StringBuilder builder = new StringBuilder(); | ||
for (Loop loop : _dataLoops) { | ||
builder.append(loop.toHtml(_definition.getLoop(), new ArrayList<>())); | ||
} | ||
return builder.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.