~ubuntu-branches/ubuntu/precise/scilab/precise

« back to all changes in this revision

Viewing changes to modules/graph/src/java/org/scilab/modules/graph/io/ScilabStringCodec.java

Tags: 5.3.0-1ubuntu1
* New upstream release
* URL in the watch file updated

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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
 
5
 * 
 
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
 
11
 *
 
12
 */
 
13
 
 
14
package org.scilab.modules.graph.io;
 
15
 
 
16
import java.util.Map;
 
17
 
 
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;
 
22
 
 
23
import com.mxgraph.io.mxCodec;
 
24
import com.mxgraph.io.mxCodecRegistry;
 
25
 
 
26
/**
 
27
 * Define serialization for a {@link ScilabString} instance.
 
28
 */
 
29
public class ScilabStringCodec extends ScilabObjectCodec {
 
30
 
 
31
    private static final String VALUE = "value";
 
32
 
 
33
    /**
 
34
     * Default constructor
 
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.
 
39
     */
 
40
    public ScilabStringCodec(Object template, String[] exclude, String[] idrefs, Map<String, String> mapping) {
 
41
        super(template, exclude, idrefs, mapping);
 
42
    }
 
43
 
 
44
        /**
 
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.
 
48
         * 
 
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. 
 
52
         */
 
53
    @Override
 
54
    public Node encode(mxCodec enc, Object obj) {
 
55
        String name = mxCodecRegistry.getName(obj);
 
56
        Node node = enc.getDocument().createElement(name);
 
57
 
 
58
        ScilabString scilabString = (ScilabString) obj;
 
59
        mxCodec.setAttribute(node, WIDTH, scilabString.getWidth());
 
60
        mxCodec.setAttribute(node, HEIGHT, scilabString.getHeight());
 
61
 
 
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);
 
69
            }
 
70
        }
 
71
        return node;
 
72
    }
 
73
 
 
74
        /**
 
75
         * Parses the given node into the object or returns a new object
 
76
         * representing the given node.
 
77
         * 
 
78
         * @param dec
 
79
         *            Codec that controls the encoding process.
 
80
         * @param node
 
81
         *            XML node to be decoded.
 
82
         * @param into
 
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.
 
86
         */
 
87
        @Override
 
88
        public Object decode(mxCodec dec, Node node, Object into) {
 
89
                ScilabString obj = null;
 
90
 
 
91
                try {
 
92
                        if (node.getNodeType() != Node.ELEMENT_NODE) {
 
93
                                throw new UnrecognizeFormatException();
 
94
                        }
 
95
                        obj = (ScilabString) cloneTemplate(node);
 
96
 
 
97
                        // attrs = {"as", "height", "width"}
 
98
                        final NamedNodeMap attrs = node.getAttributes();
 
99
                        if (attrs == null) {
 
100
                                throw new UnrecognizeFormatException();
 
101
                        }
 
102
 
 
103
                        final int height = getHeight(attrs);
 
104
                        final int width = getWidth(attrs);
 
105
 
 
106
                        if (height * width == 0) {
 
107
                                return obj;
 
108
                        }
 
109
                        
 
110
                        final String[][] data = new String[height][width];
 
111
                        fillData(node, data);
 
112
                        
 
113
                        obj.setData(data);
 
114
 
 
115
                } catch (UnrecognizeFormatException e) {
 
116
                        LogFactory.getLog(ScilabStringCodec.class).error(e);
 
117
                } catch (NumberFormatException e) {
 
118
                        LogFactory.getLog(ScilabStringCodec.class).error(e);
 
119
                }
 
120
 
 
121
                return obj;
 
122
        }
 
123
 
 
124
        /**
 
125
         * Fill the data from the node.
 
126
         * 
 
127
         * @param node
 
128
         *            the ScilabString node
 
129
         * @param data
 
130
         *            the allocated data
 
131
         * @throws UnrecognizeFormatException
 
132
         *             when we are unable to decode the node.
 
133
         */
 
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) {
 
138
                                continue;
 
139
                        }
 
140
 
 
141
                        final NamedNodeMap dataAttrs = n.getAttributes();
 
142
                        if (dataAttrs == null) {
 
143
                                throw new UnrecognizeFormatException();
 
144
                        }
 
145
 
 
146
                        final int column = getColumnIndex(dataAttrs);
 
147
                        final int line = getLineIndex(dataAttrs);
 
148
 
 
149
                        final Node v = dataAttrs.getNamedItem(VALUE);
 
150
                        if (v == null) {
 
151
                                throw new UnrecognizeFormatException();
 
152
                        }
 
153
                        data[line][column] = v.getNodeValue();
 
154
                }
 
155
        }
 
156
}