FileDocCategorySizeDatePackage
CLIPParser.javaAPI DocGlassfish v2 API13771Fri May 04 22:33:24 BST 2007com.sun.enterprise.tools.verifier

CLIPParser

public class CLIPParser extends Object

Fields Summary
protected static final com.sun.enterprise.util.LocalStringManagerImpl
smh
helper property to get to the localized strings
protected String
optarg
argument values for options that take an argument are stored here.
protected int
optopt
When an unrecognized option is encountered, getopt will return a '?' and store the value of the invalid option here.
protected String
cn
The next character to be scanned in the option-element in which the last option character we returned was found. This allows us to pick up the scan where we left last. If cn is a null string, advance to next argv
protected String
optstring
This is the string describing the valid short options.
protected LongOption[]
longopt
array of LongOption objects describing the valid long options.
protected int
longind
Stores the index into the longopt array of the long option detected
protected boolean
processed
A flag which communicates whether or not processLOpt() did all necessary processing for the current option
protected int
fno
The index of the first non-option in argv[]
protected int
lno
The index of the last non-option in argv[]
protected int
optind
Index in argv of the next element to be scanned. This is used for returning to and from the driver and for returning between successive calls to getopt. On entry to getopt zero means this is the first call. When getopt returns -1 this is the index of the first of the non-option elements that the caller should itself scan. Otherwise optind returns from one call to the next how much of argv has been parsed so far.
protected String[]
argv
The arguments to be CLIP parsed
protected String
progname
The utility name.
Logger
logger
private boolean
endparse
flag when set to true will result in getopt returning -1 the next time it is called.
Constructors Summary
public CLIPParser(String progname, String[] argv, String optstring, LongOption[] longopt)

param
progname The name to display as the program name when printing errors
param
argv String array passed as the command line to the program
param
optstring String containing a description of the valid short options for this program
param
longopt array of LongOption objects that describes the valid long options for this program


                                                                       
          
                        
        if (optstring.length() == 0)
            optstring = " "; // NOI18N

        this.progname = progname;
        this.argv = argv;
        this.optstring = optstring;
        this.longopt = longopt;
    
Methods Summary
public intgetLongind()

        return longind;
    
public java.lang.StringgetOptarg()

        return optarg;
    
public intgetOptind()

        return optind;
    
public intgetOptopt()

        return optopt;
    
public intgetopt()

return
The character representing the current option that has been parsed from the command line. If the option takes an argument, then the member optarg is set to contain the value of the argument. If an invalid option is found CLIP parser prints an error message and a ? character is returned. Member variable optopt will as usual store the option character. A -1 is returned when end of option parsing is detected. Upon return optind will be pointing to the first non-option argument in argv

        optarg = null;

        if (endparse == true)
            return (-1);

        if ((cn == null) || (cn.equals(""))) {
            if (lno > optind)
                lno = optind;
            if (fno > optind)
                fno = optind;

            if ((optind != argv.length) && argv[optind].equals("--")) { // NOI18N
                optind++;

                if (fno == lno)
                    fno = optind;

                lno = argv.length;
                optind = argv.length;
            }

            if (optind == argv.length) {
                // Set the next arg index to point at the non-options
                // that we previously skipped.
                if (fno != lno)
                    optind = fno;

                return -1;
            }

            // If we have come to a non-option stop the scan
            if (argv[optind].equals("") || (argv[optind].charAt(0) != '-") ||
                    argv[optind].equals("-")) { // NOI18N
                return -1;

            }

            // We have found another.
            if (argv[optind].startsWith("--")) // NOI18N
                cn = argv[optind].substring(2);
            else
                cn = argv[optind].substring(1);
        }

        if ((longopt != null) && (argv[optind].startsWith("--"))) { // NOI18N
            int c = processLOpt();

            if (processed)
                return c;

            // unrecognized long/short options
            if (argv[optind].startsWith("--")) { // NOI18N
                logger.log(Level.SEVERE, getClass().getName() + ".loptunknown",
                        new Object[]{progname, argv[optind]});
            } else {
                logger.log(Level.SEVERE, getClass().getName() + ".optunknown",
                        new Object[]{progname,
                                     new Character(argv[optind].charAt(0)).toString(),
                                     cn});
            }

            cn = "";
            ++optind;
            optopt = 0;

            return '?";
        }

        // handle the next short option-character
        int c = cn.charAt(0);
        if (cn.length() > 1)
            cn = cn.substring(1);
        else
            cn = "";

        String ct = null;
        if (optstring.indexOf(c) != -1)
            ct = optstring.substring(optstring.indexOf(c));

        if (cn.equals(""))
            ++optind;

        if ((ct == null) || (c == ':")) {
            /* if ( c != '?') {
             log(smh.getLocalString(getClass().getName() + ".invalidopt",
                     "{0}: invalid option [-{1} ].",
                      new Object[] {progname, new Character((char)c).toString()}));
             }*/

            optopt = c;

            return '?";
        }

        if ((ct.length() > 1) && (ct.charAt(1) == ':")) {
            if ((ct.length() > 2) && (ct.charAt(2) == ':"))
            // option that accepts an optional argument
            {
                if (!cn.equals("")) {
                    optarg = cn;
                    ++optind;
                } else {
                    optarg = null;
                }

                cn = null;
            } else {
                if (!cn.equals("")) {
                    optarg = cn;
                    ++optind;
                } else if (optind == argv.length) {
                    logger.log(Level.SEVERE, getClass().getName() + ".reqsarg",
                            new Object[]{progname,
                                         new Character((char) c).toString()});

                    optopt = c;

                    if (optstring.charAt(0) == ':")
                        return ':";
                    else
                        return '?";
                } else {
                    optarg = argv[optind];
                    ++optind;
                }

                cn = null;
            }
        }

        return c;
    
protected intprocessLOpt()
validity checker for long options.

return
the corresponding short option character or ?.

        LongOption currlopt = null;
        boolean ambiguous;
        boolean exact;
        int eoname;

        processed = true;
        ambiguous = false;
        exact = false;
        longind = -1;

        eoname = cn.indexOf("="); // NOI18N
        if (eoname == -1)
            eoname = cn.length();

        for (int i = 0; i < longopt.length; i++) {

            if (longopt[i].getName().startsWith(cn.substring(0, eoname))) {
                if (longopt[i].getName().equals(cn.substring(0, eoname))) {
                    // Exact match found
                    currlopt = longopt[i];
                    longind = i;
                    exact = true;
                    break;
                } else if (currlopt == null) {
                    // First nonexact match found
                    currlopt = longopt[i];
                    longind = i;
                } else {
                    // Second or subsequent nonexact match found
                    ambiguous = true;
                }
            }
        }

        if (ambiguous && !exact) {
            logger.log(Level.SEVERE, getClass().getName() + ".ambig",
                    new Object[]{progname, argv[optind]});
            cn = "";
            optopt = 0;
            ++optind;

            return '?";
        }

        if (currlopt != null) {
            ++optind;

            if (eoname != cn.length()) {
                if (currlopt.argPresent) {
                    if (cn.substring(eoname).length() > 1)
                        optarg = cn.substring(eoname + 1);
                    else
                        optarg = "";
                } else {
                    if (argv[optind - 1].startsWith("--")) { // NOI18N
                        logger.log(Level.SEVERE, getClass().getName() +
                                ".loptnoarg", // NOI18N
                                new Object[]{progname, currlopt.name});
                    } else {
                        logger.log(Level.SEVERE,
                                getClass().getName() + ".optnoarg", // NOI18N
                                new Object[]{progname,
                                             new Character(
                                                     argv[optind - 1].charAt(0)).toString(),
                                             currlopt.name});
                    }

                    cn = "";
                    optopt = currlopt.value;

                    return '?";
                }
            } else if (currlopt.argPresent) {
                if (optind < argv.length) {
                    optarg = argv[optind];
                    ++optind;
                } else {
                    logger.log(Level.SEVERE, getClass().getName() + ".reqargs",
                            new Object[]{progname, argv[optind - 1]});

                    cn = "";
                    optopt = currlopt.value;
                    if (optstring.charAt(0) == ':")
                        return ':";
                    else
                        return '?";
                }
            }

            cn = "";

            if (currlopt.holder != null) {
                currlopt.holder.setLength(0);
                currlopt.holder.append(currlopt.value);

                return (0);
            }

            return currlopt.value;
        }

        processed = false;

        return (0);