package com.advitha.util;
import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.apache.commons.lang.StringUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
public class UpdateXMLContent {
public static void main(String args[]) {
try {
Set<String> controllerClasses = new HashSet<String>();
String filepath = "D:\\file.xml";
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(new InputSource(StrutsConfigToSpringConfig.class.getClass()
.getResourceAsStream("/resources/new/spring-config.xml")));
Node beans = doc.getFirstChild();
NodeList beanList = doc.getElementsByTagName("bean");
for(int index = 0; index < beanList.getLength(); index++) {
Node bean = beanList.item(index);
if(bean.hasAttributes()) {
Node idAttribute = bean.getAttributes().getNamedItem("id");
Node classAttribute = bean.getAttributes().getNamedItem("class");
controllerClasses.add(classAttribute.getNodeValue());
idAttribute.setTextContent(toCamelCase(idAttribute.getNodeValue()));
}
}
System.out.println(controllerClasses);
// write the content into xml file
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(filepath));
transformer.transform(source, result);
System.out.println("Done");
} catch (ParserConfigurationException pce) {
pce.printStackTrace();
} catch (TransformerException tfe) {
tfe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (SAXException sae) {
sae.printStackTrace();
}
}
private static String toCamelCase(String input) {
int lowercaseIndex = 0;
char charArray[] = input.toCharArray();
if(Character.isUpperCase(charArray[0]) && Character.isUpperCase(charArray[1])) {
for(char ch: charArray) {
if(Character.isLowerCase(ch)) {
break;
}
lowercaseIndex++;
}
for(int index = 0; index < lowercaseIndex - 1; index++) {
charArray[index] = Character.toLowerCase(charArray[index]);
}
input = new String(charArray);
} else if(Character.isUpperCase(charArray[0])) {
input = StringUtils.uncapitalise(input);
}
return input;
}
}
import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.apache.commons.lang.StringUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
public class UpdateXMLContent {
public static void main(String args[]) {
try {
Set<String> controllerClasses = new HashSet<String>();
String filepath = "D:\\file.xml";
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(new InputSource(StrutsConfigToSpringConfig.class.getClass()
.getResourceAsStream("/resources/new/spring-config.xml")));
Node beans = doc.getFirstChild();
NodeList beanList = doc.getElementsByTagName("bean");
for(int index = 0; index < beanList.getLength(); index++) {
Node bean = beanList.item(index);
if(bean.hasAttributes()) {
Node idAttribute = bean.getAttributes().getNamedItem("id");
Node classAttribute = bean.getAttributes().getNamedItem("class");
controllerClasses.add(classAttribute.getNodeValue());
idAttribute.setTextContent(toCamelCase(idAttribute.getNodeValue()));
}
}
System.out.println(controllerClasses);
// write the content into xml file
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(filepath));
transformer.transform(source, result);
System.out.println("Done");
} catch (ParserConfigurationException pce) {
pce.printStackTrace();
} catch (TransformerException tfe) {
tfe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (SAXException sae) {
sae.printStackTrace();
}
}
private static String toCamelCase(String input) {
int lowercaseIndex = 0;
char charArray[] = input.toCharArray();
if(Character.isUpperCase(charArray[0]) && Character.isUpperCase(charArray[1])) {
for(char ch: charArray) {
if(Character.isLowerCase(ch)) {
break;
}
lowercaseIndex++;
}
for(int index = 0; index < lowercaseIndex - 1; index++) {
charArray[index] = Character.toLowerCase(charArray[index]);
}
input = new String(charArray);
} else if(Character.isUpperCase(charArray[0])) {
input = StringUtils.uncapitalise(input);
}
return input;
}
}