Skip to content

Commit 60106b8

Browse files
dependency upgraded
1 parent 38ea55f commit 60106b8

File tree

16 files changed

+637
-202
lines changed

16 files changed

+637
-202
lines changed
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
package com.contentstack.utils;
2+
3+
import com.contentstack.utils.helper.Metadata;
4+
import com.contentstack.utils.interfaces.ContentCallback;
5+
import com.contentstack.utils.interfaces.MetaToEmbedCallback;
6+
import com.contentstack.utils.interfaces.MetadataCallback;
7+
import com.contentstack.utils.interfaces.Option;
8+
import com.contentstack.utils.node.NodeToHTML;
9+
import com.contentstack.utils.render.DefaultOption;
10+
import org.jetbrains.annotations.NotNull;
11+
import org.json.JSONArray;
12+
import org.json.JSONObject;
13+
import org.jsoup.nodes.Attributes;
14+
import org.jsoup.nodes.Document;
15+
import org.jsoup.select.Elements;
16+
17+
import java.util.ArrayList;
18+
import java.util.Arrays;
19+
import java.util.List;
20+
import java.util.Optional;
21+
22+
public class AutomateCommon {
23+
24+
25+
/**
26+
* Find dot separated keys
27+
*
28+
* @param entryObj Json Object
29+
* @param path keyPath
30+
* @param contentCallback content callback
31+
*/
32+
protected static void findContent(JSONObject entryObj, String path, ContentCallback contentCallback) {
33+
String[] arrayString = path.split("\\.");
34+
getContent(arrayString, entryObj, contentCallback);
35+
}
36+
37+
38+
/**
39+
* @param arrayString list of keys available
40+
* @param entryObj entry object
41+
* @param contentCallback content callback
42+
*/
43+
private static void getContent(String[] arrayString, JSONObject entryObj, ContentCallback contentCallback) {
44+
if (arrayString != null && arrayString.length != 0) {
45+
String path = arrayString[0];
46+
if (arrayString.length == 1) {
47+
Object varContent = entryObj.opt(path);
48+
if (varContent instanceof String || varContent instanceof JSONArray || varContent instanceof JSONObject) {
49+
entryObj.put(path, contentCallback.contentObject(varContent));
50+
}
51+
} else {
52+
List<String> list = new ArrayList<>(Arrays.asList(arrayString));
53+
list.remove(path);
54+
String[] newArrayString = list.toArray(new String[0]);
55+
if (entryObj.opt(path) instanceof JSONObject) {
56+
getContent(newArrayString, entryObj.optJSONObject(path), contentCallback);
57+
} else if (entryObj.opt(path) instanceof JSONArray) {
58+
JSONArray jsonArray = entryObj.optJSONArray(path);
59+
for (int idx = 0; idx < jsonArray.length(); idx++) {
60+
getContent(newArrayString, jsonArray.optJSONObject(idx), contentCallback);
61+
}
62+
}
63+
}
64+
}
65+
}
66+
67+
68+
protected static String getStringOption(Option option, Metadata metadata, JSONObject contentToPass) {
69+
String stringOption = option.renderOptions(contentToPass, metadata);
70+
if (stringOption == null) {
71+
DefaultOption defaultOptions = new DefaultOption();
72+
stringOption = defaultOptions.renderOptions(contentToPass, metadata);
73+
}
74+
return stringOption;
75+
}
76+
77+
78+
protected static void getEmbeddedObjects(Document html, MetadataCallback metadataCallback) {
79+
Elements embeddedEntries = html.body().getElementsByClass("embedded-entry");
80+
Elements embeddedAssets = html.body().getElementsByClass("embedded-asset");
81+
embeddedEntries.forEach((entry) -> {
82+
String text = entry.text();
83+
String type = entry.attr("type");
84+
String uid = entry.attr("data-sys-entry-uid");
85+
String contentType = entry.attr("data-sys-content-type-uid");
86+
String style = entry.attr("sys-style-type");
87+
String outerHTML = entry.outerHtml();
88+
Metadata metadata = new Metadata(text, type, uid, contentType, style, outerHTML, entry.attributes());
89+
metadataCallback.embeddedObject(metadata);
90+
});
91+
92+
embeddedAssets.forEach((asset) -> {
93+
String text = asset.text();
94+
String type = asset.attr("type");
95+
String uid = asset.attr("data-sys-asset-uid");
96+
String style = asset.attr("sys-style-type");
97+
String outerHTML = asset.outerHtml();
98+
Metadata metadata = new Metadata(text, type, uid, "asset", style, outerHTML, asset.attributes());
99+
metadataCallback.embeddedObject(metadata);
100+
});
101+
}
102+
103+
104+
protected static Object enumerateContents(JSONArray contentArray, Option renderObject, MetaToEmbedCallback item) {
105+
JSONArray jsonArrayRTEContent = new JSONArray();
106+
for (Object RTE : contentArray) {
107+
JSONObject jsonObject = (JSONObject) RTE;
108+
String renderContent = enumerateContent(jsonObject, renderObject, item);
109+
jsonArrayRTEContent.put(renderContent);
110+
}
111+
return jsonArrayRTEContent;
112+
}
113+
114+
protected static String enumerateContent(JSONObject jsonObject, Option renderObject, MetaToEmbedCallback item) {
115+
if (jsonObject.length() > 0 && jsonObject.has("type") && jsonObject.has("children")) {
116+
if (jsonObject.opt("type").equals("doc")) {
117+
return _doRawProcessing(jsonObject.optJSONArray("children"), renderObject, item);
118+
}
119+
}
120+
return "";
121+
}
122+
123+
124+
private static String _doRawProcessing(@NotNull JSONArray children, Option renderObject, MetaToEmbedCallback embedItem) {
125+
StringBuilder stringBuilder = new StringBuilder();
126+
children.forEach(item -> {
127+
JSONObject child;
128+
if (item instanceof JSONObject) {
129+
child = (JSONObject) item;
130+
stringBuilder.append(_extractKeys(child, renderObject, embedItem));
131+
}
132+
});
133+
return stringBuilder.toString();
134+
}
135+
136+
137+
private static String _extractKeys(@NotNull JSONObject jsonNode, Option renderObject, MetaToEmbedCallback embedItem) {
138+
139+
if (!jsonNode.has("type") && jsonNode.has("text")) {
140+
return NodeToHTML.textNodeToHTML(jsonNode, renderObject);
141+
} else if (jsonNode.has("type")) {
142+
String nodeType = jsonNode.optString("type");
143+
if (nodeType.equalsIgnoreCase("reference")) {
144+
JSONObject attrObj = jsonNode.optJSONObject("attrs");
145+
String attrType = attrObj.optString("type");
146+
com.contentstack.utils.helper.Metadata metadata;
147+
148+
if (attrType.equalsIgnoreCase("asset")) {
149+
String text = attrObj.optString("text");
150+
String uid = attrObj.optString("asset-uid");
151+
String style = attrObj.optString("display-type");
152+
metadata = new com.contentstack.utils.helper.Metadata(text, attrType, uid,
153+
"asset", style, "", new Attributes());
154+
} else {
155+
String text = attrObj.optString("text");
156+
String uid = attrObj.optString("entry-uid");
157+
String contentType = attrObj.optString("content-type-uid");
158+
String style = attrObj.optString("display-type");
159+
metadata = new com.contentstack.utils.helper.Metadata(text, attrType, uid,
160+
contentType, style, "", new Attributes());
161+
}
162+
163+
Optional<JSONObject> filteredContent = embedItem.toEmbed(metadata);
164+
if (filteredContent.isPresent()) {
165+
JSONObject contentToPass = filteredContent.get();
166+
return getStringOption(renderObject, metadata, contentToPass);
167+
}
168+
169+
} else {
170+
return renderObject.renderNode(nodeType, jsonNode, nodeJsonArray -> _doRawProcessing(nodeJsonArray, renderObject, embedItem));
171+
}
172+
}
173+
return "";
174+
}
175+
176+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.contentstack.utils;
2+
3+
import com.contentstack.utils.interfaces.ContentCallback;
4+
import com.contentstack.utils.interfaces.MetaToEmbedCallback;
5+
import com.contentstack.utils.render.DefaultOption;
6+
import org.jetbrains.annotations.NotNull;
7+
import org.json.JSONArray;
8+
import org.json.JSONObject;
9+
10+
import java.util.Optional;
11+
import java.util.stream.StreamSupport;
12+
13+
import static com.contentstack.utils.AutomateCommon.*;
14+
15+
/**
16+
* The type Gql.
17+
*/
18+
public class GQL {
19+
20+
private static JSONArray embeddedItems = null;
21+
22+
/**
23+
* Json to html.
24+
*
25+
* @param gqlEntry the gql entry is entry @{@link JSONObject}
26+
* @param path the path is array of @{@link String}
27+
* @param renderOption the render option is instance of @{@link DefaultOption}
28+
*/
29+
public static void jsonToHTML(@NotNull JSONObject gqlEntry, @NotNull String[] path, @NotNull DefaultOption renderOption) {
30+
31+
ContentCallback callback = content -> {
32+
JSONObject contentDict = (JSONObject) content;
33+
if (contentDict.has("embedded_itemsConnection")) {
34+
JSONObject embeddedConnection = contentDict.optJSONObject("embedded_itemsConnection");
35+
if (embeddedConnection.has("edges")) {
36+
embeddedItems = embeddedConnection.optJSONArray("edges");
37+
}
38+
}
39+
40+
if (contentDict.has("json")) {
41+
MetaToEmbedCallback converter = metadata -> {
42+
if (embeddedItems != null) {
43+
Optional<JSONObject> filteredContent = StreamSupport.stream(embeddedItems.spliterator(), false)
44+
.map(val -> (JSONObject) val)
45+
.filter(itemDict -> {
46+
JSONObject nodeObject = itemDict.optJSONObject("node");
47+
if (nodeObject.has("uid")) {
48+
String uid = nodeObject.optString("uid");
49+
return uid.equals(metadata.getItemUid());
50+
}
51+
return false;
52+
}).findFirst();
53+
if (filteredContent.isPresent()) {
54+
return filteredContent;
55+
}
56+
}
57+
return Optional.empty();
58+
};
59+
Object contentJson = contentDict.opt("json");
60+
if (contentJson instanceof JSONArray) {
61+
JSONArray contentArray = (JSONArray) contentJson;
62+
return enumerateContents(contentArray, renderOption, converter);
63+
} else if (contentJson instanceof JSONObject) {
64+
JSONObject jsonObject = (JSONObject) contentJson;
65+
return enumerateContent(jsonObject, renderOption, converter);
66+
}
67+
}
68+
return null;
69+
};
70+
71+
if (path.length > 0) {
72+
for (String pathKey : path) {
73+
findContent(gqlEntry, pathKey, callback);
74+
}
75+
}
76+
}
77+
}

0 commit comments

Comments
 (0)