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.GetterUtil;
26  import com.liferay.portal.kernel.util.Html;
27  import com.liferay.portal.util.DocumentUtil;
28  import com.liferay.portal.util.HtmlImpl;
29  import com.liferay.util.FileUtil;
30  import com.liferay.util.xml.XMLFormatter;
31  import com.liferay.util.xml.XMLMerger;
32  import com.liferay.util.xml.descriptor.WebXML23Descriptor;
33  import com.liferay.util.xml.descriptor.WebXML24Descriptor;
34  
35  import java.io.IOException;
36  
37  import org.dom4j.Document;
38  import org.dom4j.DocumentException;
39  import org.dom4j.Element;
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          if (args.length == 3) {
53              new WebXMLBuilder(args[0], args[1], args[2]);
54          }
55          else {
56              throw new IllegalArgumentException();
57          }
58      }
59  
60      public static String organizeWebXML(String webXML)
61          throws DocumentException, IOException {
62  
63          Html html = new HtmlImpl();
64  
65          webXML = html.stripComments(webXML);
66  
67          double version = 2.3;
68  
69          Document doc = DocumentUtil.readDocumentFromXML(webXML);
70  
71          Element root = doc.getRootElement();
72  
73          version = GetterUtil.getDouble(root.attributeValue("version"), version);
74  
75          XMLMerger merger = null;
76  
77          if (version == 2.3) {
78              merger = new XMLMerger(new WebXML23Descriptor());
79          }
80          else {
81              merger = new XMLMerger(new WebXML24Descriptor());
82          }
83  
84          merger.organizeXML(doc);
85  
86          webXML = XMLFormatter.toString(doc);
87  
88          return webXML;
89      }
90  
91      public WebXMLBuilder(String originalWebXML, String customWebXML,
92                           String mergedWebXML) {
93  
94          try {
95              String customContent = FileUtil.read(customWebXML);
96  
97              int x = customContent.indexOf("<web-app");
98  
99              x = customContent.indexOf(">", x) + 1;
100 
101             int y = customContent.indexOf("</web-app>");
102 
103             customContent = customContent.substring(x, y);
104 
105             String originalContent = FileUtil.read(originalWebXML);
106 
107             int z = originalContent.indexOf("<web-app");
108 
109             z = originalContent.indexOf(">", z) + 1;
110 
111             String mergedContent =
112                 originalContent.substring(0, z) + customContent +
113                     originalContent.substring(z, originalContent.length());
114 
115             mergedContent = organizeWebXML(mergedContent);
116 
117             FileUtil.write(mergedWebXML, mergedContent, true);
118         }
119         catch (Exception e) {
120             e.printStackTrace();
121         }
122     }
123 
124 }