1
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
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 }