1   /**
2    * Copyright (c) 2000-2008 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.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  /**
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 = 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 }