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