| WebXML23Converter.java |
1 /**
2 * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
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.kernel.xml.Document;
29 import com.liferay.portal.kernel.xml.Element;
30 import com.liferay.portal.kernel.xml.SAXReaderUtil;
31 import com.liferay.portal.util.InitUtil;
32 import com.liferay.util.xml.XMLFormatter;
33
34 import java.util.Iterator;
35
36 /**
37 * <a href="WebXML23Converter.java.html"><b><i>View Source</i></b></a>
38 *
39 * @author Brian Wing Shun Chan
40 *
41 */
42 public class WebXML23Converter {
43
44 public static void main(String[] args) {
45 InitUtil.initWithSpring();
46
47 if (args.length == 2) {
48 new WebXML23Converter(args[0], args[1]);
49 }
50 else {
51 throw new IllegalArgumentException();
52 }
53 }
54
55 public WebXML23Converter(String input, String output) {
56 try {
57 String webXML24 = FileUtil.read(input);
58
59 Document doc = SAXReaderUtil.read(webXML24);
60
61 Element root = doc.getRootElement();
62
63 double version = GetterUtil.getDouble(
64 root.attributeValue("version"));
65
66 if (version == 2.4) {
67 System.out.println(
68 "Convert web.xml because it is Servlet 2.4");
69 }
70 else {
71 System.out.println(
72 "Do not convert web.xml because it is not Servlet 2.4");
73
74 return;
75 }
76
77 Iterator<Element> itr1 = root.elements("filter-mapping").iterator();
78
79 while (itr1.hasNext()) {
80 Element filterMapping = itr1.next();
81
82 Iterator<Element> itr2 = filterMapping.elements(
83 "dispatcher").iterator();
84
85 while (itr2.hasNext()) {
86 Element dispatcher = itr2.next();
87
88 dispatcher.detach();
89 }
90 }
91
92 String webXML23 = doc.formattedString();
93
94 int x = webXML23.indexOf("<web-app");
95 int y = webXML23.indexOf(">", x);
96
97 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());
98
99 webXML23 = StringUtil.replace(
100 webXML23,
101 new String[] {
102 "<jsp-config>", "</jsp-config>"
103 },
104 new String[] {
105 "", ""
106 });
107
108 webXML23 = XMLFormatter.toString(webXML23);
109
110 FileUtil.write(output, webXML23);
111 }
112 catch (Exception e) {
113 e.printStackTrace();
114 }
115 }
116
117 }