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.GTUPositionType;
7
8
9
10
11
12
13
14
15
16
17 public class GTUPositionAdapter extends XmlAdapter<String, GTUPositionType>
18 {
19
20 @Override
21 public GTUPositionType unmarshal(final String field) throws IllegalArgumentException
22 {
23 try
24 {
25 String clean = field.replaceAll("\\s", "");
26 if (clean.equals("FRONT"))
27 {
28 return GTUPositionType.FRONT;
29 }
30 if (clean.equals("REAR"))
31 {
32 return GTUPositionType.REAR;
33 }
34 if (clean.equals("REFERENCE"))
35 {
36 return GTUPositionType.REFERENCE;
37 }
38 }
39 catch (Exception exception)
40 {
41 CategoryLogger.always().error(exception, "Problem parsing GTUPosition '" + field + "'");
42 throw new IllegalArgumentException("Error parsing GTUPosition " + field, exception);
43 }
44 CategoryLogger.always().error("Problem parsing GTUPosition '" + field + "'");
45 throw new IllegalArgumentException("Error parsing GTUPositionType " + field);
46 }
47
48
49 @Override
50 public String marshal(final GTUPositionType gtuPosition) throws IllegalArgumentException
51 {
52 if (gtuPosition.equals(GTUPositionType.FRONT))
53 return "FRONT";
54 if (gtuPosition.equals(GTUPositionType.REAR))
55 return "REAR";
56 return "REFERENC";
57 }
58
59 }