001    /**
002     * Copyright (c) 2000-2013 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.Iterator;
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 doc = SAXReaderUtil.read(webXML24);
049    
050                            Element root = doc.getRootElement();
051    
052                            double version = GetterUtil.getDouble(
053                                    root.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                            Iterator<Element> itr1 = root.elements("filter-mapping").iterator();
066    
067                            while (itr1.hasNext()) {
068                                    Element filterMapping = itr1.next();
069    
070                                    Iterator<Element> itr2 = filterMapping.elements(
071                                            "dispatcher").iterator();
072    
073                                    while (itr2.hasNext()) {
074                                            Element dispatcher = itr2.next();
075    
076                                            dispatcher.detach();
077                                    }
078                            }
079    
080                            String webXML23 = doc.formattedString();
081    
082                            int x = webXML23.indexOf("<web-app");
083                            int y = webXML23.indexOf(">", x);
084    
085                            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);
086    
087                            webXML23 = StringUtil.replace(
088                                    webXML23,
089                                    new String[] {
090                                            "<jsp-config>", "</jsp-config>"
091                                    },
092                                    new String[] {
093                                            "", ""
094                                    });
095    
096                            webXML23 = XMLFormatter.toString(webXML23);
097    
098                            FileUtil.write(output, webXML23);
099                    }
100                    catch (Exception e) {
101                            e.printStackTrace();
102                    }
103            }
104    
105    }