Skip to content

Commit ca29f0a

Browse files
Merge pull request #33 from contentstack/next
next : support of br tag and alignment styling
2 parents b1631d1 + bb6b332 commit ca29f0a

File tree

10 files changed

+568
-481
lines changed

10 files changed

+568
-481
lines changed

.idea/.gitignore

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

Changelog.md

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,65 @@
1-
21
# Changelog
32

43
A brief description of what changes project contains
54

65
## Aug 8, 2023
7-
#### v1.2.3
8-
- Attribute support added
96

7+
#### v1.2.4
8+
9+
- Style Attribute support added
10+
- Issue fixes for missing center alignment
11+
- \n support added using br tag to support new lines
12+
13+
## Aug 8, 2023
14+
15+
#### v1.2.3
16+
17+
- Attribute support added
1018

1119
## Jun 7un 26, 2023
12-
#### v1.2.2
20+
21+
#### v1.2.2
22+
1323
- Added Support For Nested Assets
1424
- General Code Improvement
1525

1626
## Jun 26, 2023
17-
#### v1.2.1
27+
28+
#### v1.2.1
29+
1830
- New Line Issues while rendering Json object To HTML
1931

2032
## Updated Jsoup vulnerable dependency
21-
#### v1.2.0
33+
34+
#### v1.2.0
35+
2236
- Updated Jsoup vulnerable dependency
2337
- jsonToHTML function support added
2438

25-
2639
## Fixed compile Issue
27-
#### v1.1.2
40+
41+
#### v1.1.2
42+
2843
- Fixed compile Issue
2944

3045
## Transitive dependencies updated
31-
#### v1.1.1
32-
- Updated transitive dependencies to pom.xml
3346

47+
#### v1.1.1
48+
49+
- Updated transitive dependencies to pom.xml
3450

3551
## JSON RTE Feature Support
36-
#### v1.1.0
52+
53+
#### v1.1.0
54+
3755
- JSON RTE Feature Support
3856

3957
## Introducing Release Of Java Utils SDK
40-
#### v1.0.0
58+
59+
#### v1.0.0
60+
4161
- Initial release for Utils SDK
62+
4263
## Support
4364

4465
- For support, email [email protected] or join our Slack channel.

pom.xml

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@
22
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
33
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
44
<modelVersion>4.0.0</modelVersion>
5-
65
<groupId>com.contentstack.sdk</groupId>
76
<artifactId>utils</artifactId>
8-
<version>1.2.3</version>
7+
<version>1.2.4</version>
98
<packaging>jar</packaging>
109
<name>Contentstack-utils</name>
11-
<description>Java Utils SDK for Contentstack Content Delivery API, Contentstack is a headless CMS
12-
</description>
10+
<description>Java Utils SDK for Contentstack Content Delivery API, Contentstack is a headless CMS</description>
1311
<url>https://www.***REMOVED***</url>
1412

1513
<properties>
@@ -29,8 +27,8 @@
2927
<nexus-staging-maven-plugin.version>1.6.7</nexus-staging-maven-plugin.version>
3028
<maven-release-plugin.version>2.5.3</maven-release-plugin.version>
3129
<validation-version>2.0.1.Final</validation-version>
32-
<json-version>20230227</json-version>
33-
<spring-web-version>6.0.11</spring-web-version>
30+
<json-version>20230618</json-version>
31+
<spring-web-version>6.0.12</spring-web-version>
3432
<org.apache.commons-text>1.10.0</org.apache.commons-text>
3533
</properties>
3634

@@ -173,6 +171,9 @@
173171
<groupId>org.apache.maven.plugins</groupId>
174172
<artifactId>maven-javadoc-plugin</artifactId>
175173
<version>${maven-javadoc-plugin.version}</version>
174+
<configuration>
175+
<javadocExecutable>${java.home}/bin/javadoc</javadocExecutable>
176+
</configuration>
176177
<executions>
177178
<execution>
178179
<id>attach-javadocs</id>
@@ -223,6 +224,15 @@
223224
<autoReleaseAfterClose>true</autoReleaseAfterClose>
224225
</configuration>
225226
</plugin>
227+
<plugin>
228+
<groupId>org.apache.maven.plugins</groupId>
229+
<artifactId>maven-compiler-plugin</artifactId>
230+
<configuration>
231+
<source>1.8</source>
232+
<target>1.8</target>
233+
<!-- <executable>${JAVA_HOME}/bin/javac</executable>-->
234+
</configuration>
235+
</plugin>
226236
<plugin>
227237
<groupId>org.apache.maven.plugins</groupId>
228238
<artifactId>maven-release-plugin</artifactId>

src/main/java/com/contentstack/utils/Utils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ public static void jsonToHTML(@NotNull JSONObject entry, @NotNull String[] keyPa
196196
* @param jsonRTE A JSONObject representing the JSON data to be converted to HTML.
197197
* @param renderOption The `renderOption` parameter is an option that determines how the JSON content
198198
* should be rendered as HTML. It could be an enum or a class that defines different rendering options.
199-
* @param embeddeditems The `embeddeditems` parameter is a `JSONObject` that contains embedded items.
199+
* @param embeddeditems The `embedded-items` parameter is a `JSONObject` that contains embedded items.
200200
* It is used to find and retrieve embedded items based on their metadata.
201201
* @return The method is returning a String.
202202
*/
@@ -219,7 +219,7 @@ public static String jsonToHTML(@NotNull JSONObject jsonRTE, Option renderOption
219219
* @param renderOption The `renderOption` parameter is an option that determines how the JSON data
220220
* should be rendered as HTML. It could be an enum or a custom class that defines different rendering
221221
* options.
222-
* @param embeddeditems The `embeddeditems` parameter is a `JSONObject` that contains embedded items.
222+
* @param embeddeditems The `embedded-items` parameter is a `JSONObject` that contains embedded items.
223223
* It is used to find and retrieve embedded items based on the metadata provided.
224224
* @return The method is returning an Object.
225225
*/

src/main/java/com/contentstack/utils/render/DefaultOption.java

Lines changed: 63 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
import org.apache.commons.text.StringEscapeUtils;
88
import org.json.JSONObject;
99

10-
import java.util.ArrayList;
11-
import java.util.Arrays;
10+
import java.util.*;
1211

1312

1413
public class DefaultOption implements Option {
@@ -55,6 +54,8 @@ public String renderOptions(JSONObject embeddedObject, Metadata metadata) {
5554
*/
5655
@Override
5756
public String renderMark(MarkType markType, String text) {
57+
// Replace "\n" with "<br/>" tags
58+
text = renderHtmlWithLineBreaks(text);
5859
switch (markType) {
5960
case SUPERSCRIPT:
6061
return "<sup>" + text + "</sup>";
@@ -94,8 +95,7 @@ private String escapeInjectHtml(JSONObject nodeObj, String nodeType) {
9495
* rendered.
9596
* @param callback The `callback` parameter is an instance of the `NodeCallback` interface. It is
9697
* used to render the children of the current node. The `renderChildren` method of the `callback`
97-
* is called with the `children` JSON array of the current node as the argument. The
98-
* `renderChildren
98+
* is called with the `children` JSON array of the current node as the argument. The `renderChildren
9999
* @return The method `renderNode` returns a string representation of an HTML element based on the
100100
* given `nodeType` and `nodeObject`.
101101
*/
@@ -105,11 +105,12 @@ public String renderNode(String nodeType, JSONObject nodeObject, NodeCallback ca
105105
String strAttrs = strAttrs(nodeObject);
106106

107107
String children = callback.renderChildren(nodeObject.optJSONArray("children"));
108+
108109
switch (nodeType) {
109110
case "p":
110111
return "<p" + strAttrs + ">" + children + "</p>";
111112
case "a":
112-
return "<a" + strAttrs + "href=\"" + escapeInjectHtml(nodeObject, "href") + "\">" + children + "</a>";
113+
return "<a" + strAttrs + " href=\"" + escapeInjectHtml(nodeObject, "href") + "\">" + children + "</a>";
113114
case "img":
114115
String assetLink = getNodeStr(nodeObject, "asset-link");
115116
if (!assetLink.isEmpty()) {
@@ -164,6 +165,28 @@ public String renderNode(String nodeType, JSONObject nodeObject, NodeCallback ca
164165
}
165166

166167

168+
/**
169+
* Returns the string replacing </n> is with the <br/> tags
170+
*
171+
* @param content the content
172+
* @return string with br tags
173+
* @apiNote the support for the br tags are included
174+
* @since v1.3.0
175+
*/
176+
private String renderHtmlWithLineBreaks(String content) {
177+
// Replace "\n" with "<br/>" tags
178+
String htmlContent = content.replaceAll("\\n", "<br />");
179+
180+
// Now, you can render the HTML content
181+
// (You can use your rendering method here, e.g., send it to a WebView or display it in a GUI component)
182+
183+
// For demonstration purposes, let's just print it
184+
System.out.println(htmlContent);
185+
186+
return htmlContent;
187+
}
188+
189+
167190
/**
168191
* The function takes a JSONObject as input and returns a string containing the attributes and
169192
* their values, excluding certain keys.
@@ -178,12 +201,18 @@ String strAttrs(JSONObject nodeObject) {
178201
JSONObject attrsObject = nodeObject.optJSONObject("attrs");
179202
if (attrsObject != null && !attrsObject.isEmpty()) {
180203
for (String key : attrsObject.keySet()) {
181-
Object objValue = attrsObject.opt(key);
204+
Object objValue = attrsObject.opt(key);
182205
String value = objValue.toString();
183-
String[] ignoreKeys = {"href", "asset-link", "src", "url"};
184-
ArrayList<String> ignoreKeysList = new ArrayList<>(Arrays.asList(ignoreKeys));
185-
if (!ignoreKeysList.contains(key)) {
186-
result.append(" ").append(key).append("=\"").append(value).append("\"");
206+
// If style is available, do styling calculations
207+
if (Objects.equals(key, "style")) {
208+
String resultStyle = stringifyStyles(attrsObject.optJSONObject("style"));
209+
result.append(" ").append(key).append("=\"").append(resultStyle).append("\"");
210+
} else {
211+
String[] ignoreKeys = {"href", "asset-link", "src", "url"};
212+
ArrayList<String> ignoreKeysList = new ArrayList<>(Arrays.asList(ignoreKeys));
213+
if (!ignoreKeysList.contains(key)) {
214+
result.append(" ").append(key).append("=\"").append(value).append("\"");
215+
}
187216
}
188217
}
189218
}
@@ -192,6 +221,30 @@ String strAttrs(JSONObject nodeObject) {
192221
}
193222

194223

224+
private String stringifyStyles(JSONObject style) {
225+
Map<String, String> styleMap = new HashMap<>();
226+
227+
// Convert JSONObject to a Map
228+
Iterator<String> keys = style.keys();
229+
while (keys.hasNext()) {
230+
String key = keys.next();
231+
String value = style.getString(key);
232+
styleMap.put(key, value);
233+
}
234+
235+
StringBuilder styleString = new StringBuilder();
236+
237+
for (Map.Entry<String, String> entry : styleMap.entrySet()) {
238+
String property = entry.getKey();
239+
String value = entry.getValue();
240+
241+
styleString.append(property).append(": ").append(value).append("; ");
242+
}
243+
244+
return styleString.toString();
245+
}
246+
247+
195248
/**
196249
* The function retrieves the value of a specified key from a JSONObject, and if it is empty or
197250
* null, it retrieves the value of the "url" key instead.

src/test/java/com/contentstack/utils/RTEResult.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ public class RTEResult {
1010
public static String kH4Html = "<h4><strong><em><u><sub>MaNullam feugiat turpis quis elit interdum, vitae laoreet quam viverra</sub></u></em></strong></h4>";
1111
public static String kH5Html = "<h5>Mauris venenatis dui id massa sollicitudin, non bibendum nunc dictum.</h5>";
1212
public static String kH6Html = "<h6>Nunc porta diam vitae purus semper, ut consequat lorem vehicula.</h6>";
13-
public static String kOrderListHtml = "<ol><li>Morbi in quam molestie, fermentum diam vitae, bibendum ipsum.</li><li>Pellentesque mattis lacus in quam aliquam congue</li><li>Integer feugiat leo dignissim, lobortis enim vitae, mollis lectus.</li><li>Sed in ante lacinia, molestie metus eu, fringilla sapien.</li></ol>";
13+
public static String kOrderListHtml = "<ol><li redactor-attributes=\"{}\" style=\"text-align: justify; \">Morbi in quam molestie, fermentum diam vitae, bibendum ipsum.</li><li redactor-attributes=\"{}\" style=\"text-align: justify; \">Pellentesque mattis lacus in quam aliquam congue</li><li redactor-attributes=\"{}\" style=\"text-align: justify; \">Integer feugiat leo dignissim, lobortis enim vitae, mollis lectus.</li><li redactor-attributes=\"{}\" style=\"text-align: justify; \">Sed in ante lacinia, molestie metus eu, fringilla sapien.</li></ol>";
1414
public static String kIUnorderListHtml = "<ul><li>Sed quis metus sed mi hendrerit mollis vel et odio.</li><li>Integer vitae sem dignissim, elementum libero vel, fringilla massa.</li><li>Integer imperdiet arcu sit amet tortor faucibus aliquet.</li><li>Aenean scelerisque velit vitae dui vehicula, at congue massa sagittis.</li></ul>";
15-
public static String kImgHtml = "<img src=\"https://images.***REMOVED***/v3/assets/UID_13/Donald.jog.png\" />";
15+
public static String kImgHtml = "<img redactor-attributes=\"{\"asset_uid\":\"47f1aa5ae422cd1\"}\" width=\"33.69418132611637\" height=\"auto\" src=\"https://images.***REMOVED***/v3/assets/UID_13/Donald.jog.png\" />";
1616
public static String kTableHtml = "<table><thead><tr><th><p>Header 1</p></th><th><p>Header 2</p></th></tr></thead><tbody><tr><td><p>Body row 1 data 1</p></td><td><p>Body row 1 data 2</p></td></tr><tr><td><p>Body row 2 data 1</p></td><td><p>Body row 2 data 2</p></td></tr></tbody></table>";
1717
public static String kBlockquoteHtml = "<blockquote>Praesent eu ex sed nibh venenatis pretium.</blockquote>";
1818
public static String kCodeHtml = "<code>Code template.</code>";

src/test/resources/asset_displayable.json

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
[{
1+
[
2+
{
23
"title": "New entry with figure tag",
34
"rich_text_editor": [
4-
"<p> Block embedded entry </p><p><figure type=\"entry\" class=\"embedded-entry block-entry\" data-sys-entry-uid=\"55f6d8cbd7e03a1f\" data-sys-content-type-uid=\"article\" style=\"display:block;\" sys-style-type=\"block\"><span>{{title}}</span></figure></p>"
5+
"<p> Block embedded entry </p><p><figure type=\"entry\" class=\"embedded-entry block-entry\" data-sys-entry-uid=\"55f6d8cbd7e03a1f\" data-sys-content-type-uid=\"article\" style=\"display:block;\" sys-style-type=\"block\"><span>{{title}}</span></figure></p>"
56
],
67
"tags": [],
78
"locale": "en-us",
@@ -13,20 +14,25 @@
1314
"ACL": {},
1415
"_version": 1,
1516
"_in_progress": false,
16-
"_embedded_entries": [{
17+
"_embedded_entries": [
18+
{
1719
"title": "Why are Indian rating agency heads being sent on 'leave'?",
1820
"multi_line": "If you have watched the film The Big Short, you might remember a scene in which Mark Baum, the fund manager, asks the lady at the ratings agency, “Why did you rate this?” And she says, otherwise they will go to another agency two blocks down the road.\n\nDid rating agencies in India fall for the same logic? Only time will tell.\n\nFor now, the heads of two Indian rating agencies, CARE Ltd and ICRA Ltd, have been sent on leave. On Wednesday evening, CARE Ratings Ltd announced to the stock exchanges that it has sent its managing director and CEO, Rajesh Mokashi on leave with immediate effect. It comes on the back of an anonymous complaint that was sent to the markets regulator, which was then forwarded to CARE, and the ratings firm is now examining the complaint.\n\nJust a fortnight ago, the same pattern followed when concerns were raised in an anonymous representation to the securities and exchange board of India (Sebi) regarding another agency, ICRA Ltd. The markets regulator had promptly sent it forward to ICRA which then sent its managing director, Naresh Takkar, on immediate leave, until further notice.\n\nThese are two incidents just this month, and more heads are expected to roll. Ratings agencies have come under the scanner since the debacle of shadow banking firm IL&FS Ltd. Three agencies that rated the company include India Ratings & Research, ICRA and CARE. The markets regulator in December last year had said it will investigate the matter.\n\nIn fact, one look at ICRA’s website shows that on August 6, 2018, the firm had given an A+ rating to IL&FS Financial Services Ltd’s (IFIN’s) commercial paper, worth Rs 4,000 crore. Interestingly, a closer look at the document showcases problems galore.",
19-
"header_ref": [{
21+
"header_ref": [
22+
{
2023
"uid": "dd96bf9df1e109cd",
2124
"_content_type_uid": "header"
22-
}],
25+
}
26+
],
2327
"footer_ref": [
24-
"a8d1c04adeca1cd1"
28+
"a8d1c04adeca1cd1"
2529
],
26-
"reference": [{
30+
"reference": [
31+
{
2732
"uid": "35f57d9211b3d430",
2833
"_content_type_uid": "author"
29-
}],
34+
}
35+
],
3036
"tags": [],
3137
"locale": "en-us",
3238
"uid": "55f6d8cbd7e03a1f",
@@ -41,5 +47,7 @@
4147
"number": null,
4248
"url": "/why-are-indian-rating-agency-heads-being-sent-on-leave-",
4349
"_in_progress": false
44-
}]
45-
}]
50+
}
51+
]
52+
}
53+
]

0 commit comments

Comments
 (0)