1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.tools;
21  
22  import com.liferay.portal.kernel.util.FileUtil;
23  import com.liferay.portal.kernel.util.GetterUtil;
24  import com.liferay.portal.kernel.util.Html;
25  import com.liferay.portal.kernel.xml.Document;
26  import com.liferay.portal.kernel.xml.DocumentException;
27  import com.liferay.portal.kernel.xml.Element;
28  import com.liferay.portal.kernel.xml.SAXReaderUtil;
29  import com.liferay.portal.util.HtmlImpl;
30  import com.liferay.portal.util.InitUtil;
31  import com.liferay.portal.xml.DocumentImpl;
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  /**
39   * <a href="WebXMLBuilder.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Brian Wing Shun Chan
42   * @author Tang Ying Jian
43   * @author Brian Myunghun Kim
44   *
45   */
46  public class WebXMLBuilder {
47  
48      public static void main(String[] args) {
49          InitUtil.initWithSpring();
50  
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          Html html = new HtmlImpl();
63  
64          webXML = html.stripComments(webXML);
65  
66          double version = 2.3;
67  
68          Document doc = SAXReaderUtil.read(webXML);
69  
70          Element root = doc.getRootElement();
71  
72          version = GetterUtil.getDouble(root.attributeValue("version"), version);
73  
74          XMLMerger merger = null;
75  
76          if (version == 2.3) {
77              merger = new XMLMerger(new WebXML23Descriptor());
78          }
79          else {
80              merger = new XMLMerger(new WebXML24Descriptor());
81          }
82  
83          DocumentImpl docImpl = (DocumentImpl)doc;
84  
85          merger.organizeXML(docImpl.getWrappedDocument());
86  
87          webXML = doc.formattedString();
88  
89          return webXML;
90      }
91  
92      public WebXMLBuilder(
93          String originalWebXML, String customWebXML, String mergedWebXML) {
94  
95          try {
96              String customContent = FileUtil.read(customWebXML);
97  
98              int x = customContent.indexOf("<web-app");
99  
100             x = customContent.indexOf(">", x) + 1;
101 
102             int y = customContent.indexOf("</web-app>");
103 
104             customContent = customContent.substring(x, y);
105 
106             String originalContent = FileUtil.read(originalWebXML);
107 
108             int z = originalContent.indexOf("<web-app");
109 
110             z = originalContent.indexOf(">", z) + 1;
111 
112             String mergedContent =
113                 originalContent.substring(0, z) + customContent +
114                     originalContent.substring(z, originalContent.length());
115 
116             mergedContent = organizeWebXML(mergedContent);
117 
118             FileUtil.write(mergedWebXML, mergedContent, true);
119         }
120         catch (Exception e) {
121             e.printStackTrace();
122         }
123     }
124 
125 }