KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > coi > tools > os > win > RegDataContainer


1 /*
2  * IzPack - Copyright 2001-2007 Julien Ponge, All Rights Reserved.
3  *
4  * http://www.izforge.com/izpack/
5  * http://developer.berlios.de/projects/izpack/
6  *
7  * Copyright 2005-2006 Klaus Bartz
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */

21
22 package com.coi.tools.os.win;
23
24 import java.io.Serializable JavaDoc;
25
26 /**
27  * <p>
28  * Data container for Windows registry values. Windows registry values can contain different data
29  * types. It is not possible to map they all to one Java type. Therefore this class contains the
30  * different container types. DO NOT CHANGE METHODE SIGNATURES etc. without addapt the native method
31  * RegistryImpl.setValueN and RegistryImpl.getValue.
32  * </p>
33  *
34  * @author Klaus Bartz
35  *
36  */

37 public class RegDataContainer implements Cloneable JavaDoc, Serializable JavaDoc, MSWinConstants
38 {
39
40     private static final long serialVersionUID = 3979265850388066865L;
41
42     private static final int[] VALID_TYPES = { 0, 1, 2, 3, 4, 6, 7};
43
44     private long dwordData = 0;
45
46     private String JavaDoc stringData = null;
47
48     private String JavaDoc[] multiStringData = null;
49
50     private byte[] binData = null;
51
52     private int type = 0;
53
54     /**
55      * Default constructor.
56      */

57     public RegDataContainer()
58     {
59         super();
60     }
61
62     /**
63      * Creates a RegDataContainer for a special type The data self is not set. Valid types are
64      *
65      * @param type
66      * @throws IllegalArgumentException if the type is not valid
67      */

68     public RegDataContainer(int type) throws IllegalArgumentException JavaDoc
69     {
70         super();
71         if (!isValidType(type)) throw new IllegalArgumentException JavaDoc("Type is not valid");
72
73         this.type = type;
74     }
75
76     /**
77      * Creates a RegDataContainer for type REG_DWORD with the given data
78      *
79      * @param data data which should be used with this object
80      */

81     public RegDataContainer(long data)
82     {
83         super();
84         type = REG_DWORD;
85         dwordData = data;
86     }
87
88     /**
89      * Creates a RegDataContainer for type REG_SZ with the given data
90      *
91      * @param data data which should be used with this object
92      */

93     public RegDataContainer(String JavaDoc data)
94     {
95         super();
96         type = REG_SZ;
97         stringData = data;
98     }
99
100     /**
101      * Creates a RegDataContainer for type REG_MULTI_SZ with the given data
102      *
103      * @param data data which should be used with this object
104      */

105     public RegDataContainer(String JavaDoc[] data)
106     {
107         super();
108         type = REG_MULTI_SZ;
109         multiStringData = data;
110     }
111
112     /**
113      * Creates a RegDataContainer for type REG_BINARY with the given data
114      *
115      * @param data data which should be used with this object
116      */

117     public RegDataContainer(byte[] data)
118     {
119         super();
120         type = REG_BINARY;
121         binData = data;
122     }
123
124     /**
125      * Returns the binary data of this container. It will be contain only data, if the type of this
126      * object is REG_BINARY.
127      *
128      * @return binary data
129      */

130     public byte[] getBinData()
131     {
132         return binData;
133     }
134
135     /**
136      * Returns the dword data of this container. It will be contain only data, if the type of this
137      * object is REG_DWORD.
138      *
139      * @return the dword data
140      */

141     public long getDwordData()
142     {
143         return dwordData;
144     }
145
146     /**
147      * Returns the multi string data as string array of this container. It will be contain only
148      * data, if the type of this object is REG_REG_MULTI_SZ.
149      *
150      * @return the multi string data
151      */

152     public String JavaDoc[] getMultiStringData()
153     {
154         return multiStringData;
155     }
156
157     /**
158      * Returns the string data of this container. It will be contain only data, if the type of this
159      * object is REG_REG_SZ.
160      *
161      * @return the string data
162      */

163     public String JavaDoc getStringData()
164     {
165         return stringData;
166     }
167
168     /**
169      * Returns the data type handled by this object.
170      *
171      * @return the data type handled by this object
172      */

173     public int getType()
174     {
175         return type;
176     }
177
178     /**
179      * Sets the binary data to the given byte array.
180      *
181      * @param bytes data to be set
182      */

183     public void setBinData(byte[] bytes)
184     {
185         binData = bytes;
186     }
187
188     /**
189      * Sets the dword data to the given value.
190      *
191      * @param i data to be set
192      */

193     public void setDwordData(long i)
194     {
195         dwordData = i;
196     }
197
198     /**
199      * Sets the multi string data to the given string array.
200      *
201      * @param strings data to be set
202      */

203     public void setMultiStringData(String JavaDoc[] strings)
204     {
205         multiStringData = strings;
206     }
207
208     /**
209      * Sets the string data to the given value.
210      *
211      * @param string data to be set
212      */

213     public void setStringData(String JavaDoc string)
214     {
215         stringData = string;
216     }
217
218     /**
219      * Sets the type.
220      *
221      * @param i type to be set
222      */

223     public void setType(int i)
224     {
225         type = i;
226     }
227
228     /**
229      * Verifies whether the given int represents a valid type or not.
230      *
231      * @param type0 value to be verified
232      * @return whether the given int represents a valid type or not
233      */

234     public boolean isValidType(int type0)
235     {
236         for (int i = 0; i < VALID_TYPES.length; ++i)
237             if (type0 == VALID_TYPES[i]) return (true);
238         return (false);
239
240     }
241
242     /**
243      * Returns the contained data depending to the type. Dword data are transformed from long to
244      * Long.
245      *
246      * @return the contained data
247      */

248     public Object JavaDoc getDataAsObject()
249     {
250         switch (type)
251         {
252         case REG_SZ:
253         case REG_EXPAND_SZ:
254             return (getStringData());
255         case REG_BINARY:
256             return (getBinData());
257         case REG_DWORD:
258             return (new Long JavaDoc(getDwordData()));
259         case REG_MULTI_SZ:
260             return (getMultiStringData());
261         default:
262             return (null);
263         }
264     }
265
266     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc
267     {
268         RegDataContainer retval = (RegDataContainer) super.clone();
269         if (multiStringData != null)
270         {
271             retval.multiStringData = new String JavaDoc[multiStringData.length];
272             System.arraycopy(multiStringData, 0, retval.multiStringData, 0, multiStringData.length);
273         }
274         if (binData != null)
275         {
276             retval.binData = new byte[binData.length];
277             System.arraycopy(binData, 0, retval.binData, 0, binData.length);
278         }
279         return (retval);
280     }
281
282     public boolean equals(Object JavaDoc anObject)
283     {
284         if (this == anObject) return (true);
285         if (anObject instanceof RegDataContainer)
286         {
287             RegDataContainer other = (RegDataContainer) anObject;
288             if (other.type != type) return (false);
289             switch (type)
290             {
291             case REG_DWORD:
292                 return (other.dwordData == dwordData);
293             case REG_SZ:
294             case REG_EXPAND_SZ:
295                 if (stringData == null) return (other.stringData == null);
296                 return (stringData.equals(other.stringData));
297             case REG_BINARY:
298                 if (binData == null) return (other.binData == null);
299                 if (other.binData != null && binData.length == other.binData.length)
300                 {
301                     for (int i = 0; i < binData.length; ++i)
302                     {
303                         if (binData[i] != other.binData[i]) return (false);
304                     }
305                     return (true);
306                 }
307                 return (false);
308             case REG_MULTI_SZ:
309                 if (multiStringData == null) return (other.multiStringData == null);
310                 if (other.multiStringData != null
311                         && multiStringData.length == other.multiStringData.length)
312                 {
313                     for (int i = 0; i < multiStringData.length; ++i)
314                     {
315                         if (multiStringData[i] != null)
316                         {
317                             if (!multiStringData[i].equals(other.multiStringData[i]))
318                                 return (false);
319                         }
320                         else if (other.multiStringData[i] == null) return (false);
321                     }
322                     return (true);
323                 }
324                 return (false);
325             }
326         }
327         return (false);
328     }
329
330     public int hashCode()
331     {
332         int result;
333         result = (int) (dwordData ^ (dwordData >>> 32));
334         result = 29 * result + (stringData != null ? stringData.hashCode() : 0);
335         result = 29 * result + type;
336         return result;
337     }
338
339 }
340
Popular Tags