KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > exchange > simple > SerializableNodeData


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2005 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.exchange.simple;
14
15 import info.magnolia.cms.core.NodeData;
16
17 import java.io.IOException JavaDoc;
18 import java.io.InputStream JavaDoc;
19 import java.io.Serializable JavaDoc;
20 import java.util.Calendar JavaDoc;
21
22 import javax.jcr.PropertyType;
23 import javax.jcr.Value;
24
25
26 /**
27  * Date: Jun 21, 2004 Time: 2:32:31 PM
28  * @author Sameer Charles
29  * @version 2.0
30  */

31 public class SerializableNodeData implements Serializable JavaDoc {
32
33     /**
34      * Stable serialVersionUID.
35      */

36     private static final long serialVersionUID = 222L;
37
38     /**
39      * all possible storage.
40      */

41     Value value;
42
43     String JavaDoc stringValue;
44
45     long longValue;
46
47     double doubleValue;
48
49     boolean booleanValue;
50
51     Calendar JavaDoc dateValue;
52
53     /**
54      * binary value will be serialized in a byte array
55      */

56     byte[] byteArrayValue;
57
58     private String JavaDoc name;
59
60     private int type;
61
62     private transient NodeData baseNodeData;
63
64     public SerializableNodeData(NodeData nodeData) throws SerializationException {
65         this.baseNodeData = nodeData;
66         this.makeSerializable();
67         this.baseNodeData = null;
68     }
69
70     /**
71      * <p>
72      * convert NodeData type object to SerializableNodeData
73      * </p>
74      */

75     private void makeSerializable() throws SerializationException {
76         this.setName(this.baseNodeData.getName());
77         this.setType(this.baseNodeData.getType());
78         this.setData();
79     }
80
81     /**
82      * <p>
83      * Set serializable nodedata value
84      * </p>
85      */

86     private void setData() throws SerializationException {
87         switch (this.getType()) {
88             case PropertyType.STRING:
89                 this.setValue(this.baseNodeData.getString());
90                 break;
91             case PropertyType.LONG:
92                 this.setValue(this.baseNodeData.getLong());
93                 break;
94             case PropertyType.DOUBLE:
95                 this.setValue(this.baseNodeData.getDouble());
96                 break;
97             case PropertyType.BOOLEAN:
98                 this.setValue(this.baseNodeData.getBoolean());
99                 break;
100             case PropertyType.DATE:
101                 this.setValue(this.baseNodeData.getDate());
102                 break;
103             case PropertyType.BINARY:
104                 this.setBinaryAsLink(this.baseNodeData.getHandle());
105                 /*
106                  * try { this.setValue(this.baseNodeData.getValue().getStream()); } catch (Exception re) { throw new
107                  * SerializationException(re); }
108                  */

109                 break;
110             default:
111                 throw new SerializationException("Unsupported property type"); //$NON-NLS-1$
112
}
113     }
114
115     public void setName(String JavaDoc name) {
116         this.name = name;
117     }
118
119     public String JavaDoc getName() {
120         return this.name;
121     }
122
123     public void setType(int type) {
124         this.type = type;
125     }
126
127     public int getType() {
128         return this.type;
129     }
130
131     public void setValue(Value value) {
132         this.value = value;
133     }
134
135     public Value getValue() {
136         return this.value;
137     }
138
139     public void setValue(String JavaDoc value) {
140         this.stringValue = value;
141     }
142
143     public String JavaDoc getString() {
144         return this.stringValue;
145     }
146
147     public void setValue(long value) {
148         this.longValue = value;
149     }
150
151     public long getLong() {
152         return this.longValue;
153     }
154
155     public void setValue(double value) {
156         this.doubleValue = value;
157     }
158
159     public double getDouble() {
160         return this.doubleValue;
161     }
162
163     public void setValue(boolean value) {
164         this.booleanValue = value;
165     }
166
167     public boolean getBoolean() {
168         return this.booleanValue;
169     }
170
171     public void setValue(Calendar JavaDoc value) {
172         this.dateValue = value;
173     }
174
175     public Calendar JavaDoc getDate() {
176         return this.dateValue;
177     }
178
179     public void setValue(InputStream JavaDoc value, int size) throws IOException JavaDoc {
180         value.read(this.byteArrayValue, 0, size);
181     }
182
183     public void setValue(InputStream JavaDoc value) throws IOException JavaDoc {
184         int nextByte;
185         int index = 0;
186         while ((nextByte = value.read()) != -1) {
187             this.byteArrayValue[index] = (byte) nextByte;
188             index++;
189         }
190     }
191
192     public void setBinaryAsLink(String JavaDoc value) {
193         this.stringValue = value;
194     }
195
196     public String JavaDoc getBinaryAsLink() {
197         return this.stringValue;
198     }
199
200     public byte[] getByteArray() {
201         return this.byteArrayValue;
202     }
203 }
204
Popular Tags