Source code: com/flexstor/common/importprocessor/ImportCtlData.java
1 /*
2 * ImportCtlData.java
3 *
4 * Copyright $Date: 2003/08/11 02:22:37 $ FLEXSTOR.net Inc.
5 *
6 * This work is licensed for use and distribution under license terms found at
7 * http://www.flexstor.org/license.html
8 *
9 */
10
11 package com.flexstor.common.importprocessor;
12
13 import java.util.Vector;
14
15 /**
16 *
17 *
18 */
19 public class ImportCtlData extends Vector
20 {
21
22 /**
23 *
24 *
25 */
26 public ImportCtlData()
27 {
28
29 } // constructor
30
31
32
33 /**
34 *
35 *
36 */
37 public String[] getCtlData()
38 {
39 String[] saData = new String[this.size()];
40
41 this.copyInto(saData);
42 return saData;
43 } // getCtlData
44
45 /**
46 *
47 *
48 */
49 public String getValuePerKey(String sKey)
50 {
51 String sValue = "";
52 String sTemp = "";
53 String sLine = "";
54
55 sKey = sKey.toUpperCase();
56
57 for (int i = 0; i < this.size(); i++)
58 {
59 sLine = (String)this.elementAt(i);
60 sLine = sLine.trim();
61 sTemp = sLine;
62
63 if (sTemp.toUpperCase().startsWith(sKey))
64 {
65 int nDivider = sTemp.indexOf("=");
66
67 if (nDivider != -1)
68 {
69 sTemp = sLine.substring(0, nDivider);
70 sTemp = sTemp.trim();
71 if (sTemp.toUpperCase().equals(sKey))
72 {
73 sValue = sLine.substring(nDivider + 1);
74 sValue = sValue.trim();
75 break;
76 }
77 } // nDivider if
78 } // startsWith if
79 } // for
80
81 return sValue;
82 } // getValuePerKey
83
84 /**
85 *
86 *
87 */
88 public boolean getBooleanPerKey(String sOptionName)
89 {
90 boolean bValue = false;
91 String sValue = "";
92
93 sValue = getOptionValue(sOptionName);
94 // sValue = getCommandValue(sValue);
95
96 if ((sValue.toUpperCase().startsWith("TR") == true) ||
97 (sValue.toUpperCase().startsWith("Y") == true))
98 {
99 bValue = true;
100 }
101
102 return bValue;
103 } // getBooleanPerKey
104
105
106 /**
107 *
108 *
109 */
110 protected String getOptionValue(String sName)
111 {
112 String sValue = "";
113 String sTemp = "";
114 String sLine = "";
115
116 sName = sName.toUpperCase();
117
118 for (int i = 0; i < this.size(); i++)
119 {
120 sLine = (String)this.elementAt(i);
121 sLine = sLine.trim();
122 sTemp = sLine;
123
124 if (sTemp.toUpperCase().startsWith(sName))
125 {
126 int nDivider = sTemp.indexOf("=");
127
128 if (nDivider != -1)
129 {
130 sValue = sLine.substring(nDivider + 1);
131 sValue = sValue.trim();
132 } // nDivider if
133
134 break;
135 } // startsWith if
136 } // for
137
138 return sValue;
139 } // getOptionValue
140
141 /**
142 *
143 *
144 */
145 public boolean addValuePerKey(String sKey, String sValue)
146 {
147 String sAdd = sKey + "=" + sValue;
148 this.addElement(sAdd);
149
150 return true;
151 } // addValuePerKey
152
153
154 /**
155 * Parses the current line returning the command portion.
156 * @param sLineRead The current line.
157 */
158 public String getCommand(String sLineRead)
159 {
160 String sValue = "";
161
162 int nIndex = sLineRead.indexOf("=");
163
164 if (nIndex != -1)
165 {
166 sValue = sLineRead.substring(0, nIndex);
167 sValue = sValue.trim();
168 }
169
170 return sValue;
171 } // getCommandValue
172
173 /**
174 * Parses the current line returning the value portion.
175 * @param sLineRead The current line.
176 */
177 public String getCommandValue(String sLineRead)
178 {
179 String sValue = "";
180 int nIndex = sLineRead.indexOf("=");
181
182 if (nIndex != -1)
183 {
184 sValue = sLineRead.substring(nIndex + 1);
185 sValue = sValue.trim();
186 }
187
188 return sValue;
189 } // getCommandValue
190
191 /**
192 * Determines if line read is a section header.
193 * @param sInputLine The line to check.
194 */
195 private String isSection(String sInputLine)
196 {
197 String sLine = sInputLine.trim();
198 String sSectionHeader = null;
199
200 if (sLine.startsWith("[") == true)
201 {
202 int nPos = sLine.indexOf("]");
203
204 if (nPos == -1)
205 {
206 return null;
207 }
208
209 sSectionHeader = sLine.substring(1, nPos);
210 sSectionHeader = sSectionHeader.toUpperCase();
211 return sSectionHeader;
212 }
213
214 return null;
215 } // isSection
216
217
218
219 /**
220 *
221 *
222 */
223 public Vector getVectorSectionData(String sHeader)
224 {
225 Vector vTemp = new Vector();
226 String sLine = "";
227 String sSection = "";
228 boolean bHeaderFound = false;
229
230 for (int i=0; i<this.size(); i++)
231 {
232 sLine = (String)this.elementAt(i);
233 sSection = isSection(sLine);
234
235 if (sSection != null)
236 {
237 // A Section was found, if the desired section was already foud
238 // then this indicates the end of that section so quit.
239 if (bHeaderFound == true)
240 {
241 break;
242 }
243
244 // Is this the desired section?
245 else if (sSection.equals(sHeader.toUpperCase()))
246 {
247 bHeaderFound = true;
248 }
249 }
250
251 // Not a section, so if desired section has already been found
252 // then this must be a data line, so keep it.
253 else if(bHeaderFound == true)
254 {
255 vTemp.addElement(sLine);
256 }
257 } // for
258
259 return vTemp;
260 } // getVectorSectionData
261
262
263 /**
264 *
265 *
266 */
267 public String[] getArraySectionData(String sHeader)
268 {
269 // Get the data in a vector
270 Vector vTemp = getVectorSectionData(sHeader);
271
272 // Copy the vector data to an array
273 String[] saData = new String[vTemp.size()];
274 if (vTemp.size() != 0)
275 {
276 vTemp.copyInto(saData);
277 }
278
279 return saData;
280 } // getArraySectionData
281
282
283 /**
284 *
285 * Return index of the header if found, else return -1
286 */
287 public int getHeaderIndex(String sHeader)
288 {
289 String sLine = "";
290 String sSection = "";
291 int nIndex = -1;
292
293 for (int i=0; i<this.size(); i++)
294 {
295 sLine = (String)this.elementAt(i);
296 sSection = isSection(sLine);
297
298 if ((sSection != null) && (sSection.equals(sHeader.toUpperCase())))
299 {
300 nIndex = i;
301 break;
302 }
303 } // for
304
305 return nIndex;
306 } // getHeaderIndex
307
308 /**
309 *
310 *
311 */
312 public boolean removeItem(String sKey, String sValue)
313 {
314 boolean bResult = false;
315
316 // Search the vector for the specified key/value pair
317 for (int i = 0; i < this.size(); i++)
318 {
319 String sLine = (String)this.elementAt(i);
320
321 // Extract the key for this line
322 String sCommand = getCommand(sLine.trim());
323
324 if ((sCommand.equals("") == false) && (sCommand.equalsIgnoreCase(sKey)))
325 {
326 // The keys match, extract the value for this line
327 String sItemValue = getCommandValue(sLine.trim());
328 if ((sItemValue.equals("") == false) && (sItemValue.equalsIgnoreCase(sValue)))
329 {
330 // Keys and values match, delete this element
331 this.removeElementAt(i);
332 return true;
333 }
334 }
335 } // for
336
337 return bResult;
338 } // getOptionValue
339
340
341
342 } // ImportCtlData