/* $Id: PropfindResponseParser.java,v 1.1 2004/01/09 16:55:43 burkhard Exp $
 * Created on 12.05.2003 by sell
 *
 */
package de.skyrix.zsp.logic.parser;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

import de.skyrix.zsp.logic.DAVItem;

/**
 * 
 * @author sell
 * @version 
 */
public class PropfindResponseParser extends DefaultParser {

	public PropfindResponseParser(String xmlString) {
		super(xmlString);

		//System.out.println("parsing: "+xmlString);
	}

	public PropfindResponseParser(InputStream inputStream) {
		super(inputStream);
	}

	private DAVItem parseProp(Node propNode, DAVItem davResponse) {
		if (propNode == null || davResponse == null)
			return null;

		//if (davResponse.en)

		NodeList childNodes = propNode.getChildNodes();
		for (int i = 0; i < childNodes.getLength(); i++) {
			Node childNode = childNodes.item(i);
			try {
				if (childNode
					.getNodeName()
					.equals(childNode.getPrefix() + ":resourcetype")) {
					Node firstChild = childNode.getFirstChild();
					if (firstChild != null) {
						davResponse.setFolder(
							firstChild.getNodeName().equals(
								childNode.getPrefix() + ":collection"));
					}
				}
				else {
					if ("#text".equals(childNode.getNodeName()))
						continue;

					String name = childNode.getNodeName();
					String uri = childNode.getNamespaceURI();
					String value = "";

					//getFirstChild maybe null
					try {
						value = childNode.getFirstChild().getNodeValue();
					}
					catch (Exception e) {
					}

					String prefix = childNode.getPrefix();
					davResponse.setProperty(
						name.substring(prefix.length() + 1),
						prefix + ":" + uri + "##" + value);
				}
			}
			catch (Exception e) {
				e.printStackTrace();
			}

		}

		return davResponse;
	}

	private DAVItem parsePropstat(Node propstatNode, DAVItem davResponse) {
		if (propstatNode == null || davResponse == null)
			return null;

		NodeList childNodes = propstatNode.getChildNodes();
		for (int i = 0; i < childNodes.getLength(); i++) {
			Node childNode = childNodes.item(i);
			try {
				if (childNode
					.getNodeName()
					.equals(childNode.getPrefix() + ":status")) {
					if (childNode.getFirstChild() instanceof org.w3c.dom.Text)
						davResponse.setStatus(childNode.getFirstChild().getNodeValue());
				}
				else if (
					childNode.getNodeName().equals(childNode.getPrefix() + ":prop")) {
					davResponse = parseProp(childNode, davResponse);
				}
			}
			catch (Exception e) {
			}

		}

		return davResponse;
	}

	private DAVItem parseResponse(Node responseNode) {
		if (responseNode == null)
			return null;

		DAVItem davResponse = new DAVItem();

		NodeList nodes = responseNode.getChildNodes();
		for (int i = 0; i < nodes.getLength(); i++) {
			Node childNode = nodes.item(i);
			try {
				if (childNode.getNodeName().equals(childNode.getPrefix() + ":href")) {
					if (childNode.getFirstChild() instanceof org.w3c.dom.Text)
						davResponse.setLocation(childNode.getFirstChild().getNodeValue());
				}
				else if (
					childNode.getNodeName().equals(
						childNode.getPrefix() + ":propstat")) {
					davResponse = parsePropstat(childNode, davResponse);
				}
			}
			catch (Exception e) {
			}
		}
		//NamedNodeMap responseAttributes = responseNode.getAttributes();

		return davResponse;
	}

	public ArrayList parseDocument() {
		//  XML Dokument laden
		ArrayList aRet = new ArrayList();
		try {
			Document doc = load();

			NodeList nodeList = doc.getChildNodes();
			Node rootNode = nodeList.item(0);

			NodeList responseNodes = rootNode.getChildNodes();

			for (int i = 0; i < responseNodes.getLength(); i++) {
				Node responseNode = responseNodes.item(i);
				try {
					if (responseNode != null
						&& responseNode.getNodeName().equals(
							responseNode.getPrefix() + ":response")) {
						DAVItem response = parseResponse(responseNode);
						if (response != null)
							aRet.add(response);
					}
				}
				catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		catch (Exception e) {
		}
		return aRet;
	}

	private static class EH implements ErrorHandler {
		public void error(SAXParseException x) throws SAXException {
			throw x;
		}
		public void fatalError(SAXParseException x) throws SAXException {
			throw x;
		}

		public void warning(SAXParseException x) throws SAXException {
			throw x;
		}
	}

	public static void main(String[] args) throws SAXException, IOException {
		String body =
			"<?xml version=\"1.0\"?><D:multistatus xmlns:ap=\"http://apache.org/dav/props/\" xmlns:D=\"DAV:\"><D:response><D:href>http://localhost:20000/evo/so/sell/</D:href><D:propstat><D:status>HTTP/1.1 200 OK</D:status><D:prop><D:getcontentlength/><D:getlastmodified>Wed, 14 May 2003 15:59:31 GMT</D:getlastmodified><D:displayname>sell</D:displayname><ap:executable>0</ap:executable><D:resourcetype><D:collection/></D:resourcetype><D:checked-in/><D:checked-out/></D:prop></D:propstat></D:response><D:response><D:href>http://localhost:20000/evo/so/sell/IPM</D:href><D:propstat><D:status>HTTP/1.1 200 OK</D:status><D:prop><D:getcontentlength/><D:getlastmodified>Wed, 14 May 2003 15:59:31 GMT</D:getlastmodified><D:displayname>IPM</D:displayname><ap:executable>0</ap:executable><D:resourcetype><D:collection/></D:resourcetype><D:checked-in/><D:checked-out/></D:prop></D:propstat></D:response><D:response><D:href>http://localhost:20000/evo/so/sell/Search Root</D:href><D:propstat><D:status>HTTP/1.1 200 OK</D:status><D:prop><D:getcontentlength/><D:getlastmodified>Wed, 14 May 2003 15:59:31 GMT</D:getlastmodified><D:displayname>Search Root</D:displayname><ap:executable>0</ap:executable><D:resourcetype><D:collection/></D:resourcetype><D:checked-in/><D:checked-out/></D:prop></D:propstat></D:response></D:multistatus>";

		PropfindResponseParser parser = new PropfindResponseParser(body);
		System.out.println(parser.parseDocument());

		System.out.println();
	}
}
