1 /*
2  * Copyright (c) 2018 Helmut Neemann.
3  * Use of this source code is governed by the GPL v3 license
4  * that can be found in the LICENSE file.
5  */
6 package de.neemann.digital.draw.graphics;
7 
8 import com.thoughtworks.xstream.converters.Converter;
9 import com.thoughtworks.xstream.converters.MarshallingContext;
10 import com.thoughtworks.xstream.converters.UnmarshallingContext;
11 import com.thoughtworks.xstream.io.HierarchicalStreamReader;
12 import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
13 
14 /**
15  * Converter to write a polygon to a xml file
16  */
17 public class PolygonConverter implements Converter {
18 
19     @Override
canConvert(Class aClass)20     public boolean canConvert(Class aClass) {
21         return aClass.equals(Polygon.class);
22     }
23 
24     @Override
marshal(Object o, HierarchicalStreamWriter writer, MarshallingContext marshallingContext)25     public void marshal(Object o, HierarchicalStreamWriter writer, MarshallingContext marshallingContext) {
26         Polygon p = (Polygon) o;
27         writer.addAttribute("path", p.toString());
28         writer.addAttribute("evenOdd", Boolean.toString(p.getEvenOdd()));
29     }
30 
31     @Override
unmarshal(HierarchicalStreamReader reader, UnmarshallingContext unmarshallingContext)32     public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext unmarshallingContext) {
33         String path = reader.getAttribute("path");
34         boolean evenOdd = Boolean.parseBoolean(reader.getAttribute("evenOdd"));
35         final Polygon polygon = Polygon.createFromPath(path);
36         if (polygon != null)
37             polygon.setEvenOdd(evenOdd);
38         return polygon;
39     }
40 
41 }
42