-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Santhosh Gandhe <[email protected]>
- Loading branch information
Showing
5 changed files
with
342 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
.../org/opensearch/dataprepper/plugins/source/confluence/utils/HtmlToTextConversionUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package org.opensearch.dataprepper.plugins.source.confluence.utils; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import org.jsoup.Jsoup; | ||
import org.jsoup.nodes.Document; | ||
|
||
import java.util.Optional; | ||
|
||
public class HtmlToTextConversionUtil { | ||
|
||
public static JsonNode convertHtmlToText(ObjectNode jsonObject, String path) { | ||
Optional<JsonNode> valueAtGivenPath = getValueAtGivenPath(jsonObject, path); | ||
if (valueAtGivenPath.isPresent()) { | ||
String html = valueAtGivenPath.get().textValue(); | ||
String txtBody = convertHtmlToText(html); | ||
setValueAtGivenPath(jsonObject, path, txtBody); | ||
} | ||
return jsonObject; | ||
} | ||
|
||
public static void setValueAtGivenPath(ObjectNode jsonObject, String path, String value) { | ||
String[] keys = path.split("/"); | ||
JsonNode current = jsonObject; | ||
for (int i = 0; i < keys.length - 1; i++) { | ||
current = current.get(keys[i]); | ||
} | ||
((ObjectNode) current).put(keys[keys.length - 1], value); | ||
} | ||
|
||
public static Optional<JsonNode> getValueAtGivenPath(ObjectNode jsonObject, String path) { | ||
try { | ||
String[] keys = path.split("/"); | ||
ObjectNode current = jsonObject; | ||
for (int i = 0; i < keys.length - 1; i++) { | ||
current = (ObjectNode) current.get(keys[i]); | ||
} | ||
return Optional.of(current.get(keys[keys.length - 1])); | ||
} catch (Exception e) { | ||
return Optional.empty(); | ||
} | ||
} | ||
|
||
public static String convertHtmlToText(String html) { | ||
if (html == null || html.isEmpty()) { | ||
return ""; | ||
} | ||
Document document = Jsoup.parse(html); | ||
// Remove scripts and style elements | ||
document.select("script, style").remove(); | ||
return document.text(); | ||
} | ||
} |
Oops, something went wrong.