Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. public class TreeToHtmlConverter {
  2. public static String convert(TreeNode root) {
  3. if (root.isLeaf()) {
  4. return "leaf";
  5. }
  6.  
  7. String childHtml = root.getChildren().stream()
  8. .map(child -> convert(child))
  9. .collect(Collectors.joining());
  10.  
  11. return "<p>" + childHtml + "</p>";
  12. }
  13. }
  14.  
  15. public class TreeToHtmlConverter {
  16. public static TreeToHtmlResult convert(TreeNode node) {
  17. List<String> specialTags = new ArrayList<>();
  18. String html = convert(node, specialTags);
  19.  
  20. return new TreeToHtmlResult(html, specialTags);
  21. }
  22.  
  23. public static String convert(TreeNode node, List<String> specialTags) {
  24. if (node.isLeaf()) {
  25. return "leaf";
  26. }
  27.  
  28. if (node.isSpecial()) {
  29. specialTags.add(node.getName());
  30. }
  31.  
  32. String childHtml = node.getChildren().stream()
  33. .map(child -> convert(child, specialTags))
  34. .collect(Collectors.joining());
  35.  
  36. return "<p>" + childHtml + "</p>";
  37. }
  38.  
  39. private static class TreeToHtmlResult {
  40. private final String html;
  41. private final List<String> specialTags;
  42.  
  43. public TreeToHtmlResult(String html, List<String> specialTags) {
  44. this.html = html;
  45. this.specialTags = specialTags;
  46. }
  47.  
  48. public String getHtml() {
  49. return this.html;
  50. }
  51.  
  52. public List<String> getSpecialTags() {
  53. return this.specialTags;
  54. }
  55. }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement