001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.Html;
020    import com.liferay.portal.kernel.xml.Document;
021    import com.liferay.portal.kernel.xml.DocumentException;
022    import com.liferay.portal.kernel.xml.Element;
023    import com.liferay.portal.kernel.xml.SAXReaderUtil;
024    import com.liferay.portal.util.HtmlImpl;
025    import com.liferay.portal.util.InitUtil;
026    import com.liferay.portal.xml.DocumentImpl;
027    import com.liferay.util.xml.XMLMerger;
028    import com.liferay.util.xml.descriptor.WebXML23Descriptor;
029    import com.liferay.util.xml.descriptor.WebXML24Descriptor;
030    
031    import java.io.IOException;
032    
033    /**
034     * @author Brian Wing Shun Chan
035     * @author Tang Ying Jian
036     * @author Brian Myunghun Kim
037     */
038    public class WebXMLBuilder {
039    
040            public static void main(String[] args) {
041                    InitUtil.initWithSpring();
042    
043                    if (args.length == 3) {
044                            new WebXMLBuilder(args[0], args[1], args[2]);
045                    }
046                    else {
047                            throw new IllegalArgumentException();
048                    }
049            }
050    
051            public static String organizeWebXML(String webXML)
052                    throws DocumentException, IOException {
053    
054                    Html html = new HtmlImpl();
055    
056                    webXML = html.stripComments(webXML);
057    
058                    double version = 2.3;
059    
060                    Document doc = SAXReaderUtil.read(webXML);
061    
062                    Element root = doc.getRootElement();
063    
064                    version = GetterUtil.getDouble(root.attributeValue("version"), version);
065    
066                    XMLMerger merger = null;
067    
068                    if (version == 2.3) {
069                            merger = new XMLMerger(new WebXML23Descriptor());
070                    }
071                    else {
072                            merger = new XMLMerger(new WebXML24Descriptor());
073                    }
074    
075                    DocumentImpl docImpl = (DocumentImpl)doc;
076    
077                    merger.organizeXML(docImpl.getWrappedDocument());
078    
079                    webXML = doc.formattedString();
080    
081                    return webXML;
082            }
083    
084            public WebXMLBuilder(
085                    String originalWebXML, String customWebXML, String mergedWebXML) {
086    
087                    try {
088                            String customContent = FileUtil.read(customWebXML);
089    
090                            int x = customContent.indexOf("<web-app");
091    
092                            x = customContent.indexOf(">", x) + 1;
093    
094                            int y = customContent.indexOf("</web-app>");
095    
096                            customContent = customContent.substring(x, y);
097    
098                            String originalContent = FileUtil.read(originalWebXML);
099    
100                            int z = originalContent.indexOf("<web-app");
101    
102                            z = originalContent.indexOf(">", z) + 1;
103    
104                            String mergedContent =
105                                    originalContent.substring(0, z) + customContent +
106                                            originalContent.substring(z, originalContent.length());
107    
108                            mergedContent = organizeWebXML(mergedContent);
109    
110                            FileUtil.write(mergedWebXML, mergedContent, true);
111                    }
112                    catch (Exception e) {
113                            e.printStackTrace();
114                    }
115            }
116    
117    }