List of usage examples for com.liferay.portal.kernel.util StringUtil trimTrailing
public static String trimTrailing(String s)
From source file:com.liferay.knowledgebase.admin.importer.util.KBArticleMarkdownConverter.java
License:Open Source License
protected String stripIds(String content) { int index = content.indexOf("[](id="); if (index == -1) { return content; }/*from www .ja v a 2s . c om*/ StringBundler sb = new StringBundler(); do { int x = content.indexOf(StringPool.EQUAL, index); int y = content.indexOf(StringPool.CLOSE_PARENTHESIS, x); if (y != -1) { sb.append(StringUtil.trimTrailing(content.substring(0, index))); content = content.substring(y + 1); } else { if (_log.isWarnEnabled()) { String msg = content.substring(index); // Get the invalid id text from the content int spaceIndex = content.indexOf(StringPool.SPACE); if (spaceIndex != -1) { msg = content.substring(index, spaceIndex); } _log.warn("Missing ')' for web content containing header id " + msg); } // Since no close parenthesis remains in the content, stop // stripping out IDs and simply include all of the remaining // content break; } } while ((index = content.indexOf("[](id=")) != -1); sb.append(content); return sb.toString(); }
From source file:com.liferay.tools.sourceformatter.BaseSourceProcessor.java
License:Open Source License
protected String trimLine(String line, boolean allowLeadingSpaces) { if (line.trim().length() == 0) { return StringPool.BLANK; }/* www . jav a 2 s. c om*/ line = StringUtil.trimTrailing(line); if (allowLeadingSpaces || !line.startsWith(StringPool.SPACE) || line.startsWith(" *")) { return line; } if (!line.startsWith(StringPool.FOUR_SPACES)) { while (line.startsWith(StringPool.SPACE)) { line = StringUtil.replaceFirst(line, StringPool.SPACE, StringPool.BLANK); } } else { int pos = 0; String temp = line; while (temp.startsWith(StringPool.FOUR_SPACES)) { line = StringUtil.replaceFirst(line, StringPool.FOUR_SPACES, StringPool.TAB); pos++; temp = line.substring(pos); } } return line; }
From source file:com.liferay.tools.sourceformatter.JavaClass.java
License:Open Source License
protected void fixTabsAndIncorrectEmptyLines(JavaTerm javaTerm) { if (!javaTerm.isConstructor() && !javaTerm.isMethod()) { return;/*from www . j a v a2 s . com*/ } String javaTermContent = "\n" + javaTerm.getContent(); Pattern methodNameAndParametersPattern = Pattern .compile("\n" + _indent + "(private |protected |public )(.|\n)*?(\\{|;)\n"); Matcher matcher = methodNameAndParametersPattern.matcher(javaTermContent); if (!matcher.find()) { return; } String methodNameAndParameters = matcher.group(); String[] lines = StringUtil.splitLines(methodNameAndParameters); if (lines.length == 1) { if (methodNameAndParameters.endsWith("{\n") && javaTermContent.contains(methodNameAndParameters + "\n") && !javaTermContent.contains(methodNameAndParameters + "\n" + _indent + StringPool.TAB + "/*") && !javaTermContent .contains(methodNameAndParameters + "\n" + _indent + StringPool.TAB + "// ")) { String trimmedJavaTermContent = StringUtil.trimTrailing(javaTermContent); if (!trimmedJavaTermContent.endsWith("\n\n" + _indent + StringPool.CLOSE_CURLY_BRACE)) { _content = StringUtil.replace(_content, methodNameAndParameters + "\n", methodNameAndParameters); } } return; } if (methodNameAndParameters.endsWith("{\n") && !javaTermContent.contains(methodNameAndParameters + "\n") && !javaTermContent.contains(methodNameAndParameters + _indent + StringPool.CLOSE_CURLY_BRACE)) { _content = StringUtil.replace(_content, methodNameAndParameters, methodNameAndParameters + "\n"); } boolean throwsException = methodNameAndParameters.contains(_indent + "throws "); String newMethodNameAndParameters = methodNameAndParameters; int expectedTabCount = -1; for (int i = 0; i < lines.length; i++) { String line = lines[i]; if (line.contains(_indent + "throws ")) { newMethodNameAndParameters = fixLeadingTabs(newMethodNameAndParameters, line, _indent.length() + 1); break; } if (expectedTabCount == -1) { if (line.endsWith(StringPool.OPEN_PARENTHESIS)) { expectedTabCount = Math.max(JavaSourceProcessor.getLeadingTabCount(line), _indent.length()) + 1; if (throwsException && (expectedTabCount == (_indent.length() + 1))) { expectedTabCount += 1; } } } else { String previousLine = lines[i - 1]; if (previousLine.endsWith(StringPool.COMMA) || previousLine.endsWith(StringPool.OPEN_PARENTHESIS)) { newMethodNameAndParameters = fixLeadingTabs(newMethodNameAndParameters, line, expectedTabCount); } else { newMethodNameAndParameters = fixLeadingTabs(newMethodNameAndParameters, line, JavaSourceProcessor.getLeadingTabCount(previousLine) + 1); } } } _content = StringUtil.replace(_content, methodNameAndParameters, newMethodNameAndParameters); }
From source file:com.liferay.tools.sourceformatter.JavaSourceProcessor.java
License:Open Source License
protected String getCombinedLinesContent(String content, String fileName, String line, String trimmedLine, int lineLength, int lineCount, String previousLine, String linePart, int tabDiff, boolean addToPreviousLine, boolean extraSpace, boolean removeTabOnNextLine) { if (linePart == null) { String combinedLine = previousLine; if (extraSpace) { combinedLine += StringPool.SPACE; }//from w w w . ja va 2 s. c om combinedLine += trimmedLine; String nextLine = getNextLine(content, lineCount); if (nextLine == null) { return null; } if (removeTabOnNextLine) { return StringUtil.replace(content, "\n" + previousLine + "\n" + line + "\n" + nextLine + "\n", "\n" + combinedLine + "\n" + nextLine.substring(1) + "\n"); } if (line.endsWith(StringPool.OPEN_CURLY_BRACE) && (tabDiff != 0) && !previousLine.contains(" class ") && Validator.isNull(nextLine)) { return StringUtil.replace(content, "\n" + previousLine + "\n" + line + "\n", "\n" + combinedLine); } return StringUtil.replace(content, "\n" + previousLine + "\n" + line + "\n", "\n" + combinedLine + "\n"); } String firstLine = previousLine; String secondLine = line; if (addToPreviousLine) { if (extraSpace) { firstLine += StringPool.SPACE; } firstLine += linePart; secondLine = StringUtil.replaceFirst(line, linePart, StringPool.BLANK); } else { if (((linePart.length() + lineLength) <= _MAX_LINE_LENGTH) && (line.endsWith(StringPool.OPEN_CURLY_BRACE) || line.endsWith(StringPool.SEMICOLON))) { firstLine = StringUtil.replaceLast(firstLine, StringUtil.trim(linePart), StringPool.BLANK); secondLine = StringUtil.replaceLast(line, StringPool.TAB, StringPool.TAB + linePart); } else { processErrorMessage(fileName, "line break: " + fileName + " " + lineCount); return null; } } firstLine = StringUtil.trimTrailing(firstLine); return StringUtil.replace(content, "\n" + previousLine + "\n" + line + "\n", "\n" + firstLine + "\n" + secondLine + "\n"); }
From source file:com.liferay.wiki.engine.markdown.pegdown.ast.LiferayPegDownProcessor.java
License:Open Source License
protected String stripIds(String content) { int index = content.indexOf("[](id="); if (index == -1) { return content; }//from w ww. j av a 2 s . com StringBundler sb = new StringBundler(); do { int x = content.indexOf(StringPool.EQUAL, index); int y = content.indexOf(StringPool.CLOSE_PARENTHESIS, x); if (y != -1) { int z = content.indexOf("</h", y); if (z != (y + 1)) { sb.append(content.substring(0, y + 1)); } else { sb.append(StringUtil.trimTrailing(content.substring(0, index))); } content = content.substring(y + 1); } else { if (_log.isWarnEnabled()) { String msg = content.substring(index); // Get the invalid id text from the content int spaceIndex = content.indexOf(StringPool.SPACE); if (spaceIndex != -1) { msg = content.substring(index, spaceIndex); } _log.warn("Missing ')' for web content containing header id " + msg); } // Since no close parenthesis remains in the content, stop // stripping out IDs and simply include all of the remaining // content break; } } while ((index = content.indexOf("[](id=")) != -1); sb.append(content); return sb.toString(); }