All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.liferay.source.formatter.checks.BNDDefinitionKeysCheck Maven / Gradle / Ivy

There is a newer version: 1.0.1464
Show newest version
/**
 * 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.StringPool;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.source.formatter.checks.util.BNDSourceUtil;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Hugo Huijser
 */
public class BNDDefinitionKeysCheck extends DefinitionKeysCheck {

	@Override
	protected String doProcess(
		String fileName, String absolutePath, String content) {

		List definitions = getDefinitions(content);

		if (definitions.isEmpty()) {
			return content;
		}

		content = sortDefinitionKeys(
			content, definitions, new DefinitionComparator());

		Map> fileSpecificDefinitionKeysMap =
			BNDSourceUtil.getFileSpecificDefinitionKeysMap();
		Map generalDefinitionKeysMap =
			BNDSourceUtil.getDefinitionKeysMap();

		for (String definition : definitions) {
			content = _formatDefinitionKey(
				fileName, content, definition, fileSpecificDefinitionKeysMap,
				generalDefinitionKeysMap);
		}

		return content;
	}

	private String _formatDefinitionKey(
		String fileName, String content, String definition,
		Map> fileSpecificDefinitionKeysMap,
		Map generalDefinitionKeysMap) {

		if (definition.endsWith(",\\")) {
			return StringUtil.replace(
				content, definition,
				definition.substring(0, definition.length() - 2));
		}

		Matcher matcher = _definitionKeyPattern.matcher(definition);

		if (!matcher.find()) {
			return content;
		}

		String definitionKey = matcher.group(1);

		String lowerCaseDefinitionKey = StringUtil.toLowerCase(definitionKey);

		String correctKey = null;

		if (definitionKey.equals("Conditional-Package")) {
			correctKey = "-conditionalpackage";
		}
		else {
			correctKey = generalDefinitionKeysMap.get(lowerCaseDefinitionKey);
		}

		if (correctKey == null) {
			int pos = fileName.lastIndexOf(StringPool.SLASH);

			String shortFileName = fileName.substring(pos + 1);

			Map definitionKeysMap =
				fileSpecificDefinitionKeysMap.get(shortFileName);

			if (definitionKeysMap != null) {
				correctKey = definitionKeysMap.get(lowerCaseDefinitionKey);
			}
		}

		if (correctKey == null) {
			addMessage(
				fileName, "Unknown key \"" + definitionKey + "\"",
				"bnd_definition_keys.markdown");

			return content;
		}

		if (correctKey.equals(definitionKey)) {
			return StringUtil.replace(
				content, definitionKey + "=", definitionKey + ":");
		}

		if (content.startsWith(definitionKey)) {
			return StringUtil.replaceFirst(content, definitionKey, correctKey);
		}

		return StringUtil.replace(
			content, "\n" + definitionKey + ":", "\n" + correctKey + ":");
	}

	private final Pattern _definitionKeyPattern = Pattern.compile(
		"([A-Za-z-]+?)[:=]");

	private static class DefinitionComparator implements Comparator {

		@Override
		public int compare(String definition1, String definition2) {
			if (definition1.startsWith(StringPool.DASH) ^
				definition2.startsWith(StringPool.DASH)) {

				return -definition1.compareTo(definition2);
			}

			return definition1.compareTo(definition2);
		}

	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy