View Javadoc
1   package org.opentrafficsim.xml.bindings;
2   
3   import org.djutils.exceptions.Throw;
4   import org.opentrafficsim.xml.bindings.types.StringType;
5   
6   /**
7    * Adapter for String expression type (space, i.e. 'default' or 'preserve').
8    * <p>
9    * Copyright (c) 2023-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
10   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
11   * </p>
12   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
13   */
14  public class SpaceAdapter extends ExpressionAdapter<String, StringType>
15  {
16  
17      /** {@inheritDoc} */
18      @Override
19      public StringType unmarshal(final String field)
20      {
21          if (isExpression(field))
22          {
23              return new StringType(trimBrackets(field), true);
24          }
25          Throw.when(!field.equals("default") && !field.equals("preserve"), IllegalArgumentException.class,
26                  "Space value %s is not 'default' or 'preserve'.", field);
27          return new StringType(field, false);
28      }
29  
30      /** {@inheritDoc} */
31      @Override
32      public String marshal(final StringType value)
33      {
34          Throw.when(!value.isExpression() && !value.getValue().equals("default") && !value.getValue().equals("preserve"),
35                  IllegalArgumentException.class, "Space value %s is not 'default' or 'preserve'.", value.getValue());
36          return super.marshal(value);
37      }
38  
39  }