The new xml file finalDoc file is created that include information of both merged original xml and csv files and with no duplicates of gateways,vo and vres

This commit is contained in:
Biniam Abraha Masa 2024-06-25 14:31:27 +02:00
parent 28862e6cf4
commit d00dc3d552
2 changed files with 150 additions and 145 deletions

View File

@ -10,8 +10,7 @@ public class App {
public static void main(String[] args) {
String csvFilePath = "src/main/resources/updated_VREDecommisioned-240326.csv";
String xmlFilePath = "src/main/resources/doc.xml";
//String csvFilePath = "src/main/resources/onerow.csv";
//String xmlFilePath = "src/main/resources/onegate.xml";
// Initialize managers
CsvManager csvManager = new CsvManager(csvFilePath);

View File

@ -274,188 +274,193 @@ public class XmlManager implements VreRemPubInterface {
// TODO Auto-generated method stub
return null;
}
public void writeXml(HashMap<String, Gateway> map, String outputXmlFilePath) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.newDocument();
public void writeXml(HashMap<String, Gateway> map, String outputXmlFilePath) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.newDocument();
//Create root element <genericResources>
Element genericResourcesElement = document.createElement("genericResources");
document.appendChild(genericResourcesElement);
// Create root element <genericResources>
Element genericResourcesElement = document.createElement("genericResources");
document.appendChild(genericResourcesElement);
// Create <Resource> element with version attribute
Element resourceElement = document.createElement("Resource");
resourceElement.setAttribute("version", "0.4.x");
genericResourcesElement.appendChild(resourceElement);
// Create <Resource> element with version attribute
Element resourceElement = document.createElement("Resource");
resourceElement.setAttribute("version", "0.4.x");
genericResourcesElement.appendChild(resourceElement);
// Create <ID> element
Element idElement = document.createElement("ID");
idElement.appendChild(document.createTextNode("68322b8f-b916-44b9-9c93-bad8cceb9106"));
resourceElement.appendChild(idElement);
// Create <ID> element
Element idElement = document.createElement("ID");
idElement.appendChild(document.createTextNode("68322b8f-b916-44b9-9c93-bad8cceb9106"));
resourceElement.appendChild(idElement);
// Create <Type> element
Element typeElement = document.createElement("Type");
typeElement.appendChild(document.createTextNode("GenericResource"));
resourceElement.appendChild(typeElement);
// Create <Type> element
Element typeElement = document.createElement("Type");
typeElement.appendChild(document.createTextNode("GenericResource"));
resourceElement.appendChild(typeElement);
// Create <Scopes> element
Element scopesElement = document.createElement("Scopes");
resourceElement.appendChild(scopesElement);
// Create <Scopes> element
Element scopesElement = document.createElement("Scopes");
resourceElement.appendChild(scopesElement);
// Create <Scope> element within <Scopes>
Element scopeElement = document.createElement("Scope");
scopeElement.appendChild(document.createTextNode("/d4science.research-infrastructures.eu"));
scopesElement.appendChild(scopeElement);
// Create <Scope> element within <Scopes>
Element scopeElement = document.createElement("Scope");
scopeElement.appendChild(document.createTextNode("/d4science.research-infrastructures.eu"));
scopesElement.appendChild(scopeElement);
// Create <Profile> element
Element profileElement = document.createElement("Profile");
resourceElement.appendChild(profileElement);
// Create <Profile> element
Element profileElement = document.createElement("Profile");
resourceElement.appendChild(profileElement);
// Create <SecondaryType> element
Element secondaryTypeElement = document.createElement("SecondaryType");
secondaryTypeElement.appendChild(document.createTextNode("DetachedREs"));
profileElement.appendChild(secondaryTypeElement);
// Create <SecondaryType> element
Element secondaryTypeElement = document.createElement("SecondaryType");
secondaryTypeElement.appendChild(document.createTextNode("DetachedREs"));
profileElement.appendChild(secondaryTypeElement);
// Create <Name> element
Element nameElement = document.createElement("Name");
nameElement.appendChild(document.createTextNode("DetachedREsData"));
profileElement.appendChild(nameElement);
// Create <Name> element
Element nameElement = document.createElement("Name");
nameElement.appendChild(document.createTextNode("DetachedREsData"));
profileElement.appendChild(nameElement);
// Create <Description> element
Element descriptionElement1 = document.createElement("Description");
descriptionElement1.appendChild(document.createTextNode("Contains the information relating to dismissed Gateways, VOs and VREs."));
profileElement.appendChild(descriptionElement1);
// Create <Description> element
Element descriptionElement1 = document.createElement("Description");
descriptionElement1.appendChild(document.createTextNode("Contains the information relating to dismissed Gateways, VOs and VREs."));
profileElement.appendChild(descriptionElement1);
// Create <Body> element
Element bodyElement = document.createElement("Body");
profileElement.appendChild(bodyElement);
// Create <Body> element
Element bodyElement = document.createElement("Body");
profileElement.appendChild(bodyElement);
// Create <detachedres> element
Element detachedResElement = document.createElement("detachedres");
bodyElement.appendChild(detachedResElement);
// Create <detachedres> element
Element detachedResElement = document.createElement("detachedres");
bodyElement.appendChild(detachedResElement);
// Create <enabled> element within <detachedres>
Element enabledElement = document.createElement("enabled");
enabledElement.appendChild(document.createTextNode("true"));
detachedResElement.appendChild(enabledElement);
// Create <enabled> element within <detachedres>
Element enabledElement = document.createElement("enabled");
enabledElement.appendChild(document.createTextNode("true"));
detachedResElement.appendChild(enabledElement);
// Create <gateways> element within <detachedres>
Element gatewaysElement = document.createElement("gateways");
detachedResElement.appendChild(gatewaysElement);
int gatewayKey = -1;
// Create <gateways> element within <detachedres>
Element gatewaysElement = document.createElement("gateways");
detachedResElement.appendChild(gatewaysElement);
// Iterate through the map entries to populate <entry> elements under <gateways>
for (Map.Entry<String, Gateway> entry : map.entrySet()) {
Gateway gateway = entry.getValue();
int gatewayKey = -1;
// Create <entry> element
Element entryElement = document.createElement("entry");
gatewaysElement.appendChild(entryElement);
// Iterate through the map entries to populate <entry> elements under <gateways>
for (Map.Entry<String, Gateway> entry : map.entrySet()) {
Gateway gateway = entry.getValue();
// Create <key> element within <entry>
Element keyElement = document.createElement("key");
keyElement.appendChild(document.createTextNode(String.valueOf(gatewayKey)));
entryElement.appendChild(keyElement);
// Create <entry> element
Element entryElement = document.createElement("entry");
gatewaysElement.appendChild(entryElement);
// Create <value> element within <entry>
Element valueElement = document.createElement("value");
entryElement.appendChild(valueElement);
// Create <key> element within <entry>
Element keyElement = document.createElement("key");
keyElement.appendChild(document.createTextNode(String.valueOf(gatewayKey)));
entryElement.appendChild(keyElement);
// Create <scope> element within <value>
Element scopeValueElement = document.createElement("scope");
scopeValueElement.appendChild(document.createTextNode(String.valueOf(gatewayKey)));
valueElement.appendChild(scopeValueElement);
// Create <value> element within <entry>
Element valueElement = document.createElement("value");
entryElement.appendChild(valueElement);
// Create <name> element within <value>
Element nameValueElement = document.createElement("name");
String getewayName = extractGatewayName(gateway.getName());
nameValueElement.appendChild(document.createTextNode(getewayName));//nameValueElement.appendChild(document.createTextNode(getewayName.toUpperCase() + " Gateway"));
valueElement.appendChild(nameValueElement);
// Create <scope> element within <value>
Element scopeValueElement = document.createElement("scope");
scopeValueElement.appendChild(document.createTextNode(String.valueOf(gatewayKey)));
valueElement.appendChild(scopeValueElement);
Element descriptionElement = document.createElement("description");
descriptionElement.appendChild(document.createTextNode(gateway.getDescription()));
valueElement.appendChild(descriptionElement);
// Create <name> element within <value>
Element nameValueElement = document.createElement("name");
String gatewayName = extractGatewayName(gateway.getName());
nameValueElement.appendChild(document.createTextNode(gatewayName));
valueElement.appendChild(nameValueElement);
Element vosElement = document.createElement("vos");
valueElement.appendChild(vosElement);
// Create <description> element within <value>
Element descriptionElement = document.createElement("description");
descriptionElement.appendChild(document.createTextNode("No description for gateway"));
valueElement.appendChild(descriptionElement);
for (Vos vos : gateway.getVosList()) {
Element vosEntryElement = document.createElement("entry");
vosElement.appendChild(vosEntryElement);
Element vosElement = document.createElement("vos");
valueElement.appendChild(vosElement);
Element vosKeyElement = document.createElement("key");
vosKeyElement.appendChild(document.createTextNode("/d4science.research-infrastructures.eu/" + vos.getName()));
vosEntryElement.appendChild(vosKeyElement);
// Collect VREs under their respective VOs
Map<String, Element> voElements = new HashMap<>();
Element vosValueElement = document.createElement("value");
vosEntryElement.appendChild(vosValueElement);
for (Vos vos : gateway.getVosList()) {
Element vosEntryElement;
Element vosValueElement;
if (voElements.containsKey(vos.getName())) {
vosEntryElement = voElements.get(vos.getName());
vosValueElement = (Element) vosEntryElement.getElementsByTagName("value").item(0);
} else {
vosEntryElement = document.createElement("entry");
vosElement.appendChild(vosEntryElement);
Element vosScopeElement = document.createElement("scope");
vosScopeElement.appendChild(document.createTextNode("/d4science.research-infrastructures.eu/" + vos.getName()));
vosValueElement.appendChild(vosScopeElement);
Element vosNameElement = document.createElement("name");
vosNameElement.appendChild(document.createTextNode(vos.getName()));
vosValueElement.appendChild(vosNameElement);
Element vosKeyElement = document.createElement("key");
vosKeyElement.appendChild(document.createTextNode("/d4science.research-infrastructures.eu/" + vos.getName()));
vosEntryElement.appendChild(vosKeyElement);
vosValueElement = document.createElement("value");
vosEntryElement.appendChild(vosValueElement);
Element vosScopeElement = document.createElement("scope");
vosScopeElement.appendChild(document.createTextNode("/d4science.research-infrastructures.eu/" + vos.getName()));
vosValueElement.appendChild(vosScopeElement);
Element vresElement = document.createElement("vres");
vosValueElement.appendChild(vresElement);
Element vosNameElement = document.createElement("name");
vosNameElement.appendChild(document.createTextNode(vos.getName()));
vosValueElement.appendChild(vosNameElement);
for (Vres vres : vos.getVresList()) {
Element vresEntryElement = document.createElement("entry");
vresElement.appendChild(vresEntryElement);
voElements.put(vos.getName(), vosEntryElement);
}
Element vresKeyElement = document.createElement("key");
vresKeyElement.appendChild(document.createTextNode("/d4science.research-infrastructures.eu/" + vos.getName()+"/"+ vres.getName()));
vresEntryElement.appendChild(vresKeyElement);
Element vresElement = document.createElement("vres");
vosValueElement.appendChild(vresElement);
Element vresValueElement = document.createElement("value");
vresEntryElement.appendChild(vresValueElement);
//New inserted code to include the scope tag above Name tag in the output
// Element vresScopeElement = document.createElement("scope");
//vresScopeElement.appendChild(document.createTextNode(vres.getScope()));
//vresEntryElement.appendChild(vresScopeElement);
// Create <scope> element within <value>
Element vreScopeElement = document.createElement("scope");
vreScopeElement.appendChild(document.createTextNode("/d4science.research-infrastructures.eu/" + vos.getName()+"/"+ vres.getName()));
vresValueElement.appendChild(vreScopeElement);
Element vresNameElement = document.createElement("name");
vresNameElement.appendChild(document.createTextNode(vres.getName()));
vresValueElement.appendChild(vresNameElement);
for (Vres vres : vos.getVresList()) {
Element vresEntryElement = document.createElement("entry");
vresElement.appendChild(vresEntryElement);
Element vresDescriptionElement = document.createElement("description");
vresDescriptionElement.appendChild(document.createTextNode(vres.getDescription()));
vresValueElement.appendChild(vresDescriptionElement);
Element vresKeyElement = document.createElement("key");
vresKeyElement.appendChild(document.createTextNode("/d4science.research-infrastructures.eu/" + vos.getName() + "/" + vres.getName()));
vresEntryElement.appendChild(vresKeyElement);
Element vresManagerElement = document.createElement("managers");
vresManagerElement.appendChild(document.createTextNode(vres.getManager()));
vresValueElement.appendChild(vresManagerElement);
Element vresValueElement = document.createElement("value");
vresEntryElement.appendChild(vresValueElement);
Element vresStartDateElement = document.createElement("startdate");
vresStartDateElement.appendChild(document.createTextNode(vres.getStartDate()));
vresValueElement.appendChild(vresStartDateElement);
Element vreScopeElement = document.createElement("scope");
vreScopeElement.appendChild(document.createTextNode("/d4science.research-infrastructures.eu/" + vos.getName() + "/" + vres.getName()));
vresValueElement.appendChild(vreScopeElement);
Element vresEndDateElement = document.createElement("enddate");
vresEndDateElement.appendChild(document.createTextNode(vres.getEndDate()));
vresValueElement.appendChild(vresEndDateElement);
Element vresNameElement = document.createElement("name");
vresNameElement.appendChild(document.createTextNode(vres.getName()));
vresValueElement.appendChild(vresNameElement);
Element vresDescriptionElement = document.createElement("description");
vresDescriptionElement.appendChild(document.createTextNode(vres.getDescription()));
vresValueElement.appendChild(vresDescriptionElement);
Element vresManagerElement = document.createElement("managers");
vresManagerElement.appendChild(document.createTextNode(vres.getManager()));
vresValueElement.appendChild(vresManagerElement);
Element vresStartDateElement = document.createElement("startdate");
vresStartDateElement.appendChild(document.createTextNode(vres.getStartDate()));
vresValueElement.appendChild(vresStartDateElement);
Element vresEndDateElement = document.createElement("enddate");
vresEndDateElement.appendChild(document.createTextNode(vres.getEndDate()));
vresValueElement.appendChild(vresEndDateElement);
}
}
gatewayKey--;
}
gatewayKey--;
}
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(new File(outputXmlFilePath));
transformer.transform(source, result);
}
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(new File(outputXmlFilePath));
transformer.transform(source, result);
}
// Other methods...
private String extractGatewayName(String gateways) {
if (gateways.startsWith("https://")) {
@ -468,3 +473,4 @@ public class XmlManager implements VreRemPubInterface {
}
}