1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
 */
package Apache.CLI;

import java.io.File;
import java.io.FileInputStream;
import java.net.URL;
import java.util.Date;

/**
 * Allows Options to be created from a single String. The pattern contains various single character flags and via an
 * optional punctuation character, their expected type.
 *
 * <BR /><BR />Here is an Overview of PatternOptionBuilder patterns
 * 
 * <BR /><BR /><TABLE CLASS=JDBriefTable>
 * <TR>
 * <TD>a</TD>
 * <TD>-a flag</TD>
 * </TR>
 * <TR>
 * <TD>b@</TD>
 * <TD>-b [class name]</TD>
 * </TR>
 * <TR>
 * <TD>c&gt;</TD>
 * <TD>-c [file name]</TD>
 * </TR>
 * <TR>
 * <TD>d+</TD>
 * <TD>-d [class name] (creates object via empty constructor)</TD>
 * </TR>
 * <TR>
 * <TD>e%</TD>
 * <TD>-e [number] (creates Double/Long instance depending on existing of a '.')</TD>
 * </TR>
 * <TR>
 * <TD>f/</TD>
 * <TD>-f [URL]</TD>
 * </TR>
 * <TR>
 * <TD>g:</TD>
 * <TD>-g [string]</TD>
 * </TR>
 * </TABLE>
 *
 * <BR />For example, the following allows command line flags of
 * {@code '-v -p string-value -f /dir/file'}.
 * 
 * <BR /><BR />The exclamation mark precede a mandatory option.
 *
 * <BR /><BR /><DIV CLASS=SNIP>{@code
 * Options options = PatternOptionBuilder.parsePattern("vp:!f/");
 * }</DIV>
 *
 * <BR /><BR /><B CLASS=JDDescLabel>Apache Commons To-Do:</B>
 * 
 * <BR />TO-DO These need to break out to {@code OptionType} and also to be pluggable.
 */
@Torello.JavaDoc.JDHeaderBackgroundImg(EmbedTagFileID="LICENSE")
public class PatternOptionBuilder
{
    /** String class */
    public static final Class<String> STRING_VALUE = String.class;

    /** Object class */
    public static final Class<Object> OBJECT_VALUE = Object.class;

    /** Number class */
    public static final Class<Number> NUMBER_VALUE = Number.class;

    /** Date class */
    public static final Class<Date> DATE_VALUE = Date.class;

    /** Class class */
    public static final Class<?> CLASS_VALUE = Class.class;

    /// can we do this one??
    // is meant to check that the file exists, else it errors.
    // ie) it's for reading not writing.

    /** FileInputStream class */
    public static final Class<FileInputStream> EXISTING_FILE_VALUE = FileInputStream.class;

    /** File class */
    public static final Class<File> FILE_VALUE = File.class;

    /** File array class */
    public static final Class<File[]> FILES_VALUE = File[].class;

    /** URL class */
    public static final Class<URL> URL_VALUE = URL.class;

    /**
     * Retrieve the class that {@code ch} represents.
     * @param ch the specified character
     * @return The class that {@code ch} represents
     */
    public static Object getValueClass(final char ch)
    {
        switch (ch)
        {
            case '@': return PatternOptionBuilder.OBJECT_VALUE;
            case ':': return PatternOptionBuilder.STRING_VALUE;
            case '%': return PatternOptionBuilder.NUMBER_VALUE;
            case '+': return PatternOptionBuilder.CLASS_VALUE;
            case '#': return PatternOptionBuilder.DATE_VALUE;
            case '<': return PatternOptionBuilder.EXISTING_FILE_VALUE;
            case '>': return PatternOptionBuilder.FILE_VALUE;
            case '*': return PatternOptionBuilder.FILES_VALUE;
            case '/': return PatternOptionBuilder.URL_VALUE;
        }

        return null;
    }

    /**
     * Returns whether {@code ch} is a value code, i.e. whether it represents a class in a pattern.
     * @param ch the specified character
     * @return true if {@code ch} is a value code, otherwise false.
     */
    public static boolean isValueCode(final char ch)
    {
        return ch == '@' || ch == ':' || ch == '%' || ch == '+' || ch == '#' || ch == '<' ||
            ch == '>' || ch == '*' || ch == '/' || ch == '!';
    }

    /**
     * Returns the {@link Options} instance represented by {@code pattern}.
     * @param pattern the pattern string
     * @return The {@link Options} instance
     */
    public static Options parsePattern(final String pattern)
    {
        char        opt         = ' ';
        boolean     required    = false;
        Class<?>    type        = null;

        final Options options = new Options();

        for (int i = 0; i < pattern.length(); i++)
        {
            final char ch = pattern.charAt(i);

            // a value code comes after an option and specifies
            // details about it
            if (!isValueCode(ch))
            {
                if (opt != ' ')
                {
                    final Option option = Option
                        .builder(String.valueOf(opt))
                        .hasArg(type != null)
                        .required(required)
                        .type(type)
                        .build();

                    // we have a previous one to deal with
                    options.addOption(option);

                    required    = false;
                    type        = null;
                }

                opt = ch;
            }

            else if (ch == '!') required    = true;
            else                type        = (Class<?>) getValueClass(ch);
        }

        if (opt != ' ')
        {
            final Option option = Option
                .builder(String.valueOf(opt))
                .hasArg(type != null)
                .required(required)
                .type(type)
                .build();

            // we have a final one to deal with
            options.addOption(option);
        }

        return options;
    }
}