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