src/main/java/de/unixwork/uwproj/Option.java

Sat, 27 Jan 2024 12:29:50 +0100

author
Mike Becker <universe@uap-core.de>
date
Sat, 27 Jan 2024 12:29:50 +0100
changeset 108
a47b92fab3bd
parent 41
75ee588d5d9e
child 113
24f32dbd88cd
permissions
-rw-r--r--

update license URL to raw file

package de.unixwork.uwproj;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

import static de.unixwork.uwproj.Util.shId;

public class Option {
    private String arg;

    private final LinkedList<OptionValue> values = new LinkedList<>();
    private final LinkedList<OptionDefault> defaults = new LinkedList<>();

    public static Option parse(Element element) throws Exception {
        var opt = new Option();
        String arg = element.getAttribute("arg");
        if (arg.isBlank()) {
            throw new Exception("Option has no argument string");
        }
        opt.setArgument(arg);

        NodeList nodes = element.getChildNodes();
        for (int i = 0; i < nodes.getLength(); i++) {
            Node node = nodes.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                Element elm = (Element) node;
                String n = elm.getNodeName();
                if (n.equals("value")) {
                    opt.values.add(new OptionValue(opt,
                            elm.getAttribute("str"),
                            TargetData.parse(elm)
                    ));
                } else if (n.equals("default")) {
                    var def = new OptionDefault(opt);
                    def.setValueName(elm.getAttribute("value"));
                    String defPlatform = elm.getAttribute(("platform"));
                    if (!defPlatform.isBlank()) {
                        def.setPlatform(defPlatform);
                    }
                    opt.defaults.add(def);
                }
            }
        }

        return opt;
    }

    public String getArgument() {
        return arg;
    }

    public void setArgument(String arg) {
        this.arg = arg;
    }

    public String getVarName() {
        return shId("OPT_" + arg.toUpperCase());
    }

    public String getArgValue() {
        return "${ARG:" + (3 + arg.length()) + "}";
    }

    public List<OptionValue> getValues() {
        return values;
    }

    public List<OptionDefault> getDefaults() {
        return defaults;
    }

    public String getValueFunc(String value) {
        return shId("checkopt_" + arg + "_" + value);
    }

    public String getValuesString() {
        if (values.size() == 1) {
            return values.get(0).getValue();
        } else {
            return "(" + values.stream().map(OptionValue::getValue).collect(Collectors.joining("|")) + ")";
        }
    }
}

mercurial