-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\r
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">\r
-\r
- <parent>\r
- <groupId>org.opendaylight.yangtools</groupId>\r
- <artifactId>binding-generator</artifactId>\r
- <version>0.5.7-SNAPSHOT</version>\r
- </parent>\r
-\r
- <modelVersion>4.0.0</modelVersion>\r
- <artifactId>binding-generator-util</artifactId>\r
-\r
- <dependencies>\r
- <dependency>\r
- <groupId>org.opendaylight.yangtools</groupId>\r
- <artifactId>binding-model-api</artifactId>\r
- </dependency>\r
- <dependency>\r
- <groupId>org.opendaylight.yangtools</groupId>\r
- <artifactId>yang-model-api</artifactId>\r
- </dependency>\r
- <dependency>\r
- <groupId>junit</groupId>\r
- <artifactId>junit</artifactId>\r
- </dependency>\r
- <dependency>\r
- <groupId>org.opendaylight.yangtools</groupId>\r
- <artifactId>yang-binding</artifactId>\r
- </dependency>\r
- </dependencies>\r
-\r
-</project>\r
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+
+ <parent>
+ <groupId>org.opendaylight.yangtools</groupId>
+ <artifactId>binding-generator</artifactId>
+ <version>0.5.7-SNAPSHOT</version>
+ </parent>
+
+ <modelVersion>4.0.0</modelVersion>
+ <artifactId>binding-generator-util</artifactId>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.opendaylight.yangtools</groupId>
+ <artifactId>binding-model-api</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.opendaylight.yangtools</groupId>
+ <artifactId>yang-model-api</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.opendaylight.yangtools</groupId>
+ <artifactId>yang-binding</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.opendaylight.yangtools</groupId>
+ <artifactId>yang-parser-impl</artifactId>
+ <version>${yang.version}</version>
+ </dependency>
+ </dependencies>
+
+</project>
public final class BindingGeneratorUtil {
private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyMMdd");
-
+
/**
* Array of strings values which represents JAVA reserved words.
*/
* string with the parameter name
* @return string with the admissible parameter name
*/
- public static String validateParameterName(final String parameterName) {
+ public static String resolveJavaReservedWordEquivalency(final String parameterName) {
if (parameterName != null && JAVA_RESERVED_WORDS.contains(parameterName)) {
return "_" + parameterName;
}
packageNameBuilder.append(namespace);
packageNameBuilder.append(".rev");
packageNameBuilder.append(DATE_FORMAT.format(module.getRevision()));
-
+
return validateJavaPackage(packageNameBuilder.toString());
}
* name.
*/
public static String parseToClassName(String token) {
- String correctStr = token.replace(".", "");
- correctStr = parseToCamelCase(correctStr);
-
- // make first char upper-case
- char first = Character.toUpperCase(correctStr.charAt(0));
- if (first >= '0' && first <= '9') {
-
- correctStr = "_" + correctStr;
- } else {
- correctStr = first + correctStr.substring(1);
- }
- return correctStr;
+ return parseToCamelCase(token, true);
}
/**
* parameter name.
*/
public static String parseToValidParamName(final String token) {
- final String validToken = token.replace(".", "");
- String correctStr = parseToCamelCase(validToken);
-
- // make first char lower-case
- char first = Character.toLowerCase(correctStr.charAt(0));
- correctStr = first + correctStr.substring(1);
- return validateParameterName(correctStr);
- }
-
- /**
- * Converts <code>token</code> to capital letters and removes invalid
- * characters.
- *
- * @param token
- * string with characters which should be conversed to capital
- * @return string with capital letters
- */
- public static String convertToCapitalLetters(final String token) {
- String convertedStr = token.replace(" ", "_");
- convertedStr = convertedStr.replace(".", "_");
- convertedStr = convertedStr.toUpperCase();
- return convertedStr;
+ return resolveJavaReservedWordEquivalency(parseToCamelCase(token, false));
}
/**
*
* @param token
* string which should be converted to the cammel case format
+ * @param uppercase
+ * boolean value which says whether the first character of the
+ * <code>token</code> should|shuldn't be uppercased
* @return string in the cammel case format
* @throws IllegalArgumentException
* <ul>
* <li>if <code>token</code> equals null</li>
* </ul>
*/
- private static String parseToCamelCase(String token) {
+
+ private static String parseToCamelCase(final String token, final boolean uppercase) {
if (token == null) {
throw new IllegalArgumentException("Name can not be null");
}
String correctStr = token.trim();
+ correctStr = correctStr.replace(".", "");
+
if (correctStr.isEmpty()) {
throw new IllegalArgumentException("Name can not be emty");
}
correctStr = replaceWithCamelCase(correctStr, ' ');
correctStr = replaceWithCamelCase(correctStr, '-');
correctStr = replaceWithCamelCase(correctStr, '_');
+
+ String firstChar = correctStr.substring(0, 1);
+ if (uppercase) {
+ firstChar = firstChar.toUpperCase();
+ } else {
+ firstChar = firstChar.toLowerCase();
+ }
+
+ if (firstChar.matches("[0-9]")) {
+ correctStr = "_" + correctStr;
+ } else {
+ correctStr = firstChar + correctStr.substring(1);
+ }
return correctStr;
}
--- /dev/null
+package org.opendaylight.yangtools.binding.generator.util;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+
+import org.junit.Test;
+import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.Module;
+import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
+import org.opendaylight.yangtools.yang.model.parser.api.YangModelParser;
+import org.opendaylight.yangtools.yang.model.util.DataNodeIterator;
+import org.opendaylight.yangtools.yang.parser.builder.impl.ModuleBuilder;
+import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
+
+public class BindingGeneratorUtilTest {
+
+ private static List<File> loadTestResources(String testFile) {
+ final List<File> testModels = new ArrayList<File>();
+ final File listModelFile = new File(BindingGeneratorUtilTest.class.getResource(testFile).getPath());
+ testModels.add(listModelFile);
+ return testModels;
+ }
+
+ /**
+ * Tests methods:
+ * <ul>
+ * <li>moduleNamespaceToPackageName</li> - with revision
+ * <li>packageNameForGeneratedType</li>
+ * <ul>
+ * <li>validateJavaPackage</li>
+ * </ul>
+ * <li>packageNameForTypeDefinition</li> <li>moduleNamespaceToPackageName</li>
+ * - without revision </ul>
+ */
+ @Test
+ public void testBindingGeneratorUtilMethods() {
+ List<File> testModels = loadTestResources("/module.yang");
+ final YangModelParser parser = new YangParserImpl();
+ final Set<Module> modules = parser.parseYangModels(testModels);
+ String packageName = "";
+ Module module = null;
+ for (Module m : modules) {
+ module = m;
+ break;
+ }
+ assertNotNull("Module can't be null", module);
+
+ // test of the method moduleNamespaceToPackageName()
+ packageName = BindingGeneratorUtil.moduleNamespaceToPackageName(module);
+ assertEquals("Generated package name is incorrect.",
+ "org.opendaylight.yang.gen.v1.urn.m.o.d.u.l.e.n.a.m.e.t.e.s.t._case._1digit.rev130910", packageName);
+
+ // test of the method packageNameForGeneratedType()
+ DataNodeIterator it = new DataNodeIterator(module);
+ List<ContainerSchemaNode> schemaContainers = it.allContainers();
+ String subPackageNameForDataNode = "";
+ for (ContainerSchemaNode containerSchemaNode : schemaContainers) {
+ if (containerSchemaNode.getQName().getLocalName().equals("cont-inner")) {
+ subPackageNameForDataNode = BindingGeneratorUtil.packageNameForGeneratedType(packageName,
+ containerSchemaNode.getPath());
+ break;
+ }
+ }
+ assertEquals("The name of the subpackage is incorrect.",
+ "org.opendaylight.yang.gen.v1.urn.m.o.d.u.l.e.n.a.m.e.t.e.s.t._case._1digit.rev130910.cont.outter",
+ subPackageNameForDataNode);
+
+ // test of the method packageNameForTypeDefinition
+ Set<TypeDefinition<?>> typeDefinitions = module.getTypeDefinitions();
+ String subPackageNameForTypeDefinition = "";
+ for (TypeDefinition<?> tpDef : typeDefinitions) {
+ if (tpDef.getQName().getLocalName().equals("tpdf")) {
+ subPackageNameForTypeDefinition = BindingGeneratorUtil.packageNameForTypeDefinition(packageName, tpDef);
+ break;
+ }
+ }
+ assertEquals("The name of the subpackage is incorrect.",
+ "org.opendaylight.yang.gen.v1.urn.m.o.d.u.l.e.n.a.m.e.t.e.s.t._case._1digit.rev130910",
+ subPackageNameForTypeDefinition);
+
+ // test of exception part of the method moduleNamespaceToPackageName()
+ ModuleBuilder moduleBuilder = new ModuleBuilder("module-withut-revision");
+ Module moduleWithoutRevision = moduleBuilder.build();
+ boolean passedSuccesfully = false;
+ try {
+ BindingGeneratorUtil.moduleNamespaceToPackageName(moduleWithoutRevision);
+ passedSuccesfully = true;
+ } catch (IllegalArgumentException e) {
+ }
+ assertFalse("Exception 'IllegalArgumentException' wasn't raised", passedSuccesfully);
+
+ }
+
+ /**
+ * Test for the method
+ * <ul>
+ * <li>{@link BindingGeneratorUtil#validateParameterName()
+ * validateParameterName()}</li>
+ * <ul>
+ */
+ @Test
+ public void testValidateParameterName() {
+ assertNull("Return value is incorrect.", BindingGeneratorUtil.resolveJavaReservedWordEquivalency(null));
+ assertEquals("Return value is incorrect.", "whatever",
+ BindingGeneratorUtil.resolveJavaReservedWordEquivalency("whatever"));
+ assertEquals("Return value is incorrect.", "_case",
+ BindingGeneratorUtil.resolveJavaReservedWordEquivalency("case"));
+ }
+
+ /**
+ * Tests the methods:
+ * <ul>
+ * <li>parseToClassName</li>
+ * <ul>
+ * <li>parseToCamelCase</li>
+ * <ul>
+ * <li>replaceWithCamelCase</li>
+ * </ul>
+ * </ul> <li>parseToValidParamName</li>
+ * <ul>
+ * <li>parseToCamelCase</li>
+ * <ul>
+ * <li>replaceWithCamelCase</li>
+ * </ul>
+ * </ul>
+ * <ul>
+ */
+ @Test
+ public void testParsingMethods() {
+ // parseToClassName method testing
+ assertEquals("Class name has incorrect format", "SomeTestingClassName",
+ BindingGeneratorUtil.parseToClassName(" some-testing_class name "));
+ assertEquals("Class name has incorrect format", "_0SomeTestingClassName",
+ BindingGeneratorUtil.parseToClassName(" 0 some-testing_class name "));
+
+ // parseToValidParamName
+ assertEquals("Parameter name has incorrect format", "someTestingParameterName",
+ BindingGeneratorUtil.parseToValidParamName(" some-testing_parameter name "));
+ assertEquals("Parameter name has incorrect format", "_0someTestingParameterName",
+ BindingGeneratorUtil.parseToValidParamName(" 0some-testing_parameter name "));
+ }
+
+}