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