import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
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.xerces.parsers.DOMParser;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
public class StrutsConfigToSpringConfig {
public static void main(String[] args) throws SAXException, IOException, ParserConfigurationException, TransformerException {
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder;
dBuilder = dbFactory.newDocumentBuilder();
Document outputFile = dBuilder.newDocument();
Element rootElementForOutputFile = outputFile.createElement("beans");
outputFile.appendChild(rootElementForOutputFile);
DOMParser parser = new DOMParser();
// parser.parse(new InputSource(StrutsConfigToSpringConfig.class.getClass().getResourceAsStream("/resources/struts-config.xml")));
parser.parse(new InputSource(new FileInputStream("D:/struts-config.xml")));
Document doc = parser.getDocument();
NodeList root = doc.getChildNodes();
Node strutsConfigNode = DOMUtilities.getNode("struts-config", root);
Node actionMappings = DOMUtilities.getNode("action-mappings", strutsConfigNode.getChildNodes());
NodeList actions = actionMappings.getChildNodes();
int noOfActions = 0;
for(int i = 0; i < actions.getLength(); i++){
noOfActions = processActionTags(actions, noOfActions, i, outputFile, rootElementForOutputFile);
}
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
DOMSource source = new DOMSource(outputFile);
StreamResult result = new StreamResult(new File("D:\\result.xml"));
transformer.transform(source, result);
System.out.println("No. of actions tags: " + noOfActions);
}
private static int processActionTags(NodeList actions, int noOfActions,
int i, Document outputFile, Element rootElementForOutputFile) {
Node action = actions.item(i);
if(action.getNodeName().equals("action")){
++noOfActions;
Element bean = outputFile.createElement("bean");
bean.setAttribute("scope", "request");
NamedNodeMap actionAttributes = action.getAttributes();
for(int j = 0; j < actionAttributes.getLength(); j++){
Node actionAttrib = actionAttributes.item(j);
if(actionAttrib.getNodeName().equals("id")){
bean.setAttribute("id", actionAttrib.getNodeValue());
} else if(actionAttrib.getNodeName().equals("type")){
bean.setAttribute("class", actionAttrib.getNodeValue());
if(bean.getAttribute("id").equals("")) {
bean.setAttribute("id", actionAttrib.getNodeValue().substring(actionAttrib.getNodeValue().lastIndexOf(".")+1, actionAttrib.getNodeValue().length()));
}
} else if(!actionAttrib.getNodeName().equals("path")){
Element propertyElement = outputFile.createElement("property");
propertyElement.setAttribute("name", actionAttrib.getNodeName());
propertyElement.setAttribute("value", actionAttrib.getNodeValue());
bean.appendChild(propertyElement);
}
//System.out.print(actionAttrib.getNodeName() + "=" + actionAttrib.getNodeValue() + "\t");
}
//System.out.println();
processActionTagChildTags(action, bean, outputFile);
rootElementForOutputFile.appendChild(bean);
//System.out.println();
}
return noOfActions;
}
private static void processActionTagChildTags(Node action, Element bean, Document outputFile) {
NodeList actionChildTags = action.getChildNodes();
for(int k = 0; k < actionChildTags.getLength(); k++){
Node actionChildTag = actionChildTags.item(k);
if(!actionChildTag.getNodeName().equals("#text")){
NamedNodeMap actionChildTagAttributes = actionChildTag.getAttributes();
if(actionChildTagAttributes != null){
String attribs[] = {"name", "value"};
Element propertyElement = outputFile.createElement("property");
for(int j = 0; j < actionChildTagAttributes.getLength(); j++){
if(j > 1) {
break;
}
Node actionChildTagAttrib = actionChildTagAttributes.item(j);
propertyElement.setAttribute(attribs[j], actionChildTagAttrib.getNodeValue());
bean.appendChild(propertyElement);
//System.out.print(actionChildTagAttrib.getNodeName() + "=" + actionChildTagAttrib.getNodeValue() + "\t");
}
//System.out.println();
}
}
}
// outputFile.appendChild(bean);
}
}
###########################################################
Notes:
import java.io.FileInputStream;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
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.xerces.parsers.DOMParser;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
public class StrutsConfigToSpringConfig {
public static void main(String[] args) throws SAXException, IOException, ParserConfigurationException, TransformerException {
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder;
dBuilder = dbFactory.newDocumentBuilder();
Document outputFile = dBuilder.newDocument();
Element rootElementForOutputFile = outputFile.createElement("beans");
outputFile.appendChild(rootElementForOutputFile);
DOMParser parser = new DOMParser();
// parser.parse(new InputSource(StrutsConfigToSpringConfig.class.getClass().getResourceAsStream("/resources/struts-config.xml")));
parser.parse(new InputSource(new FileInputStream("D:/struts-config.xml")));
Document doc = parser.getDocument();
NodeList root = doc.getChildNodes();
Node strutsConfigNode = DOMUtilities.getNode("struts-config", root);
Node actionMappings = DOMUtilities.getNode("action-mappings", strutsConfigNode.getChildNodes());
NodeList actions = actionMappings.getChildNodes();
int noOfActions = 0;
for(int i = 0; i < actions.getLength(); i++){
noOfActions = processActionTags(actions, noOfActions, i, outputFile, rootElementForOutputFile);
}
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
DOMSource source = new DOMSource(outputFile);
StreamResult result = new StreamResult(new File("D:\\result.xml"));
transformer.transform(source, result);
System.out.println("No. of actions tags: " + noOfActions);
}
private static int processActionTags(NodeList actions, int noOfActions,
int i, Document outputFile, Element rootElementForOutputFile) {
Node action = actions.item(i);
if(action.getNodeName().equals("action")){
++noOfActions;
Element bean = outputFile.createElement("bean");
bean.setAttribute("scope", "request");
NamedNodeMap actionAttributes = action.getAttributes();
for(int j = 0; j < actionAttributes.getLength(); j++){
Node actionAttrib = actionAttributes.item(j);
if(actionAttrib.getNodeName().equals("id")){
bean.setAttribute("id", actionAttrib.getNodeValue());
} else if(actionAttrib.getNodeName().equals("type")){
bean.setAttribute("class", actionAttrib.getNodeValue());
if(bean.getAttribute("id").equals("")) {
bean.setAttribute("id", actionAttrib.getNodeValue().substring(actionAttrib.getNodeValue().lastIndexOf(".")+1, actionAttrib.getNodeValue().length()));
}
} else if(!actionAttrib.getNodeName().equals("path")){
Element propertyElement = outputFile.createElement("property");
propertyElement.setAttribute("name", actionAttrib.getNodeName());
propertyElement.setAttribute("value", actionAttrib.getNodeValue());
bean.appendChild(propertyElement);
}
//System.out.print(actionAttrib.getNodeName() + "=" + actionAttrib.getNodeValue() + "\t");
}
//System.out.println();
processActionTagChildTags(action, bean, outputFile);
rootElementForOutputFile.appendChild(bean);
//System.out.println();
}
return noOfActions;
}
private static void processActionTagChildTags(Node action, Element bean, Document outputFile) {
NodeList actionChildTags = action.getChildNodes();
for(int k = 0; k < actionChildTags.getLength(); k++){
Node actionChildTag = actionChildTags.item(k);
if(!actionChildTag.getNodeName().equals("#text")){
NamedNodeMap actionChildTagAttributes = actionChildTag.getAttributes();
if(actionChildTagAttributes != null){
String attribs[] = {"name", "value"};
Element propertyElement = outputFile.createElement("property");
for(int j = 0; j < actionChildTagAttributes.getLength(); j++){
if(j > 1) {
break;
}
Node actionChildTagAttrib = actionChildTagAttributes.item(j);
propertyElement.setAttribute(attribs[j], actionChildTagAttrib.getNodeValue());
bean.appendChild(propertyElement);
//System.out.print(actionChildTagAttrib.getNodeName() + "=" + actionChildTagAttrib.getNodeValue() + "\t");
}
//System.out.println();
}
}
}
// outputFile.appendChild(bean);
}
}
DOMUtilities.java
-----------------------
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
import org.apache.xerces.parsers.DOMParser;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
public class DOMUtilities {
static Node getNode(String tagName, NodeList nodes) {
for (int x = 0; x < nodes.getLength(); x++) {
Node node = nodes.item(x);
if (node.getNodeName().equalsIgnoreCase(tagName)
&& node.hasChildNodes()) {
return node;
}
}
return null;
}
static Set<String> getNodeValues(String tagName, NodeList nodes) {
Set<String> values = new HashSet();
for (int x = 0; x < nodes.getLength(); x++) {
Node node = nodes.item(x);
if (node.getNodeName().equalsIgnoreCase(tagName)) {
NodeList childNodes = node.getChildNodes();
for (int y = 0; y < childNodes.getLength(); y++) {
Node data = childNodes.item(y);
if (data.getNodeType() == 3) {
values.add(data.getNodeValue());
}
}
}
}
return values;
}
static Node getNodeWithGivenAttributeValue(String attribute,
String attributeValue, NodeList nodes) {
for (int x = 0; x < nodes.getLength(); x++) {
Node node = nodes.item(x);
if ((node.getAttributes() != null)
&& (node.getAttributes().getNamedItem(attribute) != null)
&& (node.getAttributes().getNamedItem(attribute)
.getNodeValue().equalsIgnoreCase(attributeValue))) {
return node;
}
}
return null;
}
static Document getDocument(String fileName) throws SAXException,
IOException {
DOMParser parser = new DOMParser();
parser.parse(new InputSource(StrutsConfigToSpringConfig.class
.getClass().getResourceAsStream(fileName)));
return parser.getDocument();
}
}
###########################################################
Notes:
- Connect to internet when you execute the program since some of the libraries use internet connection
- Remove <DOCTYPE> declaration from struts-config.xml (or *.xml) file, if the program is thrown with connection timed out
- Libraries used for this program are:
- commons-configuration-1.10.jar
- commons-lang-2.6.jar
- commons-logging-1.1.1.jar
- jackson-core-asl.jar
- jackson-mapper-asl.jar
- xercesImpl.jar
- xml-apis.jar