Committed by
Gerrit Code Review
[ONOS-3906] Utils for translator implementation.
Change-Id: I1d3617e81c332b6f0194a4b4b9e373c4297a6c67
Showing
3 changed files
with
288 additions
and
0 deletions
| 1 | +/* | ||
| 2 | + * Copyright 2016 Open Networking Laboratory | ||
| 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 org.onosproject.yangutils.utils; | ||
| 18 | + | ||
| 19 | +/** | ||
| 20 | + * Provides utility constants while generating java files. | ||
| 21 | + */ | ||
| 22 | +public final class UtilConstants { | ||
| 23 | + | ||
| 24 | + /** | ||
| 25 | + * Default constructor. | ||
| 26 | + */ | ||
| 27 | + private UtilConstants() { | ||
| 28 | + } | ||
| 29 | + | ||
| 30 | + /** | ||
| 31 | + * For java-docs. | ||
| 32 | + */ | ||
| 33 | + public static final String IMPL_CLASS_JAVA_DOC = " * Provides the implementation of "; | ||
| 34 | + public static final String BUILDER_CLASS_JAVA_DOC = " * Provides the builder implementation of "; | ||
| 35 | + public static final String INTERFACE_JAVA_DOC = " * Abstraction of an entity which provides functionalities of "; | ||
| 36 | + public static final String BUILDER_INTERFACE_JAVA_DOC = " * Builder for "; | ||
| 37 | + public static final String PACKAGE_INFO_JAVADOC = " * Generated java code for the YANG file "; | ||
| 38 | + public static final String JAVA_DOC_FIRST_LINE = "/**\n"; | ||
| 39 | + public static final String JAVA_DOC_END_LINE = " */\n"; | ||
| 40 | + public static final String JAVA_DOC_PARAM = " * @param "; | ||
| 41 | + public static final String JAVA_DOC_RETURN = " * @return "; | ||
| 42 | + public static final String JAVA_DOC_THROWS = " * @throws "; | ||
| 43 | + public static final String JAVA_DOC_SETTERS = " * Returns the builder object of "; | ||
| 44 | + public static final String JAVA_DOC_GETTERS = " * Returns the attribute "; | ||
| 45 | + public static final String JAVA_DOC_DEFAULT_CONSTRUCTOR = " * Default Constructor.\n"; | ||
| 46 | + public static final String JAVA_DOC_CONSTRUCTOR = " * Construct the object of "; | ||
| 47 | + public static final String JAVA_DOC_BUILD = " * Builds object of "; | ||
| 48 | + public static final String JAVA_DOC_BUILD_RETURN = "object of "; | ||
| 49 | + | ||
| 50 | + /** | ||
| 51 | + * Basic requirements. | ||
| 52 | + */ | ||
| 53 | + public static final String NEW_LINE = "\n"; | ||
| 54 | + public static final String NEW_LINE_ESTRIC = " *\n"; | ||
| 55 | + public static final String PERIOD = "."; | ||
| 56 | + public static final String COLAN = ":"; | ||
| 57 | + public static final String SEMI_COLAN = ";"; | ||
| 58 | + public static final String HYPHEN = "-"; | ||
| 59 | + public static final String SPACE = " "; | ||
| 60 | + public static final String TAB = "\t"; | ||
| 61 | + public static final String EQUAL = "="; | ||
| 62 | + public static final String SLASH = "/"; | ||
| 63 | + public static final String ADD = "+"; | ||
| 64 | + public static final String ASTERISK = "*"; | ||
| 65 | + public static final String AT = "@"; | ||
| 66 | + | ||
| 67 | + /** | ||
| 68 | + * For brackets. | ||
| 69 | + */ | ||
| 70 | + public static final String DIAMOND_OPEN_BRACKET = "<"; | ||
| 71 | + public static final String DIAMOND_CLOSE_BRACKET = ">"; | ||
| 72 | + public static final String SQUARE_OPEN_BRACKET = "["; | ||
| 73 | + public static final String SQUARE_CLOSE_BRACKET = "]"; | ||
| 74 | + public static final String OPEN_PARENTHESIS = "("; | ||
| 75 | + public static final String CLOSE_PARENTHESIS = ")"; | ||
| 76 | + public static final String OPEN_CURLY_BRACKET = "{"; | ||
| 77 | + public static final String CLOSE_CURLY_BRACKET = "}"; | ||
| 78 | + | ||
| 79 | + /** | ||
| 80 | + * For methods. | ||
| 81 | + */ | ||
| 82 | + public static final String GET_METHOD_PREFIX = "get"; | ||
| 83 | + public static final String SET_METHOD_PREFIX = "set"; | ||
| 84 | + | ||
| 85 | + /** | ||
| 86 | + * For indentation. | ||
| 87 | + */ | ||
| 88 | + public static final String FOUR_SPACE_INDENTATION = " "; | ||
| 89 | + public static final String EIGHT_SPACE_INDENTATION = FOUR_SPACE_INDENTATION + FOUR_SPACE_INDENTATION; | ||
| 90 | + public static final String TWELVE_SPACE_INDENTATION = FOUR_SPACE_INDENTATION + EIGHT_SPACE_INDENTATION; | ||
| 91 | + public static final String SIXTEEN_SPACE_INDENTATION = EIGHT_SPACE_INDENTATION + EIGHT_SPACE_INDENTATION; | ||
| 92 | + | ||
| 93 | + /** | ||
| 94 | + * For directories. | ||
| 95 | + */ | ||
| 96 | + public static final String YANG_GEN_DIR = "src/main/yangmodal/"; | ||
| 97 | + public static final String DEFAULT_BASE_PKG = "org.onosproject.yang.gen"; | ||
| 98 | + public static final String REVISION_PREFIX = "rev"; | ||
| 99 | + public static final String VERSION_PREFIX = "v"; | ||
| 100 | + | ||
| 101 | + /** | ||
| 102 | + * For class modifiers. | ||
| 103 | + */ | ||
| 104 | + public static final String PRIVATE = "private"; | ||
| 105 | + public static final String PUBLIC = "public"; | ||
| 106 | + public static final String PROTECTED = "protected"; | ||
| 107 | + | ||
| 108 | + /** | ||
| 109 | + * For data types. | ||
| 110 | + */ | ||
| 111 | + public static final String INT = "int"; | ||
| 112 | + public static final String VOID = "void"; | ||
| 113 | + public static final String SHORT = "short"; | ||
| 114 | + public static final String LONG = "long"; | ||
| 115 | + public static final String BOOLEAN = "boolean"; | ||
| 116 | + public static final String STRING = "String"; | ||
| 117 | + public static final String FLOAT = "float"; | ||
| 118 | + public static final String BYTE = "byte"; | ||
| 119 | + public static final String DOUBLE = "double"; | ||
| 120 | + | ||
| 121 | + /** | ||
| 122 | + * For idenifiers. | ||
| 123 | + */ | ||
| 124 | + public static final String CLASS = "class"; | ||
| 125 | + public static final String BUILDER = "Builder"; | ||
| 126 | + public static final String BUILDER_OBJECT = "builder object of "; | ||
| 127 | + public static final String INTERFACE = "interface"; | ||
| 128 | + public static final String ENUM = "enum"; | ||
| 129 | + public static final String STATIC = "static"; | ||
| 130 | + public static final String FINAL = "final"; | ||
| 131 | + public static final String PACKAGE = "package"; | ||
| 132 | + public static final String IMPORT = "import"; | ||
| 133 | + public static final String NULL = "null"; | ||
| 134 | + public static final String RETURN = "return"; | ||
| 135 | + public static final String NEW = "new"; | ||
| 136 | + public static final String THIS = "this"; | ||
| 137 | + public static final String IMPLEMENTS = "implements"; | ||
| 138 | + public static final String EXTEND = "extends"; | ||
| 139 | + public static final String IMPL = "Impl"; | ||
| 140 | + public static final String BUILD = "build"; | ||
| 141 | + public static final String OBJECT = "Object"; | ||
| 142 | + public static final String OVERRIDE = "@Override"; | ||
| 143 | + | ||
| 144 | + /** | ||
| 145 | + * For collections. | ||
| 146 | + */ | ||
| 147 | + public static final String ABSTRACT_COLLECTION = "AbstractCollection"; | ||
| 148 | + | ||
| 149 | + public static final String LIST = "List"; | ||
| 150 | + public static final String LINKED_LIST = "LinkedList"; | ||
| 151 | + public static final String ARRAY_LIST = "ArrayList"; | ||
| 152 | + public static final String ABSTRACT_LIST = "AbstractList"; | ||
| 153 | + public static final String ABSTRACT_SEQUENTAIL_LIST = "AbstractSequentialList"; | ||
| 154 | + | ||
| 155 | + public static final String SET = "Set"; | ||
| 156 | + public static final String HASH_SET = "HashSet"; | ||
| 157 | + public static final String ABSTRACT_SET = "AbstractSet"; | ||
| 158 | + public static final String LINKED_HASH_SET = "LinkedHashSet"; | ||
| 159 | + public static final String TREE_SET = "TreeSet"; | ||
| 160 | + | ||
| 161 | + public static final String MAP = "Map"; | ||
| 162 | + public static final String ABSTRACT_MAP = "AbstractMap"; | ||
| 163 | + public static final String HASH_MAP = "HashMap"; | ||
| 164 | + public static final String TREE_MAP = "TreeMap"; | ||
| 165 | + public static final String CONCURRENT_MAP = "ConcurrentMap"; | ||
| 166 | + public static final String EVENTUALLY_CONSISTENT_MAP = "EventuallyConsitentMap"; | ||
| 167 | + public static final String STACK = "stack"; | ||
| 168 | +} |
utils/yangutils/src/main/java/org/onosproject/yangutils/utils/io/impl/CopyrightHeader.java
0 → 100644
| 1 | +/* | ||
| 2 | + * Copyright 2016 Open Networking Laboratory | ||
| 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 org.onosproject.yangutils.utils.io.impl; | ||
| 18 | + | ||
| 19 | +import java.io.BufferedReader; | ||
| 20 | +import java.io.File; | ||
| 21 | +import java.io.FileOutputStream; | ||
| 22 | +import java.io.FileReader; | ||
| 23 | +import java.io.IOException; | ||
| 24 | +import java.io.InputStream; | ||
| 25 | +import java.io.OutputStream; | ||
| 26 | + | ||
| 27 | +import static org.slf4j.LoggerFactory.getLogger; | ||
| 28 | +import org.slf4j.Logger; | ||
| 29 | + | ||
| 30 | +/** | ||
| 31 | + * Provides the license header for the generated files. | ||
| 32 | + */ | ||
| 33 | +public final class CopyrightHeader { | ||
| 34 | + | ||
| 35 | + private static final Logger log = getLogger(CopyrightHeader.class); | ||
| 36 | + private static final int NULL = -1; | ||
| 37 | + private static ClassLoader classLoader = CopyrightHeader.class.getClassLoader(); | ||
| 38 | + | ||
| 39 | + /** | ||
| 40 | + * Default constructor. | ||
| 41 | + */ | ||
| 42 | + private CopyrightHeader() { | ||
| 43 | + } | ||
| 44 | + | ||
| 45 | + /** | ||
| 46 | + * Returns Copyright file header. | ||
| 47 | + * | ||
| 48 | + * @return Copyright file header | ||
| 49 | + */ | ||
| 50 | + public static String getCopyrightHeader() { | ||
| 51 | + return parseOnosHeader(); | ||
| 52 | + } | ||
| 53 | + | ||
| 54 | + /** | ||
| 55 | + * parse Copyright to the temporary file. | ||
| 56 | + * | ||
| 57 | + * @param file generated file | ||
| 58 | + * @param stream input stream | ||
| 59 | + */ | ||
| 60 | + private static String parseOnosHeader() { | ||
| 61 | + | ||
| 62 | + File temp = new File("temp.txt"); | ||
| 63 | + | ||
| 64 | + try { | ||
| 65 | + InputStream stream = classLoader.getResourceAsStream("CopyrightHeader.txt"); | ||
| 66 | + OutputStream out = new FileOutputStream(temp); | ||
| 67 | + int index; | ||
| 68 | + while ((index = stream.read()) != NULL) { | ||
| 69 | + out.write(index); | ||
| 70 | + } | ||
| 71 | + out.close(); | ||
| 72 | + return convertToString(temp.toString()); | ||
| 73 | + } catch (IOException e) { | ||
| 74 | + log.info("Failed to insert onos header in files."); | ||
| 75 | + } finally { | ||
| 76 | + temp.delete(); | ||
| 77 | + } | ||
| 78 | + return ""; | ||
| 79 | + } | ||
| 80 | + | ||
| 81 | + /** | ||
| 82 | + * Converts it to string. | ||
| 83 | + * | ||
| 84 | + * @param toAppend file to be converted. | ||
| 85 | + * @return string of file. | ||
| 86 | + * @throws IOException when fails to convert to string | ||
| 87 | + */ | ||
| 88 | + private static String convertToString(String toAppend) throws IOException { | ||
| 89 | + BufferedReader bufferReader = new BufferedReader(new FileReader(toAppend)); | ||
| 90 | + try { | ||
| 91 | + StringBuilder stringBuilder = new StringBuilder(); | ||
| 92 | + String line = bufferReader.readLine(); | ||
| 93 | + | ||
| 94 | + while (line != null) { | ||
| 95 | + stringBuilder.append(line); | ||
| 96 | + stringBuilder.append("\n"); | ||
| 97 | + line = bufferReader.readLine(); | ||
| 98 | + } | ||
| 99 | + return stringBuilder.toString(); | ||
| 100 | + } finally { | ||
| 101 | + bufferReader.close(); | ||
| 102 | + } | ||
| 103 | + } | ||
| 104 | +} |
| 1 | +/* | ||
| 2 | + * Copyright 2016 Open Networking Laboratory | ||
| 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 | + |
-
Please register or login to post a comment