1   /**
2    * Copyright (c) 2000-2007 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.GetterUtil;
26  import com.liferay.portal.kernel.util.StringUtil;
27  import com.liferay.portal.util.PortalUtil;
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  /**
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          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 = PortalUtil.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 itr1 = root.elements("filter-mapping").iterator();
76  
77              while (itr1.hasNext()) {
78                  Element filterMapping = (Element)itr1.next();
79  
80                  Iterator itr2 = filterMapping.elements("dispatcher").iterator();
81  
82                  while (itr2.hasNext()) {
83                      Element dispatcher = (Element)itr2.next();
84  
85                      dispatcher.detach();
86                  }
87              }
88  
89              String webXML23 = XMLFormatter.toString(doc);
90  
91              int x = webXML23.indexOf("<web-app");
92              int y = webXML23.indexOf(">", x);
93  
94              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());
95  
96              webXML23 = StringUtil.replace(
97                  webXML23,
98                  new String[] {
99                      "<jsp-config>", "</jsp-config>"
100                 },
101                 new String[] {
102                     "", ""
103                 });
104 
105             webXML23 = XMLFormatter.toString(webXML23);
106 
107             FileUtil.write(output, webXML23);
108         }
109         catch (Exception e) {
110             e.printStackTrace();
111         }
112     }
113 
114 }