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