001
014
015 package com.liferay.portal.tools;
016
017 import com.liferay.portal.kernel.util.FileUtil;
018 import com.liferay.portal.kernel.util.GetterUtil;
019 import com.liferay.portal.kernel.util.Html;
020 import com.liferay.portal.kernel.xml.Document;
021 import com.liferay.portal.kernel.xml.DocumentException;
022 import com.liferay.portal.kernel.xml.Element;
023 import com.liferay.portal.kernel.xml.SAXReaderUtil;
024 import com.liferay.portal.servlet.filters.absoluteredirects.AbsoluteRedirectsFilter;
025 import com.liferay.portal.util.HtmlImpl;
026 import com.liferay.portal.util.InitUtil;
027 import com.liferay.portal.xml.DocumentImpl;
028 import com.liferay.util.xml.XMLMerger;
029 import com.liferay.util.xml.descriptor.WebXML23Descriptor;
030 import com.liferay.util.xml.descriptor.WebXML24Descriptor;
031
032 import java.io.IOException;
033
034
040 public class WebXMLBuilder {
041
042 public static void main(String[] args) {
043 InitUtil.initWithSpring();
044
045 if (args.length == 3) {
046 new WebXMLBuilder(args[0], args[1], args[2]);
047 }
048 else {
049 throw new IllegalArgumentException();
050 }
051 }
052
053 public static String organizeWebXML(String webXML)
054 throws DocumentException, IOException {
055
056 Html html = new HtmlImpl();
057
058 webXML = html.stripComments(webXML);
059
060 Document document = SAXReaderUtil.read(webXML);
061
062 Element rootElement = document.getRootElement();
063
064 double version = 2.3;
065
066 version = GetterUtil.getDouble(
067 rootElement.attributeValue("version"), version);
068
069 XMLMerger xmlMerger = null;
070
071 if (version == 2.3) {
072 xmlMerger = new XMLMerger(new WebXML23Descriptor());
073 }
074 else {
075 xmlMerger = new XMLMerger(new WebXML24Descriptor());
076 }
077
078 DocumentImpl documentImpl = (DocumentImpl)document;
079
080 xmlMerger.organizeXML(documentImpl.getWrappedDocument());
081
082 webXML = document.formattedString();
083
084 return webXML;
085 }
086
087 public WebXMLBuilder(
088 String originalWebXML, String customWebXML, String mergedWebXML) {
089
090 try {
091 String customContent = getCustomContent(customWebXML);
092
093 String originalContent = FileUtil.read(originalWebXML);
094
095 String mergedContent = originalContent;
096
097 int x = customContent.indexOf("<filter-mapping>");
098
099 if (x != -1) {
100 int y = customContent.lastIndexOf("</filter-mapping>") + 17;
101
102 String filterMappings = customContent.substring(x, y);
103
104 int z = getOriginalContentIndex(originalContent);
105
106 mergedContent =
107 mergedContent.substring(0, z) + filterMappings +
108 mergedContent.substring(z, mergedContent.length());
109
110 customContent =
111 customContent.substring(0, x) +
112 customContent.substring(y + 1);
113 }
114
115 int z = getMergedContentIndex(mergedContent);
116
117 mergedContent =
118 mergedContent.substring(0, z) + customContent +
119 mergedContent.substring(z, mergedContent.length());
120
121 mergedContent = organizeWebXML(mergedContent);
122
123 FileUtil.write(mergedWebXML, mergedContent, true);
124 }
125 catch (Exception e) {
126 e.printStackTrace();
127 }
128 }
129
130 protected String getCustomContent(String customWebXML) throws IOException {
131 String customContent = FileUtil.read(customWebXML);
132
133 int x = customContent.indexOf("<web-app");
134
135 x = customContent.indexOf(">", x) + 1;
136
137 int y = customContent.indexOf("</web-app>");
138
139 return customContent.substring(x, y);
140 }
141
142 protected int getMergedContentIndex(String content) {
143 int x = content.indexOf("<web-app");
144
145 x = content.indexOf(">", x) + 1;
146
147 return x;
148 }
149
150 protected int getOriginalContentIndex(String content) {
151 int x = content.indexOf(AbsoluteRedirectsFilter.class.getName());
152
153 if (x == -1) {
154 x = content.indexOf("<web-app");
155 x = content.indexOf(">", x) + 1;
156
157 return x;
158 }
159 else {
160 x = content.lastIndexOf("<filter-name", x);
161 x = content.indexOf(">", x) + 1;
162
163 int y = content.indexOf("</filter-name>", x);
164
165 String filterName = content.substring(x, y);
166
167 x = content.lastIndexOf(filterName);
168
169 y = content.indexOf("</filter-mapping>", x);
170 y = content.indexOf(">", y) + 1;
171
172 return y;
173 }
174 }
175
176 }