summaryrefslogtreecommitdiffstats
blob: 3cbe8d83a774d59c1e0e4b0a76028263bb216ffd (plain)
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/*  Copyright (c) 2016, Isode Limited, London, England.
 *  All rights reserved.
 *
 *  Acquisition and use of this software and related materials for any
 *  purpose requires a written license agreement from Isode Limited,
 *  or a written license from an organisation licensed by Isode Limited
 *  to grant such a license.
 *
 */
package com.isode.stroke.parser.payloadparsers;

import java.util.ArrayList;
import java.util.List;

import com.isode.stroke.elements.WhiteboardColor;
import com.isode.stroke.elements.WhiteboardDeleteOperation;
import com.isode.stroke.elements.WhiteboardElement;
import com.isode.stroke.elements.WhiteboardEllipseElement;
import com.isode.stroke.elements.WhiteboardFreehandPathElement;
import com.isode.stroke.elements.WhiteboardInsertOperation;
import com.isode.stroke.elements.WhiteboardLineElement;
import com.isode.stroke.elements.WhiteboardOperation;
import com.isode.stroke.elements.WhiteboardPayload;
import com.isode.stroke.elements.WhiteboardPolygonElement;
import com.isode.stroke.elements.WhiteboardRectElement;
import com.isode.stroke.elements.WhiteboardUpdateOperation;
import com.isode.stroke.elements.WhiteboardPayload.Type;
import com.isode.stroke.elements.WhiteboardTextElement;
import com.isode.stroke.parser.AttributeMap;
import com.isode.stroke.parser.GenericPayloadParser;

public class WhiteboardParser extends GenericPayloadParser<WhiteboardPayload> {
    
    private boolean actualIsText;
    private int level_;
    private String data_;
    private WhiteboardElement wbElement;
    private WhiteboardOperation operation;

    public WhiteboardParser() {
        super(new WhiteboardPayload());
    }

    @Override
    public void handleStartElement(String element, String ns,
            AttributeMap attributes) {
        if (level_ == 0) {
            getPayloadInternal().setType(stringToType(getAttributeOr(attributes, "type", "")));
        } 
        else if (level_ == 1) {
            String type = getAttributeOr(attributes, "type", "");
            if (type.equals("insert")) {
                operation = new WhiteboardInsertOperation();
            }
            else if (type.equals("update")) {
                WhiteboardUpdateOperation updateOp = new WhiteboardUpdateOperation();
                String move = getAttributeOr(attributes, "newpos", "0");
                updateOp.setNewPos(Integer.parseInt(move));
                operation = updateOp;
            }
            else if (type.equals("delete")) {
                WhiteboardDeleteOperation deleteOp = new WhiteboardDeleteOperation();
                deleteOp.setElementID(getAttributeOr(attributes, "elementid", ""));
                operation = deleteOp;
            }
            if (operation != null) {
                operation.setID(getAttributeOr(attributes, "id", ""));
                operation.setParentID(getAttributeOr(attributes, "parentid", ""));
                try {
                    operation.setPos(getIntAttribute(attributes, "pos", 0));
                } catch (NumberFormatException e) {
                    // Dont set pos
                }
            }

        } 
        else if (level_ == 2) {
            if ("line".equals(element)) {
                int x1 = 0, y1 = 0, x2 = 0, y2 = 0;
                try {
                    x1 = getIntAttribute(attributes, "x1", 0);
                    y1 = getIntAttribute(attributes, "y1", 0);
                    x2 = getIntAttribute(attributes, "x2", 0);
                    y2 = getIntAttribute(attributes, "y2", 0);
                } catch (NumberFormatException e) {
                }
                WhiteboardLineElement whiteboardElement = new WhiteboardLineElement(x1, y1, x2, y2);

                WhiteboardColor color = new WhiteboardColor(getAttributeOr(attributes, "stroke", "#000000"));
                color.setAlpha(opacityToAlpha(getAttributeOr(attributes, "opacity", "1")));
                whiteboardElement.setColor(color);

                int penWidth = 1;
                try {
                    penWidth = getIntAttribute(attributes, "stroke-width", 1);
                } catch (NumberFormatException e) {
                    // Empty Catch
                }

                whiteboardElement.setPenWidth(penWidth);
                whiteboardElement.setID(getAttributeOr(attributes,"id",""));
                getPayloadInternal().setElement(whiteboardElement);
                wbElement = whiteboardElement;
            }
            else if ("path".equals(element)) {
                WhiteboardFreehandPathElement whiteboardElement = new WhiteboardFreehandPathElement();
                String pathData = getAttributeOr(attributes, "d", "");
                List<WhiteboardFreehandPathElement.Point> points =
                        new ArrayList<WhiteboardFreehandPathElement.Point>();
                if (!pathData.isEmpty() && pathData.charAt(0) == 'M') {
                    try {
                        int pos = 1, npos;
                        int x, y;
                        if (pathData.charAt(pos) == ' ') {
                            pos++;
                        }
                        npos = pathData.indexOf(' ',pos);
                        x = Integer.parseInt(pathData.substring(pos, npos));
                        pos = npos+1;
                        npos = pathData.indexOf('L',pos);
                        y = Integer.parseInt(pathData.substring(pos,npos));
                        pos = npos+1;
                        if (pos < pathData.length() && pathData.charAt(pos) == ' ') {
                            pos++;
                        }
                        points.add(new WhiteboardFreehandPathElement.Point(x,y));
                        while (pos < pathData.length()) {
                            npos = pathData.indexOf(' ',pos);
                            x = Integer.parseInt(pathData.substring(pos, npos));
                            pos = npos+1;
                            npos = pathData.indexOf(' ',pos);
                            y = Integer.parseInt(pathData.substring(pos, npos));
                            pos = npos+1;
                            points.add(new WhiteboardFreehandPathElement.Point(x,y));
                        }
                    } 
                    catch (NumberFormatException e) {
                        // Empty catch
                    }
                }
                whiteboardElement.setPoints(points);

                int penWidth = 1;
                try {
                    penWidth = getIntAttribute(attributes, "stroke-width", 1);
                } catch (NumberFormatException e) {
                    // Empty Catch
                }
                whiteboardElement.setPenWidth(penWidth);
                
                WhiteboardColor color = new WhiteboardColor(getAttributeOr(attributes, "stroke", "#000000"));
                color.setAlpha(opacityToAlpha(getAttributeOr(attributes, "opacity", "1")));
                whiteboardElement.setColor(color);
                whiteboardElement.setID(getAttributeOr(attributes,"id",""));
                getPayloadInternal().setElement(whiteboardElement);
                wbElement = whiteboardElement;
            }
            else if ("rect".equals(element)) {
                int x = 0, y = 0, width = 0, height = 0;
                try {
                    x = getIntAttribute(attributes, "x", 0);
                    y = getIntAttribute(attributes, "y", 0);
                    width = getIntAttribute(attributes, "width", 0);
                    height = getIntAttribute(attributes, "height", 0);
                } catch (Exception e) {
                    // Empty Catch
                }
                
                WhiteboardRectElement whiteboardElement = new WhiteboardRectElement(x,y,width,height);
                
                int penWidth = 1;
                try {
                    penWidth = getIntAttribute(attributes, "stroke-width", 1);
                } catch (NumberFormatException e) {
                    // Empty Catch Block
                }
                whiteboardElement.setPenWidth(penWidth);
                
                WhiteboardColor penColor = new WhiteboardColor(getAttributeOr(attributes, "stroke", "#000000"));
                WhiteboardColor brushColor = new WhiteboardColor(getAttributeOr(attributes, "fill", "#000000"));
                penColor.setAlpha(opacityToAlpha(getAttributeOr(attributes, "opacity", "1")));
                brushColor.setAlpha(opacityToAlpha(getAttributeOr(attributes, "fill-opacity", "1")));
                whiteboardElement.setPenColor(penColor);
                whiteboardElement.setBrushColor(brushColor);;
                whiteboardElement.setID(getAttributeOr(attributes, "id", ""));
                getPayloadInternal().setElement(whiteboardElement);
                wbElement = whiteboardElement;
            }
            else if ("polygon".equals(element)) {
                WhiteboardPolygonElement whiteboardElement = new WhiteboardPolygonElement();
                
                String pointsData = getAttributeOr(attributes, "points", "");
                List<WhiteboardPolygonElement.Point> points = new ArrayList<WhiteboardPolygonElement.Point>();
                int pos = 0;
                int npos;
                int x,y;
                try {
                    while (pos < pointsData.length()) {
                        npos = pointsData.indexOf(',', pos);
                        if (npos == -1) {
                            break;
                        }
                        x = Integer.parseInt(pointsData.substring(pos, npos));
                        pos = npos+1;
                        npos = pointsData.indexOf(' ',pos);
                        if (npos == -1) {
                            npos = pointsData.length();
                        }
                        y = Integer.parseInt(pointsData.substring(pos,npos));
                        pos = npos+1;
                        points.add(new WhiteboardPolygonElement.Point(x,y));
                    }
                } catch (NumberFormatException e) {
                    // Empty catch 
                }
                
                whiteboardElement.setPoints(points);
                
                int penWidth = 0;
                try {
                    penWidth = getIntAttribute(attributes, "stroke-width", 1);
                } catch (Exception e) {
                    // Empty catch
                }
                
                WhiteboardColor penColor = new WhiteboardColor(getAttributeOr(attributes, "stroke", "#000000"));
                WhiteboardColor brushColor = new WhiteboardColor(getAttributeOr(attributes, "fill", "#000000"));
                penColor.setAlpha(opacityToAlpha(getAttributeOr(attributes, "opacity", "1")));
                brushColor.setAlpha(opacityToAlpha(getAttributeOr(attributes, "fill-opacity", "1")));
                
                whiteboardElement.setPenColor(penColor);
                whiteboardElement.setBrushColor(brushColor);
                whiteboardElement.setID(getAttributeOr(attributes, "id", ""));
                getPayloadInternal().setElement(whiteboardElement);
                wbElement = whiteboardElement;
            }
            else if ("text".equals(element)) {
                int x = 0, y = 0;
                try {
                    x = getIntAttribute(attributes, "x", 0);
                    y = getIntAttribute(attributes, "y", 0);
                } catch (NumberFormatException e) {
                    // Empty Catch
                }
                
                WhiteboardTextElement whiteboardElement = new WhiteboardTextElement(x, y);
                
                actualIsText = true;
                WhiteboardColor color = new WhiteboardColor(getAttributeOr(attributes, "fill", "#000000"));
                color.setAlpha(opacityToAlpha(getAttributeOr(attributes, "opacity", "1")));
                whiteboardElement.setColor(color);
                
                int fontSize = 1;
                try {
                    fontSize = getIntAttribute(attributes, "font-size", 12);
                } catch (NumberFormatException e) {
                   // Empty Catch
                }
                
                whiteboardElement.setSize(fontSize);
                whiteboardElement.setID(getAttributeOr(attributes, "id", ""));
                getPayloadInternal().setElement(whiteboardElement);
                wbElement = whiteboardElement;
            }
            else if ("ellipse".equals(element)) {
                int cx = 0, cy = 0, rx = 0, ry = 0;
                try {
                    cx = getIntAttribute(attributes, "cx", 0);
                    cy = getIntAttribute(attributes, "cy", 0);
                    rx = getIntAttribute(attributes, "rx", 0);
                    ry = getIntAttribute(attributes, "ry", 0);
                } catch (NumberFormatException e) {
                    // Empty Catch
                }
                
                WhiteboardEllipseElement whiteboardElement = new WhiteboardEllipseElement(cx, cy, rx, ry);
                
                int penWidth = 1;
                try {
                    penWidth = getIntAttribute(attributes, "stroke-width", 1);
                } catch (NumberFormatException e) {
                    // Empty Catch
                }
                whiteboardElement.setPenWidth(penWidth);
                
                WhiteboardColor penColor = new WhiteboardColor(getAttributeOr(attributes, "stroke", "#000000"));
                WhiteboardColor brushColor  = new WhiteboardColor(getAttributeOr(attributes,"fill","#000000"));
                penColor.setAlpha(opacityToAlpha(getAttributeOr(attributes, "opacity", "1")));
                brushColor.setAlpha(opacityToAlpha(getAttributeOr(attributes, "fill-opacity", "1")));
                whiteboardElement.setPenColor(penColor);
                whiteboardElement.setBrushColor(brushColor);
                whiteboardElement.setID(getAttributeOr(attributes, "id", ""));
                getPayloadInternal().setElement(whiteboardElement);
                wbElement = whiteboardElement;
            }
        }
        ++level_;
    }

    public void handleEndElement(String element, String ns) {
        --level_;
        if (level_ == 0) {
            getPayloadInternal().setData(data_);
        } else if (level_ == 1) {
            if (operation instanceof WhiteboardInsertOperation) {
                WhiteboardInsertOperation insertOp = (WhiteboardInsertOperation) operation;
                insertOp.setElement(wbElement);
            }
            if (operation instanceof WhiteboardUpdateOperation) {
                WhiteboardUpdateOperation updateOp = (WhiteboardUpdateOperation) operation;
                updateOp.setElement(wbElement);
            }
            getPayloadInternal().setOperation(operation);
        } else if (level_ == 2) {
            if (element == "text") {
                actualIsText = false;
            }
        }
        
    }

    @Override
    public void handleCharacterData(String data) {
        if (level_ == 3 && actualIsText) {
            WhiteboardTextElement element = (WhiteboardTextElement) getPayloadInternal().getElement();
            element.setText(data);
        }
    }

    private WhiteboardPayload.Type stringToType(String type) {
        if (type == "data") {
            return Type.Data;
        } else if (type == "session-request") {
            return Type.SessionRequest;
        } else if (type == "session-accept") {
            return Type.SessionAccept;
        } else if (type == "session-terminate") {
            return Type.SessionTerminate;
        } else {
            return Type.UnknownType;
        }
    }
    
    private int opacityToAlpha(String opacity) {
        int value = 255;
        int location = opacity.indexOf('.');
        if (location != -1 && opacity.length() > (3+location)) {
            String stringValue = opacity.substring(location+1,location+3);
            try {
                value = Integer.parseInt(stringValue)*255/100;
            } catch (NumberFormatException nfe) {
                value = 255;
            }
        }
        return value;
    }
    
    /**
     * Gets the given attribute from a {@link AttributeMap} if it is set and none
     * {@code null}, otherwise returns a default value.
     * @param attributeMap An {@link AttributeMap}
     * @param attribute The name of the attribute to get from the map.
     * @param defaultValue Default value to return if the attribute is not set
     * (or is set to {@code null}) in the {@link AttributeMap}
     * @return The value of the attribute in the {@link AttributeMap} if it is
     * none {@code null} or {@code defaultValue}
     */
    private String getAttributeOr(AttributeMap attributeMap,String attribute,String defaultValue) {
        String value = attributeMap.getAttribute(attribute);
        if (value == null) {
            return defaultValue;
        }
        return value;
    }
    
    /**
     * Gets an int value for a given attribute in an attirbute map, or a default value
     * if that attribute is not set.
     * @param attributeMap An {@link AttributeMap}
     * @param attribute The name of the attribute to get from the map
     * @param defaultValue The default value to return if the attribute is not set.
     * @throws NumberFormatException if the attribute value can not be passed into an integer.
     * @return The value of the attribute as an int or defaultValue if it was not set.
     */
    private int getIntAttribute(AttributeMap attributeMap,String attribute,int defaultValue) throws NumberFormatException {
        String stringValue = getAttributeOr(attributeMap, attribute, String.valueOf(defaultValue));
        return Integer.parseInt(stringValue);
    }
    
}