1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.tools;
16  
17  import com.liferay.portal.kernel.util.FileUtil;
18  import com.liferay.portal.kernel.util.GetterUtil;
19  import com.liferay.portal.kernel.util.Html;
20  import com.liferay.portal.kernel.xml.Document;
21  import com.liferay.portal.kernel.xml.DocumentException;
22  import com.liferay.portal.kernel.xml.Element;
23  import com.liferay.portal.kernel.xml.SAXReaderUtil;
24  import com.liferay.portal.util.HtmlImpl;
25  import com.liferay.portal.util.InitUtil;
26  import com.liferay.portal.xml.DocumentImpl;
27  import com.liferay.util.xml.XMLMerger;
28  import com.liferay.util.xml.descriptor.WebXML23Descriptor;
29  import com.liferay.util.xml.descriptor.WebXML24Descriptor;
30  
31  import java.io.IOException;
32  
33  /**
34   * <a href="WebXMLBuilder.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Brian Wing Shun Chan
37   * @author Tang Ying Jian
38   * @author Brian Myunghun Kim
39   */
40  public class WebXMLBuilder {
41  
42      public static void main(String[] args) {
43          InitUtil.initWithSpring();
44  
45          if (args.length == 3) {
46              new WebXMLBuilder(args[0], args[1], args[2]);
47          }
48          else {
49              throw new IllegalArgumentException();
50          }
51      }
52  
53      public static String organizeWebXML(String webXML)
54          throws DocumentException, IOException {
55  
56          Html html = new HtmlImpl();
57  
58          webXML = html.stripComments(webXML);
59  
60          double version = 2.3;
61  
62          Document doc = SAXReaderUtil.read(webXML);
63  
64          Element root = doc.getRootElement();
65  
66          version = GetterUtil.getDouble(root.attributeValue("version"), version);
67  
68          XMLMerger merger = null;
69  
70          if (version == 2.3) {
71              merger = new XMLMerger(new WebXML23Descriptor());
72          }
73          else {
74              merger = new XMLMerger(new WebXML24Descriptor());
75          }
76  
77          DocumentImpl docImpl = (DocumentImpl)doc;
78  
79          merger.organizeXML(docImpl.getWrappedDocument());
80  
81          webXML = doc.formattedString();
82  
83          return webXML;
84      }
85  
86      public WebXMLBuilder(
87          String originalWebXML, String customWebXML, String mergedWebXML) {
88  
89          try {
90              String customContent = FileUtil.read(customWebXML);
91  
92              int x = customContent.indexOf("<web-app");
93  
94              x = customContent.indexOf(">", x) + 1;
95  
96              int y = customContent.indexOf("</web-app>");
97  
98              customContent = customContent.substring(x, y);
99  
100             String originalContent = FileUtil.read(originalWebXML);
101 
102             int z = originalContent.indexOf("<web-app");
103 
104             z = originalContent.indexOf(">", z) + 1;
105 
106             String mergedContent =
107                 originalContent.substring(0, z) + customContent +
108                     originalContent.substring(z, originalContent.length());
109 
110             mergedContent = organizeWebXML(mergedContent);
111 
112             FileUtil.write(mergedWebXML, mergedContent, true);
113         }
114         catch (Exception e) {
115             e.printStackTrace();
116         }
117     }
118 
119 }