2
* Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3
* Copyright (C) 2009-2009 - DIGITEO - Bruno JOFRET
4
* Copyright (C) 2010-2010 - DIGITEO - Clément DAVID
6
* This file must be used under the terms of the CeCILL.
7
* This source file is licensed as described in the file COPYING, which
8
* you should have received as part of this distribution. The terms
9
* are also available at
10
* http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
14
package org.scilab.modules.graph.io;
18
import org.apache.commons.logging.LogFactory;
19
import org.scilab.modules.types.ScilabString;
20
import org.w3c.dom.NamedNodeMap;
21
import org.w3c.dom.Node;
23
import com.mxgraph.io.mxCodec;
24
import com.mxgraph.io.mxCodecRegistry;
27
* Define serialization for a {@link ScilabString} instance.
29
public class ScilabStringCodec extends ScilabObjectCodec {
31
private static final String VALUE = "value";
35
* @param template Prototypical instance of the object to be encoded/decoded.
36
* @param exclude Optional array of fieldnames to be ignored.
37
* @param idrefs Optional array of fieldnames to be converted to/from references.
38
* @param mapping Optional mapping from field- to attributenames.
40
public ScilabStringCodec(Object template, String[] exclude, String[] idrefs, Map<String, String> mapping) {
41
super(template, exclude, idrefs, mapping);
45
* Encodes the specified object and returns a node representing then given
46
* object. Calls beforeEncode after creating the node and afterEncode
47
* with the resulting node after processing.
49
* @param enc Codec that controls the encoding process.
50
* @param obj Object to be encoded.
51
* @return Returns the resulting XML node that represents the given object.
54
public Node encode(mxCodec enc, Object obj) {
55
String name = mxCodecRegistry.getName(obj);
56
Node node = enc.getDocument().createElement(name);
58
ScilabString scilabString = (ScilabString) obj;
59
mxCodec.setAttribute(node, WIDTH, scilabString.getWidth());
60
mxCodec.setAttribute(node, HEIGHT, scilabString.getHeight());
62
for (int i = 0; i < scilabString.getHeight(); ++i) {
63
for (int j = 0; j < scilabString.getWidth(); ++j) {
64
Node data = enc.getDocument().createElement(DATA);
65
mxCodec.setAttribute(data, LINE, i);
66
mxCodec.setAttribute(data, COLUMN, j);
67
mxCodec.setAttribute(data, VALUE, scilabString.getData()[i][j]);
68
node.appendChild(data);
75
* Parses the given node into the object or returns a new object
76
* representing the given node.
79
* Codec that controls the encoding process.
81
* XML node to be decoded.
83
* Optional object to encode the node into.
84
* @return Returns the resulting object that represents the given XML node
85
* or the object given to the method as the into parameter.
88
public Object decode(mxCodec dec, Node node, Object into) {
89
ScilabString obj = null;
92
if (node.getNodeType() != Node.ELEMENT_NODE) {
93
throw new UnrecognizeFormatException();
95
obj = (ScilabString) cloneTemplate(node);
97
// attrs = {"as", "height", "width"}
98
final NamedNodeMap attrs = node.getAttributes();
100
throw new UnrecognizeFormatException();
103
final int height = getHeight(attrs);
104
final int width = getWidth(attrs);
106
if (height * width == 0) {
110
final String[][] data = new String[height][width];
111
fillData(node, data);
115
} catch (UnrecognizeFormatException e) {
116
LogFactory.getLog(ScilabStringCodec.class).error(e);
117
} catch (NumberFormatException e) {
118
LogFactory.getLog(ScilabStringCodec.class).error(e);
125
* Fill the data from the node.
128
* the ScilabString node
131
* @throws UnrecognizeFormatException
132
* when we are unable to decode the node.
134
private void fillData(Node node, final String[][] data)
135
throws UnrecognizeFormatException {
136
for (Node n = node.getFirstChild(); n != null; n = n.getNextSibling()) {
137
if (n.getNodeType() != Node.ELEMENT_NODE) {
141
final NamedNodeMap dataAttrs = n.getAttributes();
142
if (dataAttrs == null) {
143
throw new UnrecognizeFormatException();
146
final int column = getColumnIndex(dataAttrs);
147
final int line = getLineIndex(dataAttrs);
149
final Node v = dataAttrs.getNamedItem(VALUE);
151
throw new UnrecognizeFormatException();
153
data[line][column] = v.getNodeValue();