com.liferay.source.formatter.checks.SessionKeysCheck 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.StringPool;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.kernel.util.TextFormatter;
import com.liferay.portal.kernel.util.Validator;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author Hugo Huijser
*/
public class SessionKeysCheck extends BaseFileCheck {
@Override
protected String doProcess(
String fileName, String absolutePath, String content) {
content = _fixSessionKeys(content, getPatterns());
return content;
}
protected List getPatterns() {
return Arrays.asList(sessionKeyPattern);
}
protected final Pattern sessionKeyPattern = Pattern.compile(
"SessionErrors.(?:add|contains|get)\\([^;%&|!]+|" +
"SessionMessages.(?:add|contains|get)\\([^;%&|!]+",
Pattern.MULTILINE);
private String _fixSessionKey(String content, Pattern pattern) {
Matcher matcher = pattern.matcher(content);
if (!matcher.find()) {
return content;
}
String newContent = content;
do {
String match = matcher.group();
String s = null;
if (pattern.equals(sessionKeyPattern)) {
s = StringPool.COMMA;
}
else {
s = "key=";
}
int x = match.indexOf(s);
if (x == -1) {
continue;
}
x = x + s.length();
String substring = StringUtil.trim(match.substring(x));
String quote = StringPool.BLANK;
if (substring.startsWith(StringPool.APOSTROPHE)) {
quote = StringPool.APOSTROPHE;
}
else if (substring.startsWith(StringPool.QUOTE)) {
quote = StringPool.QUOTE;
}
else {
continue;
}
int y = match.indexOf(quote, x);
int z = match.indexOf(quote, y + 1);
if ((y == -1) || (z == -1)) {
continue;
}
String prefix = match.substring(0, y + 1);
String suffix = match.substring(z);
String oldKey = match.substring(y + 1, z);
boolean alphaNumericKey = true;
for (char c : oldKey.toCharArray()) {
if (!Validator.isChar(c) && !Validator.isDigit(c) &&
(c != CharPool.DASH) && (c != CharPool.UNDERLINE)) {
alphaNumericKey = false;
}
}
if (!alphaNumericKey) {
continue;
}
String newKey = TextFormatter.format(oldKey, TextFormatter.O);
newKey = TextFormatter.format(newKey, TextFormatter.M);
if (newKey.equals(oldKey)) {
continue;
}
String oldSub = prefix.concat(oldKey).concat(suffix);
String newSub = prefix.concat(newKey).concat(suffix);
newContent = StringUtil.replaceFirst(newContent, oldSub, newSub);
}
while (matcher.find());
return newContent;
}
private String _fixSessionKeys(String content, List patterns) {
for (Pattern pattern : patterns) {
content = _fixSessionKey(content, pattern);
}
return content;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy