Commit 3a1f0c9f authored by SKRIS40's avatar SKRIS40

Working on multi level div section

parent 2b427b23
# Ftl to JSON
#have to refactor the code,
The functionality is working as expected.
but code refactoring needed.
It's a POC to achieve FTL component to JSON
\ No newline at end of file
......@@ -74,7 +74,14 @@
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- <dependency>-->
<!-- below dependency is used for convertion of HTML into JSON-->
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.14.3</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.jflex</groupId>-->
<!-- <artifactId>jflex</artifactId>-->
<!-- <version>1.9.8</version>-->
......
package com.practice.migration.nov_21_23.custom;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@Getter
@Setter
@Data
@ToString
public class FTLConverter {
private String tagName;
private String id;
private String className;
private List<FTLConverter> children;
private Set<String> attributes;
public static void main(String[] args) {
String htmlString = "[#macro smartBundlingOneLine utilityNeed]\n" +
" <div class=\"smart-bundling-one-line\" id=\"smartBundlingOneLine-${utilityNeed.mainSku}\">\n" +
" <div class=\"smart-bundling-cart-icon\">\n" +
" <img src=\"[@wsgc.docurl url='images/checkout/svg/cartDesktop.svg'/]\"/>\n" +
" </div>\n" +
" [#if utilityNeed.data.copy??]\n" +
" <div class=\"smart-bundling-copy\">${utilityNeed.data.copy}</div>\n" +
" [/#if]\n" +
" [#if smartBundlingCartConfiguration.isDesktopDismissProductRecommendationEnabledCartPage()]\n" +
" <div class=\"smart-bundling-close\">\n" +
" <a><img src=\"[@wsgc.docurl url='images/svg/close.svg'/]\" class=\"smartBundlingClose\"/></a>\n" +
" </div>\n" +
" [/#if]\n" +
" </div>\n" +
"[/#macro]";
System.out.println("Final O/P"+ parseHTML(htmlString));
}
public FTLConverter(String tagName, String id, String className) {
this.tagName = tagName;
this.id = id;
this.className = className;
this.children = new ArrayList<>();
this.attributes = new HashSet<>();
}
public void addChild(FTLConverter child) {
this.children.add(child);
}
public static FTLConverter parseHTML(String htmlString) {
Pattern tagPattern = Pattern.compile("<(\\w+)(.*?)>(.*?)</\\1>", Pattern.DOTALL);
Matcher matcher = tagPattern.matcher(htmlString);
if (matcher.find()) {
String tagName = matcher.group(1);
String attributes = matcher.group(2);
String innerContent = matcher.group(3);
String id = extractAttribute(attributes, "id");
String className = extractAttribute(attributes, "class");
FTLConverter element = new FTLConverter(tagName, id, className);
// Recursively parse children
FTLConverter child = parseHTML(innerContent);
if (child != null) {
element.addChild(child);
}
return element;
}
return null;
}
private static String extractAttribute(String attributes, String attributeName) {
Pattern attributePattern = Pattern.compile(attributeName + "=\"(.*?)\"");
Matcher matcher = attributePattern.matcher(attributes);
if (matcher.find()) {
return matcher.group(1);
}
return null;
}
// Getters, setters, and other methods omitted for brevity
}
package com.practice.migration.nov_21_23.jsonLib;
import org.json.JSONArray;
import org.json.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class HTMLToJsonConverter {
public static void main(String[] args) {
String htmlString = "<div class=\"smart-bundling-cart-icon\">\n" +
" <img src=\"[@wsgc.docurl url='images/checkout/svg/cartDesktop1.svg'/]\"/>\n" +
" <img src=\"[@wsgc.docurl url='images/checkout/svg/cartDesktop3.svg'/]\"/>\n" +
" <img src=\"[@wsgc.docurl url='images/checkout/svg/cartDesktop2.svg'/]\"/>\n" +
" <div class='abc'>Test</div>\n" +
" </div>";
Document doc = Jsoup.parse(htmlString);
Element rootElement = doc.selectFirst("body > *");
// System.out.println("doc --> "+ doc +"====> "+ rootElement);
JSONObject json = new JSONObject();
traverseAndBuildJson(rootElement, json);
System.out.println(json);
}
private static void traverseAndBuildJson(Element element, JSONObject json) {
json.put("tagName", element.tagName());
if (!element.attributes().isEmpty()) {
JSONObject attributes = new JSONObject();
element
.attributes()
.forEach(attribute ->
attributes.put(attribute.getKey(), attribute.getValue())
);
json.put("attributes", attributes);
}
Elements children = element.children();
if (!children.isEmpty()) {
JSONArray childrenArray = new JSONArray();
for (Element child : children) {
JSONObject childJson = new JSONObject();
// Recursive nature --> building the children objects.
traverseAndBuildJson(child, childJson);
childrenArray.put(childJson);
}
json.put("children", childrenArray);
}
}
}
......@@ -13,7 +13,7 @@ import org.apache.commons.lang3.StringUtils;
import java.util.ArrayList;
import java.util.List;
public class HomeServiceImpl {
public class Assign_IfElse_Block_HomeServiceImpl {
private static final String FTL_String = "[#assign name_value='vinil'] \n" +
"[#if age > 19]\n" +
" Welcome, ${name} to nisum family\n" +
......
......@@ -3,28 +3,35 @@ package com.practice.migration.nov_13_23.customParser.service;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.practice.migration.FtlVueMigrationApplication;
import com.practice.migration.nov_13_23.customParser.model.request_and_response_body.FTLRequestBody;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.junit.jupiter.api.Assertions.*;
@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = FtlVueMigrationApplication.class)
class HomeServiceTest {
private static final String FTL_String = "[#assign name=${obj.name}] \n" +
"[#if age > ${age}]\n" +
" Welcome, ${name}!\n" +
"[#else]\n" +
" You are too young, ${name}!\n" +
"[/#if]";
class Assign_MultiDivTags_HomeServiceTest {
private static final String FTL_String =
"[#macro smartBundlingOneLine utilityNeed]\n" +
" <div class=\"smart-bundling-one-line\" id=\"smartBundlingOneLine-${utilityNeed.mainSku}\">\n" +
" <div class=\"smart-bundling-cart-icon\">\n" +
" <img src=\"[@wsgc.docurl url='images/checkout/svg/cartDesktop.svg'/]\"/>\n" +
" </div>\n" +
" [#if utilityNeed.data.copy??]\n" +
" <div class=\"smart-bundling-copy\">${utilityNeed.data.copy}</div>\n" +
" [/#if]\n" +
" [#if smartBundlingCartConfiguration.isDesktopDismissProductRecommendationEnabledCartPage()]\n" +
" <div class=\"smart-bundling-close\">\n" +
" <a><img src=\"[@wsgc.docurl url='images/svg/close.svg'/]\" class=\"smartBundlingClose\"/></a>\n" +
" </div>\n" +
" [/#if]\n" +
" </div>\n" +
"[/#macro]\n";
// @Test
// void contextLoads() {
public static void main(String[] ar) throws JsonProcessingException {
HomeService homeService = new HomeService();
// HomeService homeService = new HomeService();
// homeService.convertFTLtoJson(getStringJson());
homeService.convertFTLtoJson(getStringJson());
}
private static FTLRequestBody getStringJson() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment