1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class WebXML23Converter {
42  
43      public static void main(String[] args) {
44          InitUtil.initWithSpring();
45  
46          if (args.length == 2) {
47              new WebXML23Converter(args[0], args[1]);
48          }
49          else {
50              throw new IllegalArgumentException();
51          }
52      }
53  
54      public WebXML23Converter(String input, String output) {
55          try {
56              String webXML24 = FileUtil.read(input);
57  
58              Document doc = SAXReaderUtil.read(webXML24);
59  
60              Element root = doc.getRootElement();
61  
62              double version = GetterUtil.getDouble(
63                  root.attributeValue("version"));
64  
65              if (version == 2.4) {
66                  System.out.println(
67                      "Convert web.xml because it is Servlet 2.4");
68              }
69              else {
70                  System.out.println(
71                      "Do not convert web.xml because it is not Servlet 2.4");
72  
73                  return;
74              }
75  
76              Iterator<Element> itr1 = root.elements("filter-mapping").iterator();
77  
78              while (itr1.hasNext()) {
79                  Element filterMapping = itr1.next();
80  
81                  Iterator<Element> itr2 = filterMapping.elements(
82                      "dispatcher").iterator();
83  
84                  while (itr2.hasNext()) {
85                      Element dispatcher = itr2.next();
86  
87                      dispatcher.detach();
88                  }
89              }
90  
91              String webXML23 = doc.formattedString();
92  
93              int x = webXML23.indexOf("<web-app");
94              int y = webXML23.indexOf(">", x);
95  
96              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());
97  
98              webXML23 = StringUtil.replace(
99                  webXML23,
100                 new String[] {
101                     "<jsp-config>", "</jsp-config>"
102                 },
103                 new String[] {
104                     "", ""
105                 });
106 
107             webXML23 = XMLFormatter.toString(webXML23);
108 
109             FileUtil.write(output, webXML23);
110         }
111         catch (Exception e) {
112             e.printStackTrace();
113         }
114     }
115 
116 }