Skip to content

Commit 740b261

Browse files
shaileshmishrashaileshmishra
authored andcommitted
testcases added
1 parent b4c0dba commit 740b261

File tree

5 files changed

+48
-4
lines changed

5 files changed

+48
-4
lines changed

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

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,45 @@ public static void render(JSONObject entryObj, String[] keyPath, OptionsCallbac
5656
}
5757
}
5858

59-
private JSONObject findEntryByPath(JSONObject entryObj, String path) {
59+
/**
60+
* Find dot separated keys
61+
* @param entryObj Json Object
62+
* @param path keyPath
63+
* @param contentCallback content callback
64+
*/
65+
private static void findContent(JSONObject entryObj, String path, ContentCallback contentCallback ) {
66+
String [] arrayString = path.split("\\.");
67+
getContent(arrayString, entryObj, contentCallback);
68+
}
6069

61-
return null;
70+
/**
71+
* getContent accepts arrayString
72+
* @param arrayString
73+
* @param entryObj
74+
* @param contentCallback
75+
*/
76+
private static void getContent(String[] arrayString, JSONObject entryObj, ContentCallback contentCallback) {
77+
if (arrayString!=null && arrayString.length!=0){
78+
String key = arrayString[0];
79+
if (arrayString.length == 1) {
80+
Object varContent = entryObj.opt(key);
81+
if (varContent instanceof String || varContent instanceof JSONArray) {
82+
entryObj.put(key, contentCallback.contentObject(varContent));
83+
}
84+
} else {
85+
List<String> list = new ArrayList<>(Arrays.asList(arrayString));
86+
list.remove(key);
87+
String[] newArrayString = list.toArray(new String[0]);
88+
if (entryObj.opt(key) instanceof JSONObject) {
89+
getContent(newArrayString, entryObj.optJSONObject(key), contentCallback);
90+
} else if (entryObj.opt(key) instanceof JSONArray) {
91+
JSONArray jsonArray = entryObj.optJSONArray(key);
92+
for (int idx = 0; idx < jsonArray.length(); idx++) {
93+
getContent(newArrayString, jsonArray.optJSONObject(idx), contentCallback);
94+
}
95+
}
96+
}
97+
}
6298
}
6399

64100

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
package com.contentstack.utils.callbacks;
22

3+
/**
4+
* ContentCallback
5+
*/
36
public interface ContentCallback { Object contentObject(Object content); }

src/main/java/com/contentstack/utils/callbacks/MetadataCallback.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@
22

33
import com.contentstack.utils.helper.Metadata;
44

5+
/**
6+
* MetadataCallback
7+
*/
58
public interface MetadataCallback { void embeddedObject(Metadata metadata); }

src/main/java/com/contentstack/utils/helper/Metadata.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import org.jsoup.nodes.Attributes;
44

55
/**
6-
* POJO for the Embedded Objects, that helps to carry the objects
6+
* POJO(Getters and Setters) for the Embedded Objects, that helps to carry the objects
77
*/
88
public class Metadata {
99

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import com.contentstack.utils.helper.Metadata;
44
import org.json.JSONObject;
55

6-
6+
/**
7+
* DefaultOptionsCallback
8+
*/
79
public class DefaultOptionsCallback implements OptionsCallback {
810

911
/**

0 commit comments

Comments
 (0)