1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.xsdc.java;
18 
19 import java.util.Arrays;
20 import java.util.HashSet;
21 
22 class Utils {
23     private static final String[] keywords = {
24             "abstract", "assert", "boolean", "break", "byte", "case",
25             "catch", "char", "class", "const", "continue", "default",
26             "double", "do", "else", "enum", "extends", "false",
27             "final", "finally", "float", "for", "goto", "if",
28             "implements", "import", "instanceof", "int", "interface", "long",
29             "native", "new", "null", "package", "private", "protected",
30             "public", "return", "short", "static", "strictfp", "super",
31             "switch", "synchronized", "this", "throw", "throws", "transient",
32             "true", "try", "void", "volatile", "while"
33     };
34     private static final HashSet<String> keywordSet = new HashSet<>(Arrays.asList(keywords));
35 
toCamelCase(String[] words)36     private static String toCamelCase(String[] words) {
37         String res = words[0];
38         for (int idx = 1; idx < words.length; ++idx) {
39             res += capitalize(words[idx]);
40         }
41         return res;
42     }
43 
capitalize(String input)44     static String capitalize(String input) {
45         return input.substring(0, 1).toUpperCase() + input.substring(1);
46     }
47 
lowerize(String input)48     private static String lowerize(String input) {
49         return input.substring(0, 1).toLowerCase() + input.substring(1);
50     }
51 
toVariableName(String name)52     static String toVariableName(String name) throws JavaCodeGeneratorException {
53         String trimmed = toCamelCase(name.replaceAll("[^A-Za-z0-9_-]", "").split("-"));
54         if (trimmed.isEmpty()) {
55             throw new JavaCodeGeneratorException(
56                     String.format("cannot convert to a variable name : %s", name));
57         }
58         String lowered = Character.isDigit(trimmed.charAt(0)) ? "_" + trimmed
59                 : lowerize(trimmed);
60         // always starts with a lowercase or underscore character.
61         return (keywordSet.contains(lowered)) ? "_" + lowered : lowered;
62     }
63 
toClassName(String name)64     static String toClassName(String name) throws JavaCodeGeneratorException {
65         String trimmed = toCamelCase(
66                 name.replaceAll("[^A-Za-z0-9_-]", "").replaceAll("-","_").split("_"));
67         if (trimmed.isEmpty() || Character.isDigit(trimmed.charAt(0))) {
68             throw new JavaCodeGeneratorException(
69                     String.format("cannot convert to a class name : %s", name));
70         }
71         return capitalize(trimmed);
72     }
73 
toEnumName(String name)74     static String toEnumName(String name) throws JavaCodeGeneratorException {
75         if ("".equals(name)) {
76             name = "EMPTY";
77         }
78         String trimmed = name.replace(".", "_").replaceAll("[^A-Za-z0-9_]", "");
79         if (trimmed.isEmpty()) {
80             throw new JavaCodeGeneratorException(
81                     String.format("cannot convert to a variable name : %s", name));
82         }
83         String enumName = Character.isDigit(trimmed.charAt(0)) ? "_" + trimmed
84                 : trimmed;
85         return (keywordSet.contains(enumName)) ? "_" + enumName : enumName;
86     }
87 }
88 
89