/**
 * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
 *
 * The contents of this file are subject to the terms of the Liferay Enterprise
 * Subscription License ("License"). You may not use this file except in
 * compliance with the License. You can obtain a copy of the License by
 * contacting Liferay, Inc. See the License for the specific language governing
 * permissions and limitations under the License, including but not limited to
 * distribution rights of the Software.
 *
 *
 *
 */

package com.liferay.portal.tools;

import com.liferay.portal.kernel.util.FileUtil;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.kernel.xml.Document;
import com.liferay.portal.kernel.xml.Element;
import com.liferay.portal.kernel.xml.UnsecureSAXReaderUtil;
import com.liferay.portal.util.InitUtil;
import com.liferay.util.xml.XMLFormatter;

import java.util.List;

/**
 * @author Brian Wing Shun Chan
 */
public class WebXML23Converter {

	public static void main(String[] args) {
		InitUtil.initWithSpring();

		if (args.length == 2) {
			new WebXML23Converter(args[0], args[1]);
		}
		else {
			throw new IllegalArgumentException();
		}
	}

	public WebXML23Converter(String input, String output) {
		try {
			String webXML24 = FileUtil.read(input);

			Document document = UnsecureSAXReaderUtil.read(webXML24);

			Element rootElement = document.getRootElement();

			double version = GetterUtil.getDouble(
				rootElement.attributeValue("version"));

			if (version == 2.4) {
				System.out.println("Convert web.xml because it is Servlet 2.4");
			}
			else {
				System.out.println(
					"Do not convert web.xml because it is not Servlet 2.4");

				return;
			}

			List<Element> filterMappingElements = rootElement.elements(
				"filter-mapping");

			for (Element filterMappingElement : filterMappingElements) {
				List<Element> dispatcherElements =
					filterMappingElement.elements("dispatcher");

				for (Element dispatcherElement : dispatcherElements) {
					dispatcherElement.detach();
				}
			}

			String webXML23 = document.formattedString();

			int x = webXML23.indexOf("<web-app");
			int y = webXML23.indexOf(">", x);

			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 = StringUtil.replace(
				webXML23,
				new String[] {
					"<jsp-config>", "</jsp-config>"
				},
				new String[] {
					"", ""
				});

			webXML23 = XMLFormatter.toString(webXML23);

			FileUtil.write(output, webXML23);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}

}