Skip to content

Commit

Permalink
Merge branch 'dev_textmerger' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
maybeec committed Sep 14, 2020
2 parents 32e4136 + 656858e commit fb6dc28
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 14 deletions.
6 changes: 3 additions & 3 deletions 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>2.1.0</version>
<version>7.0.0</version>
<name>CobiGen - Text Merger Plug-in</name>
<description>CobiGen - Text Merger Plug-in</description>

Expand All @@ -20,7 +20,7 @@
<dependency>
<groupId>com.devonfw.cobigen</groupId>
<artifactId>core-api</artifactId>
<version>5.0.0</version>
<version>[7.0.0,)</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
Expand All @@ -31,7 +31,7 @@
<dependency>
<groupId>com.devonfw.cobigen</groupId>
<artifactId>core-test</artifactId>
<version>5.0.0</version>
<version>[7.0.0,)</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,34 @@

import java.util.List;

import com.devonfw.cobigen.api.annotation.Activation;
import com.devonfw.cobigen.api.extension.GeneratorPluginActivator;
import com.devonfw.cobigen.api.extension.Merger;
import com.devonfw.cobigen.api.extension.TriggerInterpreter;
import com.google.common.collect.Lists;

/**
* This Plugin Activator registers a merger, which only appends the patch text to the original existing file
* @author mbrunnli (06.04.2014)
* This plug-in activator registers a merger, which only appends the patch text to the original existing file
*/
@Activation(byMergeStrategy = { TextMergerPluginActivator.TEXTMERGE_APPEND,
TextMergerPluginActivator.TEXTMERGE_APPEND_WITH_NEW_LINE, TextMergerPluginActivator.TEXTMERGE_OVERRIDE })
public class TextMergerPluginActivator implements GeneratorPluginActivator {

/** Merge strategy to append text parts (prefer patch) */
static final String TEXTMERGE_OVERRIDE = "textmerge_override";

/** Merge strategy to append text parts while adding a new line before */
static final String TEXTMERGE_APPEND_WITH_NEW_LINE = "textmerge_appendWithNewLine";

/** Merge strategy to append text parts (prefer base) */
static final String TEXTMERGE_APPEND = "textmerge_append";

@Override
public List<Merger> bindMerger() {
List<Merger> merger = Lists.newLinkedList();
merger.add(new TextAppender("textmerge_append", false));
merger.add(new TextAppender("textmerge_appendWithNewLine", true));
merger.add(new TextAppender("textmerge_override", false));
merger.add(new TextAppender(TEXTMERGE_APPEND, false));
merger.add(new TextAppender(TEXTMERGE_APPEND_WITH_NEW_LINE, true));
merger.add(new TextAppender(TEXTMERGE_OVERRIDE, false));
return merger;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,18 @@
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Helper class that implements a few very specific methods to help with working on maps and regular
* expression and reduce code redundancy.
*/
public class MergeUtil {

/** Logger instance. */
private static final Logger LOG = LoggerFactory.getLogger(MergeUtil.class);

/**
* Regular expression matching what correct anchors should look like.
*/
Expand Down Expand Up @@ -50,6 +55,8 @@ public class MergeUtil {
* merge(File,String,String)
* @param toSplit
* The string that is to be split by anchors
* @param defaultMergeStrategy
* the default merge strategy to fall back
* @return a LinkedHashMap which contains anchors as keys and the following text as values
* @throws Exception
* when an anchor contains a wrong definition
Expand All @@ -58,13 +65,14 @@ public static LinkedHashMap<Anchor, String> splitByAnchors(String toSplit, Merge
throws Exception {
LinkedHashMap<Anchor, String> result = new LinkedHashMap<>();
toSplit.trim();
if (!StringUtils.substring(toSplit, 0, StringUtils.ordinalIndexOf(toSplit, "\n", 1) - lineSepLength).trim()
.matches(anchorRegexTrimmed)) {
String firstLine =
StringUtils.substring(toSplit, 0, StringUtils.ordinalIndexOf(toSplit, "\n", 1) - lineSepLength + 1).trim();
if (!firstLine.matches(anchorRegexTrimmed)) {
LOG.debug("Line {} does not match {}", firstLine, anchorRegexTrimmed);
throw new Exception(
"Incorrect document structure. Anchors are defined but there is no anchor at the start of the document.\n"
+ "See https://github.com/devonfw/cobigen/wiki/cobigen-textmerger#general and "
+ "https://github.com/devonfw/cobigen/wiki/cobigen-textmerger#error-list "
+ "for more details");
+ "https://github.com/devonfw/cobigen/wiki/cobigen-textmerger#error-list " + "for more details");
}

toSplit = toSplit + System.lineSeparator() + "anchor:::anchorend" + System.lineSeparator();
Expand Down Expand Up @@ -227,7 +235,6 @@ public static ArrayList<Anchor> joinKeySetsRetainOrder(Map<Anchor, String> base,
if (checkSame.retainAll(patch.keySet())) {
int i = 0;
int x = 0;
String tmp = "";

for (Anchor s : base.keySet()) {
i++;
Expand Down
2 changes: 1 addition & 1 deletion documentation/master-cobigen.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ DISCLAIMER: All Cobigen plugins are compatible with the latest release of Devonf
* CobiGen - XML Plug-in v7.0.0
* CobiGen - TypeScript Plug-in v7.0.0
* CobiGen - Property Plug-in v7.0.0
* CobiGen - Text Merger v2.1.0
* CobiGen - Text Merger v7.0.0
* CobiGen - JSON Plug-in v7.0.0
* CobiGen - HTML Plug-in v7.0.0
* CobiGen - Open API Plug-in v7.0.0
Expand Down

0 comments on commit fb6dc28

Please sign in to comment.