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
13
14
15
16
17
18
19 public class GenerateCliConverters
20 {
21
22
23
24
25
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
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
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
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
90 }
91 }
92 }