| /** |
| * |
| * Copyright (c) 2011, 2016 - Loetz GmbH&Co.KG (69115 Heidelberg, Germany) |
| * |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License 2.0 |
| * which accompanies this distribution, and is available at |
| * https://www.eclipse.org/legal/epl-2.0/ |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| * |
| * Contributors: |
| * Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation |
| */ |
| package org.eclipse.osbp.xtext.i18n; |
| |
| public class I18NKeyGenerator { |
| public static String key(String input) { |
| if (input != null) { |
| return value(input).replaceAll("(\\W)" ,"_").toLowerCase(); |
| } |
| return null; |
| } |
| |
| /** |
| * @see {@link #camelCaseToUnderscore(String)} |
| * @see {@link CamelCaseToLowerCaseWithUnderscoreTest} |
| * @param input |
| * @return any ", [, ] and ^ are removed. Camel case is converted into lower case with underscore |
| */ |
| public static String value(String input) { |
| if (input != null) { |
| // 2. convert camel case to lowercase with underscores |
| return camelCaseToUnderscore( |
| // 1. remove any ", [, ] and ^ |
| input.replace("\"","").replace("[","").replace("]","").replace("^","") |
| ); |
| } |
| return null; |
| } |
| |
| public static String toGetter(String input) { |
| String k = key(input); |
| return "get"+k.substring(0, 1).toUpperCase()+k.substring(1); |
| } |
| |
| /** |
| * @see {@link CamelCaseToLowerCaseWithUnderscoreTest} |
| * @param pCamelCaseString |
| * @return camel case is converted into lower case with underscore |
| */ |
| public static String camelCaseToUnderscore(String pCamelCaseString) { |
| return |
| // 1. replace any non digit and letters to underscore |
| pCamelCaseString.replaceAll("(\\W)" ,"_"). |
| // 2. convert lower case to upper case |
| replaceAll("([a-z])([A-Z])", "$1_$2"). |
| // 3. remove any double underscores that may be generated |
| replace("__", "_"). |
| // 4. to lower case now |
| toLowerCase() |
| ; |
| } |
| |
| } |