1
22
23 package com.liferay.portal.tools;
24
25 import com.liferay.portal.kernel.util.GetterUtil;
26 import com.liferay.portal.kernel.util.StringUtil;
27 import com.liferay.portal.util.DocumentUtil;
28 import com.liferay.util.FileUtil;
29 import com.liferay.util.xml.XMLFormatter;
30
31 import java.util.Iterator;
32
33 import org.dom4j.Document;
34 import org.dom4j.Element;
35
36
42 public class WebXML23Converter {
43
44 public static void main(String[] args) {
45 if (args.length == 2) {
46 new WebXML23Converter(args[0], args[1]);
47 }
48 else {
49 throw new IllegalArgumentException();
50 }
51 }
52
53 public WebXML23Converter(String input, String output) {
54 try {
55 String webXML24 = FileUtil.read(input);
56
57 Document doc = DocumentUtil.readDocumentFromXML(webXML24);
58
59 Element root = doc.getRootElement();
60
61 double version = GetterUtil.getDouble(
62 root.attributeValue("version"));
63
64 if (version == 2.4) {
65 System.out.println(
66 "Convert web.xml because it is Servlet 2.4");
67 }
68 else {
69 System.out.println(
70 "Do not convert web.xml because it is not Servlet 2.4");
71
72 return;
73 }
74
75 Iterator<Element> itr1 = root.elements("filter-mapping").iterator();
76
77 while (itr1.hasNext()) {
78 Element filterMapping = itr1.next();
79
80 Iterator<Element> itr2 = filterMapping.elements(
81 "dispatcher").iterator();
82
83 while (itr2.hasNext()) {
84 Element dispatcher = itr2.next();
85
86 dispatcher.detach();
87 }
88 }
89
90 String webXML23 = XMLFormatter.toString(doc);
91
92 int x = webXML23.indexOf("<web-app");
93 int y = webXML23.indexOf(">", x);
94
95 webXML23 = webXML23.substring(0, x) + "<!DOCTYPE web-app PUBLIC \"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN\" \"http://java.sun.com/dtd/web-app_2_3.dtd\"><web-app>" + webXML23.substring(y + 1, webXML23.length());
96
97 webXML23 = StringUtil.replace(
98 webXML23,
99 new String[] {
100 "<jsp-config>", "</jsp-config>"
101 },
102 new String[] {
103 "", ""
104 });
105
106 webXML23 = XMLFormatter.toString(webXML23);
107
108 FileUtil.write(output, webXML23);
109 }
110 catch (Exception e) {
111 e.printStackTrace();
112 }
113 }
114
115 }