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.util.PortalUtil;
025: import com.liferay.util.FileUtil;
026: import com.liferay.util.Html;
027: import com.liferay.util.xml.XMLFormatter;
028: import com.liferay.util.xml.XMLMerger;
029: import com.liferay.util.xml.descriptor.WebXML23Descriptor;
030: import com.liferay.util.xml.descriptor.WebXML24Descriptor;
031:
032: import java.io.IOException;
033:
034: import org.dom4j.Document;
035: import org.dom4j.DocumentException;
036: import org.dom4j.Element;
037:
038: /**
039: * <a href="WebXMLBuilder.java.html"><b><i>View Source</i></b></a>
040: *
041: * @author Brian Wing Shun Chan
042: * @author Tang Ying Jian
043: * @author Brian Myunghun Kim
044: *
045: */
046: public class WebXMLBuilder {
047:
048: public static void main(String[] args) {
049: if (args.length == 3) {
050: new WebXMLBuilder(args[0], args[1], args[2]);
051: } else {
052: throw new IllegalArgumentException();
053: }
054: }
055:
056: public static String organizeWebXML(String webXML)
057: throws DocumentException, IOException {
058:
059: webXML = Html.stripComments(webXML);
060:
061: double version = 2.3;
062:
063: Document doc = PortalUtil.readDocumentFromXML(webXML);
064:
065: Element root = doc.getRootElement();
066:
067: version = GetterUtil.getDouble(root.attributeValue("version"),
068: version);
069:
070: XMLMerger merger = null;
071:
072: if (version == 2.3) {
073: merger = new XMLMerger(new WebXML23Descriptor());
074: } else {
075: merger = new XMLMerger(new WebXML24Descriptor());
076: }
077:
078: merger.organizeXML(doc);
079:
080: webXML = XMLFormatter.toString(doc);
081:
082: return webXML;
083: }
084:
085: public WebXMLBuilder(String originalWebXML, String customWebXML,
086: String mergedWebXML) {
087:
088: try {
089: String customContent = FileUtil.read(customWebXML);
090:
091: int x = customContent.indexOf("<web-app");
092:
093: x = customContent.indexOf(">", x) + 1;
094:
095: int y = customContent.indexOf("</web-app>");
096:
097: customContent = customContent.substring(x, y);
098:
099: String originalContent = FileUtil.read(originalWebXML);
100:
101: int z = originalContent.indexOf("<web-app");
102:
103: z = originalContent.indexOf(">", z) + 1;
104:
105: String mergedContent = originalContent.substring(0, z)
106: + customContent
107: + originalContent.substring(z, originalContent
108: .length());
109:
110: mergedContent = organizeWebXML(mergedContent);
111:
112: FileUtil.write(mergedWebXML, mergedContent, true);
113: } catch (Exception e) {
114: e.printStackTrace();
115: }
116: }
117:
118: }
|