1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.tools;
21  
22  import com.liferay.portal.kernel.util.FileUtil;
23  import com.liferay.portal.kernel.util.GetterUtil;
24  import com.liferay.portal.kernel.util.StringUtil;
25  import com.liferay.portal.kernel.xml.Document;
26  import com.liferay.portal.kernel.xml.Element;
27  import com.liferay.portal.kernel.xml.SAXReaderUtil;
28  import com.liferay.portal.util.InitUtil;
29  import com.liferay.util.xml.XMLFormatter;
30  
31  import java.util.Iterator;
32  
33  /**
34   * <a href="WebXML23Converter.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author  Brian Wing Shun Chan
37   *
38   */
39  public class WebXML23Converter {
40  
41      public static void main(String[] args) {
42          InitUtil.initWithSpring();
43  
44          if (args.length == 2) {
45              new WebXML23Converter(args[0], args[1]);
46          }
47          else {
48              throw new IllegalArgumentException();
49          }
50      }
51  
52      public WebXML23Converter(String input, String output) {
53          try {
54              String webXML24 = FileUtil.read(input);
55  
56              Document doc = SAXReaderUtil.read(webXML24);
57  
58              Element root = doc.getRootElement();
59  
60              double version = GetterUtil.getDouble(
61                  root.attributeValue("version"));
62  
63              if (version == 2.4) {
64                  System.out.println(
65                      "Convert web.xml because it is Servlet 2.4");
66              }
67              else {
68                  System.out.println(
69                      "Do not convert web.xml because it is not Servlet 2.4");
70  
71                  return;
72              }
73  
74              Iterator<Element> itr1 = root.elements("filter-mapping").iterator();
75  
76              while (itr1.hasNext()) {
77                  Element filterMapping = itr1.next();
78  
79                  Iterator<Element> itr2 = filterMapping.elements(
80                      "dispatcher").iterator();
81  
82                  while (itr2.hasNext()) {
83                      Element dispatcher = itr2.next();
84  
85                      dispatcher.detach();
86                  }
87              }
88  
89              String webXML23 = doc.formattedString();
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 }