Friday, March 4, 2016

Xml value - absolute xpath mapper. Maps multiple paths if present for each unique value as key

 package com.mridul.mapper;  
 import org.w3c.dom.Document;  
 import org.w3c.dom.Element;  
 import org.w3c.dom.NodeList;  
 import org.w3c.dom.Node;  
 import javax.xml.parsers.DocumentBuilder;  
 import javax.xml.parsers.DocumentBuilderFactory;  
 import java.io.ByteArrayInputStream;  
 import java.io.File;  
 import java.io.IOException;  
 import java.util.*;  
 /**  
  * Created by Mridul on 2/25/2016.  
  */  
 public class Mapper {  
   private List<StringBuilder> paths = new ArrayList<>();  
   private Node rootNode;  
   private static String FORWARD_SLASH = "/";  
   private static int ROOT_COUNTER = 0;  
   private boolean textFlag = false;  
   private static Map<String, List<String>> valuePathMap=new WeakHashMap<>();  
   public boolean isTextFlag() {  
     return textFlag;  
   }  
   public void setTextFlag(boolean textFlag) {  
     this.textFlag = textFlag;  
   }  
   public Node getRootNode() {  
     return rootNode;  
   }  
   public void setRootNode(Node rootNode) {  
     this.rootNode = rootNode;  
   }  
   public static void main(String... args) {  
     try {  
       //The order of these sequential steps matter  
       Mapper mapper = new Mapper();  
       StringBuilder xmlAsString = mapper.getFileAsStringBuilder("xml/catalog.xml");  
       //StringBuilder xmlAsString = mapper.getFileAsStringBuilder("xml/auction.xml");  
       //StringBuilder xmlAsString = mapper.getFileAsStringBuilder("xml/uwm.xml");  
       mapper.retrieveAndSetRootNode(xmlAsString);  
       mapper.recursivelyPrintAllNodes(FORWARD_SLASH);  
       mapper.printValuesInMap();  
     } catch (Exception e) {  
       e.printStackTrace();  
     }  
   }  
   private StringBuilder getFileAsStringBuilder(String fileName) {  
     StringBuilder result = new StringBuilder();  
     ClassLoader classLoader = getClass().getClassLoader();  
     File file = new File(classLoader.getResource(fileName).getFile());  
     try (Scanner scanner = new Scanner(file)) {  
       while (scanner.hasNextLine()) {  
         String line = scanner.nextLine();  
         result.append(line).append("\n");  
       }  
       scanner.close();  
     } catch (IOException e) {  
       e.printStackTrace();  
     }  
     //System.out.println(result.toString());  
     return result;  
   }  
   private void retrieveAndSetRootNode(StringBuilder xml) throws Exception {  
     DocumentBuilderFactory factory =  
         DocumentBuilderFactory.newInstance();  
     DocumentBuilder builder = factory.newDocumentBuilder();  
     ByteArrayInputStream input = new ByteArrayInputStream(xml.toString().getBytes("UTF-8"));  
     Document document = builder.parse(input);  
     Element rootNode = document.getDocumentElement();  
     setRootNode(rootNode);  
   }  
   private void recursivelyPrintAllNodes(String path) {  
     Node rootNode = getRootNode();  
     String rootNodeName = rootNode.getNodeName();  
     if (ROOT_COUNTER == 0) {  
       path = path + rootNode.getNodeName();  
     }  
     ++ROOT_COUNTER;  
     String parentPath = path;  
     NodeList childNodes;  
     if (rootNode.hasChildNodes()) {  
       childNodes = rootNode.getChildNodes();  
       for (int index = 0; index < childNodes.getLength(); index++) {  
         Node currentNode = childNodes.item(index);  
         short nodeType = currentNode.getNodeType();  
         if (nodeType == Node.TEXT_NODE) {  
           String value = currentNode.getNodeValue().trim();  
           if (value.equals("")) {  
             continue;  
           } else {  
 /*            System.out.println("The current path is : " + path);  
             System.out.println("The text node value is : " + value);*/  
             storeValuePathIntoMap(value, path);  
             this.setTextFlag(true);  
           }  
         }  
         if (isTextFlag()) {  
           setTextFlag(false);  
           break;  
         }  
         if (nodeType == Node.ELEMENT_NODE) {  
           String currentNodeName = currentNode.getNodeName();  
           if (!(rootNodeName.equals(currentNodeName))) { //add path only in case of mismatch  
             path = parentPath + FORWARD_SLASH + currentNodeName;  
           }  
           setRootNode(currentNode); //setting root node to current node  
           recursivelyPrintAllNodes(path);//recursive call to iterate  
         }  
       }  
     }  
   }  
   private void storeValuePathIntoMap(String key, String value){  
     if(valuePathMap.containsKey(key)){  
       List<String> values=valuePathMap.get(key);  
       values.add(value);  
     }else{  
       valuePathMap.put(key,new ArrayList<>(Arrays.asList(value)));  
     }  
   }  
   private void printValuesInMap(){  
     for(Map.Entry<String, List<String>> entry: valuePathMap.entrySet()){  
       System.out.println("Key: " + entry.getKey());  
       System.out.println("Value: " + entry.getValue()+"\n");  
     }  
   }  
 }  

No comments:

Post a Comment