package org.b3mn.poem.handler;

import java.io.ByteArrayInputStream;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.xpath.XPathAPI;
import org.b3mn.poem.Identity;
import org.b3mn.poem.jbpm.JpdlToJson;
import org.b3mn.poem.util.HandlerWithoutModelContext;
import org.w3c.dom.Document;
import org.w3c.dom.Node;

@HandlerWithoutModelContext(uri="/new_jpdl")

public class JpdlImporter extends  HandlerBase {

	@Override
	public void doPost(HttpServletRequest req, HttpServletResponse res, Identity subject, Identity object)  {
		
		res.setContentType("application/json");
			res.setStatus(200);	
			try {
				
				PrintWriter out = res.getWriter();
				String jpdlRepresentation = req.getParameter("data");
				
				DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
				factory.setNamespaceAware(true);
				DocumentBuilder builder = factory.newDocumentBuilder();
				Document jpdlDoc = builder.parse(new ByteArrayInputStream(jpdlRepresentation.getBytes()));
				
				String result = "";
				Node node = XPathAPI.selectSingleNode(jpdlDoc, "//sql", jpdlDoc.getDocumentElement());
				result = JpdlToJson.transform(jpdlDoc);
				out.write(result);
				
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}
}
@@ -290,43 +290,8 @@
 %0D%0A%0D%0A
-import org.apache.xpath.XPathAPI;%0D%0A
 impo
@@ -445,34 +445,8 @@
 nt;%0D
-%0Aimport org.w3c.dom.Node;%0D
 %0A%0D%0A@
@@ -1163,100 +1163,8 @@
 %22;%0D%0A
-%09%09%09%09Node node = XPathAPI.selectSingleNode(jpdlDoc, %22//sql%22, jpdlDoc.getDocumentElement());%0D%0A
 %09%09%09%09

debug output removed