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

eu.cqse.check.base.string_interpolation.MessageFormatUtils Maven / Gradle / Ivy

Go to download

The Teamscale Custom Check API allows users to extend Teamscale by writing custom analyses that create findings.

There is a newer version: 2024.7.2
Show newest version
/*
 * Copyright (c) CQSE GmbH
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package eu.cqse.check.base.string_interpolation;

import org.conqat.lib.commons.assertion.CCSMAssert;

/**
 * Util class to handle formatStrings of the {@link java.text.MessageFormat} format
 * https://docs.oracle.com/javase/8/docs/api/java/text/MessageFormat.html
 */
public class MessageFormatUtils {

	/**
	 * Removes escaped sequences from a MessageFormat string.
	 * {@link MessageFormatUtils#getStateAfter(MessageFormatState, char)}
	 */
	public static String removeEscapedFromMessageFormatString(String messageFormatString) {
		StringBuilder builder = new StringBuilder(messageFormatString.length());
		MessageFormatState state = MessageFormatState.UNESCAPED;
		for (int i = 0; i < messageFormatString.length(); i++) {
			char c = messageFormatString.charAt(i);
			state = getStateAfter(state, c);
			if (state == MessageFormatState.UNESCAPED) {
				builder.append(c);
			}
		}
		return builder.toString();
	}

	private enum MessageFormatState {
		ESCAPED, UNESCAPED, ESCAPED_QUOTE, UNESCAPED_QUOTE
	}

	/**
	 * Models a finite state automaton describing the escaping mechanism of MessageFormat strings. See
	 * the
	 * MessageFormat documentation
	 * 
	 * @param state
	 *            the state from which to transition
	 * @param c
	 *            the character to transition with
	 * 
	 * @return the state this character transitions to.
	 */
	private static MessageFormatState getStateAfter(MessageFormatState state, char c) {
		if (c == '\'') {
			switch (state) {
			case UNESCAPED:
				state = MessageFormatState.UNESCAPED_QUOTE;
				break;
			case UNESCAPED_QUOTE:
				state = MessageFormatState.UNESCAPED;
				break;
			case ESCAPED:
				state = MessageFormatState.ESCAPED_QUOTE;
				break;
			case ESCAPED_QUOTE:
				state = MessageFormatState.ESCAPED;
				break;
			default:
				CCSMAssert.fail("invalid state");
			}
		} else {
			switch (state) {
			case UNESCAPED:
			case ESCAPED_QUOTE:
				state = MessageFormatState.UNESCAPED;
				break;
			case ESCAPED:
			case UNESCAPED_QUOTE:
				state = MessageFormatState.ESCAPED;
				break;
			default:
				CCSMAssert.fail("invalid state");
			}
		}
		return state;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy