Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class TreeToHtmlConverter {
- public static String convert(TreeNode root) {
- if (root.isLeaf()) {
- return "leaf";
- }
- String childHtml = root.getChildren().stream()
- .map(child -> convert(child))
- .collect(Collectors.joining());
- return "<p>" + childHtml + "</p>";
- }
- }
- public class TreeToHtmlConverter {
- public static TreeToHtmlResult convert(TreeNode node) {
- List<String> specialTags = new ArrayList<>();
- String html = convert(node, specialTags);
- return new TreeToHtmlResult(html, specialTags);
- }
- public static String convert(TreeNode node, List<String> specialTags) {
- if (node.isLeaf()) {
- return "leaf";
- }
- if (node.isSpecial()) {
- specialTags.add(node.getName());
- }
- String childHtml = node.getChildren().stream()
- .map(child -> convert(child, specialTags))
- .collect(Collectors.joining());
- return "<p>" + childHtml + "</p>";
- }
- private static class TreeToHtmlResult {
- private final String html;
- private final List<String> specialTags;
- public TreeToHtmlResult(String html, List<String> specialTags) {
- this.html = html;
- this.specialTags = specialTags;
- }
- public String getHtml() {
- return this.html;
- }
- public List<String> getSpecialTags() {
- return this.specialTags;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement