com.liferay.source.formatter.checks.XMLWebFileCheck Maven / Gradle / Ivy
/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/
package com.liferay.source.formatter.checks;
import com.liferay.petra.string.CharPool;
import com.liferay.petra.string.StringBundler;
import com.liferay.portal.kernel.util.PropertiesUtil;
import com.liferay.portal.kernel.util.PropsKeys;
import com.liferay.portal.kernel.util.StringUtil;
import java.io.IOException;
import java.util.Arrays;
import java.util.Properties;
import java.util.Set;
import java.util.TreeSet;
/**
* @author Hugo Huijser
*/
public class XMLWebFileCheck extends BaseFileCheck {
@Override
public boolean isLiferaySourceCheck() {
return true;
}
@Override
protected String doProcess(
String fileName, String absolutePath, String content)
throws IOException {
if (fileName.endsWith("portal-web/docroot/WEB-INF/web.xml")) {
content = _formatWebXML(absolutePath, content);
}
return content;
}
private String _formatWebXML(String absolutePath, String content)
throws IOException {
Properties properties = new Properties();
PropertiesUtil.load(
properties,
getPortalContent(
"portal-impl/src/portal.properties", absolutePath));
String[] locales = StringUtil.split(
properties.getProperty(PropsKeys.LOCALES));
Arrays.sort(locales);
Set urlPatterns = new TreeSet<>();
for (String locale : locales) {
int pos = locale.indexOf(CharPool.UNDERLINE);
String languageCode = locale.substring(0, pos);
urlPatterns.add(languageCode);
urlPatterns.add(locale);
urlPatterns.add(
StringUtil.replaceFirst(
locale, CharPool.UNDERLINE, CharPool.DASH));
}
StringBundler sb = new StringBundler(6 * urlPatterns.size());
for (String urlPattern : urlPatterns) {
sb.append("\t\n");
sb.append("\t\tI18n Servlet \n");
sb.append("\t\t/");
sb.append(urlPattern);
sb.append("/* \n");
sb.append("\t \n");
}
int x = content.indexOf("");
x = content.indexOf("I18n Servlet ", x);
x = content.lastIndexOf("", x) - 1;
int y = content.lastIndexOf(
"I18n Servlet ");
y = content.indexOf(" ", y) + 19;
String newContent =
content.substring(0, x) + sb.toString() + content.substring(y);
x = newContent.indexOf("");
x = newContent.indexOf("", x);
x = newContent.indexOf("", x) - 3;
y = newContent.indexOf(" ", x);
y = newContent.lastIndexOf("", y) + 15;
sb = new StringBundler((3 * urlPatterns.size()) + 1);
sb.append("\t\t\t/c/portal/protected \n");
for (String urlPattern : urlPatterns) {
sb.append("\t\t\t/");
sb.append(urlPattern);
sb.append("/c/portal/protected \n");
}
return newContent.substring(0, x) + sb.toString() +
newContent.substring(y);
}
}