GTUPositionAdapter.java

  1. package org.opentrafficsim.xml.bindings;

  2. import javax.xml.bind.annotation.adapters.XmlAdapter;

  3. import org.djutils.logger.CategoryLogger;
  4. import org.opentrafficsim.xml.bindings.types.GTUPositionType;

  5. /**
  6.  * GTUPositionAdapter to convert between XML representations of a reference point on a GTU, coded as FRONT, REAR and REFERENCE,
  7.  * and an enum type. <br>
  8.  * <br>
  9.  * Copyright (c) 2003-2018 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
  10.  * for project information <a href="https://www.simulation.tudelft.nl/" target="_blank">www.simulation.tudelft.nl</a>. The
  11.  * source code and binary code of this software is proprietary information of Delft University of Technology.
  12.  * @author <a href="https://www.tudelft.nl/averbraeck" target="_blank">Alexander Verbraeck</a>
  13.  */
  14. public class GTUPositionAdapter extends XmlAdapter<String, GTUPositionType>
  15. {
  16.     /** {@inheritDoc} */
  17.     @Override
  18.     public GTUPositionType unmarshal(final String field) throws IllegalArgumentException
  19.     {
  20.         try
  21.         {
  22.             String clean = field.replaceAll("\\s", "");
  23.             if (clean.equals("FRONT"))
  24.             {
  25.                 return GTUPositionType.FRONT;
  26.             }
  27.             if (clean.equals("REAR"))
  28.             {
  29.                 return GTUPositionType.REAR;
  30.             }
  31.             if (clean.equals("REFERENCE"))
  32.             {
  33.                 return GTUPositionType.REFERENCE;
  34.             }
  35.         }
  36.         catch (Exception exception)
  37.         {
  38.             CategoryLogger.always().error(exception, "Problem parsing GTUPosition '" + field + "'");
  39.             throw new IllegalArgumentException("Error parsing GTUPosition " + field, exception);
  40.         }
  41.         CategoryLogger.always().error("Problem parsing GTUPosition '" + field + "'");
  42.         throw new IllegalArgumentException("Error parsing GTUPositionType " + field);
  43.     }

  44.     /** {@inheritDoc} */
  45.     @Override
  46.     public String marshal(final GTUPositionType gtuPosition) throws IllegalArgumentException
  47.     {
  48.         if (gtuPosition.equals(GTUPositionType.FRONT))
  49.             return "FRONT";
  50.         if (gtuPosition.equals(GTUPositionType.REAR))
  51.             return "REAR";
  52.         return "REFERENC";
  53.     }

  54. }