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.YangGrouping; |
| 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; |
| 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.getCurNodePackage; |
| 33 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getPackageDirPathFromJavaJPackage; |
| 34 | import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getAbsolutePackagePath; |
| 35 | |
| 36 | /** |
| 37 | * Grouping information extended to support java code generation. |
| 38 | */ |
| 39 | public class YangJavaGrouping extends YangGrouping |
| 40 | implements JavaCodeGenerator, HasJavaFileInfo, |
| 41 | HasJavaImportData, HasTempJavaCodeFragmentFiles { |
| 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 | * Default constructor. |
| 62 | */ |
| 63 | public YangJavaGrouping() { |
| 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) { |
| 79 | throw new RuntimeException("Missing java info in java datamodel node"); |
| 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 | * Prepare the information for java code generation corresponding to YANG |
| 146 | * container info. |
| 147 | * |
| 148 | * @param codeGenDir code generation directory |
| 149 | * @throws IOException IO operation fail |
| 150 | */ |
| 151 | @Override |
| 152 | public void generateCodeEntry(String codeGenDir) throws IOException { |
| 153 | |
| 154 | getJavaFileInfo().setJavaName(getCaptialCase(getCamelCase(getName()))); |
| 155 | getJavaFileInfo().setPackage(getCurNodePackage(this)); |
| 156 | getJavaFileInfo().setPackageFilePath( |
| 157 | getPackageDirPathFromJavaJPackage(getJavaFileInfo().getPackage())); |
| 158 | getJavaFileInfo().setBaseCodeGenPath(codeGenDir); |
| 159 | |
| 160 | String absloutePath = getAbsolutePackagePath( |
| 161 | getJavaFileInfo().getBaseCodeGenPath(), |
| 162 | getJavaFileInfo().getPackageFilePath()); |
| 163 | |
| 164 | setTempJavaCodeFragmentFiles(new TempJavaCodeFragmentFiles( |
| 165 | getJavaFileInfo().getGeneratedFileTypes(), absloutePath, |
| 166 | getJavaFileInfo().getJavaName())); |
| 167 | |
| 168 | getTempJavaCodeFragmentFiles().addCurNodeLeavesInfoToTempFiles(this); |
| 169 | |
| 170 | getTempJavaCodeFragmentFiles().addCurNodeInfoInParentTempFile(this, false); |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Create a java file using the YANG grouping info. |
| 175 | */ |
| 176 | @Override |
| 177 | public void generateCodeExit() { |
| 178 | // TODO Auto-generated method stub |
| 179 | |
| 180 | } |
| 181 | } |