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