001    /**
002     * Copyright (c) 2000-2012 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.SAXReaderUtil;
023    import com.liferay.portal.util.InitUtil;
024    import com.liferay.util.xml.XMLFormatter;
025    
026    import java.util.List;
027    
028    /**
029     * @author Brian Wing Shun Chan
030     */
031    public class WebXML23Converter {
032    
033            public static void main(String[] args) {
034                    InitUtil.initWithSpring();
035    
036                    if (args.length == 2) {
037                            new WebXML23Converter(args[0], args[1]);
038                    }
039                    else {
040                            throw new IllegalArgumentException();
041                    }
042            }
043    
044            public WebXML23Converter(String input, String output) {
045                    try {
046                            String webXML24 = FileUtil.read(input);
047    
048                            Document document = SAXReaderUtil.read(webXML24);
049    
050                            Element rootElement = document.getRootElement();
051    
052                            double version = GetterUtil.getDouble(
053                                    rootElement.attributeValue("version"));
054    
055                            if (version == 2.4) {
056                                    System.out.println("Convert web.xml because it is Servlet 2.4");
057                            }
058                            else {
059                                    System.out.println(
060                                            "Do not convert web.xml because it is not Servlet 2.4");
061    
062                                    return;
063                            }
064    
065                            List<Element> filterMappingElements = rootElement.elements(
066                                    "filter-mapping");
067    
068                            for (Element filterMappingElement : filterMappingElements) {
069                                    List<Element> dispatcherElements =
070                                            filterMappingElement.elements("dispatcher");
071    
072                                    for (Element dispatcherElement : dispatcherElements) {
073                                            dispatcherElement.detach();
074                                    }
075                            }
076    
077                            String webXML23 = document.formattedString();
078    
079                            int x = webXML23.indexOf("<web-app");
080                            int y = webXML23.indexOf(">", x);
081    
082                            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);
083    
084                            webXML23 = StringUtil.replace(
085                                    webXML23,
086                                    new String[] {
087                                            "<jsp-config>", "</jsp-config>"
088                                    },
089                                    new String[] {
090                                            "", ""
091                                    });
092    
093                            webXML23 = XMLFormatter.toString(webXML23);
094    
095                            FileUtil.write(output, webXML23);
096                    }
097                    catch (Exception e) {
098                            e.printStackTrace();
099                    }
100            }
101    
102    }