Use the swig generated enum ScilabIntegerTypeEnum for int declaration instead of...
[scilab.git] / scilab / modules / graph / src / java / org / scilab / modules / graph / io / ScilabIntegerCodec.java
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2009-2010 - DIGITEO - ClĂ©ment DAVID
4  *
5  * This file must be used under the terms of the CeCILL.
6  * This source file is licensed as described in the file COPYING, which
7  * you should have received as part of this distribution.  The terms
8  * are also available at
9  * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
10  *
11  */
12
13 package org.scilab.modules.graph.io;
14
15 import java.util.Map;
16
17 import org.apache.commons.logging.LogFactory;
18 import org.scilab.modules.types.scilabTypes.ScilabInteger;
19 import org.scilab.modules.types.scilabTypes.ScilabIntegerTypeEnum;
20 import org.w3c.dom.NamedNodeMap;
21 import org.w3c.dom.Node;
22
23 import com.mxgraph.io.mxCodec;
24 import com.mxgraph.io.mxCodecRegistry;
25
26 /**
27  * Define serialization for a {@link ScilabInteger} instance.
28  */
29 public class ScilabIntegerCodec extends ScilabObjectCodec {
30
31         private static final String BUNSIGNED = "bUnsigned"; /* With old IntegerType */
32         private static final String VALUE = "value";
33         private static final String PREC = "precision"; /* With old IntegerType */
34         private static final String PRECISION = "intPrecision"; /* with ScilabIntegerTypeEnum */
35
36         /**
37          * Default constructor
38          * @param template Prototypical instance of the object to be encoded/decoded.
39          * @param exclude Optional array of fieldnames to be ignored.
40          * @param idrefs Optional array of fieldnames to be converted to/from references.
41          * @param mapping Optional mapping from field- to attributenames.
42          */
43         public ScilabIntegerCodec(Object template, String[] exclude, String[] idrefs, Map<String, String> mapping) {
44         super(template, exclude, idrefs, mapping);
45
46         }
47
48         /**
49          * Encodes the specified object and returns a node representing then given
50          * object. Calls beforeEncode after creating the node and afterEncode
51          * with the resulting node after processing.
52          * 
53          * @param enc Codec that controls the encoding process.
54          * @param obj Object to be encoded.
55          * @return Returns the resulting XML node that represents the given object. 
56          */
57         @Override
58         public Node encode(mxCodec enc, Object obj) {
59                 String name = mxCodecRegistry.getName(obj);
60         Node node = enc.getDocument().createElement(name);
61
62         ScilabInteger scilabInteger = (ScilabInteger) obj;
63         mxCodec.setAttribute(node, WIDTH, scilabInteger.getWidth());
64         mxCodec.setAttribute(node, HEIGHT, scilabInteger.getHeight());
65         mxCodec.setAttribute(node, PRECISION, scilabInteger.getPrec().name());
66
67         for (int i = 0; i < scilabInteger.getHeight(); ++i) {
68                 for (int j = 0; j < scilabInteger.getWidth(); ++j) {
69                 Node data = enc.getDocument().createElement(DATA);
70                 mxCodec.setAttribute(data, LINE, i);
71                 mxCodec.setAttribute(data, COLUMN, j);
72                 mxCodec.setAttribute(data, VALUE, scilabInteger.getData()[i][j]);
73                 node.appendChild(data);
74                 }
75         }
76         return node;
77         }
78
79         /**
80          * Parses the given node into the object or returns a new object
81          * representing the given node.
82          * 
83          * @param dec
84          *                      Codec that controls the encoding process.
85          * @param node
86          *                      XML node to be decoded.
87          * @param into
88          *                      Optional object to encode the node into.
89          * @return Returns the resulting object that represents the given XML node
90          *               or the object given to the method as the into parameter.
91          */
92         @Override
93         public Object decode(mxCodec dec, Node node, Object into) {
94                 ScilabInteger obj = null;
95
96                 try {
97                         if (node.getNodeType() != Node.ELEMENT_NODE) {
98                                 throw new UnrecognizeFormatException();
99                         }
100                         obj = (ScilabInteger) cloneTemplate(node);
101
102                         // attrs = {"as", "height", "width"}
103                         final NamedNodeMap attrs = node.getAttributes();
104                         if (attrs == null) {
105                                 throw new UnrecognizeFormatException();
106                         }
107
108                         final int height = getHeight(attrs);
109                         final int width = getWidth(attrs);
110
111                         if (height * width == 0) {
112                                 return obj;
113                         }
114                         
115
116                         final Node precNode = attrs.getNamedItem(PRECISION);
117                         ScilabIntegerTypeEnum precision = ScilabIntegerTypeEnum.valueOf(precNode.getNodeValue());
118
119                         final Node prec = attrs.getNamedItem(PREC);
120                         if (prec != null) {
121                                 /* Old version, we have to convert to the new one */
122                                 final Node u = attrs.getNamedItem(BUNSIGNED);
123                                 final boolean unsigned;
124                                 /*
125                                  * the default boolean value is false, this value is not serialized
126                                  * by jgraphx this if we doesn't have attribute the value is
127                                  * "false".
128                                  */
129                                 unsigned = u != null;
130                                 
131                                 precision = ScilabInteger.convertOldType(prec.getNodeValue(), unsigned);
132
133                         }
134
135
136                         switch (precision) {
137                                 case sci_int8:
138                                 case sci_uint8:
139                                         final byte[][] data8 = new byte[height][width];
140                                         fillData(node, data8);
141                                         obj.setData(data8, precision == ScilabIntegerTypeEnum.sci_uint8);
142                                         break;
143                                 case sci_int16:
144                                 case sci_uint16:
145                                         final short[][] data16 = new short[height][width];
146                                         fillData(node, data16);
147                                         obj.setData(data16, precision == ScilabIntegerTypeEnum.sci_uint16);
148                                         break;
149                                 case sci_int32:
150                                 case sci_uint32:
151                                         final int[][] data32 = new int[height][width];
152                                         fillData(node, data32);
153                                         obj.setData(data32, precision == ScilabIntegerTypeEnum.sci_uint32);
154                                         break;
155                                 default:
156                                         final long[][] data64 = new long[height][width];
157                                         fillData(node, data64);
158                                         obj.setData(data64, precision == ScilabIntegerTypeEnum.sci_uint64);
159                                         break;
160                         }
161
162                 } catch (UnrecognizeFormatException e) {
163                         LogFactory.getLog(ScilabIntegerCodec.class).error(e);
164                 } catch (NumberFormatException e) {
165                         LogFactory.getLog(ScilabIntegerCodec.class).error(e);
166                 }
167                 return obj;
168         }
169         
170         /**
171          * Fill the data from the node.
172          * 
173          * @param node
174          *                      the ScilabInteger node
175          * @param data
176          *                      the allocated data
177          * @throws UnrecognizeFormatException
178          *                       when we are unable to decode the node.
179          */
180         private void fillData(Node node, byte[][] data)
181                         throws UnrecognizeFormatException {
182                 for (Node n = node.getFirstChild(); n != null; n = n.getNextSibling()) {
183                         if (n.getNodeType() != Node.ELEMENT_NODE) {
184                                 continue;
185                         }
186
187                         final NamedNodeMap dataAttrs = n.getAttributes();
188                         if (dataAttrs == null) {
189                                 throw new UnrecognizeFormatException();
190                         }
191
192                         final int column = getColumnIndex(dataAttrs);
193                         final int line = getLineIndex(dataAttrs);
194
195                         final Node v = dataAttrs.getNamedItem(VALUE);
196                         if (v == null) {
197                                 throw new UnrecognizeFormatException();
198                         }
199                         
200                         data[line][column] = Byte.parseByte(v.getNodeValue());
201                 }
202         }
203         
204         /**
205          * Fill the data from the node.
206          * 
207          * @param node
208          *                      the ScilabInteger node
209          * @param data
210          *                      the allocated data
211          * @throws UnrecognizeFormatException
212          *                       when we are unable to decode the node.
213          */
214         private void fillData(Node node, short[][] data)
215                         throws UnrecognizeFormatException {
216                 for (Node n = node.getFirstChild(); n != null; n = n.getNextSibling()) {
217                         if (n.getNodeType() != Node.ELEMENT_NODE) {
218                                 continue;
219                         }
220
221                         final NamedNodeMap dataAttrs = n.getAttributes();
222                         if (dataAttrs == null) {
223                                 throw new UnrecognizeFormatException();
224                         }
225
226                         final int column = getColumnIndex(dataAttrs);
227                         final int line = getLineIndex(dataAttrs);
228
229                         final Node v = dataAttrs.getNamedItem(VALUE);
230                         if (v == null) {
231                                 throw new UnrecognizeFormatException();
232                         }
233                         
234                         data[line][column] = Short.parseShort(v.getNodeValue());
235                 }
236         }
237         
238         /**
239          * Fill the data from the node.
240          * 
241          * @param node
242          *                      the ScilabInteger node
243          * @param data
244          *                      the allocated data
245          * @throws UnrecognizeFormatException
246          *                       when we are unable to decode the node.
247          */
248         private void fillData(Node node, int[][] data)
249                         throws UnrecognizeFormatException {
250                 for (Node n = node.getFirstChild(); n != null; n = n.getNextSibling()) {
251                         if (n.getNodeType() != Node.ELEMENT_NODE) {
252                                 continue;
253                         }
254
255                         final NamedNodeMap dataAttrs = n.getAttributes();
256                         if (dataAttrs == null) {
257                                 throw new UnrecognizeFormatException();
258                         }
259
260                         final int column = getColumnIndex(dataAttrs);
261                         final int line = getLineIndex(dataAttrs);
262
263                         final Node v = dataAttrs.getNamedItem(VALUE);
264                         if (v == null) {
265                                 throw new UnrecognizeFormatException();
266                         }
267                         
268                         data[line][column] = Integer.parseInt(v.getNodeValue());
269                 }
270         }
271         
272         /**
273          * Fill the data from the node.
274          * 
275          * @param node
276          *                      the ScilabInteger node
277          * @param data
278          *                      the allocated data
279          * @throws UnrecognizeFormatException
280          *                       when we are unable to decode the node.
281          */
282         private void fillData(Node node, long[][] data)
283                         throws UnrecognizeFormatException {
284                 for (Node n = node.getFirstChild(); n != null; n = n.getNextSibling()) {
285                         if (n.getNodeType() != Node.ELEMENT_NODE) {
286                                 continue;
287                         }
288
289                         final NamedNodeMap dataAttrs = n.getAttributes();
290                         if (dataAttrs == null) {
291                                 throw new UnrecognizeFormatException();
292                         }
293
294                         final int column = getColumnIndex(dataAttrs);
295                         final int line = getLineIndex(dataAttrs);
296
297                         final Node v = dataAttrs.getNamedItem(VALUE);
298                         if (v == null) {
299                                 throw new UnrecognizeFormatException();
300                         }
301                         
302                         data[line][column] = Long.parseLong(v.getNodeValue());
303                 }
304         }
305 }