/modules/util/source-formatter/src/main/java/com/liferay/source/formatter/check/JSPVarNameCheck.java

https://github.com/danielreuther/liferay-portal · Java · 63 lines · 34 code · 13 blank · 16 comment · 6 complexity · f6ccc4bb306113ce13984e64198e6ede MD5 · raw file

  1. /**
  2. * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or modify it under
  5. * the terms of the GNU Lesser General Public License as published by the Free
  6. * Software Foundation; either version 2.1 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  12. * details.
  13. */
  14. package com.liferay.source.formatter.check;
  15. import com.liferay.petra.string.StringBundler;
  16. import com.liferay.portal.kernel.util.StringUtil;
  17. import java.util.regex.Matcher;
  18. import java.util.regex.Pattern;
  19. /**
  20. * @author Hugo Huijser
  21. */
  22. public class JSPVarNameCheck extends BaseFileCheck {
  23. @Override
  24. protected String doProcess(
  25. String fileName, String absolutePath, String content) {
  26. Matcher matcher = _varNamePattern.matcher(content);
  27. while (matcher.find()) {
  28. String varName = matcher.group(1);
  29. String expectedVarName = null;
  30. if (varName.matches(".*[a-z]Url")) {
  31. expectedVarName = StringUtil.replaceLast(varName, "Url", "URL");
  32. }
  33. else if (varName.matches(".*[a-z]Html")) {
  34. expectedVarName = StringUtil.replaceLast(
  35. varName, "Html", "HTML");
  36. }
  37. if (expectedVarName != null) {
  38. addMessage(
  39. fileName,
  40. StringBundler.concat(
  41. "Rename var '", varName, "' to '", expectedVarName,
  42. "'"),
  43. getLineNumber(content, matcher.start()));
  44. }
  45. }
  46. return content;
  47. }
  48. private static final Pattern _varNamePattern = Pattern.compile(
  49. "\\svar=\"(\\w+)\"");
  50. }