package org.maskat.demo.add.remote_equal.vo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;

import javax.servlet.http.HttpServletRequest;


import org.maskat.demo.AbstractValueObj;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import com.sun.org.apache.xerces.internal.parsers.DOMParser;

public class ValueObj extends AbstractValueObj {

	public int arg1 = 0;

	public int arg2 = 0;

	public int result;

	public ValueObj(HttpServletRequest req) {
		super(req);

		// XML解析部分、フレームワークでやってくれるはず。。
		try {
			BufferedReader reader = new BufferedReader(req.getReader());
			String line = null;
			String xml = "";
			while ((line = reader.readLine()) != null) {
				xml += line;
			}
			System.out.println(xml);
			DOMParser parser = new DOMParser();
			parser.parse(new InputSource(new StringReader(xml)));

			NodeList paramNodeList = parser.getDocument().getChildNodes().item(
					0).getChildNodes();
			for (int i = 0; i < paramNodeList.getLength(); i++) {
				Node node = paramNodeList.item(i);
				if ("arg1".equals(node.getNodeName())) {
					try {
						arg1 = Integer.parseInt(node.getChildNodes().item(0)
								.getNodeValue());
					} catch (Exception e) {
					}
				}
				if ("arg2".equals(node.getNodeName())) {
					try {
						arg2 = Integer.parseInt(node.getChildNodes().item(0)
								.getNodeValue());
					} catch (Exception e) {
					}
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
			return;
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// XML解析部分、フレームワークでやってくれるはず。。

	}

	public String tempGetXML() {
		return "<?xml version=\"1.0\" encoding=\"UTF-8\"?><sample><result>"
				+ result + "</result></sample>";
	}

}
