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.ArcDirection;
7   
8   /**
9    * LeftRightAdapter to convert between XML representations of an arc direction, coded as L | LEFT | R | RIGHT | CLOCKWISE |
10   * COUNTERCLOCKWISE, and an enum type. <br>
11   * <br>
12   * Copyright (c) 2003-2018 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
13   * for project information <a href="https://www.simulation.tudelft.nl/" target="_blank">www.simulation.tudelft.nl</a>. The
14   * source code and binary code of this software is proprietary information of Delft University of Technology.
15   * @author <a href="https://www.tudelft.nl/averbraeck" target="_blank">Alexander Verbraeck</a>
16   */
17  public class LeftRightAdapter extends XmlAdapter<String, ArcDirection>
18  {
19      /** {@inheritDoc} */
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      /** {@inheritDoc} */
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  }