田源
2024-03-07 4b4083fd73dc27ece42f4835483565eef0e4f608
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/**
 * $Id: mxStylesheetCodec.java,v 1.11 2011-06-13 08:18:42 gaudenz Exp $
 * Copyright (c) 2006-2010, JGraph Ltd
 */
package com.vci.rmip.workflow.client.editor.code;
 
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
 
import org.w3c.dom.Element;
import org.w3c.dom.Node;
 
import com.mxgraph.util.mxUtils;
import com.mxgraph.view.mxStylesheet;
 
/**
 * Codec for mxStylesheets. This class is created and registered
 * dynamically at load time and used implicitely via mxCodec
 * and the mxCodecRegistry.
 */
public class GraphStylesheetCodec extends GraphObjectCodec
{
 
    /**
     * Constructs a new model codec.
     */
    public GraphStylesheetCodec()
    {
        this(new mxStylesheet());
    }
 
    /**
     * Constructs a new stylesheet codec for the given template.
     */
    public GraphStylesheetCodec(Object template)
    {
        this(template, null, null, null);
    }
 
    /**
     * Constructs a new model codec for the given arguments.
     */
    public GraphStylesheetCodec(Object template, String[] exclude,
            String[] idrefs, Map<String, String> mapping)
    {
        super(template, exclude, idrefs, mapping);
    }
 
    /**
     * Encodes the given mxStylesheet.
     */
    public Node encode(GraphCodec enc, Object obj)
    {
        Element node = enc.document.createElement(getName());
 
        if (obj instanceof mxStylesheet)
        {
            mxStylesheet stylesheet = (mxStylesheet) obj;
            Iterator<Map.Entry<String, Map<String, Object>>> it = stylesheet
                    .getStyles().entrySet().iterator();
 
            while (it.hasNext())
            {
                Map.Entry<String, Map<String, Object>> entry = it.next();
 
                Element styleNode = enc.document.createElement("add");
                String stylename = entry.getKey();
                styleNode.setAttribute("as", stylename);
 
                Map<String, Object> style = entry.getValue();
                Iterator<Map.Entry<String, Object>> it2 = style.entrySet()
                        .iterator();
 
                while (it2.hasNext())
                {
                    Map.Entry<String, Object> entry2 = it2.next();
                    Element entryNode = enc.document.createElement("add");
                    entryNode.setAttribute("as",
                            String.valueOf(entry2.getKey()));
                    entryNode.setAttribute("value", getStringValue(entry2));
                    styleNode.appendChild(entryNode);
                }
 
                if (styleNode.getChildNodes().getLength() > 0)
                {
                    node.appendChild(styleNode);
                }
            }
        }
 
        return node;
    }
 
    /**
     * Returns the string for encoding the given value.
     */
    protected String getStringValue(Map.Entry<String, Object> entry)
    {
        if (entry.getValue() instanceof Boolean)
        {
            return ((Boolean) entry.getValue()) ? "1" : "0";
        }
 
        return entry.getValue().toString();
    }
 
    /**
     * Decodes the given mxStylesheet.
     */
    public Object decode(GraphCodec dec, Node node, Object into)
    {
        Object obj = null;
 
        if (node instanceof Element)
        {
            String id = ((Element) node).getAttribute("id");
            obj = dec.objects.get(id);
 
            if (obj == null)
            {
                obj = into;
 
                if (obj == null)
                {
                    obj = cloneTemplate(node);
                }
 
                if (id != null && id.length() > 0)
                {
                    dec.putObject(id, obj);
                }
            }
 
            node = node.getFirstChild();
 
            while (node != null)
            {
                if (!processInclude(dec, node, obj)
                        && node.getNodeName().equals("add")
                        && node instanceof Element)
                {
                    String as = ((Element) node).getAttribute("as");
 
                    if (as != null && as.length() > 0)
                    {
                        String extend = ((Element) node).getAttribute("extend");
                        Map<String, Object> style = (extend != null) ? ((mxStylesheet) obj)
                                .getStyles().get(extend) : null;
 
                        if (style == null)
                        {
                            style = new Hashtable<String, Object>();
                        }
                        else
                        {
                            style = new Hashtable<String, Object>(style);
                        }
 
                        Node entry = node.getFirstChild();
 
                        while (entry != null)
                        {
                            if (entry instanceof Element)
                            {
                                Element entryElement = (Element) entry;
                                String key = entryElement.getAttribute("as");
 
                                if (entry.getNodeName().equals("add"))
                                {
                                    String text = entry.getTextContent();
                                    Object value = null;
 
                                    if (text != null && text.length() > 0)
                                    {
                                        value = mxUtils.eval(text);
                                    }
                                    else
                                    {
                                        value = entryElement
                                                .getAttribute("value");
 
                                    }
 
                                    if (value != null)
                                    {
                                        style.put(key, value);
                                    }
                                }
                                else if (entry.getNodeName().equals("remove"))
                                {
                                    style.remove(key);
                                }
                            }
 
                            entry = entry.getNextSibling();
                        }
 
                        ((mxStylesheet) obj).putCellStyle(as, style);
                    }
                }
 
                node = node.getNextSibling();
            }
        }
 
        return obj;
    }
 
}