Advertisement
Guest User

Converter.java

a guest
Jul 11th, 2017
467
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.96 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.FileReader;
  3. import java.io.IOException;
  4. import java.util.ArrayList;
  5. import java.util.HashMap;
  6. import java.util.List;
  7. import java.util.Map;
  8.  
  9. import com.google.gson.GsonBuilder;
  10.  
  11. public class Converter {
  12.     public static void main(String[] args) throws IOException {
  13.         final Map<String, Object> holder = new HashMap<>();
  14.         holder.put("hierarchy", fromCsvFile("mapping.csv"));
  15.  
  16.         System.out.println(new GsonBuilder().setPrettyPrinting().create().toJson(holder));
  17.     }
  18.  
  19.     public static class Node {
  20.         private String label;
  21.         private List<Node> children;
  22.  
  23.         public String getLabel() {
  24.             return label;
  25.         }
  26.  
  27.         public void setLabel(String label) {
  28.             this.label = label;
  29.         }
  30.  
  31.         public List<Node> getChildren() {
  32.             return children;
  33.         }
  34.  
  35.         public void setChildren(List<Node> children) {
  36.             this.children = children;
  37.         }
  38.  
  39.         public void addChild(Node child) {
  40.             if (children == null) {
  41.                 children = new ArrayList<>();
  42.             }
  43.             children.add(child);
  44.         }
  45.     }
  46.  
  47.     public static List<Node> fromCsvFile(String filename) throws IOException {
  48.         Node root = new Node();
  49.         Map<String, Node> existingNodes = new HashMap<>();
  50.  
  51.         try (BufferedReader input = new BufferedReader(new FileReader(filename))) {
  52.             String parentName, childName;
  53.             String[] entry;
  54.  
  55.             String line = input.readLine();
  56.             while (line != null) {
  57.                 entry = line.split(",");
  58.                 parentName = entry[0].trim();
  59.                 childName = entry[1].trim();
  60.  
  61.                 // find or create
  62.                 Node node = existingNodes.get(parentName);
  63.                 if (node == null) {
  64.                     // new top level node
  65.                     node = new Node();
  66.                     node.setLabel(parentName);
  67.                     root.addChild(node);
  68.                     existingNodes.put(parentName, node);
  69.                 }
  70.  
  71.                 // add child
  72.                 Node child = new Node();
  73.                 child.setLabel(childName);
  74.                 node.addChild(child);
  75.                 existingNodes.put(childName, child);
  76.  
  77.                 // next line
  78.                 line = input.readLine();
  79.             }
  80.         }
  81.  
  82.         return root.getChildren();
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement