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