Gaurav Agrawal | f576642 | 2016-03-23 19:04:17 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0f7908b | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Gaurav Agrawal | f576642 | 2016-03-23 19:04:17 +0530 | [diff] [blame] | 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.translator.tojava.javamodel; |
| 18 | |
| 19 | import java.io.IOException; |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 20 | |
Gaurav Agrawal | f576642 | 2016-03-23 19:04:17 +0530 | [diff] [blame] | 21 | import org.onosproject.yangutils.datamodel.YangOutput; |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 22 | import org.onosproject.yangutils.translator.exception.TranslatorException; |
Gaurav Agrawal | f576642 | 2016-03-23 19:04:17 +0530 | [diff] [blame] | 23 | import org.onosproject.yangutils.translator.tojava.HasJavaFileInfo; |
| 24 | import org.onosproject.yangutils.translator.tojava.HasJavaImportData; |
| 25 | import org.onosproject.yangutils.translator.tojava.HasTempJavaCodeFragmentFiles; |
| 26 | import org.onosproject.yangutils.translator.tojava.JavaCodeGenerator; |
| 27 | import org.onosproject.yangutils.translator.tojava.JavaFileInfo; |
| 28 | import org.onosproject.yangutils.translator.tojava.JavaImportData; |
| 29 | import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles; |
| 30 | |
| 31 | import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_INTERFACE_WITH_BUILDER; |
Gaurav Agrawal | f576642 | 2016-03-23 19:04:17 +0530 | [diff] [blame] | 32 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase; |
| 33 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCaptialCase; |
| 34 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCurNodePackage; |
| 35 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getPackageDirPathFromJavaJPackage; |
| 36 | import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.createPackage; |
| 37 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getAbsolutePackagePath; |
| 38 | |
| 39 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame^] | 40 | * Represents output information extended to support java code generation. |
Gaurav Agrawal | f576642 | 2016-03-23 19:04:17 +0530 | [diff] [blame] | 41 | */ |
| 42 | public class YangJavaOutput extends YangOutput |
| 43 | implements JavaCodeGenerator, HasJavaFileInfo, |
| 44 | HasJavaImportData, HasTempJavaCodeFragmentFiles { |
| 45 | |
| 46 | /** |
| 47 | * Contains information of the java file being generated. |
| 48 | */ |
| 49 | private JavaFileInfo javaFileInfo; |
| 50 | |
| 51 | /** |
| 52 | * Contains information of the imports to be inserted in the java file |
| 53 | * generated. |
| 54 | */ |
| 55 | private JavaImportData javaImportData; |
| 56 | |
| 57 | /** |
| 58 | * File handle to maintain temporary java code fragments as per the code |
| 59 | * snippet types. |
| 60 | */ |
| 61 | private TempJavaCodeFragmentFiles tempFileHandle; |
| 62 | |
| 63 | /** |
| 64 | * Creates an instance of java output. |
| 65 | */ |
| 66 | public YangJavaOutput() { |
| 67 | super(); |
| 68 | setJavaFileInfo(new JavaFileInfo()); |
| 69 | setJavaImportData(new JavaImportData()); |
| 70 | getJavaFileInfo().setGeneratedFileTypes(GENERATE_INTERFACE_WITH_BUILDER); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Returns the generated java file information. |
| 75 | * |
| 76 | * @return generated java file information |
| 77 | */ |
| 78 | @Override |
| 79 | public JavaFileInfo getJavaFileInfo() { |
| 80 | |
| 81 | if (javaFileInfo == null) { |
Bharat saraswal | 780eca3 | 2016-04-05 12:45:45 +0530 | [diff] [blame] | 82 | throw new TranslatorException("Missing java info in java datamodel node"); |
Gaurav Agrawal | f576642 | 2016-03-23 19:04:17 +0530 | [diff] [blame] | 83 | } |
| 84 | return javaFileInfo; |
| 85 | } |
| 86 | |
| 87 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame^] | 88 | * Sets the java file info object. |
Gaurav Agrawal | f576642 | 2016-03-23 19:04:17 +0530 | [diff] [blame] | 89 | * |
| 90 | * @param javaInfo java file info object |
| 91 | */ |
| 92 | @Override |
| 93 | public void setJavaFileInfo(JavaFileInfo javaInfo) { |
Gaurav Agrawal | f576642 | 2016-03-23 19:04:17 +0530 | [diff] [blame] | 94 | javaFileInfo = javaInfo; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Returns the data of java imports to be included in generated file. |
| 99 | * |
| 100 | * @return data of java imports to be included in generated file |
| 101 | */ |
| 102 | @Override |
| 103 | public JavaImportData getJavaImportData() { |
Gaurav Agrawal | f576642 | 2016-03-23 19:04:17 +0530 | [diff] [blame] | 104 | return javaImportData; |
| 105 | } |
| 106 | |
| 107 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame^] | 108 | * Sets the data of java imports to be included in generated file. |
Gaurav Agrawal | f576642 | 2016-03-23 19:04:17 +0530 | [diff] [blame] | 109 | * |
| 110 | * @param javaImportData data of java imports to be included in generated |
| 111 | * file |
| 112 | */ |
| 113 | @Override |
| 114 | public void setJavaImportData(JavaImportData javaImportData) { |
Gaurav Agrawal | f576642 | 2016-03-23 19:04:17 +0530 | [diff] [blame] | 115 | this.javaImportData = javaImportData; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Returns the temporary file handle. |
| 120 | * |
| 121 | * @return temporary file handle |
| 122 | */ |
| 123 | @Override |
| 124 | public TempJavaCodeFragmentFiles getTempJavaCodeFragmentFiles() { |
Gaurav Agrawal | f576642 | 2016-03-23 19:04:17 +0530 | [diff] [blame] | 125 | return tempFileHandle; |
| 126 | } |
| 127 | |
| 128 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame^] | 129 | * Sets temporary file handle. |
Gaurav Agrawal | f576642 | 2016-03-23 19:04:17 +0530 | [diff] [blame] | 130 | * |
| 131 | * @param fileHandle temporary file handle |
| 132 | */ |
| 133 | @Override |
| 134 | public void setTempJavaCodeFragmentFiles(TempJavaCodeFragmentFiles fileHandle) { |
Gaurav Agrawal | f576642 | 2016-03-23 19:04:17 +0530 | [diff] [blame] | 135 | tempFileHandle = fileHandle; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Prepare the information for java code generation corresponding to YANG |
| 140 | * container info. |
| 141 | * |
| 142 | * @param codeGenDir code generation directory |
| 143 | * @throws IOException IO operation fail |
| 144 | */ |
| 145 | @Override |
| 146 | public void generateCodeEntry(String codeGenDir) throws IOException { |
| 147 | |
| 148 | getJavaFileInfo().setJavaName(getCaptialCase(getCamelCase(getName()))); |
| 149 | getJavaFileInfo().setPackage(getCurNodePackage(this)); |
| 150 | getJavaFileInfo().setPackageFilePath( |
| 151 | getPackageDirPathFromJavaJPackage(getJavaFileInfo().getPackage())); |
| 152 | getJavaFileInfo().setBaseCodeGenPath(codeGenDir); |
| 153 | |
| 154 | String absloutePath = getAbsolutePackagePath( |
| 155 | getJavaFileInfo().getBaseCodeGenPath(), |
| 156 | getJavaFileInfo().getPackageFilePath()); |
| 157 | createPackage(absloutePath, getName()); |
| 158 | setTempJavaCodeFragmentFiles(new TempJavaCodeFragmentFiles( |
| 159 | getJavaFileInfo().getGeneratedFileTypes(), absloutePath, |
| 160 | getJavaFileInfo().getJavaName())); |
| 161 | |
| 162 | getTempJavaCodeFragmentFiles().addCurNodeLeavesInfoToTempFiles(this); |
| 163 | |
| 164 | getTempJavaCodeFragmentFiles().addCurNodeInfoInParentTempFile(this, false); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Create a java file using the YANG grouping info. |
| 169 | * |
| 170 | * @throws IOException IO operation fail |
| 171 | */ |
| 172 | @Override |
| 173 | public void generateCodeExit() throws IOException { |
Bharat saraswal | 84366c5 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 174 | getTempJavaCodeFragmentFiles().generateJavaFile(GENERATE_INTERFACE_WITH_BUILDER, this); |
Gaurav Agrawal | f576642 | 2016-03-23 19:04:17 +0530 | [diff] [blame] | 175 | } |
| 176 | } |