View Javadoc
1   package org.djunits.generator;
2   
3   import java.io.IOException;
4   import java.net.URISyntaxException;
5   import java.nio.file.Files;
6   import java.nio.file.Paths;
7   import java.time.Instant;
8   import java.util.Collections;
9   import java.util.List;
10  
11  /**
12   * GenerateCliConverters.java. <br>
13   * <br>
14   * Copyright (c) 2003-2018 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
15   * for project information <a href="https://www.simulation.tudelft.nl/" target="_blank">www.simulation.tudelft.nl</a>. The
16   * source code and binary code of this software is proprietary information of Delft University of Technology.
17   * @author <a href="https://www.tudelft.nl/averbraeck" target="_blank">Alexander Verbraeck</a>
18   */
19  public class GenerateCliConverters
20  {
21  
22      /**
23       * @param args String[]; blank
24       * @throws IOException on i/o error
25       * @throws URISyntaxException on i/o error
26       */
27      public static void main(String[] args) throws IOException, URISyntaxException
28      {
29          String generationTime = Instant.now().toString();
30          List<String> types = Files.readAllLines(Paths.get(URLResource.getResource("/TYPES_REL.txt").toURI()));
31          List<String> absRelTypes = Files.readAllLines(Paths.get(URLResource.getResource("/TYPES_ABS_REL.txt").toURI()));
32          for (String ar : absRelTypes)
33          {
34              String[] arParts = ar.split(",");
35              types.add(arParts[0]);
36              types.add(arParts[1]);
37          }
38          Collections.sort(types);
39  
40          // @formatter:off
41          System.out.println("    /**\r\n" + 
42              "     * Register all DJUNITS converters for a CommandLine.\n" + 
43              "     * @param cmd String; the CommandLine for which the DJUNITS converters should be registered\n" + 
44              "     */\n" + 
45              "    @Generated(value = \"" + GenerateCliConverters.class.getName() + "\", date = \"" + generationTime + "\")\n" +
46              "    public static void registerAll(final CommandLine cmd)\n" + 
47              "    {"); 
48          // @formatter:on
49          for (String type : types)
50          {
51              System.out.println("        cmd.registerConverter(" + type + ".class, new " + type.toUpperCase() + "());");
52          }
53          System.out.println("    }\n");
54  
55          for (String type : types)
56          {
57              String an = type.startsWith("A") || type.startsWith("E") || type.startsWith("I") || type.startsWith("O")
58                      || type.startsWith("U") ? "an" : "a";
59              String spaced = "";
60              if (type.toLowerCase().equals("anglesolid"))
61              {
62                  spaced = "Solid Angle";
63              }
64              else
65              {
66                  boolean first = true;
67                  for (char c : type.toCharArray())
68                  {
69                      spaced += (Character.isUpperCase(c) && !first) ? " " + c : c;
70                      first = false;
71                  }
72              }
73              String an2 = spaced.startsWith("A") || spaced.startsWith("E") || spaced.startsWith("I") || spaced.startsWith("O")
74                      || spaced.startsWith("U") ? "an" : "a";
75              // @formatter:off
76              System.out.println("    /**\n" + 
77                      "     * Convert " + an2 + " " + spaced.toLowerCase() + " String with unit on the command line to " + an + " " + type + " scalar.\n" + 
78                      "     */\n" + 
79                      "    public static class " + type.toUpperCase() + " implements ITypeConverter<" + type + ">\n" + 
80                      "    {\n" + 
81                      "        /** {@inheritDoc} */\n" + 
82                      "        @Override\n" + 
83                      "        @Generated(value = \"" + GenerateCliConverters.class.getName() + "\", date = \"" + generationTime + "\")\n" +
84                      "        public " + type + " convert(final String value) throws Exception\n" + 
85                      "        {\n" + 
86                      "            return " + type + ".valueOf(value);\n" + 
87                      "        }\n" + 
88                      "    }\n");
89              // @formatter:on
90          }
91      }
92  }