package org.xdef.impl.parsers;

import org.xdef.msg.XDEF;
import org.xdef.sys.SException;
import org.xdef.XDNamedValue;
import org.xdef.proc.XXNode;
import org.xdef.impl.code.DefContainer;
import org.xdef.impl.code.DefLong;
import org.xdef.XDContainer;
import static org.xdef.XDParser.LENGTH;
import static org.xdef.XDParser.MAXLENGTH;
import static org.xdef.XDParser.MINLENGTH;
import static org.xdef.XDParser.WS_PRESERVE;
import org.xdef.sys.SRuntimeException;

/** Parser of X-script "CDATA" type.
 * @author Vaclav Trojan
 */
public class XDParseCDATA extends XSParseString {

    public XDParseCDATA() {super(); _whiteSpace = WS_PRESERVE; _minLength = 1; _maxLength = -1;}

    @Override
    public int getLegalKeys() {
        return //PATTERN +
//			ENUMERATION +
//			WHITESPACE +
//			MAXINCLUSIVE +
//			MAXEXCLUSIVE +
//			MININCLUSIVE +
//			MINEXCLUSIVE +
//			TOTALDIGITS +
//			FRACTIONDIGITS +
            LENGTH +
            MAXLENGTH +
            MINLENGTH +
//			NORMALIZE +
//			SEPARATOR +
//			ITEM +
//			BASE +
            0;
    }

    @Override
    public final void setNamedParams(final XXNode xnode, final XDContainer params) throws SException {
        _minLength = _maxLength = -1;
        XDNamedValue[] pars;
        if (params == null || (pars = params.getXDNamedItems()) == null) {
            return;
        }
        for (XDNamedValue x: pars) {
            switch (x.getName()) {
                case "length": _minLength = _maxLength = x.getValue().intValue(); break;
                case "minLength": _minLength = x.getValue().intValue(); break;
                case "maxLength": _maxLength = x.getValue().intValue(); break;
            }
        }
        if (_minLength >= 0 && _maxLength >= 0 && _minLength > _maxLength) {
            throw new SException(XDEF.XDEF808); //Incorrect combination of maximum and minimum
        }
    }

    @Override
    public void setParseSQParams(final Object... params) {
        if (params != null && params.length >= 1) {
            Object par1 = params[0];
            _minLength = Integer.parseInt(par1.toString());
            switch (params.length) {
                case 1: _maxLength=_minLength; return;
                case 2: _maxLength=Integer.parseInt(params[1].toString()); return;
            }
            throw new SRuntimeException("Incorrect number of parameters");
        }
    }

    @Override
    public final XDContainer getNamedParams() {
        XDContainer map = new DefContainer();
        if (_minLength == _maxLength) {
            if (_minLength >= 0) {
                map.setXDNamedItem("length", new DefLong(_minLength));
            }
        } else {
            if (_minLength != -1) {
                map.setXDNamedItem("minLength", new DefLong(_minLength));
            }
            if (_maxLength != -1) {
                map.setXDNamedItem("maxLength", new DefLong(_maxLength));
            }
        }
        return map;
    }
}