001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.tools;
016    
017    import com.liferay.portal.kernel.util.FileUtil;
018    import com.liferay.portal.kernel.util.GetterUtil;
019    import com.liferay.portal.kernel.util.StringUtil;
020    import com.liferay.portal.kernel.xml.Document;
021    import com.liferay.portal.kernel.xml.Element;
022    import com.liferay.portal.kernel.xml.UnsecureSAXReaderUtil;
023    import com.liferay.util.xml.Dom4jUtil;
024    
025    import java.util.List;
026    
027    /**
028     * @author Brian Wing Shun Chan
029     */
030    public class WebXML23Converter {
031    
032            public static void main(String[] args) {
033                    ToolDependencies.wireBasic();
034    
035                    if (args.length == 2) {
036                            new WebXML23Converter(args[0], args[1]);
037                    }
038                    else {
039                            throw new IllegalArgumentException();
040                    }
041            }
042    
043            public WebXML23Converter(String input, String output) {
044                    try {
045                            String webXML24 = FileUtil.read(input);
046    
047                            Document document = UnsecureSAXReaderUtil.read(webXML24);
048    
049                            Element rootElement = document.getRootElement();
050    
051                            double version = GetterUtil.getDouble(
052                                    rootElement.attributeValue("version"));
053    
054                            if (version == 2.4) {
055                                    System.out.println("Convert web.xml because it is Servlet 2.4");
056                            }
057                            else {
058                                    System.out.println(
059                                            "Do not convert web.xml because it is not Servlet 2.4");
060    
061                                    return;
062                            }
063    
064                            List<Element> filterMappingElements = rootElement.elements(
065                                    "filter-mapping");
066    
067                            for (Element filterMappingElement : filterMappingElements) {
068                                    List<Element> dispatcherElements =
069                                            filterMappingElement.elements("dispatcher");
070    
071                                    for (Element dispatcherElement : dispatcherElements) {
072                                            dispatcherElement.detach();
073                                    }
074                            }
075    
076                            String webXML23 = document.formattedString();
077    
078                            int x = webXML23.indexOf("<web-app");
079                            int y = webXML23.indexOf(">", x);
080    
081                            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);
082    
083                            webXML23 = StringUtil.replace(
084                                    webXML23, new String[] {"<jsp-config>", "</jsp-config>"},
085                                    new String[] {"", ""});
086    
087                            webXML23 = Dom4jUtil.toString(webXML23);
088    
089                            FileUtil.write(output, webXML23);
090                    }
091                    catch (Exception e) {
092                            e.printStackTrace();
093                    }
094            }
095    
096    }