/** * Escapes the HREF attribute so that it is safe to use as an HREF attribute. * * @param href the HREF attribute to escape * @return the escaped HREF attribute, or <code>null</code> if the HREF attribute is <code>null * </code> */ @Override public String escapeHREF(String href) { if (href == null) { return null; } if (href.length() == 0) { return StringPool.BLANK; } int index = href.indexOf(StringPool.COLON); if (index == 4) { String protocol = StringUtil.toLowerCase(href.substring(0, 4)); if (protocol.equals("data")) { href = StringUtil.replaceFirst(href, CharPool.COLON, "%3a"); } } else if (index == 10) { String protocol = StringUtil.toLowerCase(href.substring(0, 10)); if (protocol.equals("javascript")) { href = StringUtil.replaceFirst(href, CharPool.COLON, "%3a"); } } return escapeAttribute(href); }
protected String trimLine(String line, boolean allowLeadingSpaces) { if (line.trim().length() == 0) { return StringPool.BLANK; } 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; }
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; } 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; }
@Override public String replaceExportContentReferences( PortletDataContext portletDataContext, StagedModel stagedModel, String content, boolean exportReferencedContent, boolean escapeContent) throws Exception { JournalFeed feed = (JournalFeed) stagedModel; Group group = _groupLocalService.getGroup(portletDataContext.getScopeGroupId()); String newGroupFriendlyURL = group.getFriendlyURL(); newGroupFriendlyURL = newGroupFriendlyURL.substring(1); String[] friendlyURLParts = StringUtil.split(feed.getTargetLayoutFriendlyUrl(), StringPool.FORWARD_SLASH); String oldGroupFriendlyURL = friendlyURLParts[2]; if (newGroupFriendlyURL.equals(oldGroupFriendlyURL)) { String targetLayoutFriendlyUrl = StringUtil.replaceFirst( feed.getTargetLayoutFriendlyUrl(), StringPool.SLASH + newGroupFriendlyURL + StringPool.SLASH, StringPool.SLASH + DATA_HANDLER_GROUP_FRIENDLY_URL + StringPool.SLASH); feed.setTargetLayoutFriendlyUrl(targetLayoutFriendlyUrl); } Group targetLayoutGroup = _groupLocalService.fetchFriendlyURLGroup( portletDataContext.getCompanyId(), StringPool.SLASH + oldGroupFriendlyURL); boolean privateLayout = false; if (!PropsValues.LAYOUT_FRIENDLY_URL_PUBLIC_SERVLET_MAPPING.equals( StringPool.SLASH + friendlyURLParts[1])) { privateLayout = true; } String targetLayoutFriendlyURL = StringPool.SLASH + friendlyURLParts[3]; Layout targetLayout = _layoutLocalService.fetchLayoutByFriendlyURL( targetLayoutGroup.getGroupId(), privateLayout, targetLayoutFriendlyURL); Element feedElement = portletDataContext.getExportDataElement(feed); portletDataContext.addReferenceElement( feed, feedElement, targetLayout, PortletDataContext.REFERENCE_TYPE_DEPENDENCY, true); return content; }
@Override protected void format() throws Exception { _unusedVariablesExclusions = getExclusionsProperties("source_formatter_jsp_unused_variables_exclusions.properties"); String[] excludes = new String[] {"**\\null.jsp", "**\\tools\\**"}; String[] includes = new String[] {"**\\*.jsp", "**\\*.jspf", "**\\*.vm"}; List<String> fileNames = getFileNames(excludes, includes); Pattern pattern = Pattern.compile("\\s*@\\s*include\\s*file=['\"](.*)['\"]"); for (String fileName : fileNames) { File file = new File(BASEDIR + fileName); fileName = StringUtil.replace(fileName, StringPool.BACK_SLASH, StringPool.SLASH); String content = fileUtil.read(file); Matcher matcher = pattern.matcher(content); String newContent = content; while (matcher.find()) { newContent = StringUtil.replaceFirst( newContent, matcher.group(), "@ include file=\"" + matcher.group(1) + "\"", matcher.start()); } compareAndAutoFixContent(file, fileName, content, newContent); if (portalSource && mainReleaseVersion.equals(MAIN_RELEASE_LATEST_VERSION) && fileName.endsWith("/init.jsp") && !fileName.endsWith("/common/init.jsp")) { addImportCounts(content); } _jspContents.put(fileName, newContent); } if (portalSource && !mainReleaseVersion.equals(MAIN_RELEASE_VERSION_6_1_0)) { moveFrequentlyUsedImportsToCommonInit(4); } for (String fileName : fileNames) { format(fileName); } }
protected String convertProcess( ActionRequest actionRequest, ActionResponse actionResponse, String cmd) throws Exception { ActionResponseImpl actionResponseImpl = (ActionResponseImpl) actionResponse; PortletSession portletSession = actionRequest.getPortletSession(); String className = StringUtil.replaceFirst(cmd, "convertProcess.", StringPool.BLANK); ConvertProcess convertProcess = (ConvertProcess) InstancePool.get(className); String[] parameters = convertProcess.getParameterNames(); if (parameters != null) { String[] values = new String[parameters.length]; for (int i = 0; i < parameters.length; i++) { String parameter = className + StringPool.PERIOD + parameters[i]; if (parameters[i].contains(StringPool.EQUAL)) { String[] parameterPair = StringUtil.split(parameters[i], CharPool.EQUAL); parameter = className + StringPool.PERIOD + parameterPair[0]; } values[i] = ParamUtil.getString(actionRequest, parameter); } convertProcess.setParameterValues(values); } String path = convertProcess.getPath(); if (path != null) { PortletURL portletURL = actionResponseImpl.createRenderURL(); portletURL.setWindowState(WindowState.MAXIMIZED); portletURL.setParameter("struts_action", path); return portletURL.toString(); } else { MaintenanceUtil.maintain(portletSession.getId(), className); MessageBusUtil.sendMessage(DestinationNames.CONVERT_PROCESS, className); return null; } }
@Override public String escapeJSLink(String link) { if (Validator.isNull(link)) { return StringPool.BLANK; } if (link.indexOf(StringPool.COLON) == 10) { String protocol = StringUtil.toLowerCase(link.substring(0, 10)); if (protocol.equals("javascript")) { link = StringUtil.replaceFirst(link, CharPool.COLON, "%3a"); } } return link; }
protected String formatJavaTerms( String fileName, String absolutePath, String content, String javaClassContent, List<String> javaTermSortExclusions, List<String> testAnnotationsExclusions) throws Exception { JavaClass javaClass = new JavaClass(fileName, absolutePath, javaClassContent, StringPool.TAB); String newJavaClassContent = javaClass.formatJavaTerms(javaTermSortExclusions, testAnnotationsExclusions); if (!javaClassContent.equals(newJavaClassContent)) { return StringUtil.replaceFirst(content, javaClassContent, newJavaClassContent); } return content; }
protected String fixSessionKey(String fileName, 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 if (pattern.equals(taglibSessionKeyPattern)) { s = "key="; } int x = match.indexOf(s); if (x == -1) { continue; } x = x + s.length(); String substring = match.substring(x).trim(); 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; }
protected String sortAttributes( String fileName, String line, int lineCount, boolean allowApostropheDelimeter) { String s = line; int x = s.indexOf(StringPool.SPACE); if (x == -1) { return line; } s = s.substring(x + 1); String previousAttribute = null; String previousAttributeAndValue = null; boolean wrongOrder = false; for (x = 0; ; ) { x = s.indexOf(StringPool.EQUAL); if ((x == -1) || (s.length() <= (x + 1))) { return line; } String attribute = s.substring(0, x); if (!isAttributName(attribute)) { return line; } if (Validator.isNotNull(previousAttribute) && (previousAttribute.compareTo(attribute) > 0)) { wrongOrder = true; } s = s.substring(x + 1); char delimeter = s.charAt(0); if ((delimeter != CharPool.APOSTROPHE) && (delimeter != CharPool.QUOTE)) { if (delimeter != CharPool.AMPERSAND) { processErrorMessage(fileName, "delimeter: " + fileName + " " + lineCount); } return line; } s = s.substring(1); String value = null; int y = -1; while (true) { y = s.indexOf(delimeter, y + 1); if ((y == -1) || (s.length() <= (y + 1))) { return line; } value = s.substring(0, y); if (value.startsWith("<%")) { int endJavaCodeSignCount = StringUtil.count(value, "%>"); int startJavaCodeSignCount = StringUtil.count(value, "<%"); if (endJavaCodeSignCount == startJavaCodeSignCount) { break; } } else { int greaterThanCount = StringUtil.count(value, StringPool.GREATER_THAN); int lessThanCount = StringUtil.count(value, StringPool.LESS_THAN); if (greaterThanCount == lessThanCount) { break; } } } if (delimeter == CharPool.APOSTROPHE) { if (!value.contains(StringPool.QUOTE)) { line = StringUtil.replace( line, StringPool.APOSTROPHE + value + StringPool.APOSTROPHE, StringPool.QUOTE + value + StringPool.QUOTE); return sortAttributes(fileName, line, lineCount, allowApostropheDelimeter); } else if (!allowApostropheDelimeter) { String newValue = StringUtil.replace(value, StringPool.QUOTE, """); line = StringUtil.replace( line, StringPool.APOSTROPHE + value + StringPool.APOSTROPHE, StringPool.QUOTE + newValue + StringPool.QUOTE); return sortAttributes(fileName, line, lineCount, allowApostropheDelimeter); } } StringBundler sb = new StringBundler(5); sb.append(attribute); sb.append(StringPool.EQUAL); sb.append(delimeter); sb.append(value); sb.append(delimeter); String currentAttributeAndValue = sb.toString(); if (wrongOrder) { if ((StringUtil.count(line, currentAttributeAndValue) == 1) && (StringUtil.count(line, previousAttributeAndValue) == 1)) { line = StringUtil.replaceFirst(line, previousAttributeAndValue, currentAttributeAndValue); line = StringUtil.replaceLast(line, currentAttributeAndValue, previousAttributeAndValue); return sortAttributes(fileName, line, lineCount, allowApostropheDelimeter); } return line; } s = s.substring(y + 1); if (s.startsWith(StringPool.GREATER_THAN)) { x = s.indexOf(StringPool.SPACE); if (x == -1) { return line; } s = s.substring(x + 1); previousAttribute = null; previousAttributeAndValue = null; } else { s = StringUtil.trimLeading(s); previousAttribute = attribute; previousAttributeAndValue = currentAttributeAndValue; } } }
protected String formatJSP(String fileName, String absolutePath, String content) throws IOException { StringBundler sb = new StringBundler(); UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(new UnsyncStringReader(content)); int lineCount = 0; String line = null; String previousLine = StringPool.BLANK; String currentAttributeAndValue = null; String previousAttribute = null; String previousAttributeAndValue = null; boolean readAttributes = false; String currentException = null; String previousException = null; boolean hasUnsortedExceptions = false; boolean javaSource = false; while ((line = unsyncBufferedReader.readLine()) != null) { lineCount++; if (!fileName.contains("jsonw") || !fileName.endsWith("action.jsp")) { line = trimLine(line, false); } if (line.contains("<aui:button ") && line.contains("type=\"button\"")) { processErrorMessage(fileName, "aui:button " + fileName + " " + lineCount); } if (line.contains("debugger.")) { processErrorMessage(fileName, "debugger " + fileName + " " + lineCount); } String trimmedLine = StringUtil.trimLeading(line); String trimmedPreviousLine = StringUtil.trimLeading(previousLine); checkStringBundler(trimmedLine, fileName, lineCount); checkEmptyCollection(trimmedLine, fileName, lineCount); if (trimmedLine.equals("<%") || trimmedLine.equals("<%!")) { javaSource = true; } else if (trimmedLine.equals("%>")) { javaSource = false; } if (javaSource || trimmedLine.contains("<%= ")) { checkInefficientStringMethods(line, fileName, absolutePath, lineCount); } if (javaSource && portalSource && !isExcluded(_unusedVariablesExclusions, fileName, lineCount) && !_jspContents.isEmpty() && hasUnusedVariable(fileName, trimmedLine)) { processErrorMessage(fileName, "Unused variable: " + fileName + " " + lineCount); } if (!trimmedLine.equals("%>") && line.contains("%>") && !line.contains("--%>") && !line.contains(" %>")) { line = StringUtil.replace(line, "%>", " %>"); } if (line.contains("<%=") && !line.contains("<%= ")) { line = StringUtil.replace(line, "<%=", "<%= "); } if (trimmedPreviousLine.equals("%>") && Validator.isNotNull(line) && !trimmedLine.equals("-->")) { sb.append("\n"); } else if (Validator.isNotNull(previousLine) && !trimmedPreviousLine.equals("<!--") && trimmedLine.equals("<%")) { sb.append("\n"); } else if (trimmedPreviousLine.equals("<%") && Validator.isNull(line)) { continue; } else if (trimmedPreviousLine.equals("<%") && trimmedLine.startsWith("//")) { sb.append("\n"); } else if (Validator.isNull(previousLine) && trimmedLine.equals("%>") && (sb.index() > 2)) { String lineBeforePreviousLine = sb.stringAt(sb.index() - 3); if (!lineBeforePreviousLine.startsWith("//")) { sb.setIndex(sb.index() - 1); } } if ((trimmedLine.startsWith("if (") || trimmedLine.startsWith("else if (") || trimmedLine.startsWith("while (")) && trimmedLine.endsWith(") {")) { checkIfClauseParentheses(trimmedLine, fileName, lineCount); } if (readAttributes) { if (!trimmedLine.startsWith(StringPool.FORWARD_SLASH) && !trimmedLine.startsWith(StringPool.GREATER_THAN)) { int pos = trimmedLine.indexOf(StringPool.EQUAL); if (pos != -1) { String attribute = trimmedLine.substring(0, pos); if (!trimmedLine.endsWith(StringPool.APOSTROPHE) && !trimmedLine.endsWith(StringPool.GREATER_THAN) && !trimmedLine.endsWith(StringPool.QUOTE)) { processErrorMessage(fileName, "attribute: " + fileName + " " + lineCount); readAttributes = false; } else if (trimmedLine.endsWith(StringPool.APOSTROPHE) && !trimmedLine.contains(StringPool.QUOTE)) { line = StringUtil.replace(line, StringPool.APOSTROPHE, StringPool.QUOTE); readAttributes = false; } else if (Validator.isNotNull(previousAttribute)) { if (!isAttributName(attribute) && !attribute.startsWith(StringPool.LESS_THAN)) { processErrorMessage(fileName, "attribute: " + fileName + " " + lineCount); readAttributes = false; } else if (Validator.isNull(previousAttributeAndValue) && (previousAttribute.compareTo(attribute) > 0)) { previousAttributeAndValue = previousLine; currentAttributeAndValue = line; } } if (!readAttributes) { previousAttribute = null; previousAttributeAndValue = null; } else { previousAttribute = attribute; } } } else { previousAttribute = null; readAttributes = false; } } if (!hasUnsortedExceptions) { int i = line.indexOf("<liferay-ui:error exception=\"<%="); if (i != -1) { currentException = line.substring(i + 33); if (Validator.isNotNull(previousException) && (previousException.compareTo(currentException) > 0)) { hasUnsortedExceptions = true; } } if (!hasUnsortedExceptions) { previousException = currentException; currentException = null; } } if (trimmedLine.startsWith(StringPool.LESS_THAN) && !trimmedLine.startsWith("<%") && !trimmedLine.startsWith("<!")) { if (!trimmedLine.contains(StringPool.GREATER_THAN) && !trimmedLine.contains(StringPool.SPACE)) { readAttributes = true; } else { line = sortAttributes(fileName, line, lineCount, true); } } if (!trimmedLine.contains(StringPool.DOUBLE_SLASH) && !trimmedLine.startsWith(StringPool.STAR)) { while (trimmedLine.contains(StringPool.TAB)) { line = StringUtil.replaceLast(line, StringPool.TAB, StringPool.SPACE); trimmedLine = StringUtil.replaceLast(trimmedLine, StringPool.TAB, StringPool.SPACE); } while (trimmedLine.contains(StringPool.DOUBLE_SPACE) && !trimmedLine.contains(StringPool.QUOTE + StringPool.DOUBLE_SPACE) && !fileName.endsWith(".vm")) { line = StringUtil.replaceLast(line, StringPool.DOUBLE_SPACE, StringPool.SPACE); trimmedLine = StringUtil.replaceLast(trimmedLine, StringPool.DOUBLE_SPACE, StringPool.SPACE); } } if (!fileName.endsWith("/touch.jsp")) { int x = line.indexOf("<%@ include file"); if (x != -1) { x = line.indexOf(StringPool.QUOTE, x); int y = line.indexOf(StringPool.QUOTE, x + 1); if (y != -1) { String includeFileName = line.substring(x + 1, y); Matcher matcher = _jspIncludeFilePattern.matcher(includeFileName); if (!matcher.find()) { processErrorMessage(fileName, "include: " + fileName + " " + lineCount); } } } } line = replacePrimitiveWrapperInstantiation(fileName, line, lineCount); previousLine = line; sb.append(line); sb.append("\n"); } unsyncBufferedReader.close(); content = sb.toString(); if (content.endsWith("\n")) { content = content.substring(0, content.length() - 1); } content = formatTaglibQuotes(fileName, content, StringPool.QUOTE); content = formatTaglibQuotes(fileName, content, StringPool.APOSTROPHE); if (Validator.isNotNull(previousAttributeAndValue)) { content = StringUtil.replaceFirst( content, previousAttributeAndValue + "\n" + currentAttributeAndValue, currentAttributeAndValue + "\n" + previousAttributeAndValue); } if (hasUnsortedExceptions) { if ((StringUtil.count(content, currentException) > 1) || (StringUtil.count(content, previousException) > 1)) { processErrorMessage(fileName, "unsorted exceptions: " + fileName); } else { content = StringUtil.replaceFirst(content, previousException, currentException); content = StringUtil.replaceLast(content, currentException, previousException); } } return content; }