Skip to content

Commit

Permalink
Merge remote-tracking branch 'kentico/release/2.0'
Browse files Browse the repository at this point in the history
# Conflicts:
#	delivery/src/main/java/com/kenticocloud/delivery/RichTextElementConverter.java
  • Loading branch information
aweigold committed Oct 14, 2018
2 parents 98bd20d + 0082bfb commit b71c352
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 39 deletions.
1 change: 1 addition & 0 deletions .idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .travis/build.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash

set -e # Exit with nonzero exit code if anything fails
SOURCE_BRANCH="master"
SOURCE_BRANCH="release/2.0"

if [ "${TRAVIS_PULL_REQUEST}" != "false" -o "${TRAVIS_BRANCH}" != "${SOURCE_BRANCH}" ]; then
./gradlew build
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![Javadocs](http://javadoc.io/badge/com.kenticocloud/delivery.svg)](http://javadoc.io/doc/com.kenticocloud/delivery)
[![SonarQube](http://img.shields.io/badge/SonarQube-Results-blue.svg)](https://sonarcloud.io/dashboard?id=com.kenticocloud%3Adelivery-sdk-java)
[![MavenCentral](http://img.shields.io/badge/Maven_Central-2.0.1-yellow.svg)](https://oss.sonatype.org/content/groups/public/com/kenticocloud/delivery/)
[![MavenCentral](http://img.shields.io/badge/Maven_Central-2.0.2-yellow.svg)](https://oss.sonatype.org/content/groups/public/com/kenticocloud/delivery/)
[![Forums](https://img.shields.io/badge/chat-on%20forums-orange.svg)](https://forums.kenticocloud.com)

## Summary
Expand All @@ -20,7 +20,7 @@ repositories {
}
dependencies {
compile('com.kenticocloud:delivery:2.0.1')
compile('com.kenticocloud:delivery:2.0.2')
}
```

Expand All @@ -29,7 +29,7 @@ Or via your Maven POM:
<dependency>
<groupId>com.kenticocloud</groupId>
<artifactId>delivery</artifactId>
<version>2.0.1</version>
<version>2.0.2</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@
*/

subprojects { project ->
version '2.0.2-SNAPSHOT'
version '2.0.2'
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,38 +68,8 @@ > matches the character > literally (case sensitive)
*/
private Pattern linkPattern = Pattern.compile("<a[^>]+?data-item-id=\"(?<id>[^\"]+)\"[^>]*>");

/*
Regex prior to the Java \ escapes:
<object type=\"application\/kenticocloud" data-type=\"(?<type>[^\"]+\") data-codename=\"(?<codename>[^\"]+\")><\/object>
Regex explanation:
<object type= matches the characters <object type= literally (case sensitive)
\" matches the character " literally (case sensitive)
application matches the characters application literally (case sensitive)
\/ matches the character / literally (case sensitive)
kenticocloud" data-type= matches the characters kenticocloud" data-type= literally (case sensitive)
\" matches the character " literally (case sensitive)
Named Capture Group type (?<type>[^\"]+\")
Match a single character not present in the list below [^\"]+
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
\" matches the character " literally (case sensitive)
\" matches the character " literally (case sensitive)
data-codename= matches the characters data-codename= literally (case sensitive)
\" matches the character " literally (case sensitive)
Named Capture Group codename (?<codename>[^\"]+\")
Match a single character not present in the list below [^\"]+
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
\" matches the character " literally (case sensitive)
\" matches the character " literally (case sensitive)
>< matches the characters >< literally (case sensitive)\/ matches the character / literally (case sensitive)
object> matches the characters object> literally (case sensitive)
Global pattern flags
g modifier: global. All matches (don't return after first match)
*/
private Pattern linkedItemPattern = Pattern.compile(
"<object type=\"application/kenticocloud\" data-type=\"" +
"(?<type>[^\"]+)\" data-codename=\"" +
"(?<codename>[^\"]+)\"></object>");
private Pattern linkedItemPattern = Pattern.compile("<object type=\"application/kenticocloud\" " +
"(?<attrs>[^>]+)></object>");

public RichTextElementConverter(
ContentLinkUrlResolver contentLinkUrlResolver,
Expand Down Expand Up @@ -179,7 +149,9 @@ private String resolveLinkedItemsRecursively(
StringBuffer buffer = new StringBuffer();

while (matcher.find()) {
String codename = matcher.group("codename");
String attrs = matcher.group("attrs");
InlineModularContentDataAttributes dataAttributes = InlineModularContentDataAttributes.fromAttrs(attrs);
String codename = dataAttributes.getCodename();
ContentItem linkedItem = element.getParent().getLinkedItem(codename);
InternalInlineContentItemResolver resolver = null;
// Check to see if we encountered a cycle in our tree, if so, halt resolution
Expand Down Expand Up @@ -260,4 +232,37 @@ private TemplateEngineInlineContentItemsResolver getTemplateResolver(TemplateEng
private interface InternalInlineContentItemResolver {
String resolve();
}

@lombok.Getter
@lombok.Setter
@lombok.Builder
private static class InlineModularContentDataAttributes {
private static Pattern DATA_ATTRIBUTE_PATTERN = Pattern.compile("(data-\\w+)=\"(\\w+)\"");

private String type;
private String rel;
private String codename;

private static InlineModularContentDataAttributes fromAttrs(String attrs) {
Matcher matcher = DATA_ATTRIBUTE_PATTERN.matcher(attrs);
InlineModularContentDataAttributesBuilder builder = InlineModularContentDataAttributes.builder();
while (matcher.find()) {
String key = matcher.group(1);
String value = matcher.group(2);
switch (key) {
case "data-type":
builder.type(value);
break;
case "data-rel":
builder.rel(value);
break;
case "data-codename":
builder.codename(value);
default:
break;
}
}
return builder.build();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,52 @@ public String resolve(CustomItem data) {
contentItem.setStronglyTypedContentItemConverter(stronglyTypedContentItemConverter);
original.parent = contentItem;
original.setValue(
"<p><object type=\"application/kenticocloud\" data-type=\"item\" data-codename=\"donate_with_us\"></object></p>");
"<p><object type=\"application/kenticocloud\" data-type=\"item\" data-rel=\"link\" data-codename=\"donate_with_us\"></object></p>");
RichTextElement converted = converter.convert(original);
Assert.assertEquals(
"<p>Please donate with us.</p>",
converted.getValue());
}

@Test
public void testModularContentReplacementWithUnexpectedDataAttributesAndOrder() {
StronglyTypedContentItemConverter stronglyTypedContentItemConverter = new StronglyTypedContentItemConverter();
stronglyTypedContentItemConverter.registerType(CustomItem.class);
stronglyTypedContentItemConverter.registerInlineContentItemsResolver(new InlineContentItemsResolver<CustomItem>() {
@Override
public String resolve(CustomItem data) {
return data.getMessageText();
}
});
RichTextElementConverter converter = new RichTextElementConverter(
null,
null,
null,
null,
stronglyTypedContentItemConverter);
RichTextElement original = new RichTextElement();
original.setModularContent(Collections.singletonList("donate_with_us"));
ContentItem contentItem = new ContentItem();
contentItem.setModularContentProvider(() -> {
ContentItem donateWithUs = new ContentItem();
System system = new System();
system.setType("item");
donateWithUs.setSystem(system);
TextElement textElement = new TextElement();
textElement.setValue("Please donate with us.");
HashMap<String, Element> elements = new HashMap<>();
elements.put("message_text", textElement);
donateWithUs.setElements(elements);
donateWithUs.setModularContentProvider(HashMap::new);
donateWithUs.setStronglyTypedContentItemConverter(stronglyTypedContentItemConverter);
HashMap<String, ContentItem> modularContent = new HashMap<>();
modularContent.put("donate_with_us", donateWithUs);
return modularContent;
});
contentItem.setStronglyTypedContentItemConverter(stronglyTypedContentItemConverter);
original.parent = contentItem;
original.setValue(
"<p><object type=\"application/kenticocloud\" data-codename=\"donate_with_us\" new-value=\"unexpected\" data-new=\"unexpected\" data-type=\"item\" data-rel=\"link\"></object></p>");
RichTextElement converted = converter.convert(original);
Assert.assertEquals(
"<p>Please donate with us.</p>",
Expand Down

0 comments on commit b71c352

Please sign in to comment.