First prototype of CSV and XML parse methods
This commit is contained in:
parent
4f5e1d768e
commit
397250fa51
|
@ -1,16 +1,15 @@
|
|||
package org.gcube.vremanagement;
|
||||
|
||||
|
||||
public class App{
|
||||
|
||||
public static void main (String[] args)
|
||||
{
|
||||
//create here the CSV and XML managers object to parse and sync the XML with the CSV file
|
||||
CsvManager csvManager = new CsvManager();
|
||||
|
||||
|
||||
XmlManager xmlManager = new XmlManager();
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public static void main (String[] args)
|
||||
{
|
||||
// Create here the CSV and XML managers object to parse and sync the XML with the CSV file
|
||||
CsvManager csvManager = new CsvManager("/home/biniam/eclipse-workspace/vre-removed-publisher/src/main/resources/updated_VREDecommisioned-240326.csv");
|
||||
XmlManager xmlManager = new XmlManager("/home/biniam/eclipse-workspace/vre-removed-publisher/src/main/resources/doc.xml");
|
||||
|
||||
// Parse CSV and XML files
|
||||
csvManager.parse();
|
||||
xmlManager.parse();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,27 +1,60 @@
|
|||
package org.gcube.vremanagement;
|
||||
|
||||
/*
|
||||
* Class used to parse and update the CSV file
|
||||
*/
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class CsvManager implements VreRemPubInterface {
|
||||
|
||||
public CsvManager()
|
||||
{
|
||||
System.out.println("CSV manager created!");
|
||||
}
|
||||
|
||||
|
||||
public class CsvManager implements VreRemPubInterface {
|
||||
private String filePath;
|
||||
private ArrayList<String[]> dataList;
|
||||
|
||||
public CsvManager(String filePath) {
|
||||
this.filePath = filePath;
|
||||
System.out.println("CSV manager created!");
|
||||
this.dataList = new ArrayList<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void parse() {
|
||||
try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
|
||||
String line;
|
||||
while ((line = br.readLine()) != null) {
|
||||
String[] values = line.split(",");
|
||||
dataList.add(values);
|
||||
}
|
||||
System.out.println("CSV file parsed successfully.");
|
||||
// Optionally, print the data here
|
||||
System.out.println("Parsed CSV data: " + dataList);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void update() {
|
||||
// Implement update logic if needed
|
||||
}
|
||||
|
||||
@Override
|
||||
public void parse() {
|
||||
public void find() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update() {
|
||||
public void read() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
// Other methods...
|
||||
}
|
||||
|
|
|
@ -5,4 +5,7 @@ public interface VreRemPubInterface {
|
|||
void parse(); //method used to parse the file (e.g., xml or csv) in a data structure
|
||||
void update(); //method used to update the file (e.g., xml or csv)
|
||||
//add here other common methods needed by both Csv and Xml Manager
|
||||
}
|
||||
void find();
|
||||
void read();
|
||||
void write();
|
||||
}
|
||||
|
|
|
@ -1,30 +1,75 @@
|
|||
package org.gcube.vremanagement;
|
||||
|
||||
/*Testing how to push an commit
|
||||
* Class used to parse and update the XML file
|
||||
*/
|
||||
import java.io.File;
|
||||
import java.util.HashMap;
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.Node;
|
||||
import org.w3c.dom.NodeList;
|
||||
|
||||
public class XmlManager implements VreRemPubInterface {
|
||||
private String filePath;
|
||||
private HashMap<String, Integer> tagCountMap;
|
||||
|
||||
public XmlManager(String filePath) {
|
||||
this.filePath = filePath;
|
||||
System.out.println("XML manager created!");
|
||||
this.tagCountMap = new HashMap<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void parse() {
|
||||
try {
|
||||
File xmlFile = new File(filePath);
|
||||
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
|
||||
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
|
||||
Document doc = dBuilder.parse(xmlFile);
|
||||
doc.getDocumentElement().normalize();
|
||||
NodeList nodeList = doc.getElementsByTagName("*"); // Get all elements
|
||||
|
||||
for (int i = 0; i < nodeList.getLength(); i++) {
|
||||
Node node = nodeList.item(i);
|
||||
if (node.getNodeType() == Node.ELEMENT_NODE) {
|
||||
String tagName = node.getNodeName();
|
||||
// Update tag count in the HashMap
|
||||
tagCountMap.put(tagName, tagCountMap.getOrDefault(tagName, 0) + 1);
|
||||
}
|
||||
}
|
||||
System.out.println("XML file parsed successfully.");
|
||||
// Optionally, print the tag count map here
|
||||
System.out.println("Tag count map: " + tagCountMap);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update() {
|
||||
// Implement update logic if needed
|
||||
}
|
||||
|
||||
// Other methods...
|
||||
|
||||
|
||||
//constructor
|
||||
public XmlManager()
|
||||
{
|
||||
System.out.println("XML manager created!");
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void parse() {
|
||||
public void find() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update() {
|
||||
public void read() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@Override
|
||||
public void write() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
// Other methods...
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue