package org.xbib.io.webdav.client;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xbib.io.webdav.api.DavConstants;
import org.xbib.io.webdav.api.XMLizable;
import org.xbib.io.webdav.common.DavException;
import org.xbib.io.webdav.common.DomUtil;
import org.xbib.io.webdav.common.ElementIterator;

public class RebindInfo implements XMLizable {

    private final String segment;
    private final String href;

    public RebindInfo(String href, String segment) {
        this.href = href;
        this.segment = segment;
    }

    public String getHref() {
        return this.href;
    }

    public String getSegment() {
        return this.segment;
    }

    /**
     * Build an <code>RebindInfo</code> object from the root element present
     * in the request body.
     *
     * @param root the root element of the request body
     * @return a RebindInfo object containing segment and href
     * @throws DavException if the REBIND request is malformed
     */
    public static RebindInfo createFromXml(Element root) throws DavException {
        if (!DomUtil.matches(root, "rebind", DavConstants.NAMESPACE)) {
            //LOG.log(Level.WARNING, "DAV:rebind element expected");
            throw new DavException(400);
        }
        String href = null;
        String segment = null;
        ElementIterator it = DomUtil.getChildren(root);
        while (it.hasNext()) {
            Element elt = it.nextElement();
            if (DomUtil.matches(elt, "segment", DavConstants.NAMESPACE)) {
                if (segment == null) {
                    segment = DomUtil.getText(elt);
                } else {
                    //LOG.log(Level.WARNING, "unexpected multiple occurrence of DAV:segment element");
                    throw new DavException(400);
                }
            } else if (DomUtil.matches(elt, "href", DavConstants.NAMESPACE)) {
                if (href == null) {
                    href = DomUtil.getText(elt);
                } else {
                    //LOG.log(Level.WARNING, "unexpected multiple occurrence of DAV:href element");
                    throw new DavException(400);
                }
            } else {
                //LOG.log(Level.WARNING, "unexpected element " + elt.getLocalName());
                throw new DavException(400);
            }
        }
        if (href == null) {
            //LOG.log(Level.WARNING, "DAV:href element expected");
            throw new DavException(400);
        }
        if (segment == null) {
            //LOG.log(Level.WARNING, "DAV:segment element expected");
            throw new DavException(400);
        }
        return new RebindInfo(href, segment);
    }

    @Override
    public Element toXml(Document document) {
        Element rebindElt = DomUtil.createElement(document, "rebind", DavConstants.NAMESPACE);
        Element hrefElt = DomUtil.createElement(document, "href", DavConstants.NAMESPACE, this.href);
        Element segElt = DomUtil.createElement(document, "segment", DavConstants.NAMESPACE, this.segment);
        rebindElt.appendChild(hrefElt);
        rebindElt.appendChild(segElt);
        return rebindElt;
    }
}
