View Javadoc
1   package org.opentrafficsim.xml.bindings;
2   
3   import javax.xml.bind.annotation.adapters.XmlAdapter;
4   
5   import org.opentrafficsim.xml.bindings.types.DrivingDirectionType;
6   
7   /**
8    * DrivingDirectionAdapter to convert between XML representations of a driving direction, and an enum type. <br>
9    * <br>
10   * Copyright (c) 2003-2018 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
11   * for project information <a href="https://www.simulation.tudelft.nl/" target="_blank">www.simulation.tudelft.nl</a>. The
12   * source code and binary code of this software is proprietary information of Delft University of Technology.
13   * @author <a href="https://www.tudelft.nl/averbraeck" target="_blank">Alexander Verbraeck</a>
14   */
15  public class DrivingDirectionAdapter extends XmlAdapter<String, DrivingDirectionType>
16  {
17      /** {@inheritDoc} */
18      @Override
19      public DrivingDirectionType unmarshal(final String field) throws IllegalArgumentException
20      {
21          try
22          {
23              String clean = field.replaceAll("\\s", "");
24              if (clean.equals("FORWARD"))
25              {
26                  return DrivingDirectionType.DIR_PLUS;
27              }
28              if (clean.equals("BACKWARD"))
29              {
30                  return DrivingDirectionType.DIR_MINUS;
31              }
32              if (clean.equals("BOTH"))
33              {
34                  return DrivingDirectionType.DIR_BOTH;
35              }
36              if (clean.equals("NONE"))
37              {
38                  return DrivingDirectionType.DIR_NONE;
39              }
40          }
41          catch (Exception exception)
42          {
43              throw new IllegalArgumentException("Error parsing DrivingDirectionType " + field, exception);
44          }
45          throw new IllegalArgumentException("Error parsing DrivingDirectionType " + field);
46      }
47  
48      /** {@inheritDoc} */
49      @Override
50      public String marshal(final DrivingDirectionType drivingDirection) throws IllegalArgumentException
51      {
52          if (drivingDirection.equals(DrivingDirectionType.DIR_PLUS))
53              return "FORWARD";
54          if (drivingDirection.equals(DrivingDirectionType.DIR_MINUS))
55              return "BACKWARD";
56          if (drivingDirection.equals(DrivingDirectionType.DIR_BOTH))
57              return "BOTH";
58          return "NONE";
59      }
60  
61  }