1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.tools;
16  
17  import com.liferay.portal.kernel.util.FileUtil;
18  import com.liferay.portal.kernel.util.GetterUtil;
19  import com.liferay.portal.kernel.util.StringUtil;
20  import com.liferay.portal.kernel.xml.Document;
21  import com.liferay.portal.kernel.xml.Element;
22  import com.liferay.portal.kernel.xml.SAXReaderUtil;
23  import com.liferay.portal.util.InitUtil;
24  import com.liferay.util.xml.XMLFormatter;
25  
26  import java.util.Iterator;
27  
28  /**
29   * <a href="WebXML23Converter.java.html"><b><i>View Source</i></b></a>
30   *
31   * @author Brian Wing Shun Chan
32   */
33  public class WebXML23Converter {
34  
35      public static void main(String[] args) {
36          InitUtil.initWithSpring();
37  
38          if (args.length == 2) {
39              new WebXML23Converter(args[0], args[1]);
40          }
41          else {
42              throw new IllegalArgumentException();
43          }
44      }
45  
46      public WebXML23Converter(String input, String output) {
47          try {
48              String webXML24 = FileUtil.read(input);
49  
50              Document doc = SAXReaderUtil.read(webXML24);
51  
52              Element root = doc.getRootElement();
53  
54              double version = GetterUtil.getDouble(
55                  root.attributeValue("version"));
56  
57              if (version == 2.4) {
58                  System.out.println(
59                      "Convert web.xml because it is Servlet 2.4");
60              }
61              else {
62                  System.out.println(
63                      "Do not convert web.xml because it is not Servlet 2.4");
64  
65                  return;
66              }
67  
68              Iterator<Element> itr1 = root.elements("filter-mapping").iterator();
69  
70              while (itr1.hasNext()) {
71                  Element filterMapping = itr1.next();
72  
73                  Iterator<Element> itr2 = filterMapping.elements(
74                      "dispatcher").iterator();
75  
76                  while (itr2.hasNext()) {
77                      Element dispatcher = itr2.next();
78  
79                      dispatcher.detach();
80                  }
81              }
82  
83              String webXML23 = doc.formattedString();
84  
85              int x = webXML23.indexOf("<web-app");
86              int y = webXML23.indexOf(">", x);
87  
88              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());
89  
90              webXML23 = StringUtil.replace(
91                  webXML23,
92                  new String[] {
93                      "<jsp-config>", "</jsp-config>"
94                  },
95                  new String[] {
96                      "", ""
97                  });
98  
99              webXML23 = XMLFormatter.toString(webXML23);
100 
101             FileUtil.write(output, webXML23);
102         }
103         catch (Exception e) {
104             e.printStackTrace();
105         }
106     }
107 
108 }