1
22
23 package com.liferay.portal.tools;
24
25 import com.liferay.portal.kernel.util.GetterUtil;
26 import com.liferay.portal.kernel.util.Html;
27 import com.liferay.portal.util.DocumentUtil;
28 import com.liferay.portal.util.HtmlImpl;
29 import com.liferay.util.FileUtil;
30 import com.liferay.util.xml.XMLFormatter;
31 import com.liferay.util.xml.XMLMerger;
32 import com.liferay.util.xml.descriptor.WebXML23Descriptor;
33 import com.liferay.util.xml.descriptor.WebXML24Descriptor;
34
35 import java.io.IOException;
36
37 import org.dom4j.Document;
38 import org.dom4j.DocumentException;
39 import org.dom4j.Element;
40
41
49 public class WebXMLBuilder {
50
51 public static void main(String[] args) {
52 if (args.length == 3) {
53 new WebXMLBuilder(args[0], args[1], args[2]);
54 }
55 else {
56 throw new IllegalArgumentException();
57 }
58 }
59
60 public static String organizeWebXML(String webXML)
61 throws DocumentException, IOException {
62
63 Html html = new HtmlImpl();
64
65 webXML = html.stripComments(webXML);
66
67 double version = 2.3;
68
69 Document doc = DocumentUtil.readDocumentFromXML(webXML);
70
71 Element root = doc.getRootElement();
72
73 version = GetterUtil.getDouble(root.attributeValue("version"), version);
74
75 XMLMerger merger = null;
76
77 if (version == 2.3) {
78 merger = new XMLMerger(new WebXML23Descriptor());
79 }
80 else {
81 merger = new XMLMerger(new WebXML24Descriptor());
82 }
83
84 merger.organizeXML(doc);
85
86 webXML = XMLFormatter.toString(doc);
87
88 return webXML;
89 }
90
91 public WebXMLBuilder(String originalWebXML, String customWebXML,
92 String mergedWebXML) {
93
94 try {
95 String customContent = FileUtil.read(customWebXML);
96
97 int x = customContent.indexOf("<web-app");
98
99 x = customContent.indexOf(">", x) + 1;
100
101 int y = customContent.indexOf("</web-app>");
102
103 customContent = customContent.substring(x, y);
104
105 String originalContent = FileUtil.read(originalWebXML);
106
107 int z = originalContent.indexOf("<web-app");
108
109 z = originalContent.indexOf(">", z) + 1;
110
111 String mergedContent =
112 originalContent.substring(0, z) + customContent +
113 originalContent.substring(z, originalContent.length());
114
115 mergedContent = organizeWebXML(mergedContent);
116
117 FileUtil.write(mergedWebXML, mergedContent, true);
118 }
119 catch (Exception e) {
120 e.printStackTrace();
121 }
122 }
123
124 }