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
49 public class WebXMLBuilder {
50
51 public static void main(String[] args) {
52 InitUtil.initWithSpring();
53
54 if (args.length == 3) {
55 new WebXMLBuilder(args[0], args[1], args[2]);
56 }
57 else {
58 throw new IllegalArgumentException();
59 }
60 }
61
62 public static String organizeWebXML(String webXML)
63 throws DocumentException, IOException {
64
65 Html html = new HtmlImpl();
66
67 webXML = html.stripComments(webXML);
68
69 double version = 2.3;
70
71 Document doc = SAXReaderUtil.read(webXML);
72
73 Element root = doc.getRootElement();
74
75 version = GetterUtil.getDouble(root.attributeValue("version"), version);
76
77 XMLMerger merger = null;
78
79 if (version == 2.3) {
80 merger = new XMLMerger(new WebXML23Descriptor());
81 }
82 else {
83 merger = new XMLMerger(new WebXML24Descriptor());
84 }
85
86 DocumentImpl docImpl = (DocumentImpl)doc;
87
88 merger.organizeXML(docImpl.getWrappedDocument());
89
90 webXML = doc.formattedString();
91
92 return webXML;
93 }
94
95 public WebXMLBuilder(
96 String originalWebXML, String customWebXML, String mergedWebXML) {
97
98 try {
99 String customContent = FileUtil.read(customWebXML);
100
101 int x = customContent.indexOf("<web-app");
102
103 x = customContent.indexOf(">", x) + 1;
104
105 int y = customContent.indexOf("</web-app>");
106
107 customContent = customContent.substring(x, y);
108
109 String originalContent = FileUtil.read(originalWebXML);
110
111 int z = originalContent.indexOf("<web-app");
112
113 z = originalContent.indexOf(">", z) + 1;
114
115 String mergedContent =
116 originalContent.substring(0, z) + customContent +
117 originalContent.substring(z, originalContent.length());
118
119 mergedContent = organizeWebXML(mergedContent);
120
121 FileUtil.write(mergedWebXML, mergedContent, true);
122 }
123 catch (Exception e) {
124 e.printStackTrace();
125 }
126 }
127
128 }