Initial opendaylight infrastructure commit!!
[controller.git] / opendaylight / sal / yang-prototype / code-generator / binding-generator-util / src / main / java / org / opendaylight / controller / binding / generator / util / CodeGeneratorHelper.java
1 /*\r
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.\r
3  *\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
7  */\r
8 package org.opendaylight.controller.binding.generator.util;\r
9 \r
10 public class CodeGeneratorHelper {\r
11 \r
12     public static String parseToClassName(String token) {\r
13         String correctStr = parseToCamelCase(token);\r
14 \r
15         // make first char upper-case\r
16         char first = Character.toUpperCase(correctStr.charAt(0));\r
17         correctStr = first + correctStr.substring(1);\r
18         return correctStr;\r
19     }\r
20 \r
21     public static String parseToParamName(String token) {\r
22         String correctStr = parseToCamelCase(token);\r
23 \r
24         // make first char lower-case\r
25         char first = Character.toLowerCase(correctStr.charAt(0));\r
26         correctStr = first + correctStr.substring(1);\r
27         return correctStr;\r
28     }\r
29 \r
30     private static String parseToCamelCase(String token) {\r
31         if (token == null) {\r
32             throw new NullPointerException("Name can not be null");\r
33         }\r
34 \r
35         String correctStr = token.trim();\r
36         if (correctStr.length() == 0) {\r
37             throw new IllegalArgumentException("Name can not be emty");\r
38         }\r
39 \r
40         correctStr = replaceWithCamelCase(correctStr, ' ');\r
41         correctStr = replaceWithCamelCase(correctStr, '-');\r
42         correctStr = replaceWithCamelCase(correctStr, '_');\r
43         return correctStr;\r
44     }\r
45 \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
49 \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
54             // 'text'\r
55             if (sb.length() == 0) {\r
56                 throw new IllegalArgumentException("Name can not be '"\r
57                         + toBeRemoved + "'");\r
58             }\r
59             String replacement = String.valueOf(sb.charAt(toBeRemovedPos))\r
60                     .toUpperCase();\r
61             sb.setCharAt(toBeRemovedPos, replacement.charAt(0));\r
62             toBeRemovedPos = sb.indexOf(toBeRemoved);\r
63         }\r
64         return sb.toString();\r
65     }\r
66 \r
67 }\r