package de.undercouch.citeproc.csl.internal.locale;

import de.undercouch.citeproc.helper.NodeHelper;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.util.Objects;

public class LTerm {
    private final String name;
    private final Form form;
    private final String single;
    private final String multiple;

    public enum Form {
        LONG,
        SHORT,
        VERB,
        VERB_SHORT,
        SYMBOL;

        public static Form fromString(String str) {
            switch (str) {
                case "long":
                    return LONG;
                case "short":
                    return SHORT;
                case "verb":
                    return VERB;
                case "verb-short":
                    return VERB_SHORT;
                case "symbol":
                    return SYMBOL;
                default:
                    throw new IllegalArgumentException("Unknown term form: " + str);
            }
        }
    }

    public LTerm(Node node) {
        name = NodeHelper.getAttrValue(node, "name");
        String strForm = NodeHelper.getAttrValue(node, "form");
        if (strForm == null) {
            strForm = "long";
        }
        this.form = Form.fromString(strForm);

        String single = null;
        String multiple = null;
        NodeList children = node.getChildNodes();
        for (int i = 0; i < children.getLength(); ++i) {
            Node c = children.item(i);
            if (c.getNodeType() != Node.ELEMENT_NODE) {
                continue;
            }
            String nodeName = c.getNodeName();
            switch (nodeName) {
                case "single":
                    single = c.getTextContent();
                    break;
                case "multiple":
                    multiple = c.getTextContent();
                    break;
                default:
                    break;
            }
        }

        if (single == null) {
            single = node.getTextContent();
            if (single == null) {
                throw new IllegalStateException("Invalid term value");
            }
        }

        if (multiple == null) {
            multiple = single;
        }

        this.single = single;
        this.multiple = multiple;
    }

    public String getName() {
        return name;
    }

    public Form getForm() {
        return form;
    }

    public String getSingle() {
        return single;
    }

    public String getMultiple() {
        return multiple;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        LTerm term = (LTerm)o;
        return name.equals(term.name) &&
                form.equals(term.form) &&
                single.equals(term.single) &&
                multiple.equals(term.multiple);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, form, single, multiple);
    }
}

@@ -177,16 +177,84 @@
 jects;%0A%0A
+/**%0A * A term defined in a local file%0A * @author Michel Kraemer%0A */%0A
 public c
@@ -403,106 +403,640 @@
     
-public enum Form %7B%0A        LONG,%0A        SHORT,%0A        VERB,%0A        VERB_SHORT,%0A        SYMBOL;%0A
+/**%0A     * The form in which the term was defined%0A     */%0A    public enum Form %7B%0A        /**%0A         * A long term%0A         */%0A        LONG,%0A%0A        /**%0A         * A short term%0A         */%0A        SHORT,%0A%0A        /**%0A         * A verb%0A         */%0A        VERB,%0A%0A        /**%0A         * The short form of a verb%0A         */%0A        VERB_SHORT,%0A%0A        /**%0A         * A symbol%0A         */%0A        SYMBOL;%0A%0A        /**%0A         * Parse the term from from a string%0A         * @param str the string to parse%0A         * @return the term form%0A         * @throws IllegalArgumentException if the string contains an unknown form%0A         */
 %0A   
@@ -1572,32 +1572,118 @@
        %7D%0A    %7D%0A%0A
+    /**%0A     * Parses a term from an XML node%0A     * @param node the XML node%0A     */%0A
     public LTerm
@@ -2967,179 +2967,581 @@
     
-public String getName() %7B%0A        return name;%0A    %7D%0A%0A    public Form getForm() %7B%0A        return form;%0A    %7D%0A%0A    public String getSingle() %7B%0A        return single;%0A    %7D%0A
+/**%0A     * Get the term's name%0A     * @return the term's name%0A     */%0A    public String getName() %7B%0A        return name;%0A    %7D%0A%0A    /**%0A     * Get the term's form%0A     * @return the term's form%0A     */%0A    public Form getForm() %7B%0A        return form;%0A    %7D%0A%0A    /**%0A     * Get the singular form of the term%0A     * @return the singular form%0A     */%0A    public String getSingle() %7B%0A        return single;%0A    %7D%0A%0A    /**%0A     * Get the plural form of the term (might be the same as the singular form%0A     * if no plural form was defined)%0A     * @return the plural form%0A     */
 %0A   

Add missing javadoc