/* $Id: PropfindQueryParser.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 javax.xml.parsers.DocumentBuilderFactory;

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;

/**Parser for the queries from outlook (zidelook) client;
 * 
 * @author sell
 * @version 
 */
public class PropfindQueryParser extends DefaultParser {

	public PropfindQueryParser(String xmlString) {
		super(xmlString);
	}

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

	protected void setBuilderFactoryPreferences(DocumentBuilderFactory builder) {
		super.setBuilderFactoryPreferences(builder);
		builder.setNamespaceAware(false);
	}

	private ArrayList parseProp(Node propNode) {
		if (propNode == null)
			return null;

		ArrayList attributeList = new ArrayList();

		NodeList childNodes = propNode.getChildNodes();
		for (int i = 0; i < childNodes.getLength(); i++) {
			Node childNode = childNodes.item(i);
			try {
				if ("#text".equals(childNode.getNodeName()))
					continue;

				String name = childNode.getNodeName();
				attributeList.add(name);

			}
			catch (Exception e) {
				e.printStackTrace();
			}
		}

		return attributeList;
	}

	public ArrayList parseDocument() {
		//  load XML document
		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) {
						if ("prop".equals(responseNode.getNodeName()))
							return parseProp(responseNode);
            else if ("allprop".equals(responseNode.getNodeName()))
              return null;
					}
				}
				catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	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\" encoding=\"utf-8\"?>"
				+ "<propfind xmlns=\"DAV:\">"
				+ "  <prop>"
				+ "    <displayname xmlns=\"DAV:\"/>"
				+ "    <resourcetype xmlns=\"DAV:\"/>"
				+ "    <x3001001E xmlns=\"http://schemas.microsoft.com/mapi/proptag/\"/>"
				+ "    <x3613001E xmlns=\"http://schemas.microsoft.com/mapi/proptag/\"/>"
				+ "    <x39000003 xmlns=\"http://schemas.microsoft.com/mapi/proptag/\"/>"
				+ "    <x36010003 xmlns=\"http://schemas.microsoft.com/mapi/proptag/\"/>"
				+ "    <x360A000B xmlns=\"http://schemas.microsoft.com/mapi/proptag/\"/>"
				+ "    <x36030003 xmlns=\"http://schemas.microsoft.com/mapi/proptag/\"/>"
				+ "    <x36020003 xmlns=\"http://schemas.microsoft.com/mapi/proptag/\"/>"
				+ "  </prop>"
				+ "</propfind>";

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