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.HtmlUtil;
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.UnsecureSAXReaderUtil;
024    import com.liferay.portal.servlet.filters.absoluteredirects.AbsoluteRedirectsFilter;
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     * @author Minhchau Dang
038     */
039    public class WebXMLBuilder {
040    
041            public static void main(String[] args) {
042                    InitUtil.initWithSpring();
043    
044                    if (args.length == 3) {
045                            new WebXMLBuilder(args[0], args[1], args[2]);
046                    }
047                    else {
048                            throw new IllegalArgumentException();
049                    }
050            }
051    
052            public static String organizeWebXML(String webXML)
053                    throws DocumentException, IOException {
054    
055                    webXML = HtmlUtil.stripComments(webXML);
056    
057                    Document document = UnsecureSAXReaderUtil.read(webXML);
058    
059                    Element rootElement = document.getRootElement();
060    
061                    double version = 2.3;
062    
063                    version = GetterUtil.getDouble(
064                            rootElement.attributeValue("version"), version);
065    
066                    XMLMerger xmlMerger = null;
067    
068                    if (version == 2.3) {
069                            xmlMerger = new XMLMerger(new WebXML23Descriptor());
070                    }
071                    else {
072                            xmlMerger = new XMLMerger(new WebXML24Descriptor());
073                    }
074    
075                    DocumentImpl documentImpl = (DocumentImpl)document;
076    
077                    xmlMerger.organizeXML(documentImpl.getWrappedDocument());
078    
079                    webXML = document.formattedString();
080    
081                    return webXML;
082            }
083    
084            public WebXMLBuilder(
085                    String originalWebXML, String customWebXML, String mergedWebXML) {
086    
087                    try {
088                            String customContent = getCustomContent(customWebXML);
089    
090                            String originalContent = FileUtil.read(originalWebXML);
091    
092                            String mergedContent = originalContent;
093    
094                            int x = customContent.indexOf("<filter-mapping>");
095    
096                            if (x != -1) {
097                                    int y = customContent.lastIndexOf("</filter-mapping>") + 17;
098    
099                                    String filterMappings = customContent.substring(x, y);
100    
101                                    int z = getOriginalContentIndex(originalContent);
102    
103                                    mergedContent =
104                                            mergedContent.substring(0, z) + filterMappings +
105                                                    mergedContent.substring(z);
106    
107                                    customContent =
108                                            customContent.substring(0, x) +
109                                                    customContent.substring(y + 1);
110                            }
111    
112                            int z = getMergedContentIndex(mergedContent);
113    
114                            mergedContent =
115                                    mergedContent.substring(0, z) + customContent +
116                                            mergedContent.substring(z);
117    
118                            mergedContent = organizeWebXML(mergedContent);
119    
120                            FileUtil.write(mergedWebXML, mergedContent, true);
121                    }
122                    catch (Exception e) {
123                            e.printStackTrace();
124                    }
125            }
126    
127            protected String getCustomContent(String customWebXML) throws IOException {
128                    String customContent = FileUtil.read(customWebXML);
129    
130                    int x = customContent.indexOf("<web-app");
131    
132                    x = customContent.indexOf(">", x) + 1;
133    
134                    int y = customContent.indexOf("</web-app>");
135    
136                    return customContent.substring(x, y);
137            }
138    
139            protected int getMergedContentIndex(String content) {
140                    int x = content.indexOf("<web-app");
141    
142                    x = content.indexOf(">", x) + 1;
143    
144                    return x;
145            }
146    
147            protected int getOriginalContentIndex(String content) {
148                    int x = content.indexOf(AbsoluteRedirectsFilter.class.getName());
149    
150                    if (x == -1) {
151                            x = content.indexOf("<web-app");
152                            x = content.indexOf(">", x) + 1;
153    
154                            return x;
155                    }
156    
157                    x = content.lastIndexOf("<filter-name", x);
158                    x = content.indexOf(">", x) + 1;
159    
160                    int y = content.indexOf("</filter-name>", x);
161    
162                    String filterName = content.substring(x, y);
163    
164                    x = content.lastIndexOf(filterName);
165    
166                    y = content.indexOf("</filter-mapping>", x);
167                    y = content.indexOf(">", y) + 1;
168    
169                    return y;
170            }
171    
172    }