Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 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 | package org.onosproject.yangutils.translator.tojava.javamodel; |
| 17 | |
| 18 | import java.io.IOException; |
| 19 | |
| 20 | import org.onosproject.yangutils.datamodel.YangModule; |
| 21 | import org.onosproject.yangutils.translator.tojava.HasJavaFileInfo; |
| 22 | import org.onosproject.yangutils.translator.tojava.HasJavaImportData; |
| 23 | import org.onosproject.yangutils.translator.tojava.HasTempJavaCodeFragmentFiles; |
| 24 | import org.onosproject.yangutils.translator.tojava.JavaCodeGenerator; |
| 25 | import org.onosproject.yangutils.translator.tojava.JavaFileInfo; |
| 26 | import org.onosproject.yangutils.translator.tojava.JavaImportData; |
| 27 | import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles; |
| 28 | |
| 29 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_INTERFACE_WITH_BUILDER; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 30 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase; |
| 31 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCaptialCase; |
| 32 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getPackageDirPathFromJavaJPackage; |
| 33 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getRootPackage; |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 34 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getAbsolutePackagePath; |
| 35 | |
| 36 | /** |
| 37 | * Module information extended to support java code generation. |
| 38 | */ |
| 39 | public class YangJavaModule extends YangModule |
janani b | a302749 | 2016-03-24 12:43:48 +0530 | [diff] [blame^] | 40 | implements JavaCodeGenerator, HasJavaFileInfo, |
| 41 | HasJavaImportData, HasTempJavaCodeFragmentFiles { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 42 | |
| 43 | /** |
| 44 | * Contains the information of the java file being generated. |
| 45 | */ |
| 46 | private JavaFileInfo javaFileInfo; |
| 47 | |
| 48 | /** |
| 49 | * Contains information of the imports to be inserted in the java file |
| 50 | * generated. |
| 51 | */ |
| 52 | private JavaImportData javaImportData; |
| 53 | |
| 54 | /** |
| 55 | * File handle to maintain temporary java code fragments as per the code |
| 56 | * snippet types. |
| 57 | */ |
| 58 | private TempJavaCodeFragmentFiles tempFileHandle; |
| 59 | |
| 60 | /** |
| 61 | * Create a YANG node of module type. |
| 62 | */ |
| 63 | public YangJavaModule() { |
| 64 | super(); |
| 65 | setJavaFileInfo(new JavaFileInfo()); |
| 66 | setJavaImportData(new JavaImportData()); |
| 67 | getJavaFileInfo().setGeneratedFileTypes(GENERATE_INTERFACE_WITH_BUILDER); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Get the generated java file information. |
| 72 | * |
| 73 | * @return generated java file information |
| 74 | */ |
| 75 | @Override |
| 76 | public JavaFileInfo getJavaFileInfo() { |
| 77 | |
| 78 | if (javaFileInfo == null) { |
janani b | a302749 | 2016-03-24 12:43:48 +0530 | [diff] [blame^] | 79 | throw new RuntimeException("Missing java info in java datamodel node."); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 80 | } |
| 81 | return javaFileInfo; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Set the java file info object. |
| 86 | * |
| 87 | * @param javaInfo java file info object |
| 88 | */ |
| 89 | @Override |
| 90 | public void setJavaFileInfo(JavaFileInfo javaInfo) { |
| 91 | |
| 92 | javaFileInfo = javaInfo; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Get the data of java imports to be included in generated file. |
| 97 | * |
| 98 | * @return data of java imports to be included in generated file |
| 99 | */ |
| 100 | @Override |
| 101 | public JavaImportData getJavaImportData() { |
| 102 | |
| 103 | return javaImportData; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Set the data of java imports to be included in generated file. |
| 108 | * |
| 109 | * @param javaImportData data of java imports to be included in generated |
| 110 | * file |
| 111 | */ |
| 112 | @Override |
| 113 | public void setJavaImportData(JavaImportData javaImportData) { |
| 114 | |
| 115 | this.javaImportData = javaImportData; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Get the temporary file handle. |
| 120 | * |
| 121 | * @return temporary file handle |
| 122 | */ |
| 123 | @Override |
| 124 | public TempJavaCodeFragmentFiles getTempJavaCodeFragmentFiles() { |
| 125 | |
| 126 | if (tempFileHandle == null) { |
| 127 | throw new RuntimeException("missing temp file hand for current node " |
| 128 | + getJavaFileInfo().getJavaName()); |
| 129 | } |
| 130 | return tempFileHandle; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Set temporary file handle. |
| 135 | * |
| 136 | * @param fileHandle temporary file handle |
| 137 | */ |
| 138 | @Override |
| 139 | public void setTempJavaCodeFragmentFiles(TempJavaCodeFragmentFiles fileHandle) { |
| 140 | |
| 141 | tempFileHandle = fileHandle; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Generates java code for module. |
| 146 | * |
| 147 | * @param baseCodeGenDir code generation directory |
| 148 | * @throws IOException when fails to generate the source files |
| 149 | */ |
| 150 | @Override |
| 151 | public void generateCodeEntry(String baseCodeGenDir) throws IOException { |
| 152 | |
| 153 | getJavaFileInfo().setJavaName(getCaptialCase(getCamelCase(getName()))); |
| 154 | getJavaFileInfo().setPackage(getRootPackage(getVersion(), getNameSpace().getUri(), |
| 155 | getRevision().getRevDate())); |
| 156 | getJavaFileInfo().setPackageFilePath( |
| 157 | getPackageDirPathFromJavaJPackage(getJavaFileInfo().getPackage())); |
| 158 | getJavaFileInfo().setBaseCodeGenPath(baseCodeGenDir); |
| 159 | |
| 160 | String absloutePath = getAbsolutePackagePath( |
| 161 | getJavaFileInfo().getBaseCodeGenPath(), |
| 162 | getJavaFileInfo().getPackageFilePath()); |
| 163 | |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 164 | setTempJavaCodeFragmentFiles(new TempJavaCodeFragmentFiles( |
| 165 | getJavaFileInfo().getGeneratedFileTypes(), absloutePath, |
| 166 | getJavaFileInfo().getJavaName())); |
| 167 | |
| 168 | getTempJavaCodeFragmentFiles().addCurNodeLeavesInfoToTempFiles(this); |
| 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public void generateCodeExit() throws IOException { |
| 173 | |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 174 | getTempJavaCodeFragmentFiles().setCurYangNode(this); |
| 175 | getTempJavaCodeFragmentFiles().generateJavaFile(GENERATE_INTERFACE_WITH_BUILDER, this); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 176 | return; |
| 177 | } |
| 178 | } |