package org.xbib.io.webdav.common;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xbib.io.webdav.api.DavConstants;
import org.xbib.io.webdav.api.XMLizable;

/**
 * <code>ParentElement</code> wraps en element of the parent set of a resource. A <code>java.util.Set</code> of
 * <code>ParentElement</code> objects may serve as the value object of the <code>ParentSet</code> DavProperty.
 */
public class ParentElement implements XMLizable {

    private final String href;
    private final String segment;

    public ParentElement(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>ParentElement</code> object from an XML element DAV:parent
     *
     * @param root the DAV:parent element
     * @return a ParentElement object
     * @throws DavException if the DAV:parent element is malformed
     */
    public static ParentElement createFromXml(Element root) throws DavException {
        if (!DomUtil.matches(root, "parent", DavConstants.NAMESPACE)) {
            //LOG.log(Level.WARNING, "DAV:paret 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 ParentElement(href, segment);
    }

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