List of usage examples for com.liferay.portal.kernel.util StringPool PERIOD
String PERIOD
To view the source code for com.liferay.portal.kernel.util StringPool PERIOD.
Click Source Link
From source file:com.liferay.tools.sourceformatter.CSSSourceProcessor.java
License:Open Source License
protected String fixComments(String content) { Matcher matcher = _commentPattern.matcher(content); while (matcher.find()) { String[] words = StringUtil.split(matcher.group(1), CharPool.SPACE); for (int i = 1; i < words.length; i++) { String previousWord = words[i - 1]; if (previousWord.endsWith(StringPool.PERIOD) || previousWord.equals(StringPool.SLASH)) { continue; }/*w w w . j av a 2 s.co m*/ String word = words[i]; if ((word.length() > 1) && Character.isUpperCase(word.charAt(0)) && StringUtil.isLowerCase(word.substring(1))) { content = StringUtil.replaceFirst(content, word, StringUtil.toLowerCase(word), matcher.start()); } } } return content; }
From source file:com.liferay.tools.sourceformatter.ImportPackage.java
License:Open Source License
public String getPackageLevel() { int pos = _importString.indexOf(StringPool.PERIOD); pos = _importString.indexOf(StringPool.PERIOD, pos + 1); if (pos == -1) { pos = _importString.indexOf(StringPool.PERIOD); }/*from ww w . j a v a 2 s. co m*/ return _importString.substring(0, pos); }
From source file:com.liferay.tools.sourceformatter.JavaSourceProcessor.java
License:Open Source License
protected void checkSystemEventAnnotations(String content, String fileName) throws Exception { if (!portalSource || !fileName.endsWith("PortletDataHandler.java")) { return;/* w w w.ja v a 2 s. c om*/ } int pos = content.indexOf("setDeletionSystemEventStagedModelTypes"); if (pos == -1) { return; } String deletionSystemEventStagedModelTypes = content.substring(pos, content.indexOf(");", pos)); Matcher matcher = _stagedModelTypesPattern.matcher(deletionSystemEventStagedModelTypes); while (matcher.find()) { String stagedModelTypeClassName = matcher.group(1); pos = stagedModelTypeClassName.indexOf(".class"); if (pos == -1) { pos = stagedModelTypeClassName.indexOf("Constants"); } if (pos == -1) { return; } String className = stagedModelTypeClassName.substring(0, pos); Pattern packageNamePattern = Pattern .compile("import (com\\.liferay\\.[a-zA-Z\\.]*)\\.model\\." + className + ";"); Matcher packageNameMatcher = packageNamePattern.matcher(content); if (!packageNameMatcher.find()) { return; } StringBundler sb = new StringBundler(6); sb.append(BASEDIR); sb.append(fileName.substring(0, fileName.indexOf("/src/") + 5)); sb.append(StringUtil.replace(packageNameMatcher.group(1), StringPool.PERIOD, StringPool.SLASH)); sb.append("/service/impl/"); sb.append(className); sb.append("LocalServiceImpl.java"); String localServiceImplFileName = sb.toString(); String localServiceImplContent = fileUtil.read(localServiceImplFileName); if (localServiceImplContent == null) { System.out.println("Unable to read " + localServiceImplFileName); return; } if (!localServiceImplContent.contains("@SystemEvent")) { processErrorMessage(fileName, "Missing deletion system event: " + localServiceImplFileName); } } }
From source file:com.liferay.tools.sourceformatter.JavaSourceProcessor.java
License:Open Source License
protected void checkUnprocessedExceptions(String content, File file, String packagePath, String fileName) throws IOException { List<String> importedExceptionClassNames = null; JavaDocBuilder javaDocBuilder = null; for (int lineCount = 1;;) { Matcher catchExceptionMatcher = _catchExceptionPattern.matcher(content); if (!catchExceptionMatcher.find()) { return; }// w w w . jav a 2 s . c o m String beforeCatchCode = content.substring(0, catchExceptionMatcher.start()); lineCount = lineCount + StringUtil.count(beforeCatchCode, "\n") + 1; String exceptionClassName = catchExceptionMatcher.group(2); String exceptionVariableName = catchExceptionMatcher.group(3); String tabs = catchExceptionMatcher.group(1); int pos = content.indexOf("\n" + tabs + StringPool.CLOSE_CURLY_BRACE, catchExceptionMatcher.end() - 1); String insideCatchCode = content.substring(catchExceptionMatcher.end(), pos + 1); Pattern exceptionVariablePattern = Pattern.compile("\\W" + exceptionVariableName + "\\W"); Matcher exceptionVariableMatcher = exceptionVariablePattern.matcher(insideCatchCode); if (exceptionVariableMatcher.find()) { content = content.substring(catchExceptionMatcher.start() + 1); continue; } if (javaDocBuilder == null) { javaDocBuilder = new JavaDocBuilder(); javaDocBuilder.addSource(file); } if (importedExceptionClassNames == null) { importedExceptionClassNames = getImportedExceptionClassNames(javaDocBuilder); } String originalExceptionClassName = exceptionClassName; if (!exceptionClassName.contains(StringPool.PERIOD)) { for (String exceptionClass : importedExceptionClassNames) { if (exceptionClass.endsWith(StringPool.PERIOD + exceptionClassName)) { exceptionClassName = exceptionClass; break; } } } if (!exceptionClassName.contains(StringPool.PERIOD)) { exceptionClassName = packagePath + StringPool.PERIOD + exceptionClassName; } com.thoughtworks.qdox.model.JavaClass exceptionClass = javaDocBuilder .getClassByName(exceptionClassName); while (true) { String packageName = exceptionClass.getPackageName(); if (!packageName.contains("com.liferay")) { break; } exceptionClassName = exceptionClass.getName(); if (exceptionClassName.equals("PortalException") || exceptionClassName.equals("SystemException")) { processErrorMessage(fileName, "Unprocessed " + originalExceptionClassName + ": " + fileName + " " + lineCount); break; } com.thoughtworks.qdox.model.JavaClass exceptionSuperClass = exceptionClass.getSuperJavaClass(); if (exceptionSuperClass == null) { break; } exceptionClass = exceptionSuperClass; } content = content.substring(catchExceptionMatcher.start() + 1); } }
From source file:com.liferay.tools.sourceformatter.JavaSourceProcessor.java
License:Open Source License
@Override protected String doFormat(File file, String fileName, String absolutePath, String content) throws Exception { if (isGenerated(content)) { return content; }//www .j a v a2 s. c o m String className = file.getName(); int pos = className.lastIndexOf(StringPool.PERIOD); className = className.substring(0, pos); String packagePath = fileName; int packagePathX = packagePath.indexOf("/src/"); if (packagePathX == -1) { packagePathX = packagePath.indexOf("/integration/") + 8; } int packagePathY = packagePath.lastIndexOf(StringPool.SLASH); if ((packagePathX + 5) >= packagePathY) { packagePath = StringPool.BLANK; } else { packagePath = packagePath.substring(packagePathX + 5, packagePathY); } packagePath = StringUtil.replace(packagePath, StringPool.SLASH, StringPool.PERIOD); if (packagePath.endsWith(".model")) { if (content.contains("extends " + className + "Model")) { return content; } } String newContent = content; if (newContent.contains("$\n */")) { processErrorMessage(fileName, "*: " + fileName); newContent = StringUtil.replace(newContent, "$\n */", "$\n *\n */"); } newContent = fixCopyright(newContent, absolutePath, fileName); if (newContent.contains(className + ".java.html")) { processErrorMessage(fileName, "Java2HTML: " + fileName); } if (newContent.contains(" * @author Raymond Aug") && !newContent.contains(" * @author Raymond Aug\u00e9")) { newContent = newContent.replaceFirst("Raymond Aug.++", "Raymond Aug\u00e9"); processErrorMessage(fileName, "UTF-8: " + fileName); } newContent = fixDataAccessConnection(className, newContent); newContent = fixSessionKey(fileName, newContent, sessionKeyPattern); newContent = StringUtil.replace(newContent, new String[] { "com.liferay.portal.PortalException", "com.liferay.portal.SystemException", "com.liferay.util.LocalizationUtil" }, new String[] { "com.liferay.portal.kernel.exception.PortalException", "com.liferay.portal.kernel.exception.SystemException", "com.liferay.portal.kernel.util.LocalizationUtil" }); newContent = StringUtil.replace(newContent, " final static ", " static final "); newContent = fixCompatClassImports(absolutePath, newContent); newContent = stripJavaImports(newContent, packagePath, className); newContent = StringUtil.replace(newContent, new String[] { ";\n/**", "\t/*\n\t *", "catch(", "else{", "if(", "for(", "while(", "List <", "){\n", "]{\n", ";;\n" }, new String[] { ";\n\n/**", "\t/**\n\t *", "catch (", "else {", "if (", "for (", "while (", "List<", ") {\n", "] {\n", ";\n" }); while (true) { Matcher matcher = _incorrectLineBreakPattern.matcher(newContent); if (!matcher.find()) { break; } newContent = StringUtil.replaceFirst(newContent, StringPool.NEW_LINE, StringPool.BLANK, matcher.start()); } newContent = sortAnnotations(newContent, StringPool.BLANK); Matcher matcher = _logPattern.matcher(newContent); if (matcher.find()) { String logClassName = matcher.group(1); if (!logClassName.equals(className)) { newContent = StringUtil.replaceLast(newContent, logClassName + ".class)", className + ".class)"); } } if (!isExcluded(_staticLogVariableExclusions, absolutePath)) { newContent = StringUtil.replace(newContent, "private Log _log", "private static final Log _log"); } if (newContent.contains("*/\npackage ")) { processErrorMessage(fileName, "package: " + fileName); } if (!newContent.endsWith("\n\n}") && !newContent.endsWith("{\n}")) { processErrorMessage(fileName, "}: " + fileName); } if (portalSource && !_allowUseServiceUtilInServiceImpl && !className.equals("BaseServiceImpl") && className.endsWith("ServiceImpl") && newContent.contains("ServiceUtil.")) { processErrorMessage(fileName, "ServiceUtil: " + fileName); } // LPS-34911 if (portalSource && !isExcluded(_upgradeServiceUtilExclusions, absolutePath) && fileName.contains("/portal/upgrade/") && !fileName.contains("/test/") && newContent.contains("ServiceUtil.")) { processErrorMessage(fileName, "ServiceUtil: " + fileName); } if (!isRunsOutsidePortal(absolutePath) && !isExcluded(_proxyExclusions, absolutePath) && newContent.contains("import java.lang.reflect.Proxy;")) { processErrorMessage(fileName, "Proxy: " + fileName); } if (newContent.contains("import edu.emory.mathcs.backport.java")) { processErrorMessage(fileName, "edu.emory.mathcs.backport.java: " + fileName); } if (newContent.contains("import jodd.util.StringPool")) { processErrorMessage(fileName, "jodd.util.StringPool: " + fileName); } // LPS-45027 if (newContent.contains("com.liferay.portal.kernel.util.UnmodifiableList")) { processErrorMessage(fileName, "Use java.util.Collections.unmodifiableList instead of " + "com.liferay.portal.kernel.util.UnmodifiableList: " + fileName); } // LPS-28266 for (int pos1 = -1;;) { pos1 = newContent.indexOf(StringPool.TAB + "try {", pos1 + 1); if (pos1 == -1) { break; } int pos2 = newContent.indexOf(StringPool.TAB + "try {", pos1 + 1); int pos3 = newContent.indexOf("\"select count(", pos1); if ((pos2 != -1) && (pos3 != -1) && (pos2 < pos3)) { continue; } int pos4 = newContent.indexOf("rs.getLong(1)", pos1); int pos5 = newContent.indexOf(StringPool.TAB + "finally {", pos1); if ((pos3 == -1) || (pos4 == -1) || (pos5 == -1)) { break; } if ((pos3 < pos4) && (pos4 < pos5)) { processErrorMessage(fileName, "Use getInt(1) for count: " + fileName); } } // LPS-33070 matcher = _processCallablePattern.matcher(content); if (matcher.find() && !content.contains("private static final long serialVersionUID")) { processErrorMessage(fileName, "Assign ProcessCallable implementation a serialVersionUID: " + fileName); } checkLanguageKeys(fileName, newContent, languageKeyPattern); newContent = StringUtil.replace(newContent, StringPool.TAB + "for (;;) {", StringPool.TAB + "while (true) {"); // LPS-36174 if (_checkUnprocessedExceptions && !fileName.contains("/test/")) { checkUnprocessedExceptions(newContent, file, packagePath, fileName); } // LPS-39508 if (!isExcluded(_secureRandomExclusions, absolutePath) && !isRunsOutsidePortal(absolutePath) && content.contains("java.security.SecureRandom") && !content.contains("javax.crypto.KeyGenerator")) { processErrorMessage(fileName, "Use SecureRandomUtil or com.liferay.portal.kernel.security." + "SecureRandom instead of java.security.SecureRandom: " + fileName); } // LPS-41315 checkLogLevel(newContent, fileName, "debug"); checkLogLevel(newContent, fileName, "info"); checkLogLevel(newContent, fileName, "trace"); checkLogLevel(newContent, fileName, "warn"); // LPS-46632 checkSystemEventAnnotations(newContent, fileName); // LPS-41205 if (fileName.contains("/upgrade/") && newContent.contains("LocaleUtil.getDefault()")) { processErrorMessage(fileName, "Use UpgradeProcessUtil.getDefaultLanguageId(companyId) " + "instead of LocaleUtil.getDefault(): " + fileName); } // LPS-46017 newContent = StringUtil.replace(newContent, " static interface ", " interface "); // LPS-47055 newContent = fixSystemExceptions(newContent); // LPS-47648 if (portalSource && fileName.contains("/test/integration/")) { newContent = StringUtil.replace(newContent, "FinderCacheUtil.clearCache();", StringPool.BLANK); } // LPS-47682 newContent = fixIncorrectParameterTypeForLanguageUtil(newContent, false, fileName); if (portalSource && fileName.contains("/portal-service/") && content.contains("import javax.servlet.jsp.")) { processErrorMessage(fileName, "Never import javax.servlet.jsp.* from portal-service " + fileName); } // LPS-48153 //newContent = applyDiamondOperator(newContent); // LPS-49552 checkFinderCacheInterfaceMethod(fileName, newContent); newContent = fixIncorrectEmptyLineBeforeCloseCurlyBrace(newContent, fileName); pos = newContent.indexOf("\npublic "); if (pos != -1) { String javaClassContent = newContent.substring(pos + 1); String beforeJavaClass = newContent.substring(0, pos + 1); int javaClassLineCount = StringUtil.count(beforeJavaClass, "\n") + 1; newContent = formatJavaTerms(className, fileName, absolutePath, newContent, javaClassContent, javaClassLineCount, _checkJavaFieldTypesExclusions, _javaTermAccessLevelModifierExclusions, _javaTermSortExclusions, _testAnnotationsExclusions); } newContent = formatJava(fileName, absolutePath, newContent); return StringUtil.replace(newContent, "\n\n\n", "\n\n"); }
From source file:com.liferay.tools.sourceformatter.JavaSourceProcessor.java
License:Open Source License
protected String formatJava(String fileName, String absolutePath, String content) throws Exception { StringBundler sb = new StringBundler(); try (UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader( new UnsyncStringReader(content))) { String line = null;//from w ww .ja va 2 s .co m String previousLine = StringPool.BLANK; int lineCount = 0; String ifClause = StringPool.BLANK; String packageName = StringPool.BLANK; String regexPattern = StringPool.BLANK; while ((line = unsyncBufferedReader.readLine()) != null) { lineCount++; line = trimLine(line, false); if (line.startsWith("package ")) { packageName = line.substring(8, line.length() - 1); } if (line.startsWith("import ")) { if (line.endsWith(".*;")) { processErrorMessage(fileName, "import: " + fileName + " " + lineCount); } int pos = line.lastIndexOf(StringPool.PERIOD); if (pos != -1) { String importPackageName = line.substring(7, pos); if (importPackageName.equals(packageName)) { continue; } } } if (line.contains(StringPool.TAB + "for (") && line.contains(":") && !line.contains(" :")) { line = StringUtil.replace(line, ":", " :"); } // LPS-42924 if (line.contains("PortalUtil.getClassNameId(") && fileName.endsWith("ServiceImpl.java")) { processErrorMessage(fileName, "Use classNameLocalService.getClassNameId: " + fileName + " " + lineCount); } // LPS-42599 if (!isExcluded(_hibernateSQLQueryExclusions, absolutePath) && line.contains("= session.createSQLQuery(") && content.contains("com.liferay.portal.kernel.dao.orm.Session")) { line = StringUtil.replace(line, "createSQLQuery", "createSynchronizedSQLQuery"); } line = replacePrimitiveWrapperInstantiation(fileName, line, lineCount); String trimmedLine = StringUtil.trimLeading(line); // LPS-45649 if (trimmedLine.startsWith("throw new IOException(") && line.contains("e.getMessage()")) { line = StringUtil.replace(line, ".getMessage()", StringPool.BLANK); } // LPS-45492 if (trimmedLine.contains("StopWatch stopWatch = null;")) { processErrorMessage(fileName, "Do not set stopwatch to null: " + fileName + " " + lineCount); } checkStringBundler(trimmedLine, fileName, lineCount); checkEmptyCollection(trimmedLine, fileName, lineCount); if (trimmedLine.startsWith("* @deprecated") && _addMissingDeprecationReleaseVersion) { if (!trimmedLine.startsWith("* @deprecated As of ")) { line = StringUtil.replace(line, "* @deprecated", "* @deprecated As of " + getMainReleaseVersion()); } else { String version = trimmedLine.substring(20); version = StringUtil.split(version, StringPool.SPACE)[0]; version = StringUtil.replace(version, StringPool.COMMA, StringPool.BLANK); if (StringUtil.count(version, StringPool.PERIOD) == 1) { line = StringUtil.replaceFirst(line, version, version + ".0"); } } } if (trimmedLine.startsWith("* @see ") && (StringUtil.count(trimmedLine, StringPool.AT) > 1)) { processErrorMessage(fileName, "Do not use @see with another annotation: " + fileName + " " + lineCount); } checkInefficientStringMethods(line, fileName, absolutePath, lineCount); if (trimmedLine.startsWith(StringPool.EQUAL)) { processErrorMessage(fileName, "line break: " + fileName + " " + lineCount); } if (line.contains("ActionForm form")) { processErrorMessage(fileName, "Rename form to actionForm: " + fileName + " " + lineCount); } if (line.contains("ActionMapping mapping")) { processErrorMessage(fileName, "Rename mapping to ActionMapping: " + fileName + " " + lineCount); } if (fileName.contains("/upgrade/") && line.contains("rs.getDate(")) { processErrorMessage(fileName, "Use rs.getTimeStamp: " + fileName + " " + lineCount); } if (!trimmedLine.equals("{") && line.endsWith("{") && !line.endsWith(" {")) { line = StringUtil.replaceLast(line, "{", " {"); } line = sortExceptions(line); if (trimmedLine.startsWith("if (") || trimmedLine.startsWith("else if (") || trimmedLine.startsWith("while (") || Validator.isNotNull(ifClause)) { ifClause = ifClause + line + StringPool.NEW_LINE; if (line.endsWith(") {")) { String newIfClause = checkIfClause(ifClause, fileName, lineCount); if (!ifClause.equals(newIfClause) && content.contains(ifClause)) { return StringUtil.replace(content, ifClause, newIfClause); } ifClause = StringPool.BLANK; } else if (line.endsWith(StringPool.SEMICOLON)) { ifClause = StringPool.BLANK; } } if (trimmedLine.startsWith("Pattern ") || Validator.isNotNull(regexPattern)) { regexPattern = regexPattern + trimmedLine; if (trimmedLine.endsWith(");")) { // LPS-41084 checkRegexPattern(regexPattern, fileName, lineCount); regexPattern = StringPool.BLANK; } } if (!trimmedLine.contains(StringPool.DOUBLE_SLASH) && !trimmedLine.startsWith(StringPool.STAR)) { String strippedQuotesLine = stripQuotes(trimmedLine, CharPool.QUOTE); for (int x = 0;;) { x = strippedQuotesLine.indexOf(StringPool.EQUAL, x + 1); if (x == -1) { break; } char c = strippedQuotesLine.charAt(x - 1); if (Character.isLetterOrDigit(c)) { line = StringUtil.replace(line, c + "=", c + " ="); break; } if (x == (strippedQuotesLine.length() - 1)) { break; } c = strippedQuotesLine.charAt(x + 1); if (Character.isLetterOrDigit(c)) { line = StringUtil.replace(line, "=" + c, "= " + c); break; } } while (trimmedLine.contains(StringPool.TAB)) { line = StringUtil.replaceLast(line, StringPool.TAB, StringPool.SPACE); trimmedLine = StringUtil.replaceLast(trimmedLine, StringPool.TAB, StringPool.SPACE); } if (line.contains(StringPool.TAB + StringPool.SPACE) && !previousLine.endsWith("&&") && !previousLine.endsWith("||") && !previousLine.contains(StringPool.TAB + "((") && !previousLine.contains(StringPool.TAB + StringPool.LESS_THAN) && !previousLine.contains(StringPool.TAB + StringPool.SPACE) && !previousLine.contains(StringPool.TAB + "for (") && !previousLine.contains(StringPool.TAB + "implements ") && !previousLine.contains(StringPool.TAB + "throws ")) { line = StringUtil.replace(line, StringPool.TAB + StringPool.SPACE, StringPool.TAB); } while (trimmedLine.contains(StringPool.DOUBLE_SPACE) && !trimmedLine.contains(StringPool.QUOTE + StringPool.DOUBLE_SPACE) && !fileName.contains("Test")) { line = StringUtil.replaceLast(line, StringPool.DOUBLE_SPACE, StringPool.SPACE); trimmedLine = StringUtil.replaceLast(trimmedLine, StringPool.DOUBLE_SPACE, StringPool.SPACE); } if (!line.contains(StringPool.QUOTE)) { int pos = line.indexOf(") "); if (pos != -1) { String linePart = line.substring(pos + 2); if (Character.isLetter(linePart.charAt(0)) && !linePart.startsWith("default") && !linePart.startsWith("instanceof") && !linePart.startsWith("throws")) { line = StringUtil.replaceLast(line, StringPool.SPACE + linePart, linePart); } } if ((trimmedLine.startsWith("private ") || trimmedLine.startsWith("protected ") || trimmedLine.startsWith("public ")) && !line.contains(StringPool.EQUAL) && line.contains(" (")) { line = StringUtil.replace(line, " (", "("); } if (line.contains(" [")) { line = StringUtil.replace(line, " [", "["); } for (int x = -1;;) { int posComma = line.indexOf(StringPool.COMMA, x + 1); int posSemicolon = line.indexOf(StringPool.SEMICOLON, x + 1); if ((posComma == -1) && (posSemicolon == -1)) { break; } x = Math.min(posComma, posSemicolon); if (x == -1) { x = Math.max(posComma, posSemicolon); } if (line.length() > (x + 1)) { char nextChar = line.charAt(x + 1); if ((nextChar != CharPool.APOSTROPHE) && (nextChar != CharPool.CLOSE_PARENTHESIS) && (nextChar != CharPool.SPACE) && (nextChar != CharPool.STAR)) { line = StringUtil.insert(line, StringPool.SPACE, x + 1); } } if (x > 0) { char previousChar = line.charAt(x - 1); if (previousChar == CharPool.SPACE) { line = line.substring(0, x - 1).concat(line.substring(x)); } } } } if ((line.contains(" && ") || line.contains(" || ")) && line.endsWith(StringPool.OPEN_PARENTHESIS)) { processErrorMessage(fileName, "line break: " + fileName + " " + lineCount); } if (trimmedLine.endsWith(StringPool.PLUS) && !trimmedLine.startsWith(StringPool.OPEN_PARENTHESIS)) { int closeParenthesisCount = StringUtil.count(strippedQuotesLine, StringPool.CLOSE_PARENTHESIS); int openParenthesisCount = StringUtil.count(strippedQuotesLine, StringPool.OPEN_PARENTHESIS); if (openParenthesisCount > closeParenthesisCount) { processErrorMessage(fileName, "line break: " + fileName + " " + lineCount); } } int x = strippedQuotesLine.indexOf(", "); if (x != -1) { String linePart = strippedQuotesLine.substring(0, x); int closeParenthesisCount = StringUtil.count(linePart, StringPool.CLOSE_PARENTHESIS); int openParenthesisCount = StringUtil.count(linePart, StringPool.OPEN_PARENTHESIS); if (closeParenthesisCount > openParenthesisCount) { processErrorMessage(fileName, "line break: " + fileName + " " + lineCount); } } else if (trimmedLine.endsWith(StringPool.COMMA) && !trimmedLine.startsWith("for (")) { int closeParenthesisCount = StringUtil.count(strippedQuotesLine, StringPool.CLOSE_PARENTHESIS); int openParenthesisCount = StringUtil.count(strippedQuotesLine, StringPool.OPEN_PARENTHESIS); if (closeParenthesisCount < openParenthesisCount) { processErrorMessage(fileName, "line break: " + fileName + " " + lineCount); } } if (line.contains(StringPool.COMMA) && !line.contains(StringPool.CLOSE_PARENTHESIS) && !line.contains(StringPool.GREATER_THAN) && !line.contains(StringPool.QUOTE) && line.endsWith(StringPool.OPEN_PARENTHESIS)) { processErrorMessage(fileName, "line break: " + fileName + " " + lineCount); } if (line.endsWith(" +") || line.endsWith(" -") || line.endsWith(" *") || line.endsWith(" /")) { x = line.indexOf(" = "); if (x != -1) { int y = line.indexOf(StringPool.QUOTE); if ((y == -1) || (x < y)) { processErrorMessage(fileName, "line break: " + fileName + " " + lineCount); } } } if (line.endsWith(" throws") || (previousLine.endsWith(StringPool.OPEN_PARENTHESIS) && line.contains(" throws ") && line.endsWith(StringPool.OPEN_CURLY_BRACE))) { processErrorMessage(fileName, "line break: " + fileName + " " + lineCount); } if (trimmedLine.startsWith(StringPool.PERIOD) || (line.endsWith(StringPool.PERIOD) && line.contains(StringPool.EQUAL))) { processErrorMessage(fileName, "line break: " + fileName + " " + lineCount); } if (trimmedLine.startsWith(StringPool.CLOSE_CURLY_BRACE) && line.endsWith(StringPool.OPEN_CURLY_BRACE)) { Matcher matcher = _lineBreakPattern.matcher(trimmedLine); if (!matcher.find()) { processErrorMessage(fileName, "line break: " + fileName + " " + lineCount); } } } if (line.contains(" ") && !line.matches("\\s*\\*.*")) { if (!fileName.endsWith("StringPool.java")) { processErrorMessage(fileName, "tab: " + fileName + " " + lineCount); } } if (line.contains(" {") && !line.matches("\\s*\\*.*")) { processErrorMessage(fileName, "{:" + fileName + " " + lineCount); } int lineLength = getLineLength(line); if (!line.startsWith("import ") && !line.startsWith("package ") && !line.matches("\\s*\\*.*")) { if (fileName.endsWith("Table.java") && line.contains("String TABLE_SQL_CREATE = ")) { } else if (fileName.endsWith("Table.java") && line.contains("String TABLE_SQL_DROP = ")) { } else if (fileName.endsWith("Table.java") && line.contains(" index IX_")) { } else if (lineLength > _MAX_LINE_LENGTH) { if (!isExcluded(_lineLengthExclusions, absolutePath, lineCount) && !isAnnotationParameter(content, trimmedLine)) { String truncateLongLinesContent = getTruncateLongLinesContent(content, line, trimmedLine, lineCount); if (truncateLongLinesContent != null) { return truncateLongLinesContent; } processErrorMessage(fileName, "> 80: " + fileName + " " + lineCount); } } else { int lineLeadingTabCount = getLeadingTabCount(line); int previousLineLeadingTabCount = getLeadingTabCount(previousLine); if (!trimmedLine.startsWith("//")) { if (previousLine.endsWith(StringPool.COMMA) && previousLine.contains(StringPool.OPEN_PARENTHESIS) && !previousLine.contains("for (") && (lineLeadingTabCount > previousLineLeadingTabCount)) { processErrorMessage(fileName, "line break: " + fileName + " " + lineCount); } if ((lineLeadingTabCount == previousLineLeadingTabCount) && (previousLine.endsWith(StringPool.EQUAL) || previousLine.endsWith(StringPool.OPEN_PARENTHESIS))) { processErrorMessage(fileName, "tab: " + fileName + " " + lineCount); } if (Validator.isNotNull(trimmedLine)) { if (((previousLine.endsWith(StringPool.COLON) && previousLine.contains(StringPool.TAB + "for ")) || (previousLine.endsWith(StringPool.OPEN_PARENTHESIS) && previousLine.contains(StringPool.TAB + "if "))) && ((previousLineLeadingTabCount + 2) != lineLeadingTabCount)) { processErrorMessage(fileName, "line break: " + fileName + " " + lineCount); } if (previousLine.endsWith(StringPool.OPEN_CURLY_BRACE) && !trimmedLine.startsWith(StringPool.CLOSE_CURLY_BRACE) && ((previousLineLeadingTabCount + 1) != lineLeadingTabCount)) { processErrorMessage(fileName, "tab: " + fileName + " " + lineCount); } } if (previousLine.endsWith(StringPool.PERIOD)) { int x = trimmedLine.indexOf(StringPool.OPEN_PARENTHESIS); if ((x != -1) && ((getLineLength(previousLine) + x) < _MAX_LINE_LENGTH) && (trimmedLine.endsWith(StringPool.OPEN_PARENTHESIS) || (trimmedLine.charAt(x + 1) != CharPool.CLOSE_PARENTHESIS))) { processErrorMessage(fileName, "line break: " + fileName + " " + lineCount); } } int diff = lineLeadingTabCount - previousLineLeadingTabCount; if (trimmedLine.startsWith("throws ") && ((diff == 0) || (diff > 1))) { processErrorMessage(fileName, "tab: " + fileName + " " + lineCount); } if ((diff == 2) && (previousLineLeadingTabCount > 0) && line.endsWith(StringPool.SEMICOLON) && !previousLine.contains(StringPool.TAB + "try (")) { line = StringUtil.replaceFirst(line, StringPool.TAB, StringPool.BLANK); } if ((previousLine.contains(" class ") || previousLine.contains(" enum ")) && previousLine.endsWith(StringPool.OPEN_CURLY_BRACE) && Validator.isNotNull(line) && !trimmedLine.startsWith(StringPool.CLOSE_CURLY_BRACE)) { processErrorMessage(fileName, "line break: " + fileName + " " + lineCount); } } String combinedLinesContent = getCombinedLinesContent(content, fileName, absolutePath, line, trimmedLine, lineLength, lineCount, previousLine, lineLeadingTabCount, previousLineLeadingTabCount); if (combinedLinesContent != null) { return combinedLinesContent; } } } if (lineCount > 1) { sb.append(previousLine); if (Validator.isNotNull(previousLine) && Validator.isNotNull(trimmedLine) && !previousLine.contains("/*") && !previousLine.endsWith("*/")) { String trimmedPreviousLine = StringUtil.trimLeading(previousLine); if ((trimmedPreviousLine.startsWith("// ") && !trimmedLine.startsWith("// ")) || (!trimmedPreviousLine.startsWith("// ") && trimmedLine.startsWith("// "))) { sb.append("\n"); } else if (!trimmedPreviousLine.endsWith(StringPool.OPEN_CURLY_BRACE) && !trimmedPreviousLine.endsWith(StringPool.COLON) && (trimmedLine.startsWith("for (") || trimmedLine.startsWith("if ("))) { sb.append("\n"); } else if (previousLine.endsWith(StringPool.TAB + StringPool.CLOSE_CURLY_BRACE) && !trimmedLine.startsWith(StringPool.CLOSE_CURLY_BRACE) && !trimmedLine.startsWith(StringPool.CLOSE_PARENTHESIS) && !trimmedLine.startsWith(StringPool.DOUBLE_SLASH) && !trimmedLine.equals("*/") && !trimmedLine.startsWith("catch ") && !trimmedLine.startsWith("else ") && !trimmedLine.startsWith("finally ") && !trimmedLine.startsWith("while ")) { sb.append("\n"); } } sb.append("\n"); } previousLine = line; } sb.append(previousLine); } String newContent = sb.toString(); if (newContent.endsWith("\n")) { newContent = newContent.substring(0, newContent.length() - 1); } return newContent; }
From source file:com.liferay.tools.sourceformatter.JavaSourceProcessor.java
License:Open Source License
protected String getCombinedLinesContent(String content, String fileName, String absolutePath, String line, String trimmedLine, int lineLength, int lineCount, String previousLine, int lineTabCount, int previousLineTabCount) { if (Validator.isNull(line) || Validator.isNull(previousLine) || isExcluded(_fitOnSingleLineExclusions, absolutePath, lineCount)) { return null; }//w w w.j a v a 2 s .c o m String trimmedPreviousLine = StringUtil.trimLeading(previousLine); if (line.contains("// ") || line.contains("*/") || line.contains("*/") || previousLine.contains("// ") || previousLine.contains("*/") || previousLine.contains("*/")) { return null; } int tabDiff = lineTabCount - previousLineTabCount; if (previousLine.endsWith(" extends")) { return getCombinedLinesContent(content, fileName, line, trimmedLine, lineLength, lineCount, previousLine, "extends", tabDiff, false, false, false); } if (previousLine.endsWith(" implements")) { return getCombinedLinesContent(content, fileName, line, trimmedLine, lineLength, lineCount, previousLine, "implements ", tabDiff, false, false, false); } if (trimmedLine.startsWith("+ ") || trimmedLine.startsWith("- ") || trimmedLine.startsWith("|| ") || trimmedLine.startsWith("&& ")) { int pos = trimmedLine.indexOf(StringPool.SPACE); String linePart = trimmedLine.substring(0, pos); return getCombinedLinesContent(content, fileName, line, trimmedLine, lineLength, lineCount, previousLine, linePart, tabDiff, true, true, false); } int previousLineLength = getLineLength(previousLine); if ((trimmedLine.length() + previousLineLength) < _MAX_LINE_LENGTH) { if (trimmedPreviousLine.startsWith("for ") && previousLine.endsWith(StringPool.COLON) && line.endsWith(StringPool.OPEN_CURLY_BRACE)) { return getCombinedLinesContent(content, fileName, line, trimmedLine, lineLength, lineCount, previousLine, null, tabDiff, false, true, false); } if (line.endsWith(StringPool.SEMICOLON) && !previousLine.endsWith(StringPool.COLON) && !previousLine.endsWith(StringPool.OPEN_BRACKET) && !previousLine.endsWith(StringPool.OPEN_CURLY_BRACE) && !previousLine.endsWith(StringPool.OPEN_PARENTHESIS) && !previousLine.endsWith(StringPool.PERIOD) && (lineTabCount == (previousLineTabCount + 1))) { return getCombinedLinesContent(content, fileName, line, trimmedLine, lineLength, lineCount, previousLine, null, tabDiff, false, true, false); } if ((trimmedPreviousLine.startsWith("if ") || trimmedPreviousLine.startsWith("else ")) && (previousLine.endsWith("||") || previousLine.endsWith("&&")) && line.endsWith(StringPool.OPEN_CURLY_BRACE)) { return getCombinedLinesContent(content, fileName, line, trimmedLine, lineLength, lineCount, previousLine, null, tabDiff, false, true, false); } if ((trimmedLine.startsWith("extends ") || trimmedLine.startsWith("implements ") || trimmedLine.startsWith("throws")) && (line.endsWith(StringPool.OPEN_CURLY_BRACE) || line.endsWith(StringPool.SEMICOLON)) && (lineTabCount == (previousLineTabCount + 1))) { return getCombinedLinesContent(content, fileName, line, trimmedLine, lineLength, lineCount, previousLine, null, tabDiff, false, true, false); } if (previousLine.endsWith(StringPool.EQUAL) && line.endsWith(StringPool.OPEN_PARENTHESIS)) { String nextLine = getNextLine(content, lineCount); if (nextLine.endsWith(StringPool.SEMICOLON)) { return getCombinedLinesContent(content, fileName, line, trimmedLine, lineLength, lineCount, previousLine, null, tabDiff, false, true, true); } } } if (((trimmedLine.length() + previousLineLength) <= _MAX_LINE_LENGTH) && (previousLine.endsWith(StringPool.OPEN_BRACKET) || previousLine.endsWith(StringPool.OPEN_PARENTHESIS) || previousLine.endsWith(StringPool.PERIOD)) && line.endsWith(StringPool.SEMICOLON)) { return getCombinedLinesContent(content, fileName, line, trimmedLine, lineLength, lineCount, previousLine, null, tabDiff, false, false, false); } if (previousLine.endsWith(StringPool.EQUAL) && line.endsWith(StringPool.SEMICOLON)) { String tempLine = trimmedLine; for (int pos = 0;;) { pos = tempLine.indexOf(StringPool.DASH); if (pos == -1) { pos = tempLine.indexOf(StringPool.PLUS); } if (pos == -1) { pos = tempLine.indexOf(StringPool.SLASH); } if (pos == -1) { pos = tempLine.indexOf(StringPool.STAR); } if (pos == -1) { pos = tempLine.indexOf("||"); } if (pos == -1) { pos = tempLine.indexOf("&&"); } if (pos == -1) { break; } String linePart = tempLine.substring(0, pos); int openParenthesisCount = StringUtil.count(linePart, StringPool.OPEN_PARENTHESIS); int closeParenthesisCount = StringUtil.count(linePart, StringPool.CLOSE_PARENTHESIS); if (openParenthesisCount == closeParenthesisCount) { return null; } tempLine = tempLine.substring(0, pos) + tempLine.substring(pos + 1); } int x = trimmedLine.indexOf(StringPool.OPEN_PARENTHESIS); if (x == 0) { x = trimmedLine.indexOf(StringPool.OPEN_PARENTHESIS, 1); } if (x != -1) { int y = trimmedLine.indexOf(StringPool.CLOSE_PARENTHESIS, x); int z = trimmedLine.indexOf(StringPool.QUOTE); if (((x + 1) != y) && ((z == -1) || (z > x))) { char previousChar = trimmedLine.charAt(x - 1); if ((previousChar != CharPool.CLOSE_PARENTHESIS) && (previousChar != CharPool.OPEN_PARENTHESIS) && (previousChar != CharPool.SPACE) && (previousLineLength + 1 + x) < _MAX_LINE_LENGTH) { String linePart = trimmedLine.substring(0, x + 1); if (linePart.startsWith(StringPool.OPEN_PARENTHESIS) && !linePart.contains(StringPool.CLOSE_PARENTHESIS)) { return null; } return getCombinedLinesContent(content, fileName, line, trimmedLine, lineLength, lineCount, previousLine, linePart, tabDiff, true, true, false); } } } } if (previousLine.endsWith(StringPool.COMMA) && (previousLineTabCount == lineTabCount) && !previousLine.contains(StringPool.CLOSE_CURLY_BRACE) && (line.endsWith(") {") || !line.endsWith(StringPool.OPEN_CURLY_BRACE))) { int x = trimmedLine.indexOf(StringPool.COMMA); if (x != -1) { while ((previousLineLength + 1 + x) < _MAX_LINE_LENGTH) { String linePart = trimmedLine.substring(0, x + 1); if (isValidJavaParameter(linePart)) { if (trimmedLine.equals(linePart)) { return getCombinedLinesContent(content, fileName, line, trimmedLine, lineLength, lineCount, previousLine, null, tabDiff, false, true, false); } else { return getCombinedLinesContent(content, fileName, line, trimmedLine, lineLength, lineCount, previousLine, linePart + StringPool.SPACE, tabDiff, true, true, false); } } String partAfterComma = trimmedLine.substring(x + 1); int pos = partAfterComma.indexOf(StringPool.COMMA); if (pos == -1) { break; } x = x + pos + 1; } } else if (!line.endsWith(StringPool.OPEN_PARENTHESIS) && !line.endsWith(StringPool.PLUS) && !line.endsWith(StringPool.PERIOD) && (!trimmedLine.startsWith("new ") || !line.endsWith(StringPool.OPEN_CURLY_BRACE)) && ((trimmedLine.length() + previousLineLength) < _MAX_LINE_LENGTH)) { return getCombinedLinesContent(content, fileName, line, trimmedLine, lineLength, lineCount, previousLine, null, tabDiff, false, true, false); } } if (!previousLine.endsWith(StringPool.OPEN_PARENTHESIS)) { return null; } if (StringUtil.count(previousLine, StringPool.OPEN_PARENTHESIS) > 1) { int pos = trimmedPreviousLine.lastIndexOf(StringPool.OPEN_PARENTHESIS, trimmedPreviousLine.length() - 2); if ((pos > 0) && Character.isLetterOrDigit(trimmedPreviousLine.charAt(pos - 1))) { String filePart = trimmedPreviousLine.substring(pos + 1); if (!filePart.contains(StringPool.CLOSE_PARENTHESIS) && !filePart.contains(StringPool.QUOTE)) { return getCombinedLinesContent(content, fileName, line, trimmedLine, lineLength, lineCount, previousLine, filePart, tabDiff, false, false, false); } } } if ((trimmedLine.length() + previousLineLength) > _MAX_LINE_LENGTH) { return null; } if (line.endsWith(StringPool.COMMA)) { String strippedQuotesLine = stripQuotes(trimmedLine, CharPool.QUOTE); int openParenthesisCount = StringUtil.count(strippedQuotesLine, StringPool.OPEN_PARENTHESIS); int closeParenthesisCount = StringUtil.count(strippedQuotesLine, StringPool.CLOSE_PARENTHESIS); if (closeParenthesisCount > openParenthesisCount) { return getCombinedLinesContent(content, fileName, line, trimmedLine, lineLength, lineCount, previousLine, null, tabDiff, false, false, false); } } if (((line.endsWith(StringPool.OPEN_CURLY_BRACE) && !trimmedLine.startsWith("new ")) || line.endsWith(StringPool.CLOSE_PARENTHESIS)) && (trimmedPreviousLine.startsWith("else ") || trimmedPreviousLine.startsWith("if ") || trimmedPreviousLine.startsWith("private ") || trimmedPreviousLine.startsWith("protected ") || trimmedPreviousLine.startsWith("public "))) { return getCombinedLinesContent(content, fileName, line, trimmedLine, lineLength, lineCount, previousLine, null, tabDiff, false, false, false); } return null; }
From source file:com.liferay.tools.sourceformatter.JSPSourceProcessor.java
License:Open Source License
protected void addImportCounts(String content) { Matcher matcher = _importsPattern.matcher(content); while (matcher.find()) { String importName = matcher.group(1); int count = 0; if (_importCountMap.containsKey(importName)) { count = _importCountMap.get(importName); } else {//from w ww. j a v a 2 s. com int pos = importName.lastIndexOf(StringPool.PERIOD); String importClassName = importName.substring(pos + 1); if (_importClassNames.contains(importClassName)) { _duplicateImportClassNames.add(importClassName); } else { _importClassNames.add(importClassName); } } _importCountMap.put(importName, count + 1); } }
From source file:com.liferay.tools.sourceformatter.JSPSourceProcessor.java
License:Open Source License
protected void addJSPUnusedImports(String fileName, List<String> importLines, List<String> unneededImports) { for (String importLine : importLines) { int x = importLine.indexOf(StringPool.QUOTE); int y = importLine.indexOf(StringPool.QUOTE, x + 1); if ((x == -1) || (y == -1)) { continue; }//from w ww .j a va 2 s . com String className = importLine.substring(x + 1, y); className = className.substring(className.lastIndexOf(StringPool.PERIOD) + 1); String regex = "[^A-Za-z0-9_\"]" + className + "[^A-Za-z0-9_\"]"; if (hasUnusedJSPTerm(fileName, regex, "class")) { unneededImports.add(importLine); } } }
From source file:com.liferay.tools.sourceformatter.JSPSourceProcessor.java
License:Open Source License
protected void moveFrequentlyUsedImportsToCommonInit(int minCount) throws IOException { if (_importCountMap.isEmpty()) { return;//from w ww .ja v a 2s. c o m } String commonInitFileName = "portal-web/docroot/html/common/init.jsp"; File commonInitFile = null; String commonInitFileContent = null; int x = -1; for (Map.Entry<String, Integer> importCount : _importCountMap.entrySet()) { Integer count = importCount.getValue(); if (count < minCount) { continue; } String importName = importCount.getKey(); int y = importName.lastIndexOf(StringPool.PERIOD); String importClassName = importName.substring(y + 1); if (_duplicateImportClassNames.contains(importClassName)) { continue; } if (commonInitFileContent == null) { commonInitFile = new File(commonInitFileName); commonInitFileContent = fileUtil.read(commonInitFile); x = commonInitFileContent.indexOf("<%@ page import"); } commonInitFileContent = StringUtil.insert(commonInitFileContent, "<%@ page import=\"" + importName + "\" %>\n", x); } if (commonInitFileContent != null) { fileUtil.write(commonInitFile, commonInitFileContent); _jspContents.put(commonInitFileName, commonInitFileContent); } }