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.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  /**
38   * <a href="WebXML23Converter.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author  Brian Wing Shun Chan
41   *
42   */
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 }