001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.tools;
022:
023: import com.liferay.portal.kernel.util.GetterUtil;
024: import com.liferay.portal.kernel.util.StringUtil;
025: import com.liferay.portal.util.PortalUtil;
026: import com.liferay.util.FileUtil;
027: import com.liferay.util.xml.XMLFormatter;
028:
029: import java.util.Iterator;
030:
031: import org.dom4j.Document;
032: import org.dom4j.Element;
033:
034: /**
035: * <a href="WebXML23Converter.java.html"><b><i>View Source</i></b></a>
036: *
037: * @author Brian Wing Shun Chan
038: *
039: */
040: public class WebXML23Converter {
041:
042: public static void main(String[] args) {
043: if (args.length == 2) {
044: new WebXML23Converter(args[0], args[1]);
045: } else {
046: throw new IllegalArgumentException();
047: }
048: }
049:
050: public WebXML23Converter(String input, String output) {
051: try {
052: String webXML24 = FileUtil.read(input);
053:
054: Document doc = PortalUtil.readDocumentFromXML(webXML24);
055:
056: Element root = doc.getRootElement();
057:
058: double version = GetterUtil.getDouble(root
059: .attributeValue("version"));
060:
061: if (version == 2.4) {
062: System.out
063: .println("Convert web.xml because it is Servlet 2.4");
064: } else {
065: System.out
066: .println("Do not convert web.xml because it is not Servlet 2.4");
067:
068: return;
069: }
070:
071: Iterator itr1 = root.elements("filter-mapping").iterator();
072:
073: while (itr1.hasNext()) {
074: Element filterMapping = (Element) itr1.next();
075:
076: Iterator itr2 = filterMapping.elements("dispatcher")
077: .iterator();
078:
079: while (itr2.hasNext()) {
080: Element dispatcher = (Element) itr2.next();
081:
082: dispatcher.detach();
083: }
084: }
085:
086: String webXML23 = XMLFormatter.toString(doc);
087:
088: int x = webXML23.indexOf("<web-app");
089: int y = webXML23.indexOf(">", x);
090:
091: webXML23 = webXML23.substring(0, x)
092: + "<!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>"
093: + webXML23.substring(y + 1, webXML23.length());
094:
095: webXML23 = StringUtil.replace(webXML23, new String[] {
096: "<jsp-config>", "</jsp-config>" }, new String[] {
097: "", "" });
098:
099: webXML23 = XMLFormatter.toString(webXML23);
100:
101: FileUtil.write(output, webXML23);
102: } catch (Exception e) {
103: e.printStackTrace();
104: }
105: }
106:
107: }
|