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

com.liferay.poshi.runner.prose.PoshiProseMatcher Maven / Gradle / Ivy

/**
 * 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.poshi.runner.prose;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Peter Yoo
 */
public class PoshiProseMatcher {

	public static PoshiProseMatcher getPoshiProseMatcher(String poshiProse) {
		String key = _toString(poshiProse);

		return poshiProseMatcherMap.get(key);
	}

	public static void storePoshiProseMatcher(
			String poshiProse, String macroNamespacedClassCommandName)
		throws Exception {

		List possiblePoshiProseStrings = _getPossiblePoshiProseStrings(
			poshiProse);

		for (String possiblePoshiProseString : possiblePoshiProseStrings) {
			String key = _toString(possiblePoshiProseString);

			PoshiProseMatcher ppm = poshiProseMatcherMap.get(key);

			if ((ppm != null) &&
				!macroNamespacedClassCommandName.equals(
					ppm.getMacroNamespacedClassCommandName())) {

				StringBuilder sb = new StringBuilder();

				sb.append("Duplicate prose '");
				sb.append(key);
				sb.append("' already exists for ");

				sb.append(ppm.getMacroNamespacedClassCommandName());

				sb.append("\n in ");
				sb.append(macroNamespacedClassCommandName);

				throw new RuntimeException(sb.toString());
			}

			poshiProseMatcherMap.put(
				key,
				new PoshiProseMatcher(
					poshiProse, macroNamespacedClassCommandName));
		}
	}

	public String getMacroNamespacedClassCommandName() {
		return _macroNamespacedClassCommandName;
	}

	public String getPoshiProse() {
		return _poshiProse;
	}

	public List getVarNames() {
		return _varNames;
	}

	@Override
	public String toString() {
		return _toString(_poshiProse);
	}

	protected static final Map poshiProseMatcherMap =
		new HashMap<>();

	private static List _getPossibleAlternateStrings(
		String proseString) {

		List possibleAlternateStrings = new ArrayList<>();

		if (proseString == null) {
			return possibleAlternateStrings;
		}

		Matcher alternateTextMatcher = _alternateTextPattern.matcher(
			proseString);

		if (alternateTextMatcher.find()) {
			List possiblePrefixes = new ArrayList<>();

			possiblePrefixes.add(
				alternateTextMatcher.group(1) +
					alternateTextMatcher.group("alternateText1"));

			possiblePrefixes.add(
				alternateTextMatcher.group(1) +
					alternateTextMatcher.group("alternateText2"));

			List possiblePostfixes = _getPossibleAlternateStrings(
				alternateTextMatcher.group(4));

			for (String possiblePrefix : possiblePrefixes) {
				for (String possiblePostfix : possiblePostfixes) {
					possibleAlternateStrings.add(
						possiblePrefix + possiblePostfix);
				}
			}
		}
		else {
			possibleAlternateStrings.add(proseString);
		}

		return possibleAlternateStrings;
	}

	private static List _getPossibleOptionalStrings(
		String proseString) {

		List possibleOptionalStrings = new ArrayList<>();

		if (proseString == null) {
			return possibleOptionalStrings;
		}

		Matcher optionalTextMatcher = _optionalTextPattern.matcher(proseString);

		if (optionalTextMatcher.find()) {
			List possiblePrefixes = new ArrayList<>();

			possiblePrefixes.add(optionalTextMatcher.group(1));

			possiblePrefixes.add(
				optionalTextMatcher.group(1) +
					optionalTextMatcher.group("optionalText"));

			List possiblePostfixes = _getPossiblePoshiProseStrings(
				optionalTextMatcher.group(3));

			for (String possiblePrefix : possiblePrefixes) {
				for (String possibleSecondPartString : possiblePostfixes) {
					possibleOptionalStrings.add(
						possiblePrefix + possibleSecondPartString);
				}
			}
		}
		else {
			possibleOptionalStrings.add(proseString);
		}

		return possibleOptionalStrings;
	}

	private static List _getPossiblePoshiProseStrings(
		String proseString) {

		List possiblePoshiProseStrings = new ArrayList<>();

		if (proseString == null) {
			return possiblePoshiProseStrings;
		}

		List possibleAlternateStrings = _getPossibleAlternateStrings(
			proseString);

		for (String possibleAlternateString : possibleAlternateStrings) {
			possiblePoshiProseStrings.addAll(
				_getPossibleOptionalStrings(possibleAlternateString));
		}

		return possiblePoshiProseStrings;
	}

	private static String _toString(String matchingString) {
		return matchingString.replaceAll("\\$\\{.+?\\}", "\"\"");
	}

	private PoshiProseMatcher(
		String poshiProse, String macroNamespacedClassCommandName) {

		_poshiProse = poshiProse;
		_macroNamespacedClassCommandName = macroNamespacedClassCommandName;

		Matcher matcher = _poshiProseVarPattern.matcher(_poshiProse);

		while (matcher.find()) {
			_varNames.add(matcher.group(1));
		}
	}

	private static final Pattern _alternateTextPattern = Pattern.compile(
		"(.*?)(?\\w+)\\/(?\\w+)(.*)");
	private static final Pattern _optionalTextPattern = Pattern.compile(
		"(.*?)\\((?.*?)\\)(.*)");
	private static final Pattern _poshiProseVarPattern = Pattern.compile(
		"\\$\\{(.+?)\\}");

	private final String _macroNamespacedClassCommandName;
	private final String _poshiProse;
	private final List _varNames = new ArrayList<>();

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy