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.LaneKeepingType;
7   
8   /**
9    * LaneKeepingAdapter to convert between XML representations of LaneKeeping 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 LaneKeepingAdapter extends XmlAdapter<String, LaneKeepingType>
17  {
18      /** {@inheritDoc} */
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      /** {@inheritDoc} */
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  }