Skip to content

Commit

Permalink
Merge branch 'dev_textmerger'
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeSchumacherCapgemini committed Dec 4, 2020
2 parents 2346fc9 + 74c86c8 commit e0a0b0f
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 61 deletions.
5 changes: 5 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,8 @@
*.pdf binary
*.exe binary
*.zip binary

# special line ending handling for test files
cobigen/cobigen-textmerger/src/test/resources/TestBaseLineDelimiter.txt eol=lf
cobigen/cobigen-textmerger/src/test/resources/PatchBaseLineDelimiter.txt eol=crlf
cobigen/cobigen-textmerger/src/test/resources/MergeBaseLineDelimiter.txt eol=lf
2 changes: 1 addition & 1 deletion cobigen/cobigen-textmerger/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>textmerger</artifactId>
<packaging>jar</packaging>
<version>7.0.0</version>
<version>7.1.0</version>
<name>CobiGen - Text Merger Plug-in</name>
<description>CobiGen - Text Merger Plug-in</description>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
Expand All @@ -11,6 +13,8 @@

import com.devonfw.cobigen.api.exception.MergeException;
import com.devonfw.cobigen.api.extension.Merger;
import com.devonfw.cobigen.api.util.StringUtil;
import com.devonfw.cobigen.api.util.SystemUtil;
import com.devonfw.cobigen.textmerger.anchorextension.Anchor;
import com.devonfw.cobigen.textmerger.anchorextension.MergeStrategy;
import com.devonfw.cobigen.textmerger.anchorextension.MergeUtil;
Expand All @@ -26,6 +30,9 @@ public class TextAppender implements Merger {
*/
private String type;

/**
* Default merge strategy
*/
private MergeStrategy defaultStrat;

/**
Expand Down Expand Up @@ -68,13 +75,24 @@ public String getType() {
@Override
public String merge(File base, String patch, String targetCharset) throws MergeException {
String mergedString;
String lineDelimiterBase;

Path path = Paths.get(base.getAbsolutePath());

try {
mergedString = FileUtils.readFileToString(base, targetCharset);
lineDelimiterBase = SystemUtil.determineLineDelimiter(path, targetCharset);

} catch (IOException e) {
throw new MergeException(base, "Could not read base file.", e);
}
try {
mergedString = merge(mergedString, patch);
if (lineDelimiterBase.isEmpty()) {
lineDelimiterBase = System.lineSeparator();
}

mergedString =
merge(mergedString, StringUtil.consolidateLineEndings(patch, lineDelimiterBase), lineDelimiterBase);
} catch (Exception e) {
throw new MergeException(base, e.getMessage(), e);
}
Expand All @@ -87,26 +105,28 @@ public String merge(File base, String patch, String targetCharset) throws MergeE
* target {@link String} to be merged into
* @param patch
* {@link String} patch, which should be applied to the base file
* @param lineDelimiter
* String line delimiter to use
* @return Merged text (not null)
* @throws Exception
* When there is some problem about anchors
*/
public String merge(String base, String patch) throws Exception {
public String merge(String base, String patch, String lineDelimiter) throws Exception {
String mergedString = "";
if (MergeUtil.hasAnchors(patch)) {
LinkedHashMap<Anchor, String> splitBase = MergeUtil.splitByAnchors(base, defaultStrat);
LinkedHashMap<Anchor, String> splitPatch = MergeUtil.splitByAnchors(patch, defaultStrat);
LinkedHashMap<Anchor, String> splitBase = MergeUtil.splitByAnchors(base, defaultStrat, lineDelimiter);
LinkedHashMap<Anchor, String> splitPatch = MergeUtil.splitByAnchors(patch, defaultStrat, lineDelimiter);

String footer = "footer";
String header = "header";
String toAppend = "";

if (MergeUtil.hasKeyMatchingDocumentPart(header, splitBase)) {
toAppend = MergeUtil.appendText(toAppend, header, splitBase, true, true);
toAppend = MergeUtil.appendText(toAppend, header, splitBase, true, true, lineDelimiter);
mergedString = MergeUtil.addTextAndDeleteCurrentAnchor(mergedString, toAppend, splitPatch, splitBase,
MergeUtil.getKeyMatchingDocumentPart(header, splitBase));
} else if (MergeUtil.hasKeyMatchingDocumentPart(header, splitPatch)) {
toAppend = MergeUtil.appendText(toAppend, header, splitPatch, true, true);
toAppend = MergeUtil.appendText(toAppend, header, splitPatch, true, true, lineDelimiter);
mergedString = MergeUtil.addTextAndDeleteCurrentAnchor(mergedString, toAppend, splitPatch, splitBase,
MergeUtil.getKeyMatchingDocumentPart(header, splitPatch));
}
Expand All @@ -127,16 +147,20 @@ public String merge(String base, String patch) throws Exception {
if (tmpAnchor.getNewlineName().matches("(newline_).+")) {
switch (tmpAnchor.getNewlineName().toLowerCase()) {
case "newline_appendbefore":
toAppend += MergeUtil.appendText(toAppend, docPart, splitBase, false, false);
toAppend = MergeUtil.appendText(toAppend, docPart, splitPatch, true, true);
toAppend += MergeUtil.appendText(toAppend, docPart, splitBase, false, false,
lineDelimiter);
toAppend = MergeUtil.appendText(toAppend, docPart, splitPatch, true, true,
lineDelimiter);
mergedString = MergeUtil.addTextAndDeleteCurrentAnchor(mergedString, toAppend,
splitPatch, splitBase, tmpAnchor);
break;
case "newline_appendafter":
case "newline_append":
toAppend = MergeUtil.appendText(toAppend, docPart, splitBase, false, false);
toAppend += System.lineSeparator();
toAppend = MergeUtil.appendText(toAppend, docPart, splitPatch, false, true);
toAppend = MergeUtil.appendText(toAppend, docPart, splitBase, false, false,
lineDelimiter);
toAppend += lineDelimiter;
toAppend = MergeUtil.appendText(toAppend, docPart, splitPatch, false, true,
lineDelimiter);
mergedString = MergeUtil.addTextAndDeleteCurrentAnchor(mergedString, toAppend,
splitPatch, splitBase, tmpAnchor);
break;
Expand All @@ -147,18 +171,22 @@ public String merge(String base, String patch) throws Exception {
} else if (tmpAnchor.getNewlineName().matches(".*(newline)")) {
switch (tmpAnchor.getNewlineName().toLowerCase()) {
case "appendbefore_newline":
toAppend += System.lineSeparator();
toAppend = MergeUtil.appendText(toAppend, docPart, splitBase, false, false);
toAppend = MergeUtil.appendText(toAppend, docPart, splitPatch, true, true);
toAppend += lineDelimiter;
toAppend = MergeUtil.appendText(toAppend, docPart, splitBase, false, false,
lineDelimiter);
toAppend = MergeUtil.appendText(toAppend, docPart, splitPatch, true, true,
lineDelimiter);
mergedString = MergeUtil.addTextAndDeleteCurrentAnchor(mergedString, toAppend,
splitPatch, splitBase, tmpAnchor);
break;
case "newline":
case "appendafter_newline":
case "append_newline":
toAppend = MergeUtil.appendText(toAppend, docPart, splitBase, false, false);
toAppend = MergeUtil.appendText(toAppend, docPart, splitPatch, false, true);
toAppend += System.lineSeparator();
toAppend = MergeUtil.appendText(toAppend, docPart, splitBase, false, false,
lineDelimiter);
toAppend = MergeUtil.appendText(toAppend, docPart, splitPatch, false, true,
lineDelimiter);
toAppend += lineDelimiter;
mergedString = MergeUtil.addTextAndDeleteCurrentAnchor(mergedString, toAppend,
splitPatch, splitBase, tmpAnchor);
break;
Expand All @@ -169,25 +197,31 @@ public String merge(String base, String patch) throws Exception {
} else {
switch (mergeStrat) {
case APPENDBEFORE:
toAppend = MergeUtil.appendText(toAppend, docPart, splitBase, false, false);
toAppend = MergeUtil.appendText(toAppend, docPart, splitPatch, true, true);
toAppend = MergeUtil.appendText(toAppend, docPart, splitBase, false, false,
lineDelimiter);
toAppend = MergeUtil.appendText(toAppend, docPart, splitPatch, true, true,
lineDelimiter);
mergedString = MergeUtil.addTextAndDeleteCurrentAnchor(mergedString, toAppend,
splitPatch, splitBase, tmpAnchor);
break;
case APPENDAFTER:
case APPEND:
toAppend = MergeUtil.appendText(toAppend, docPart, splitBase, false, false);
toAppend = MergeUtil.appendText(toAppend, docPart, splitPatch, false, true);
toAppend = MergeUtil.appendText(toAppend, docPart, splitBase, false, false,
lineDelimiter);
toAppend = MergeUtil.appendText(toAppend, docPart, splitPatch, false, true,
lineDelimiter);
mergedString = MergeUtil.addTextAndDeleteCurrentAnchor(mergedString, toAppend,
splitPatch, splitBase, tmpAnchor);
break;
case OVERRIDE:
toAppend = MergeUtil.appendText(toAppend, docPart, splitPatch, false, true);
toAppend = MergeUtil.appendText(toAppend, docPart, splitPatch, false, true,
lineDelimiter);
mergedString = MergeUtil.addTextAndDeleteCurrentAnchor(mergedString, toAppend,
splitPatch, splitBase, tmpAnchor);
break;
case NOMERGE:
toAppend = MergeUtil.appendText(toAppend, docPart, splitBase, false, true);
toAppend = MergeUtil.appendText(toAppend, docPart, splitBase, false, true,
lineDelimiter);
mergedString = MergeUtil.addTextAndDeleteCurrentAnchor(mergedString, toAppend,
splitPatch, splitBase, tmpAnchor);
break;
Expand All @@ -203,22 +237,23 @@ public String merge(String base, String patch) throws Exception {
}

} else {
toAppend = MergeUtil.appendText(toAppend, docPart, splitBase, false, true);
toAppend =
MergeUtil.appendText(toAppend, docPart, splitBase, false, true, lineDelimiter);
mergedString = MergeUtil.addTextAndDeleteCurrentAnchor(mergedString, toAppend,
splitPatch, splitBase, tmpAnchor);
}
} else if (MergeUtil.hasKeyMatchingDocumentPart(docPart, splitPatch)) {
toAppend = MergeUtil.appendText(toAppend, docPart, splitPatch, false, true);
toAppend = MergeUtil.appendText(toAppend, docPart, splitPatch, false, true, lineDelimiter);
mergedString = MergeUtil.addTextAndDeleteCurrentAnchor(mergedString, toAppend, splitPatch,
splitBase, tmpAnchor);
}
}
} else {
if (MergeUtil.hasKeyMatchingDocumentPart(footer, splitBase)) {
toAppend = MergeUtil.appendText(toAppend, footer, splitBase, false, true);
toAppend = MergeUtil.appendText(toAppend, footer, splitBase, false, true, lineDelimiter);
mergedString += toAppend;
} else if (MergeUtil.hasKeyMatchingDocumentPart(footer, splitPatch)) {
toAppend = MergeUtil.appendText(toAppend, footer, splitPatch, false, true);
toAppend = MergeUtil.appendText(toAppend, footer, splitPatch, false, true, lineDelimiter);
mergedString += toAppend;
}
break;
Expand All @@ -231,7 +266,7 @@ public String merge(String base, String patch) throws Exception {
mergedString = patch;
return mergedString;
} else if (withNewLineBeforehand) {
mergedString += System.lineSeparator();
mergedString += lineDelimiter;
}
mergedString += patch;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,14 @@ public class MergeUtil {
* The string that is to be split by anchors
* @param defaultMergeStrategy
* the default merge strategy to fall back
* @param lineDelimiter
* String line delimiter to use
* @return a LinkedHashMap which contains anchors as keys and the following text as values
* @throws Exception
* when an anchor contains a wrong definition
*/
public static LinkedHashMap<Anchor, String> splitByAnchors(String toSplit, MergeStrategy defaultMergeStrategy)
throws Exception {
public static LinkedHashMap<Anchor, String> splitByAnchors(String toSplit, MergeStrategy defaultMergeStrategy,
String lineDelimiter) throws Exception {
LinkedHashMap<Anchor, String> result = new LinkedHashMap<>();
toSplit.trim();
String firstLine =
Expand All @@ -75,7 +77,7 @@ public static LinkedHashMap<Anchor, String> splitByAnchors(String toSplit, Merge
+ "https://github.com/devonfw/cobigen/wiki/cobigen-textmerger#error-list " + "for more details");
}

toSplit = toSplit + System.lineSeparator() + "anchor:::anchorend" + System.lineSeparator();
toSplit = toSplit + lineDelimiter + "anchor:::anchorend" + lineDelimiter;
Anchor anchor;
int anchorCount = 0;
Pattern regex = Pattern.compile(anchorRegex);
Expand Down Expand Up @@ -168,26 +170,27 @@ public static Anchor getKeyMatchingDocumentPart(String docPart, Map<Anchor, Stri
* Specifies whether the new text should be appended before or after the existing text.
* @param withKey
* Specifies whether the key of the map should be appended too, or just the value.
* @param lineDelimiter
* String line delimiter to use
* @return The original text with new text appended to it. An empty string if there is no key in the map
* matching the regular expression.
*/
public static String appendText(String text, String docPart, Map<Anchor, String> m, boolean before,
boolean withKey) {
public static String appendText(String text, String docPart, Map<Anchor, String> m, boolean before, boolean withKey,
String lineDelimiter) {
Anchor key = MergeUtil.getKeyMatchingDocumentPart(docPart, m);
if (key.getNewlineName().equalsIgnoreCase("newline_appendbefore") && withKey) {
return System.lineSeparator() + key.getAnchor() + System.lineSeparator() + System.lineSeparator()
+ m.get(key) + text;
return lineDelimiter + key.getAnchor() + lineDelimiter + lineDelimiter + m.get(key) + text;
} else if (before) {
if (withKey) {
return System.lineSeparator() + key.getAnchor() + System.lineSeparator() + m.get(key) + text;
return lineDelimiter + key.getAnchor() + lineDelimiter + m.get(key) + text;
} else {
return System.lineSeparator() + m.get(key) + text;
return lineDelimiter + m.get(key) + text;
}
} else {
if (withKey) {
return System.lineSeparator() + key.getAnchor() + text + System.lineSeparator() + m.get(key);
return lineDelimiter + key.getAnchor() + text + lineDelimiter + m.get(key);
} else {
return text + System.lineSeparator() + m.get(key);
return text + lineDelimiter + m.get(key);
}
}
}
Expand Down
Loading

0 comments on commit e0a0b0f

Please sign in to comment.