1   /**
2    * Copyright (c) 2000-2008 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.util.DocumentUtil;
29  import com.liferay.portal.util.HtmlImpl;
30  import com.liferay.portal.util.InitUtil;
31  import com.liferay.util.xml.XMLFormatter;
32  import com.liferay.util.xml.XMLMerger;
33  import com.liferay.util.xml.descriptor.WebXML23Descriptor;
34  import com.liferay.util.xml.descriptor.WebXML24Descriptor;
35  
36  import java.io.IOException;
37  
38  import org.dom4j.Document;
39  import org.dom4j.DocumentException;
40  import org.dom4j.Element;
41  
42  /**
43   * <a href="WebXMLBuilder.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Brian Wing Shun Chan
46   * @author Tang Ying Jian
47   * @author Brian Myunghun Kim
48   *
49   */
50  public class WebXMLBuilder {
51  
52      static {
53          InitUtil.init();
54      }
55  
56      public static void main(String[] args) {
57          if (args.length == 3) {
58              new WebXMLBuilder(args[0], args[1], args[2]);
59          }
60          else {
61              throw new IllegalArgumentException();
62          }
63      }
64  
65      public static String organizeWebXML(String webXML)
66          throws DocumentException, IOException {
67  
68          Html html = new HtmlImpl();
69  
70          webXML = html.stripComments(webXML);
71  
72          double version = 2.3;
73  
74          Document doc = DocumentUtil.readDocumentFromXML(webXML);
75  
76          Element root = doc.getRootElement();
77  
78          version = GetterUtil.getDouble(root.attributeValue("version"), version);
79  
80          XMLMerger merger = null;
81  
82          if (version == 2.3) {
83              merger = new XMLMerger(new WebXML23Descriptor());
84          }
85          else {
86              merger = new XMLMerger(new WebXML24Descriptor());
87          }
88  
89          merger.organizeXML(doc);
90  
91          webXML = XMLFormatter.toString(doc);
92  
93          return webXML;
94      }
95  
96      public WebXMLBuilder(String originalWebXML, String customWebXML,
97                           String mergedWebXML) {
98  
99          try {
100             String customContent = FileUtil.read(customWebXML);
101 
102             int x = customContent.indexOf("<web-app");
103 
104             x = customContent.indexOf(">", x) + 1;
105 
106             int y = customContent.indexOf("</web-app>");
107 
108             customContent = customContent.substring(x, y);
109 
110             String originalContent = FileUtil.read(originalWebXML);
111 
112             int z = originalContent.indexOf("<web-app");
113 
114             z = originalContent.indexOf(">", z) + 1;
115 
116             String mergedContent =
117                 originalContent.substring(0, z) + customContent +
118                     originalContent.substring(z, originalContent.length());
119 
120             mergedContent = organizeWebXML(mergedContent);
121 
122             FileUtil.write(mergedWebXML, mergedContent, true);
123         }
124         catch (Exception e) {
125             e.printStackTrace();
126         }
127     }
128 
129 }