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.LaneKeepingType;
6   
7   /**
8    * LaneKeepingAdapter to convert between XML representations of LaneKeeping 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 LaneKeepingAdapter extends XmlAdapter<String, LaneKeepingType>
16  {
17      /** {@inheritDoc} */
18      @Override
19      public LaneKeepingType unmarshal(final String field) throws IllegalArgumentException
20      {
21          try
22          {
23              String lkpStr = field.replaceAll("\\s", "");
24              if (lkpStr.equals("KEEPRIGHT"))
25              {
26                  return LaneKeepingType.KEEPRIGHT;
27              }
28              else if (lkpStr.equals("KEEPLEFT"))
29              {
30                  return LaneKeepingType.KEEPLEFT;
31              }
32              else if (lkpStr.equals("KEEPLANE"))
33              {
34                  return LaneKeepingType.KEEPLANE;
35              }
36          }
37          catch (Exception exception)
38          {
39              throw new IllegalArgumentException("Error parsing LaneKeeping " + field, exception);
40          }
41          throw new IllegalArgumentException("Error parsing LaneKeeping " + field);
42      }
43  
44      /** {@inheritDoc} */
45      @Override
46      public String marshal(final LaneKeepingType laneKeeping) throws IllegalArgumentException
47      {
48          if (laneKeeping.equals(LaneKeepingType.KEEPRIGHT))
49              return "KEEPRIGHT";
50          if (laneKeeping.equals(LaneKeepingType.KEEPLEFT))
51              return "KEEPLEFT";
52          if (laneKeeping.equals(LaneKeepingType.KEEPLANE))
53              return "KEEPLANE";
54          throw new IllegalArgumentException("Error parsing LaneKeeping " + laneKeeping);
55      }
56  
57  }