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