2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
\r
4 * This program and the accompanying materials are made available under the
\r
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
\r
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
\r
8 package org.opendaylight.controller.binding.generator.util;
\r
10 public class CodeGeneratorHelper {
\r
12 public static String parseToClassName(String token) {
\r
13 String correctStr = parseToCamelCase(token);
\r
15 // make first char upper-case
\r
16 char first = Character.toUpperCase(correctStr.charAt(0));
\r
17 correctStr = first + correctStr.substring(1);
\r
21 public static String parseToParamName(String token) {
\r
22 String correctStr = parseToCamelCase(token);
\r
24 // make first char lower-case
\r
25 char first = Character.toLowerCase(correctStr.charAt(0));
\r
26 correctStr = first + correctStr.substring(1);
\r
30 private static String parseToCamelCase(String token) {
\r
31 if (token == null) {
\r
32 throw new NullPointerException("Name can not be null");
\r
35 String correctStr = token.trim();
\r
36 if (correctStr.length() == 0) {
\r
37 throw new IllegalArgumentException("Name can not be emty");
\r
40 correctStr = replaceWithCamelCase(correctStr, ' ');
\r
41 correctStr = replaceWithCamelCase(correctStr, '-');
\r
42 correctStr = replaceWithCamelCase(correctStr, '_');
\r
46 private static String replaceWithCamelCase(String text, char removalChar) {
\r
47 StringBuilder sb = new StringBuilder(text);
\r
48 String toBeRemoved = String.valueOf(removalChar);
\r
50 int toBeRemovedPos = sb.indexOf(toBeRemoved);
\r
51 while (toBeRemovedPos != -1) {
\r
52 sb.replace(toBeRemovedPos, toBeRemovedPos + 1, "");
\r
53 // check if 'toBeRemoved' character is not the only character in
\r
55 if (sb.length() == 0) {
\r
56 throw new IllegalArgumentException("Name can not be '"
\r
57 + toBeRemoved + "'");
\r
59 String replacement = String.valueOf(sb.charAt(toBeRemovedPos))
\r
61 sb.setCharAt(toBeRemovedPos, replacement.charAt(0));
\r
62 toBeRemovedPos = sb.indexOf(toBeRemoved);
\r
64 return sb.toString();
\r