1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class WebXMLBuilder {
49  
50      public static void main(String[] args) {
51          InitUtil.initWithSpring();
52  
53          if (args.length == 3) {
54              new WebXMLBuilder(args[0], args[1], args[2]);
55          }
56          else {
57              throw new IllegalArgumentException();
58          }
59      }
60  
61      public static String organizeWebXML(String webXML)
62          throws DocumentException, IOException {
63  
64          Html html = new HtmlImpl();
65  
66          webXML = html.stripComments(webXML);
67  
68          double version = 2.3;
69  
70          Document doc = SAXReaderUtil.read(webXML);
71  
72          Element root = doc.getRootElement();
73  
74          version = GetterUtil.getDouble(root.attributeValue("version"), version);
75  
76          XMLMerger merger = null;
77  
78          if (version == 2.3) {
79              merger = new XMLMerger(new WebXML23Descriptor());
80          }
81          else {
82              merger = new XMLMerger(new WebXML24Descriptor());
83          }
84  
85          DocumentImpl docImpl = (DocumentImpl)doc;
86  
87          merger.organizeXML(docImpl.getWrappedDocument());
88  
89          webXML = doc.formattedString();
90  
91          return webXML;
92      }
93  
94      public WebXMLBuilder(
95          String originalWebXML, String customWebXML, String mergedWebXML) {
96  
97          try {
98              String customContent = FileUtil.read(customWebXML);
99  
100             int x = customContent.indexOf("<web-app");
101 
102             x = customContent.indexOf(">", x) + 1;
103 
104             int y = customContent.indexOf("</web-app>");
105 
106             customContent = customContent.substring(x, y);
107 
108             String originalContent = FileUtil.read(originalWebXML);
109 
110             int z = originalContent.indexOf("<web-app");
111 
112             z = originalContent.indexOf(">", z) + 1;
113 
114             String mergedContent =
115                 originalContent.substring(0, z) + customContent +
116                     originalContent.substring(z, originalContent.length());
117 
118             mergedContent = organizeWebXML(mergedContent);
119 
120             FileUtil.write(mergedWebXML, mergedContent, true);
121         }
122         catch (Exception e) {
123             e.printStackTrace();
124         }
125     }
126 
127 }