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