Skip to content

Commit 6ab4a6b

Browse files
document formated
1 parent dc01235 commit 6ab4a6b

20 files changed

+388
-195
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
<maven-release-plugin.version>2.5.3</maven-release-plugin.version>
3131
<validation-version>2.0.1.Final</validation-version>
3232
<json-version>20230227</json-version>
33-
<spring-web-version>6.0.7</spring-web-version>
33+
<spring-web-version>6.0.11</spring-web-version>
3434
<org.apache.commons-text>1.10.0</org.apache.commons-text>
3535
</properties>
3636

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

Lines changed: 90 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,16 @@ private AutomateCommon() {
2929
}
3030

3131
/**
32-
* Find dot separated keys
32+
* The function "findContent" splits a given path string into an array of strings, and then calls the
33+
* "getContent" function with the array, a JSONObject, and a contentCallback as parameters.
3334
*
34-
* @param entryObj Json Object
35-
* @param path keyPath
36-
* @param contentCallback content callback
35+
* @param entryObj A JSONObject that contains the data you want to search through.
36+
* @param path The `path` parameter is a string that represents the path to a specific content in a
37+
* JSON object. It is used to navigate through the JSON object and find the desired content.
38+
* @param contentCallback The `contentCallback` parameter is an instance of the `ContentCallback`
39+
* interface. It is used to provide a callback mechanism for handling the content found during the
40+
* search process. The `ContentCallback` interface typically defines a method that will be called with
41+
* the content found at each step of the search.
3742
*/
3843
protected static void findContent(JSONObject entryObj, String path, ContentCallback contentCallback) {
3944
String[] arrayString = path.split("\\.");
@@ -42,9 +47,15 @@ protected static void findContent(JSONObject entryObj, String path, ContentCallb
4247

4348

4449
/**
45-
* @param arrayString list of keys available
46-
* @param entryObj entry object
47-
* @param contentCallback content callback
50+
* The function `getContent` recursively traverses a JSON object and modifies its content based on
51+
* a callback function.
52+
*
53+
* @param arrayString An array of strings representing the path to the content in the JSON object.
54+
* @param entryObj The `entryObj` parameter is a JSONObject that contains the data to be processed.
55+
* @param contentCallback The `contentCallback` parameter is an instance of the `ContentCallback`
56+
* interface. This interface defines a method called `contentObject()` which takes an object as
57+
* input and returns a modified version of that object. The purpose of this callback is to allow
58+
* the caller of the `getContent()` method to customize
4859
*/
4960
private static void getContent(String[] arrayString, JSONObject entryObj, ContentCallback contentCallback) {
5061
if (arrayString != null && arrayString.length != 0) {
@@ -71,6 +82,20 @@ private static void getContent(String[] arrayString, JSONObject entryObj, Conten
7182
}
7283

7384

85+
/**
86+
* The function returns a string representation of an option by rendering it with the provided
87+
* content and metadata, falling back to default options if necessary.
88+
*
89+
* @param option The "option" parameter is an object of type Option, which represents a specific
90+
* option that can be rendered. It likely has properties and methods that allow it to generate the
91+
* desired output based on the provided content and metadata.
92+
* @param metadata Metadata is an object that contains additional information about the option. It
93+
* may include details such as the option's label, description, and any other relevant information.
94+
* @param contentToPass The `contentToPass` parameter is a `JSONObject` that contains the data or
95+
* content that needs to be passed to the `renderOptions` method of the `Option` or `DefaultOption`
96+
* class. This data or content is used to generate the string representation of the option.
97+
* @return The method is returning a string value.
98+
*/
7499
protected static String getStringOption(Option option, Metadata metadata, JSONObject contentToPass) {
75100
String stringOption = option.renderOptions(contentToPass, metadata);
76101
if (stringOption == null) {
@@ -81,6 +106,17 @@ protected static String getStringOption(Option option, Metadata metadata, JSONOb
81106
}
82107

83108

109+
/**
110+
* The function retrieves embedded objects from an HTML document and passes them to a callback
111+
* function for further processing.
112+
*
113+
* @param html The "html" parameter is a Document object that represents an HTML document. It is
114+
* used to extract embedded objects from the HTML document.
115+
* @param metadataCallback The `metadataCallback` parameter is an instance of a class that
116+
* implements the `MetadataCallback` interface. This interface defines a method called
117+
* `embeddedObject` which is called for each embedded object found in the HTML document. The
118+
* `embeddedObject` method takes a `Metadata` object as a parameter,
119+
*/
84120
protected static void getEmbeddedObjects(Document html, MetadataCallback metadataCallback) {
85121
Elements embeddedEntries = html.body().getElementsByClass("embedded-entry");
86122
Elements embeddedAssets = html.body().getElementsByClass("embedded-asset");
@@ -107,6 +143,16 @@ protected static void getEmbeddedObjects(Document html, MetadataCallback metadat
107143
}
108144

109145

146+
/**
147+
* The function takes a JSONArray of content, iterates through each element, and calls another
148+
* function to render and enumerate the content, returning a JSONArray of the rendered content.
149+
*
150+
* @param contentArray A JSONArray containing the content to be enumerated.
151+
* @param renderObject The renderObject parameter is an instance of the Option class.
152+
* @param item The "item" parameter is a callback function that takes in a "Meta" object and
153+
* returns a value to be embedded in the rendered content.
154+
* @return The method is returning a JSONArray object.
155+
*/
110156
protected static Object enumerateContents(JSONArray contentArray, Option renderObject, MetaToEmbedCallback item) {
111157
JSONArray jsonArrayRTEContent = new JSONArray();
112158
for (Object RTE : contentArray) {
@@ -117,8 +163,19 @@ protected static Object enumerateContents(JSONArray contentArray, Option renderO
117163
return jsonArrayRTEContent;
118164
}
119165

166+
/**
167+
* The function checks if a JSON object has a specific type and children, and if so, it performs
168+
* some processing on the children.
169+
*
170+
* @param jsonObject A JSONObject that contains the content to be enumerated.
171+
* @param renderObject The `renderObject` parameter is of type `Option`. It is used as an argument
172+
* in the `doRawProcessing` method.
173+
* @param item The "item" parameter is a callback function that takes in a "Meta" object and returns
174+
* a string.
175+
* @return An empty string.
176+
*/
120177
protected static String enumerateContent(JSONObject jsonObject, Option renderObject, MetaToEmbedCallback item) {
121-
if (jsonObject.length() > 0 && jsonObject.has("type") && jsonObject.has("children")) {
178+
if (!jsonObject.isEmpty() && jsonObject.has("type") && jsonObject.has("children")) {
122179
if (jsonObject.opt("type").equals("doc")) {
123180
return doRawProcessing(jsonObject.optJSONArray("children"), renderObject, item);
124181
}
@@ -127,6 +184,17 @@ protected static String enumerateContent(JSONObject jsonObject, Option renderObj
127184
}
128185

129186

187+
/**
188+
* The function `doRawProcessing` processes a JSONArray of children, extracting keys from each
189+
* child and appending the results to a StringBuilder, then returns the final string.
190+
*
191+
* @param children A JSONArray containing the children items to process.
192+
* @param renderObject The parameter "renderObject" is of type "Option". It is used as an option to
193+
* specify how the keys should be rendered during processing.
194+
* @param embedItem The `embedItem` parameter is a callback function that takes a `Meta` object as
195+
* input and returns an embedded representation of that object.
196+
* @return The method is returning a string.
197+
*/
130198
private static String doRawProcessing(@NotNull JSONArray children, Option renderObject, MetaToEmbedCallback embedItem) {
131199
StringBuilder stringBuilder = new StringBuilder();
132200
children.forEach(item -> {
@@ -140,6 +208,20 @@ private static String doRawProcessing(@NotNull JSONArray children, Option render
140208
}
141209

142210

211+
/**
212+
* The function extracts keys from a JSON object and returns a string based on the type of the
213+
* node.
214+
*
215+
* @param jsonNode The `jsonNode` parameter is a `JSONObject` that represents a node in a JSON
216+
* structure. It is used to extract information from the node and perform certain operations based
217+
* on its properties.
218+
* @param renderObject The `renderObject` parameter is an object of type `Option`. It is used to
219+
* specify rendering options for the extracted keys.
220+
* @param embedItem The `embedItem` parameter is a callback function that takes a `Metadata` object
221+
* as input and returns an optional `JSONObject`. This callback is used to embed metadata into the
222+
* rendered content.
223+
* @return The method is returning a String.
224+
*/
143225
private static String extractKeys(@NotNull JSONObject jsonNode, Option renderObject, MetaToEmbedCallback embedItem) {
144226

145227
if (!jsonNode.has("type") && jsonNode.has("text")) {

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

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,35 @@
1212

1313
import static com.contentstack.utils.AutomateCommon.*;
1414

15-
/**
16-
* The type Gql.
17-
*/
15+
1816
public class GQL {
1917

2018

19+
// The `private GQL() throws IllegalAccessException` is a private constructor of the `GQL` class. It is
20+
// throwing an `IllegalAccessException` with a message "Invalid Access! Could not create instance of
21+
// GQL".
2122
private GQL() throws IllegalAccessException {
2223
throw new IllegalAccessException("Invalid Access! Could not create instance of GQL");
2324
}
2425

2526

27+
// The line `static JSONArray embeddedItems = null` declares a static variable named
28+
// `embeddedItems` of type `JSONArray` and initializes it with a value of `null`. This
29+
// variable is used to store the embedded items extracted from the GraphQL entry object
30+
// during the conversion process.
2631
private static JSONArray embeddedItems = null;
2732

33+
2834
/**
29-
* Json to html.
35+
* The function `jsonToHTML` converts a JSON object to HTML based on a given path and render
36+
* option.
3037
*
31-
* @param gqlEntry
32-
* the gql entry is entry @{@link JSONObject}
33-
* @param path
34-
* the path is array of @{@link String}
35-
* @param renderOption
36-
* the render option is instance of @{@link DefaultOption}
38+
* @param gqlEntry The `gqlEntry` parameter is a JSONObject that represents the GraphQL entry
39+
* object. It contains the data that needs to be converted to HTML.
40+
* @param path An array of strings representing the path to the desired content in the JSON object.
41+
* @param renderOption The `renderOption` parameter is of type `DefaultOption` and is used to
42+
* specify the rendering options for the HTML output. It is an enum that contains different options
43+
* for rendering the content.
3744
*/
3845
public static void jsonToHTML(@NotNull JSONObject gqlEntry, @NotNull String[] path, @NotNull DefaultOption renderOption) {
3946

@@ -49,16 +56,14 @@ public static void jsonToHTML(@NotNull JSONObject gqlEntry, @NotNull String[] pa
4956
if (contentDict.has("json")) {
5057
MetaToEmbedCallback converter = metadata -> {
5158
if (embeddedItems != null) {
52-
Optional<JSONObject> filteredContent = StreamSupport.stream(embeddedItems.spliterator(), false)
53-
.map(JSONObject.class::cast)
54-
.filter(itemDict -> {
55-
JSONObject nodeObject = itemDict.optJSONObject("node");
56-
if (nodeObject.has("uid")) {
57-
String uid = nodeObject.optString("uid");
58-
return uid.equals(metadata.getItemUid());
59-
}
60-
return false;
61-
}).findFirst();
59+
Optional<JSONObject> filteredContent = StreamSupport.stream(embeddedItems.spliterator(), false).map(JSONObject.class::cast).filter(itemDict -> {
60+
JSONObject nodeObject = itemDict.optJSONObject("node");
61+
if (nodeObject.has("uid")) {
62+
String uid = nodeObject.optString("uid");
63+
return uid.equals(metadata.getItemUid());
64+
}
65+
return false;
66+
}).findFirst();
6267
if (filteredContent.isPresent()) {
6368
return filteredContent;
6469
}

0 commit comments

Comments
 (0)