001: /*
002: * IzPack - Copyright 2001-2008 Julien Ponge, All Rights Reserved.
003: *
004: * http://izpack.org/
005: * http://izpack.codehaus.org/
006: *
007: * Copyright 2005-2006 Klaus Bartz
008: *
009: * Licensed under the Apache License, Version 2.0 (the "License");
010: * you may not use this file except in compliance with the License.
011: * You may obtain a copy of the License at
012: *
013: * http://www.apache.org/licenses/LICENSE-2.0
014: *
015: * Unless required by applicable law or agreed to in writing, software
016: * distributed under the License is distributed on an "AS IS" BASIS,
017: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018: * See the License for the specific language governing permissions and
019: * limitations under the License.
020: */
021:
022: package com.coi.tools.os.win;
023:
024: import java.io.Serializable;
025:
026: /**
027: * <p>
028: * Data container for Windows registry values. Windows registry values can contain different data
029: * types. It is not possible to map they all to one Java type. Therefore this class contains the
030: * different container types. DO NOT CHANGE METHODE SIGNATURES etc. without addapt the native method
031: * RegistryImpl.setValueN and RegistryImpl.getValue.
032: * </p>
033: *
034: * @author Klaus Bartz
035: *
036: */
037: public class RegDataContainer implements Cloneable, Serializable,
038: MSWinConstants {
039:
040: private static final long serialVersionUID = 3979265850388066865L;
041:
042: private static final int[] VALID_TYPES = { 0, 1, 2, 3, 4, 6, 7 };
043:
044: private long dwordData = 0;
045:
046: private String stringData = null;
047:
048: private String[] multiStringData = null;
049:
050: private byte[] binData = null;
051:
052: private int type = 0;
053:
054: /**
055: * Default constructor.
056: */
057: public RegDataContainer() {
058: super ();
059: }
060:
061: /**
062: * Creates a RegDataContainer for a special type The data self is not set. Valid types are
063: *
064: * @param type
065: * @throws IllegalArgumentException if the type is not valid
066: */
067: public RegDataContainer(int type) throws IllegalArgumentException {
068: super ();
069: if (!isValidType(type))
070: throw new IllegalArgumentException("Type is not valid");
071:
072: this .type = type;
073: }
074:
075: /**
076: * Creates a RegDataContainer for type REG_DWORD with the given data
077: *
078: * @param data data which should be used with this object
079: */
080: public RegDataContainer(long data) {
081: super ();
082: type = REG_DWORD;
083: dwordData = data;
084: }
085:
086: /**
087: * Creates a RegDataContainer for type REG_SZ with the given data
088: *
089: * @param data data which should be used with this object
090: */
091: public RegDataContainer(String data) {
092: super ();
093: type = REG_SZ;
094: stringData = data;
095: }
096:
097: /**
098: * Creates a RegDataContainer for type REG_MULTI_SZ with the given data
099: *
100: * @param data data which should be used with this object
101: */
102: public RegDataContainer(String[] data) {
103: super ();
104: type = REG_MULTI_SZ;
105: multiStringData = data;
106: }
107:
108: /**
109: * Creates a RegDataContainer for type REG_BINARY with the given data
110: *
111: * @param data data which should be used with this object
112: */
113: public RegDataContainer(byte[] data) {
114: super ();
115: type = REG_BINARY;
116: binData = data;
117: }
118:
119: /**
120: * Returns the binary data of this container. It will be contain only data, if the type of this
121: * object is REG_BINARY.
122: *
123: * @return binary data
124: */
125: public byte[] getBinData() {
126: return binData;
127: }
128:
129: /**
130: * Returns the dword data of this container. It will be contain only data, if the type of this
131: * object is REG_DWORD.
132: *
133: * @return the dword data
134: */
135: public long getDwordData() {
136: return dwordData;
137: }
138:
139: /**
140: * Returns the multi string data as string array of this container. It will be contain only
141: * data, if the type of this object is REG_REG_MULTI_SZ.
142: *
143: * @return the multi string data
144: */
145: public String[] getMultiStringData() {
146: return multiStringData;
147: }
148:
149: /**
150: * Returns the string data of this container. It will be contain only data, if the type of this
151: * object is REG_REG_SZ.
152: *
153: * @return the string data
154: */
155: public String getStringData() {
156: return stringData;
157: }
158:
159: /**
160: * Returns the data type handled by this object.
161: *
162: * @return the data type handled by this object
163: */
164: public int getType() {
165: return type;
166: }
167:
168: /**
169: * Sets the binary data to the given byte array.
170: *
171: * @param bytes data to be set
172: */
173: public void setBinData(byte[] bytes) {
174: binData = bytes;
175: }
176:
177: /**
178: * Sets the dword data to the given value.
179: *
180: * @param i data to be set
181: */
182: public void setDwordData(long i) {
183: dwordData = i;
184: }
185:
186: /**
187: * Sets the multi string data to the given string array.
188: *
189: * @param strings data to be set
190: */
191: public void setMultiStringData(String[] strings) {
192: multiStringData = strings;
193: }
194:
195: /**
196: * Sets the string data to the given value.
197: *
198: * @param string data to be set
199: */
200: public void setStringData(String string) {
201: stringData = string;
202: }
203:
204: /**
205: * Sets the type.
206: *
207: * @param i type to be set
208: */
209: public void setType(int i) {
210: type = i;
211: }
212:
213: /**
214: * Verifies whether the given int represents a valid type or not.
215: *
216: * @param type0 value to be verified
217: * @return whether the given int represents a valid type or not
218: */
219: public boolean isValidType(int type0) {
220: for (int aVALID_TYPES : VALID_TYPES) {
221: if (type0 == aVALID_TYPES) {
222: return (true);
223: }
224: }
225: return (false);
226:
227: }
228:
229: /**
230: * Returns the contained data depending to the type. Dword data are transformed from long to
231: * Long.
232: *
233: * @return the contained data
234: */
235: public Object getDataAsObject() {
236: switch (type) {
237: case REG_SZ:
238: case REG_EXPAND_SZ:
239: return (getStringData());
240: case REG_BINARY:
241: return (getBinData());
242: case REG_DWORD:
243: return (getDwordData());
244: case REG_MULTI_SZ:
245: return (getMultiStringData());
246: default:
247: return (null);
248: }
249: }
250:
251: public Object clone() throws CloneNotSupportedException {
252: RegDataContainer retval = (RegDataContainer) super .clone();
253: if (multiStringData != null) {
254: retval.multiStringData = new String[multiStringData.length];
255: System.arraycopy(multiStringData, 0,
256: retval.multiStringData, 0, multiStringData.length);
257: }
258: if (binData != null) {
259: retval.binData = new byte[binData.length];
260: System.arraycopy(binData, 0, retval.binData, 0,
261: binData.length);
262: }
263: return (retval);
264: }
265:
266: public boolean equals(Object anObject) {
267: if (this == anObject)
268: return (true);
269: if (anObject instanceof RegDataContainer) {
270: RegDataContainer other = (RegDataContainer) anObject;
271: if (other.type != type)
272: return (false);
273: switch (type) {
274: case REG_DWORD:
275: return (other.dwordData == dwordData);
276: case REG_SZ:
277: case REG_EXPAND_SZ:
278: if (stringData == null)
279: return (other.stringData == null);
280: return (stringData.equals(other.stringData));
281: case REG_BINARY:
282: if (binData == null)
283: return (other.binData == null);
284: if (other.binData != null
285: && binData.length == other.binData.length) {
286: for (int i = 0; i < binData.length; ++i) {
287: if (binData[i] != other.binData[i])
288: return (false);
289: }
290: return (true);
291: }
292: return (false);
293: case REG_MULTI_SZ:
294: if (multiStringData == null)
295: return (other.multiStringData == null);
296: if (other.multiStringData != null
297: && multiStringData.length == other.multiStringData.length) {
298: for (int i = 0; i < multiStringData.length; ++i) {
299: if (multiStringData[i] != null) {
300: if (!multiStringData[i]
301: .equals(other.multiStringData[i]))
302: return (false);
303: } else if (other.multiStringData[i] == null)
304: return (false);
305: }
306: return (true);
307: }
308: return (false);
309: }
310: }
311: return (false);
312: }
313:
314: public int hashCode() {
315: int result;
316: result = (int) (dwordData ^ (dwordData >>> 32));
317: result = 29 * result
318: + (stringData != null ? stringData.hashCode() : 0);
319: result = 29 * result + type;
320: return result;
321: }
322:
323: }
|