Skip to content

Commit 9a07c01

Browse files
committed
scm4j/scm4j-releaser/Avoid !!omap in repositories parameters #9
avoid BOM in files
1 parent d72ae65 commit 9a07c01

File tree

10 files changed

+119
-65
lines changed

10 files changed

+119
-65
lines changed

src/main/java/org/scm4j/commons/RegexConfig.java

Lines changed: 50 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
package org.scm4j.commons;
22

3+
import org.yaml.snakeyaml.Yaml;
4+
import org.yaml.snakeyaml.nodes.Node;
5+
import org.yaml.snakeyaml.nodes.NodeId;
6+
7+
import java.io.BufferedReader;
38
import java.io.IOException;
9+
import java.io.StringReader;
410
import java.util.LinkedHashMap;
511
import java.util.Map;
612

7-
import org.yaml.snakeyaml.Yaml;
8-
913
public class RegexConfig {
1014
public static final String URL_SEPARATOR = URLContentLoader.URL_SEPARATOR;
15+
public static final String OMAP_TAG = "!!omap";
1116
private final LinkedHashMap<Object, Object> content = new LinkedHashMap<>();
1217

1318
@SuppressWarnings("unchecked")
@@ -21,10 +26,13 @@ public void loadFromYamlUrls(String... separatedUrls) throws IOException {
2126
continue;
2227
}
2328
String content = loader.getContentFromUrl(url);
29+
2430
if (!content.isEmpty()) {
31+
content = prependOmapIfNeed(content, yaml);
2532
LinkedHashMap<Object, Object> map;
2633
try {
2734
map = (LinkedHashMap<Object, Object>) yaml.load(content);
35+
//map = (LinkedHashMap<Object, Object>) yaml.loadAs(content, Map.class);
2836
} catch (Exception e) {
2937
throw new EConfig("failed to load config from yaml content at " + url + ": " + e.getMessage(), e);
3038
}
@@ -36,16 +44,40 @@ public void loadFromYamlUrls(String... separatedUrls) throws IOException {
3644
}
3745
}
3846
}
39-
47+
48+
String prependOmapIfNeed(String content, Yaml yaml) throws IOException {
49+
if (isNotEmptyAndHasNoOMAPTag(content)) {
50+
StringReader sr = new StringReader(content);
51+
Node node = yaml.compose(sr);
52+
if (node.getNodeId().equals(NodeId.sequence)) {
53+
return OMAP_TAG + "\r\n" + content;
54+
}
55+
return content;
56+
}
57+
return content;
58+
}
59+
60+
private boolean isNotEmptyAndHasNoOMAPTag(String content) throws IOException {
61+
StringReader sr = new StringReader(content);
62+
BufferedReader br = new BufferedReader(sr);
63+
String line = br.readLine();
64+
while (line != null) {
65+
CommentedString cs = new CommentedString(line);
66+
if (cs.isValuable()) {
67+
return !cs.getStrNoComment().trim().startsWith(OMAP_TAG);
68+
}
69+
line = br.readLine();
70+
}
71+
return false;
72+
}
73+
4074
@SuppressWarnings("unchecked")
4175
public <T> T getPropByName(String nameToMatch, String propName, T defaultValue) {
42-
if (content != null) {
43-
for (Object key : content.keySet()) {
44-
if (key == null || nameToMatch.matches((String) key)) {
45-
Map<?, ?> props = (Map<?, ?>) content.get(key);
46-
if (props.containsKey(propName)) {
47-
return (T) props.get(propName);
48-
}
76+
for (Object key : content.keySet()) {
77+
if (key == null || nameToMatch.matches((String) key)) {
78+
Map<?, ?> props = (Map<?, ?>) content.get(key);
79+
if (props.containsKey(propName)) {
80+
return (T) props.get(propName);
4981
}
5082
}
5183
}
@@ -54,16 +86,14 @@ public <T> T getPropByName(String nameToMatch, String propName, T defaultValue)
5486

5587
public String getPlaceholderedStringByName(String nameToMatch, Object propName, String defaultValue) {
5688
String result = defaultValue;
57-
if (content != null) {
58-
for (Object key : content.keySet()) {
59-
if (key == null || nameToMatch.matches((String) key)) {
60-
Map<?, ?> props = (Map<?, ?>) content.get(key);
61-
if (props.containsKey(propName)) {
62-
result = (String) props.get(propName);
63-
if (result != null)
64-
result = nameToMatch.replaceFirst(key == null ? ".*" : (String) key, result);
65-
break;
66-
}
89+
for (Object key : content.keySet()) {
90+
if (key == null || nameToMatch.matches((String) key)) {
91+
Map<?, ?> props = (Map<?, ?>) content.get(key);
92+
if (props.containsKey(propName)) {
93+
result = (String) props.get(propName);
94+
if (result != null)
95+
result = nameToMatch.replaceFirst(key == null ? ".*" : (String) key, result);
96+
break;
6797
}
6898
}
6999
}

src/main/java/org/scm4j/commons/URLContentLoader.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package org.scm4j.commons;
22

3+
import org.apache.commons.io.ByteOrderMark;
34
import org.apache.commons.io.IOUtils;
5+
import org.apache.commons.io.input.BOMInputStream;
46

57
import java.io.IOException;
68
import java.io.InputStream;
@@ -48,7 +50,10 @@ private String getWithDefaultProtocol(String urlStr) {
4850

4951
public String getContentFromUrl(URL url) throws IOException {
5052
try (InputStream inputStream = url.openStream()) {
51-
return IOUtils.toString(inputStream, StandardCharsets.UTF_8.name());
53+
BOMInputStream bOMInputStream = new BOMInputStream(inputStream);
54+
ByteOrderMark bom = bOMInputStream.getBOM();
55+
String charsetName = bom == null ? StandardCharsets.UTF_8.toString() : bom.getCharsetName();
56+
return IOUtils.toString(bOMInputStream, charsetName);
5257
}
5358
}
5459

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
11
package org.scm4j.commons;
22

3+
import org.apache.commons.io.FileUtils;
4+
import org.apache.commons.lang3.StringUtils;
35
import org.junit.Before;
46
import org.junit.Test;
5-
import org.scm4j.commons.EConfig;
6-
import org.scm4j.commons.RegexConfig;
7+
import org.yaml.snakeyaml.Yaml;
78

9+
import java.io.File;
810
import java.io.IOException;
11+
import java.nio.charset.StandardCharsets;
912

1013
import static org.junit.Assert.*;
1114

1215
public class RegexConfigTest {
13-
16+
1417
private RegexConfig config;
15-
private String url1 = this.getClass().getResource("urls-omap.yml").toString();
16-
private String url2 = this.getClass().getResource("urls-omap-bom.yml").toString();
17-
private String url3 = this.getClass().getResource("urls-omap-last.yml").toString();
18-
private String url4 = this.getClass().getResource("urls-empty.yml").toString();
19-
private String url5 = this.getClass().getResource("urls-wrong-content.yml").toString();
18+
private String seqOmap = this.getClass().getResource("sequence-omap.yml").toString();
19+
private String mapping = this.getClass().getResource("mapping.yml").toString();
20+
private String seq = this.getClass().getResource("sequence.yml").toString();
21+
private String empty = this.getClass().getResource("empty.yml").toString();
22+
private String wrongContent = this.getClass().getResource("wrong-content.yml").toString();
23+
private String seqBOM = this.getClass().getResource("sequence-bom.yml").toString();
2024

2125
@Before
2226
public void setUp() {
@@ -25,7 +29,7 @@ public void setUp() {
2529

2630
@Test
2731
public void testGetPropByName() throws IOException {
28-
config.loadFromYamlUrls(url1, url2 + ";" + url3);
32+
config.loadFromYamlUrls(seqOmap, seqBOM + ";" + seq + ";" + mapping);
2933
assertEquals("value1and2", config.getPropByName("node1", "prop1and2", null));
3034
assertEquals("value1and2", config.getPropByName("node2", "prop1and2", null));
3135
assertEquals("default value", config.getPropByName("node1", "unexisting_prop", "default value"));
@@ -38,17 +42,17 @@ public void testGetPropByName() throws IOException {
3842
Boolean boolValue = config.getPropByName("unexisting_node", "booleanProp", null);
3943
assertEquals(true, boolValue);
4044
}
41-
45+
4246
@Test
4347
public void testGetPlaceholderedStringByName() throws IOException {
44-
config.loadFromYamlUrls(url1, url2 + ";" + url3);
48+
config.loadFromYamlUrls(seqOmap, seqBOM + ";" + seq);
4549
assertEquals("value4_placeholder", config.getPlaceholderedStringByName("node4placeholder", "prop4", null));
4650
assertEquals("unexisting_node", config.getPlaceholderedStringByName("unexisting_node", "placeholderedProp", null));
4751
}
48-
52+
4953
@Test
5054
public void testEmptyConfig() throws IOException {
51-
config.loadFromYamlUrls(url4);
55+
config.loadFromYamlUrls(empty);
5256
assertTrue(config.isEmpty());
5357
}
5458

@@ -57,14 +61,35 @@ public void testEmptyUrls() throws IOException {
5761
config.loadFromYamlUrls("");
5862
assertTrue(config.isEmpty());
5963
}
60-
64+
6165
@Test
6266
public void testWrongContent() throws IOException {
6367
try {
64-
config.loadFromYamlUrls(url5);
68+
config.loadFromYamlUrls(wrongContent);
6569
fail();
6670
} catch (EConfig e) {
67-
71+
6872
}
6973
}
70-
}
74+
75+
@Test
76+
public void testPrependOmap() throws Exception {
77+
Yaml yaml = new Yaml();
78+
File seqOmap = new File(this.getClass().getResource("sequence-omap.yml").toURI());
79+
File mapping = new File(this.getClass().getResource("mapping.yml").toURI());
80+
File seq = new File(this.getClass().getResource("sequence.yml").toURI());
81+
File empty = new File(this.getClass().getResource("empty.yml").toURI());
82+
83+
String content = config.prependOmapIfNeed(FileUtils.readFileToString(seqOmap, StandardCharsets.UTF_8), yaml);
84+
assertTrue(StringUtils.countMatches(content, RegexConfig.OMAP_TAG) == 1);
85+
86+
content = config.prependOmapIfNeed(FileUtils.readFileToString(mapping, StandardCharsets.UTF_8), yaml);
87+
assertTrue(StringUtils.countMatches(content, RegexConfig.OMAP_TAG) == 0);
88+
89+
content = config.prependOmapIfNeed(FileUtils.readFileToString(seq, StandardCharsets.UTF_8), yaml);
90+
assertTrue(StringUtils.countMatches(content, RegexConfig.OMAP_TAG) == 1);
91+
92+
content = config.prependOmapIfNeed(FileUtils.readFileToString(empty, StandardCharsets.UTF_8), yaml);
93+
assertTrue(StringUtils.countMatches(content, RegexConfig.OMAP_TAG) == 0);
94+
}
95+
}

src/test/java/org/scm4j/commons/URLContentLoaderTest.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,24 @@
1313

1414
public class URLContentLoaderTest {
1515

16-
private static final String URLS_OMAP_BOM_FILE = "urls-omap-bom.yml";
17-
private static final String URLS_OMAP_FILE = "urls-omap.yml";
18-
private static final String URLS_OMAP_LAST_FILE = "urls-omap-last.yml";
16+
private static final String SEQ_BOM = "sequence-bom.yml";
17+
private static final String SEQ_OMAP = "sequence-omap.yml";
18+
private static final String SEQ = "sequence.yml";
1919

2020
@Test
2121
public void testGetContentFromUrls() throws Exception {
2222
URLContentLoader loader = new URLContentLoader();
23-
URL url1 = this.getClass().getResource(URLS_OMAP_FILE);
24-
URL url2 = this.getClass().getResource(URLS_OMAP_BOM_FILE);
25-
URL url3 = this.getClass().getResource(URLS_OMAP_LAST_FILE);
23+
URL url1 = this.getClass().getResource(SEQ_OMAP);
24+
URL url2 = this.getClass().getResource(SEQ_BOM);
25+
URL url3 = this.getClass().getResource(SEQ);
2626
String content1 = FileUtils.readFileToString(new File(url1.toURI()), StandardCharsets.UTF_8);
2727
String content2 = FileUtils.readFileToString(new File(url2.toURI()), StandardCharsets.UTF_8);
2828
String content3 = FileUtils.readFileToString(new File(url3.toURI()), StandardCharsets.UTF_8);
2929
List<String> contents = loader.getContentsFromUrls(url1.toString(), url2.toString() + URLContentLoader.URL_SEPARATOR + url3.toString());
30-
assertEquals(String.join("", content1, content2, content3), String.join("", contents));
30+
assertEquals(String.join("", content1, content2.substring(1), content3), String.join("", contents));
3131

3232
contents = loader.getContentsFromUrls(Arrays.asList(url1, url2));
33-
assertEquals(String.join("", content1, content2), String.join("", contents));
33+
assertEquals(String.join("", content1, content2.substring(1)), String.join("", contents));
3434

3535
contents = loader.getContentsFromUrls("", URLContentLoader.URL_SEPARATOR + url1.toString());
3636
assertEquals(content1, String.join("", contents));
@@ -39,14 +39,14 @@ public void testGetContentFromUrls() throws Exception {
3939
@Test
4040
public void testFileProtocolOmitting() throws Exception {
4141
URLContentLoader loader = new URLContentLoader();
42-
URL url1 = this.getClass().getResource(URLS_OMAP_FILE);
43-
URL url2 = this.getClass().getResource(URLS_OMAP_BOM_FILE);
42+
URL url1 = this.getClass().getResource(SEQ_OMAP);
43+
URL url2 = this.getClass().getResource(SEQ_BOM);
4444
File file1 = new File(url1.toURI());
4545
File file2 = new File(url2.toURI());
4646
String content1 = FileUtils.readFileToString(file1, StandardCharsets.UTF_8);
4747
String content2 = FileUtils.readFileToString(file2, StandardCharsets.UTF_8);
4848
List<String> contents = loader.getContentsFromUrls(file1.toString(), file2.toString());
49-
assertEquals(String.join("", content1, content2), String.join("", contents));
49+
assertEquals(String.join("", content1, content2.substring(1)), String.join("", contents));
5050
}
5151
}
5252

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node3(.*):
2+
prop3: value3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
!!omap
2-
- node1|node2:
3-
prop1and2: value1and2
4-
- node3(.*):
5-
prop3: value3
6-
1+
!!omap
2+
- node1|node2:
3+
prop1and2: value1and2
4+
75

Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
!!omap
2-
- ~:
3-
defaultProp: defaultValue
4-
placeholderedProp: $0
5-
intProp: 1
1+
- ~:
2+
defaultProp: defaultValue
3+
placeholderedProp: $0
4+
intProp: 1
65
booleanProp: true

src/test/resources/org/scm4j/commons/urls-omap-bom.yml

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)