blob: b3e9ff53082557beaf70a7c09b07fde70ccd4809 [file] [log] [blame]
Bharat saraswal4bf8b152016-02-25 02:26:43 +05301/*
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
17package org.onosproject.yangutils.utils.io.impl;
18
19import java.io.BufferedInputStream;
20import java.io.BufferedOutputStream;
21import java.io.File;
22import java.io.FileInputStream;
23import java.io.FileNotFoundException;
24import java.io.FileOutputStream;
25import java.io.IOException;
26import java.io.InputStream;
27import java.io.ObjectInput;
28import java.io.ObjectInputStream;
29import java.io.ObjectOutput;
30import java.io.ObjectOutputStream;
31import java.io.OutputStream;
32import java.util.ArrayList;
33import java.util.List;
34
35/**
Vinod Kumar Sc4216002016-03-03 19:55:30 +053036 * Provides storage for Temp data while traversing data model tree for code
37 * generation.
Bharat saraswal4bf8b152016-02-25 02:26:43 +053038 */
39public final class TempDataStore {
40
41 /**
42 * Data Store types.
43 */
44 public static enum TempDataStoreType {
45
46 /**
47 * Methods.
48 */
49 GETTER_METHODS,
50
51 /**
52 * Methods.
53 */
54 BUILDER_METHODS,
55
56 /**
57 * Methods.
58 */
59 BUILDER_INTERFACE_METHODS,
60
61 /**
62 * Methods.
63 */
64 IMPL_METHODS,
65
66 /**
67 * Attributes.
68 */
69 ATTRIBUTE,
70
71 /**
72 * Imports.
73 */
74 IMPORT
75 }
76
77 /**
78 * File name string for Temp files of methods.
79 */
80 private static final String GETTER_METHOD_FILE_NAME = "TempGetterMethodDataStore";
81
82 /**
83 * File name string for Temp files of methods.
84 */
85 private static final String BUILDER_METHOD_FILE_NAME = "TempBuilderMethodDataStore";
86
87 /**
88 * File name string for Temp files of methods.
89 */
90 private static final String BUILDER_INTERFACE_METHOD_FILE_NAME = "TempBuilderInterfaceMethodDataStore";
91
92 /**
93 * File name string for Temp files of methods.
94 */
95 private static final String IMPL_METHOD_FILE_NAME = "TempImplMethodDataStore";
96
97 /**
98 * File name string for Temp files of attributes.
99 */
100 private static final String ATTRIBUTE_FILE_NAME = "TempAttributeDataStore";
101
102 /**
103 * File name string for Temp files of imports.
104 */
105 private static final String IMPORT_FILE_NAME = "TempImportDataStore";
106
107 /**
108 * File extension of Temp files.
109 */
110 private static final String FILE_EXTENSION = ".tmp";
111
112 /**
113 * Directory for generating Temp files.
114 */
115 private static final String GEN_DIR = "target/";
116
117 /**
118 * Buffer size.
119 */
120 private static final int BUFFER_SIZE = 8 * 1024;
121
122 /**
123 * Default constructor.
124 */
125 private TempDataStore() {
126 }
127
128 /**
129 * Writes specific info to a Temp file.
130 *
131 * @param data data to be stored
132 * @param type type of Temp data store
133 * @param className class name
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530134 * @throws IOException when fails to create a Temp data file
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530135 */
136 public static void setTempData(String data, TempDataStoreType type, String className) throws IOException {
137
138 String fileName = "";
139 if (type.equals(TempDataStoreType.ATTRIBUTE)) {
140 fileName = ATTRIBUTE_FILE_NAME;
141 } else if (type.equals(TempDataStoreType.GETTER_METHODS)) {
142 fileName = GETTER_METHOD_FILE_NAME;
143 } else if (type.equals(TempDataStoreType.BUILDER_INTERFACE_METHODS)) {
144 fileName = BUILDER_INTERFACE_METHOD_FILE_NAME;
145 } else if (type.equals(TempDataStoreType.BUILDER_METHODS)) {
146 fileName = BUILDER_METHOD_FILE_NAME;
147 } else if (type.equals(TempDataStoreType.IMPL_METHODS)) {
148 fileName = IMPL_METHOD_FILE_NAME;
149 } else {
150 fileName = IMPORT_FILE_NAME;
151 }
152
153 File dir = new File(GEN_DIR + className + File.separator);
154 dir.mkdirs();
155 try {
156 OutputStream file = new FileOutputStream(GEN_DIR + className + File.separator + fileName + FILE_EXTENSION);
157 OutputStream buffer = new BufferedOutputStream(file, BUFFER_SIZE);
158
159 ObjectOutput output = new ObjectOutputStream(buffer);
160 try {
161 output.writeObject(data);
162 } finally {
163 output.close();
164 }
165 } catch (IOException ex) {
166 throw new IOException("failed to serialize data");
167 }
168 }
169
170 /**
171 * Get the Temp data.
172 *
173 * @param type type of Temp data store
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530174 * @param className name of the class
175 * @return list of attribute info
176 * @throws IOException when fails to read from the file
177 * @throws ClassNotFoundException when class is missing
178 * @throws FileNotFoundException when file is missing
Bharat saraswal4bf8b152016-02-25 02:26:43 +0530179 */
180 public static List<String> getTempData(TempDataStoreType type, String className)
181 throws IOException, FileNotFoundException, ClassNotFoundException {
182
183 String fileName = "";
184 if (type.equals(TempDataStoreType.ATTRIBUTE)) {
185 fileName = ATTRIBUTE_FILE_NAME;
186 } else if (type.equals(TempDataStoreType.GETTER_METHODS)) {
187 fileName = GETTER_METHOD_FILE_NAME;
188 } else if (type.equals(TempDataStoreType.BUILDER_INTERFACE_METHODS)) {
189 fileName = BUILDER_INTERFACE_METHOD_FILE_NAME;
190 } else if (type.equals(TempDataStoreType.BUILDER_METHODS)) {
191 fileName = BUILDER_METHOD_FILE_NAME;
192 } else if (type.equals(TempDataStoreType.IMPL_METHODS)) {
193 fileName = IMPL_METHOD_FILE_NAME;
194 } else {
195 fileName = IMPORT_FILE_NAME;
196 }
197 try {
198 InputStream file = new FileInputStream(GEN_DIR + className + File.separator + fileName + FILE_EXTENSION);
199 InputStream buffer = new BufferedInputStream(file);
200 ObjectInput input = new ObjectInputStream(buffer);
201 try {
202 String data = (String) input.readObject();
203 List<String> recoveredData = new ArrayList<>();
204 recoveredData.add(data);
205 return recoveredData;
206 } finally {
207 input.close();
208 file.close();
209 }
210 } catch (FileNotFoundException ex) {
211 throw new FileNotFoundException("No such file or directory.");
212 } catch (ClassNotFoundException ex) {
213 throw new ClassNotFoundException("failed to fetch the Temp data file.");
214 }
215 }
216}