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.GtuPositionType;
7   
8   /**
9    * GTUPositionAdapter to convert between XML representations of a reference point on a GTU, coded as FRONT, REAR and REFERENCE,
10   * and an enum type.
11   * <p>
12   * Copyright (c) 2013-2023 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
13   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
14   * </p>
15   * @author <a href="https://github.com/averbraeck" target="_blank">Alexander Verbraeck</a>
16   */
17  public class GtuPositionAdapter extends XmlAdapter<String, GtuPositionType>
18  {
19      /** {@inheritDoc} */
20      @Override
21      public GtuPositionType unmarshal(final String field) throws IllegalArgumentException
22      {
23          try
24          {
25              String clean = field.replaceAll("\\s", "");
26              if (clean.equals("FRONT"))
27              {
28                  return GtuPositionType.FRONT;
29              }
30              if (clean.equals("REAR"))
31              {
32                  return GtuPositionType.REAR;
33              }
34              if (clean.equals("REFERENCE"))
35              {
36                  return GtuPositionType.REFERENCE;
37              }
38          }
39          catch (Exception exception)
40          {
41              CategoryLogger.always().error(exception, "Problem parsing GTUPosition '" + field + "'");
42              throw new IllegalArgumentException("Error parsing GTUPosition " + field, exception);
43          }
44          CategoryLogger.always().error("Problem parsing GTUPosition '" + field + "'");
45          throw new IllegalArgumentException("Error parsing GTUPositionType " + field);
46      }
47  
48      /** {@inheritDoc} */
49      @Override
50      public String marshal(final GtuPositionType gtuPosition) throws IllegalArgumentException
51      {
52          if (gtuPosition.equals(GtuPositionType.FRONT))
53              return "FRONT";
54          if (gtuPosition.equals(GtuPositionType.REAR))
55              return "REAR";
56          return "REFERENC";
57      }
58  
59  }