Skip to content

Commit 1a1c48c

Browse files
shaileshmishrashaileshmishra
authored andcommitted
files improved
1 parent 740b261 commit 1a1c48c

File tree

4 files changed

+13
-59
lines changed

4 files changed

+13
-59
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ local.properties
8181
.idea/**/usage.statistics.xml
8282
.idea/**/dictionaries
8383
.idea/**/shelf
84+
.idea/
8485

8586
# Generated files
8687
.idea/**/contentModel.xml

.idea/codeStyles/Project.xml

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

.idea/codeStyles/codeStyleConfig.xml

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

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

Lines changed: 12 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,20 @@
22

33
import com.contentstack.utils.callbacks.ContentCallback;
44
import com.contentstack.utils.callbacks.MetadataCallback;
5+
import com.contentstack.utils.callbacks.OptionsCallback;
56
import com.contentstack.utils.helper.Metadata;
67
import com.contentstack.utils.render.DefaultOptionsCallback;
7-
import com.contentstack.utils.callbacks.OptionsCallback;
88
import org.json.JSONArray;
99
import org.json.JSONObject;
1010
import org.jsoup.Jsoup;
1111
import org.jsoup.nodes.Document;
1212
import org.jsoup.select.Elements;
1313

1414
import java.util.*;
15-
import java.util.logging.Logger;
1615
import java.util.stream.StreamSupport;
1716

1817
public class Utils {
1918

20-
// Interface Metadata Callback
21-
private interface MetadataCallback { void embeddedObject(Metadata metadata); }
22-
23-
private static final Logger logger = Logger.getLogger(Utils.class.getName());
24-
2519
/**
2620
* @param entryObj: Objects that contains RTE with embedded objects
2721
* @param keyPath keyPath
@@ -68,10 +62,10 @@ private static void findContent(JSONObject entryObj, String path, ContentCallbac
6862
}
6963

7064
/**
71-
* getContent accepts arrayString
72-
* @param arrayString
73-
* @param entryObj
74-
* @param contentCallback
65+
*
66+
* @param arrayString list of keys available
67+
* @param entryObj entry object
68+
* @param contentCallback content callback
7569
*/
7670
private static void getContent(String[] arrayString, JSONObject entryObj, ContentCallback contentCallback) {
7771
if (arrayString!=null && arrayString.length!=0){
@@ -108,20 +102,17 @@ public void render(JSONArray jsonArray, String[] keyPath, OptionsCallback render
108102
jsonArray.forEach(jsonObj-> render((JSONObject) jsonObj, keyPath, renderObject));
109103
}
110104

111-
112105
/**
113106
* Accepts to render content on the basis of below content
114-
* @param rteStringify String of the rte available for the embedding
107+
* @param rteStringify String of the rte available for the embedding
115108
* @param embedObject JSONObject to get the _embedded_object (_embedded_entries/_embedded_assets)
116-
* @param options Options take takes input as (StyleType type, JSONObject embeddedObject)
109+
* @param optionsCallback Options take takes input as (StyleType type, JSONObject embeddedObject)
117110
* @return String of rte with replaced tag
118111
*/
119-
public static String renderContent(String rteStringify, JSONObject embedObject, Options options) {
120-
112+
public static String renderContent(String rteStringify, JSONObject embedObject, OptionsCallback optionsCallback) {
121113
final String[] sReplaceRTE = {rteStringify};
122114
Document html = Jsoup.parse(rteStringify);
123115
getEmbeddedObjects(html, metadata -> {
124-
125116
Optional<JSONObject> filteredContent = Optional.empty();
126117
boolean available = embedObject.has("_embedded_items");
127118
if (available) {
@@ -134,7 +125,6 @@ public static String renderContent(String rteStringify, JSONObject embedObject,
134125
sReplaceRTE[0] = html.body().html().replace(metadata.getOuterHTML(), stringOption);
135126
}
136127
});
137-
138128
return sReplaceRTE[0];
139129
}
140130

@@ -159,24 +149,7 @@ public static JSONArray renderContents(JSONArray rteArray, JSONObject entryObjec
159149

160150
/**
161151
* Matches the uid and _content_type_uid from the
162-
*
163-
* @param jsonArray JSONArray: array of the _embedded_entries
164-
* @param metadata EmbeddedObject: contains the model class information
165-
* @return Optional<JSONObject>
166-
*/
167-
private static Optional<JSONObject> findEmbeddedEntry(JSONArray jsonArray, Metadata metadata) {
168-
Optional<JSONObject> filteredContent = StreamSupport.stream(jsonArray.spliterator(), false)
169-
.map(val -> (JSONObject) val)
170-
.filter(val -> val.optString("uid").equalsIgnoreCase(metadata.getItemUid()))
171-
.filter(val -> val.optString("_content_type_uid").equalsIgnoreCase(metadata.getContentTypeUid()))
172-
.findFirst();
173-
return filteredContent;
174-
}
175-
176-
/**
177-
* Matches the uid and _content_type_uid from the
178-
*
179-
* @param jsonArray JSONArray: array of the _embedded_assets
152+
* @param jsonObject JSONObject: jsonObject of the _embedded_assets
180153
* @param metadata EmbeddedObject: contains the model class information
181154
* @return Optional<JSONObject>
182155
*/
@@ -195,23 +168,18 @@ private static Optional<JSONObject> findEmbeddedItems(JSONObject jsonObject, Met
195168
}
196169

197170
private static String getStringOption(OptionsCallback optionsCallback, Metadata metadata, JSONObject contentToPass) {
198-
// TODO: Sending HashMap as HTML Attributes
199-
String stringOption = options.renderOptions(
200-
contentToPass, metadata);
171+
String stringOption = optionsCallback.renderOptions(contentToPass, metadata);
201172
if (stringOption == null) {
202-
DefaultOptions defaultOptions = new DefaultOptions();
203-
stringOption = defaultOptions.renderOptions(
204-
contentToPass, metadata);
173+
DefaultOptionsCallback defaultOptions = new DefaultOptionsCallback();
174+
stringOption = defaultOptions.renderOptions(contentToPass, metadata);
205175
}
206176
return stringOption;
207177
}
208178

209179

210180
private static void getEmbeddedObjects(Document html, MetadataCallback metadataCallback) {
211-
212181
Elements embeddedEntries = html.body().getElementsByClass("embedded-entry");
213182
Elements embeddedAssets = html.body().getElementsByClass("embedded-asset");
214-
215183
embeddedEntries.forEach((entry) -> {
216184
String text = entry.text();
217185
String type = entry.attr("type");

0 commit comments

Comments
 (0)