parameterlist.java

来自「jpeg2000编解码」· Java 代码 · 共 484 行 · 第 1/2 页

JAVA
484
字号
/* * CVS identifier: * * $Id: ParameterList.java,v 1.1.1.1 2002/07/22 09:26:53 grosbois Exp $ * * Class:                   ParameterList * * Description:             Class to hold parameters. * * * * COPYRIGHT: *  * This software module was originally developed by Rapha雔 Grosbois and * Diego Santa Cruz (Swiss Federal Institute of Technology-EPFL); Joel * Askel鰂 (Ericsson Radio Systems AB); and Bertrand Berthelot, David * Bouchard, F閘ix Henry, Gerard Mozelle and Patrice Onno (Canon Research * Centre France S.A) in the course of development of the JPEG2000 * standard as specified by ISO/IEC 15444 (JPEG 2000 Standard). This * software module is an implementation of a part of the JPEG 2000 * Standard. Swiss Federal Institute of Technology-EPFL, Ericsson Radio * Systems AB and Canon Research Centre France S.A (collectively JJ2000 * Partners) agree not to assert against ISO/IEC and users of the JPEG * 2000 Standard (Users) any of their rights under the copyright, not * including other intellectual property rights, for this software module * with respect to the usage by ISO/IEC and Users of this software module * or modifications thereof for use in hardware or software products * claiming conformance to the JPEG 2000 Standard. Those intending to use * this software module in hardware or software products are advised that * their use may infringe existing patents. The original developers of * this software module, JJ2000 Partners and ISO/IEC assume no liability * for use of this software module or modifications thereof. No license * or right to this software module is granted for non JPEG 2000 Standard * conforming products. JJ2000 Partners have full right to use this * software module for his/her own purpose, assign or donate this * software module to any third party and to inhibit third parties from * using this software module for non JPEG 2000 Standard conforming * products. This copyright notice must be included in all copies or * derivative works of this software module. *  * Copyright (c) 1999/2000 JJ2000 Partners. * */package jj2000.j2k.util;import java.util.*;/** * This class holds modules options and parameters as they are provided to the * encoder or the decoder. Each option and its associated parameters are * stored as strings. * * <p>This class is built on the standard Java Properties class. Consequently, * it offers facilities to load and write parameters from/to a file. In the * meantime, a ParameterList object can also handle default parameters for * each option.</p> * * <p>Each parameter can be retrieved as a string or as an specific primitive * type (int, float, etc).</p> * * <p>For more details see the Properties class.</p> * * <p>Note that this class does not support multiple occurrences of parameters * (for a parameter name, only one value is possible). Also there is no * particular order of the parameters.</p> * * @see Properties * */public class ParameterList extends Properties {    /**     * Constructs an empty ParameterList object. It can be later completed by     * adding elements one by one, by loading them from a file, or by     * initializing them from an argument string.     * */    public ParameterList() {        super();    }    /**     * Constructs an empty ParameterList object with the provided default     * parameters. The list can be later updated by adding elements one by     * one, by loading them from a file, or by initializing them from an     * argument string.     *     * @param def The defaults parameters     * */    public ParameterList(ParameterList def) {        super(def);    }    /**     * Returns the default ParameterList.     *     * @return Default ParameterList     * */    public ParameterList getDefaultParameterList() {	return (ParameterList)defaults;    }    /**     * Parses the parameters from an argument list, such as as the one in the     * command line, and integrates them in this parameter list.     *     * <p>All options must be preceded by '-' and then followed by one or more     * words, which constitues the values. The name of the options constitute     * the name of the parameters. The only exception is for boolean options,     * in which case if they are preceded by '-' they will be turned on, and     * if preceded by '+' they will be turned off. The string value of a     * boolean option is "on" or "off". Note that the '-' and '+' characters     * can not precede any word which would be a value for an option unless     * they are numeric values (otherwise it would be considered as a boolean     * option). Note also that the name of an option can not start with a     * number.</p>     *     * <p>No option can appear more than once. If so happens an exception is     * thrown.</p>     *     * <p>For instance the string:     *     * <quote> "-Ffilters w5x3 -Wlev 5 -Qtype reversible </quote>     *     * <p>will create the following parameter list:     *     * <pre>     * Ffilers  w5x3     * Wlev     5     * Qtype    reversible     * </pre></p>     *     * @param argv The argument list.     *     * @exception StringFormatException if there are invalid arguments in     * 'argv'     * */    public void parseArgs(String argv[]) {        int k;        char c,c2;        String pname;        StringBuffer pvalue;        // Read options        k = -1;        // Skip empty arguments        do {            k++;            if (k >= argv.length) {                // Nothing to put in parameters                return;            }        }        while (argv[k].length() <= 0);        // Check that we start with an option and that its is not a number        c = argv[k].charAt(0);        if (c != '-' && c != '+') { // It's not an option            throw new StringFormatException("Argument list does not" +                                            " start with an option: " +                                            argv[k]);        }        if (argv[k].length() >= 2 &&            Character.isDigit(argv[k].charAt(1))) {            throw new StringFormatException("Numeric option name: "+argv[k]);        }        pvalue = new StringBuffer();        while (k < argv.length) {            // Read parameter name            if (argv[k].length() <= 1) {                throw new StringFormatException("Option \"" + argv[k] +                                                "\" is too short.");            }            c = argv[k].charAt(0);            pname = argv[k++];            pvalue.setLength(0);            // Are there any more arguments?            if (k >= argv.length) {                // No more words in argument list => must be boolean                pvalue.append((c == '-') ? "on" : "off");            }            else {                c2 = argv[k].charAt(0);                // Is next word an option or a value?                if (c2 == '-' || c2 == '+') { // Next word could be an option                    if (argv[k].length() <= 1) {                        throw                            new StringFormatException("Option or argument \""                                                      +argv[k]+                                                      "\" too short");                    }                    if (!Character.isDigit(argv[k].charAt(1))) {                        // Not a number => we have a boolean option in pname                        pvalue.append((c == '-') ? "on" : "off");                    }                }                if (pvalue.length() == 0) { // No value yet                    // It should not a boolean option, read the values                    if (c == '+') {                        throw new StringFormatException("Boolean option \"" +                                                        pname +                                                        "\" has a value");                    }                    // We have at least one value                    pvalue.append(argv[k++]);                    while (k < argv.length) {                        // If empty string skip it                        if (argv[k].length() == 0) {                            k++;                            continue;                        }                        c = argv[k].charAt(0);                        if (c == '-' || c == '+') {                            // Next word could be an option                            if (argv[k].length() <= 1) {                                throw new                                    StringFormatException("Option or " +                                                          "argument \""                                                          +argv[k]+                                                          "\" too short");                            }                            if (!Character.isDigit(argv[k].charAt(1))) {                                // It's an option => stop                                break;                            }                        }                        pvalue.append(' '); // Add a space                        pvalue.append(argv[k++]);                    }                }            }            // Now put parameter and value in the list            if (get(pname.substring(1)) != null) {                // Option is repeated => ERROR                throw new StringFormatException("Option \""+pname+                                                "\" appears more than once");            }            put(pname.substring(1),pvalue.toString());        }    }    /**     * Returns the value of the named parameter, as a string. The value can     * come from teh defaults, if there are.     *

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?