Defect Fix for namespace with special character support in YANG
Change-Id: I8cc5b9dce58023c5965b07ac36cc4b5858f91699
diff --git a/src/main/java/org/onosproject/yangutils/translator/exception/TranslatorException.java b/src/main/java/org/onosproject/yangutils/translator/exception/TranslatorException.java
new file mode 100644
index 0000000..218e24d
--- /dev/null
+++ b/src/main/java/org/onosproject/yangutils/translator/exception/TranslatorException.java
@@ -0,0 +1,79 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.yangutils.translator.exception;
+
+/**
+ * Provides custom translator exception for translator's operations.
+ */
+public class TranslatorException extends RuntimeException {
+
+ private static final long serialVersionUID = 20160311L;
+ private String fileName;
+
+ /**
+ * Create a new translator exception.
+ */
+ public TranslatorException() {
+ super();
+ }
+
+ /**
+ * Creates a new translator exception with given message.
+ *
+ * @param message the detail of exception in string
+ */
+ public TranslatorException(String message) {
+ super(message);
+ }
+
+ /**
+ * Creates a new translator exception from given message and cause.
+ *
+ * @param message the detail of exception in string
+ * @param cause underlying cause of the error
+ */
+ public TranslatorException(final String message, final Throwable cause) {
+ super(message, cause);
+ }
+
+ /**
+ * Creates a new translator exception from cause.
+ *
+ * @param cause underlying cause of the error
+ */
+ public TranslatorException(final Throwable cause) {
+ super(cause);
+ }
+
+ /**
+ * Returns generated file name for the exception.
+ *
+ * @return generated file name for the exception
+ */
+ public String getFileName() {
+ return this.fileName;
+ }
+
+ /**
+ * Sets file name in translator exception.
+ *
+ * @param fileName generated file name
+ */
+ public void setFileName(String fileName) {
+ this.fileName = fileName;
+ }
+}
diff --git a/src/main/java/org/onosproject/yangutils/translator/exception/package-info.java b/src/main/java/org/onosproject/yangutils/translator/exception/package-info.java
new file mode 100644
index 0000000..7432328
--- /dev/null
+++ b/src/main/java/org/onosproject/yangutils/translator/exception/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * Custom exception for translator.
+ */
+package org.onosproject.yangutils.translator.exception;
diff --git a/src/main/java/org/onosproject/yangutils/translator/tojava/utils/JavaIdentifierSyntax.java b/src/main/java/org/onosproject/yangutils/translator/tojava/utils/JavaIdentifierSyntax.java
index 00e953b..dd543ca 100644
--- a/src/main/java/org/onosproject/yangutils/translator/tojava/utils/JavaIdentifierSyntax.java
+++ b/src/main/java/org/onosproject/yangutils/translator/tojava/utils/JavaIdentifierSyntax.java
@@ -18,6 +18,7 @@
import java.util.ArrayList;
+import org.onosproject.yangutils.translator.exception.TranslatorException;
import org.onosproject.yangutils.utils.UtilConstants;
/**
@@ -25,8 +26,15 @@
*/
public final class JavaIdentifierSyntax {
+ private static final int MAX_MONTHS = 12;
+ private static final int MAX_DAYS = 31;
+ private static final int INDEX_ZERO = 0;
+ private static final int INDEX_ONE = 1;
+ private static final int INDEX_TWO = 2;
+ private static final int INDEX_THREE = 3;
+
/**
- * Util class, with static functions only.
+ * Default constructor.
*/
private JavaIdentifierSyntax() {
}
@@ -71,9 +79,9 @@
*/
public static String getPkgFromNameSpace(String nameSpace) {
ArrayList<String> pkgArr = new ArrayList<String>();
- nameSpace = nameSpace.replace("\"", "");
-
- String[] nameSpaceArr = nameSpace.split(UtilConstants.COLAN);
+ nameSpace = nameSpace.replace(UtilConstants.QUOTES, UtilConstants.EMPTY_STRING);
+ String properNameSpace = nameSpace.replaceAll(UtilConstants.REGEX_WITH_SPECIAL_CHAR, UtilConstants.COLAN);
+ String[] nameSpaceArr = properNameSpace.split(UtilConstants.COLAN);
for (String nameSpaceString : nameSpaceArr) {
pkgArr.add(nameSpaceString);
@@ -86,19 +94,31 @@
*
* @param date YANG module revision
* @return revision string
+ * @throws TranslatorException when date is invalid.
*/
- public static String getYangRevisionStr(String date) {
+ public static String getYangRevisionStr(String date) throws TranslatorException {
String[] revisionArr = date.split(UtilConstants.HYPHEN);
String rev = "rev";
- for (String element : revisionArr) {
- Integer val = Integer.parseInt(element);
- if (val < 10) {
- rev = rev + "0";
+ String year = revisionArr[INDEX_ZERO];
+ char[] yearBytes = year.toCharArray();
+ rev = rev + yearBytes[INDEX_TWO] + yearBytes[INDEX_THREE];
+
+ if ((Integer.parseInt(revisionArr[INDEX_ONE]) <= MAX_MONTHS)
+ && Integer.parseInt(revisionArr[INDEX_TWO]) <= MAX_DAYS) {
+ for (int i = INDEX_ONE; i < revisionArr.length; i++) {
+
+ Integer val = Integer.parseInt(revisionArr[i]);
+ if (val < 10) {
+ rev = rev + "0";
+ }
+ rev = rev + val;
}
- rev = rev + val;
+
+ return rev;
+ } else {
+ throw new TranslatorException("Date in revision is not proper: " + date);
}
- return rev;
}
/**
@@ -109,10 +129,14 @@
*/
public static String getPkgFrmArr(ArrayList<String> pkgArr) {
- String pkg = "";
+ String pkg = UtilConstants.EMPTY_STRING;
int size = pkgArr.size();
int i = 0;
for (String member : pkgArr) {
+ boolean presenceOfKeyword = UtilConstants.JAVA_KEY_WORDS.contains(member);
+ if (presenceOfKeyword || (member.matches(UtilConstants.REGEX_FOR_FIRST_DIGIT))) {
+ member = UtilConstants.UNDER_SCORE + member;
+ }
pkg = pkg + member;
if (i != size - 1) {
pkg = pkg + UtilConstants.PERIOD;
@@ -174,4 +198,14 @@
public static String getCaptialCase(String yangIdentifier) {
return yangIdentifier.substring(0, 1).toUpperCase() + yangIdentifier.substring(1);
}
+
+ /**
+ * Translate the YANG identifier name to java identifier with first letter in small.
+ *
+ * @param yangIdentifier identifier in YANG file.
+ * @return corresponding java identifier
+ */
+ public static String getLowerCase(String yangIdentifier) {
+ return yangIdentifier.substring(0, 1).toLowerCase() + yangIdentifier.substring(1);
+ }
}
diff --git a/src/main/java/org/onosproject/yangutils/utils/UtilConstants.java b/src/main/java/org/onosproject/yangutils/utils/UtilConstants.java
index f596bf8..63c1fe7 100644
--- a/src/main/java/org/onosproject/yangutils/utils/UtilConstants.java
+++ b/src/main/java/org/onosproject/yangutils/utils/UtilConstants.java
@@ -16,6 +16,9 @@
package org.onosproject.yangutils.utils;
+import java.util.Arrays;
+import java.util.List;
+
/**
* Provides utility constants while generating java files.
*/
@@ -41,6 +44,8 @@
public static final String JAVA_DOC_RETURN = " * @return ";
public static final String JAVA_DOC_THROWS = " * @throws ";
public static final String JAVA_DOC_SETTERS = " * Returns the builder object of ";
+ public static final String JAVA_DOC_OF = " * Returns the object of ";
+ public static final String JAVA_DOC_SETTERS_COMMON = " * Sets the value of ";
public static final String JAVA_DOC_GETTERS = " * Returns the attribute ";
public static final String JAVA_DOC_DEFAULT_CONSTRUCTOR = " * Default Constructor.\n";
public static final String JAVA_DOC_CONSTRUCTOR = " * Construct the object of ";
@@ -51,9 +56,11 @@
* Basic requirements.
*/
public static final String NEW_LINE = "\n";
+ public static final String EMPTY_STRING = "";
public static final String NEW_LINE_ESTRIC = " *\n";
public static final String PERIOD = ".";
public static final String COLAN = ":";
+ public static final String UNDER_SCORE = "_";
public static final String SEMI_COLAN = ";";
public static final String HYPHEN = "-";
public static final String SPACE = " ";
@@ -63,6 +70,25 @@
public static final String ADD = "+";
public static final String ASTERISK = "*";
public static final String AT = "@";
+ public static final String QUOTES = "\"";
+ public static final String AND = "&";
+ public static final String COMMA = ",";
+ public static final String ADD_STRING = "add";
+ public static final String CHECK_NOT_NULL_STRING = "checkNotNull";
+ public static final String HASH_CODE_STRING = "hashCode";
+ public static final String EQUALS_STRING = "equals";
+ public static final String OBJECT_STRING = "Object";
+ public static final String INSTANCE_OF = " instanceof ";
+
+ public static final String VALUE = "value";
+
+ public static final String IF = "if";
+ public static final String FOR = "for";
+ public static final String WHILE = "while";
+ public static final String OF = "of";
+
+ public static final String TRUE = "true";
+ public static final String FALSE = "false";
/**
* For brackets.
@@ -193,7 +219,23 @@
public static final String DOUBLE_WRAPPER = "Double";
/**
- * For idenifiers.
+ * List of keywords in java, this is used for checking if the input does not contain these keywords.
+ */
+ public static final List JAVA_KEY_WORDS = Arrays.asList("abstract", "assert", "boolean", "break", "byte", "case",
+ "catch", "char", "class", "const", "continue", "default", "do", "double", "else", "extends", "false",
+ "final", "finally", "float", "for", "goto", "if", "implements", "import", "instanceof", "int", "interface",
+ "long", "native", "new", "null", "package", "private", "protected", "public", "return", "short", "static",
+ "strictfp", "super", "switch", "synchronized", "this", "throw", "throws", "transient", "true", "try",
+ "void", "volatile", "while");
+
+ /**
+ * Defining regular expression.
+ */
+ public static final String REGEX_WITH_SPECIAL_CHAR = "[ : / - @ $ # ' * + , ; = ]+";
+ public static final String REGEX_FOR_FIRST_DIGIT = "\\d.*";
+
+ /**
+ * For identifiers.
*/
public static final String CLASS = "class";
public static final String BUILDER = "Builder";
@@ -218,7 +260,9 @@
/**
* For collections.
*/
- public static final String COLLECTION_IMPORTS = "import java.util.";
+ public static final String COLLECTION_IMPORTS = "java.util";
+ public static final String MORE_OBJECT_IMPORT = "import com.google.common.base.MoreObjects;\n";
+ public static final String JAVA_UTIL_OBJECTS_IMPORT = "import java.util.Objects;\n";
public static final String ABSTRACT_COLLECTION = "AbstractCollection";
public static final String LIST = "List";