1 package org.opentrafficsim.xml.bindings;
2
3 import javax.xml.bind.annotation.adapters.XmlAdapter;
4
5 import org.djutils.logger.CategoryLogger;
6 import org.opentrafficsim.xml.bindings.types.LaneKeepingType;
7
8
9
10
11
12
13
14
15
16 public class LaneKeepingAdapter extends XmlAdapter<String, LaneKeepingType>
17 {
18
19 @Override
20 public LaneKeepingType unmarshal(final String field) throws IllegalArgumentException
21 {
22 try
23 {
24 String lkpStr = field.replaceAll("\\s", "");
25 if (lkpStr.equals("KEEPRIGHT"))
26 {
27 return LaneKeepingType.KEEPRIGHT;
28 }
29 else if (lkpStr.equals("KEEPLEFT"))
30 {
31 return LaneKeepingType.KEEPLEFT;
32 }
33 else if (lkpStr.equals("KEEPLANE"))
34 {
35 return LaneKeepingType.KEEPLANE;
36 }
37 }
38 catch (Exception exception)
39 {
40 CategoryLogger.always().error(exception, "Problem parsing LaneKeeping '" + field + "'");
41 throw new IllegalArgumentException("Error parsing LaneKeeping " + field, exception);
42 }
43 CategoryLogger.always().error("Problem parsing LaneKeeping '" + field + "'");
44 throw new IllegalArgumentException("Error parsing LaneKeeping " + field);
45 }
46
47
48 @Override
49 public String marshal(final LaneKeepingType laneKeeping) throws IllegalArgumentException
50 {
51 if (laneKeeping.equals(LaneKeepingType.KEEPRIGHT))
52 return "KEEPRIGHT";
53 if (laneKeeping.equals(LaneKeepingType.KEEPLEFT))
54 return "KEEPLEFT";
55 if (laneKeeping.equals(LaneKeepingType.KEEPLANE))
56 return "KEEPLANE";
57 throw new IllegalArgumentException("Error parsing LaneKeeping " + laneKeeping);
58 }
59
60 }