1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.tools;
24  
25  import com.liferay.portal.kernel.util.FileUtil;
26  import com.liferay.portal.kernel.util.GetterUtil;
27  import com.liferay.portal.kernel.util.Html;
28  import com.liferay.portal.kernel.xml.Document;
29  import com.liferay.portal.kernel.xml.DocumentException;
30  import com.liferay.portal.kernel.xml.Element;
31  import com.liferay.portal.kernel.xml.SAXReaderUtil;
32  import com.liferay.portal.util.HtmlImpl;
33  import com.liferay.portal.util.InitUtil;
34  import com.liferay.portal.xml.DocumentImpl;
35  import com.liferay.util.xml.XMLMerger;
36  import com.liferay.util.xml.descriptor.WebXML23Descriptor;
37  import com.liferay.util.xml.descriptor.WebXML24Descriptor;
38  
39  import java.io.IOException;
40  
41  /**
42   * <a href="WebXMLBuilder.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Brian Wing Shun Chan
45   * @author Tang Ying Jian
46   * @author Brian Myunghun Kim
47   *
48   */
49  public class WebXMLBuilder {
50  
51      public static void main(String[] args) {
52          InitUtil.initWithSpring();
53  
54          if (args.length == 3) {
55              new WebXMLBuilder(args[0], args[1], args[2]);
56          }
57          else {
58              throw new IllegalArgumentException();
59          }
60      }
61  
62      public static String organizeWebXML(String webXML)
63          throws DocumentException, IOException {
64  
65          Html html = new HtmlImpl();
66  
67          webXML = html.stripComments(webXML);
68  
69          double version = 2.3;
70  
71          Document doc = SAXReaderUtil.read(webXML);
72  
73          Element root = doc.getRootElement();
74  
75          version = GetterUtil.getDouble(root.attributeValue("version"), version);
76  
77          XMLMerger merger = null;
78  
79          if (version == 2.3) {
80              merger = new XMLMerger(new WebXML23Descriptor());
81          }
82          else {
83              merger = new XMLMerger(new WebXML24Descriptor());
84          }
85  
86          DocumentImpl docImpl = (DocumentImpl)doc;
87  
88          merger.organizeXML(docImpl.getWrappedDocument());
89  
90          webXML = doc.formattedString();
91  
92          return webXML;
93      }
94  
95      public WebXMLBuilder(
96          String originalWebXML, String customWebXML, String mergedWebXML) {
97  
98          try {
99              String customContent = FileUtil.read(customWebXML);
100 
101             int x = customContent.indexOf("<web-app");
102 
103             x = customContent.indexOf(">", x) + 1;
104 
105             int y = customContent.indexOf("</web-app>");
106 
107             customContent = customContent.substring(x, y);
108 
109             String originalContent = FileUtil.read(originalWebXML);
110 
111             int z = originalContent.indexOf("<web-app");
112 
113             z = originalContent.indexOf(">", z) + 1;
114 
115             String mergedContent =
116                 originalContent.substring(0, z) + customContent +
117                     originalContent.substring(z, originalContent.length());
118 
119             mergedContent = organizeWebXML(mergedContent);
120 
121             FileUtil.write(mergedWebXML, mergedContent, true);
122         }
123         catch (Exception e) {
124             e.printStackTrace();
125         }
126     }
127 
128 }