package com.dbxml.db.common.xslt;

/*
 * dbXML - Native XML Database
 * Copyright (C) 1999-2004  The dbXML Group, L.L.C.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Id: XSLTQuery.java,v 1.4 2004/02/12 00:17:56 bradford Exp $
 */

import com.dbxml.db.common.adapters.DOMAdapter;
import com.dbxml.db.common.query.QueryBase;
import com.dbxml.db.common.xpath.XPathQueryResolver;
import com.dbxml.db.core.Collection;
import com.dbxml.db.core.DBException;
import com.dbxml.db.core.data.Key;
import com.dbxml.db.core.query.CompilationException;
import com.dbxml.db.core.query.ProcessingException;
import com.dbxml.db.core.query.Query;
import com.dbxml.db.core.query.QueryException;
import com.dbxml.db.core.query.ResultSet;
import com.dbxml.db.core.query.helpers.ResultSetWrapper;
import com.dbxml.db.core.transaction.Transaction;
import com.dbxml.xml.NamespaceMap;
import com.dbxml.xml.dom.DOMHelper;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.dom.DOMSource;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * XSLTQuery
 */

final class XSLTQuery extends QueryBase {
   private static final String XSLT = "xslt";
   private static final String SOURCE = "source";
   private static final String PARAMS = "params";
   private static final String PARAM = "param";
   private static final String STYLESHEET = "stylesheet";
   private static final String DOCUMENT = "document";
   private static final String XPATH = "xpath";
   private static final String NAME = "name";
   private static final String VALUE = "value";

   private Map params = new HashMap();

   private XSLTQueryResolver resolver;

   private DOMAdapter domAdapter;
   private Transformer transformer;
   private Source src;

   public XSLTQuery(Collection context, String query, NamespaceMap nsMap, Key[] keys, XSLTQueryResolver resolver) throws QueryException {
      super(context, query, nsMap, keys);
      this.resolver = resolver;
      domAdapter = new DOMAdapter(context);
   }

   public void compileQuery() throws CompilationException {
      try {
         Document doc = DOMHelper.parseText(query);
         Element root = doc.getDocumentElement();
         if ( root.getLocalName().equals(XSLT) && root.getNamespaceURI().equals(Query.NSURI) ) {
            NodeList nl = root.getChildNodes();
            for ( int i = 0; i < nl.getLength(); i++ ) {
               Node n = nl.item(i);
               if ( n.getNodeType() != Node.ELEMENT_NODE )
                  continue;

               Element e = (Element)n;
               if ( e.getNamespaceURI().equals(Query.NSURI) ) {
                  String tagName = e.getLocalName();
                  if ( tagName.equals(SOURCE) )
                     processSource(e);
                  else if ( tagName.equals(PARAMS) )
                     processParams(e);
                  else if ( tagName.equals(STYLESHEET) )
                     processStyles(e);
                  else
                     throw new CompilationException("Unknown Tag '" + tagName + "'");
               }
               else
                  throw new CompilationException("Invalid Namespace '" + e.getNamespaceURI() + "'");
            }
         }
      }
      catch ( Exception e ) {
         throw new CompilationException("Error Parsing XSLT Query", e);
      }

      if ( transformer == null || src == null )
         throw new CompilationException("Unknown Error Compiling XSLT Templates");

      Iterator iter = params.entrySet().iterator();
      while ( iter.hasNext() ) {
         Map.Entry entry = (Map.Entry)iter.next();
         String name = (String)entry.getKey();
         String value = (String)entry.getValue();
         transformer.setParameter(name, value);
      }
   }

   private void processSource(Element sourceElem) throws QueryException {
      Transaction tx = new Transaction();
      try {
         String xpath = sourceElem.getAttribute(XPATH);
         String docName = sourceElem.getAttribute(DOCUMENT);

         boolean hasDocName = docName != null && docName.length() > 0;
         boolean hasXpath = xpath != null && xpath.length() > 0;

         if ( hasDocName && hasXpath )
            throw new CompilationException("'document' and 'xpath' attributes are mutually exclusive");
         else if ( hasDocName ) {
            Document doc = null;
            try {
               doc = domAdapter.getDocument(tx, docName);
            }
            catch ( DBException e ) {
               // Null is ok
            }

            if ( doc != null ) {
               try {
                  String path = context.getCanonicalDocumentName(new Key(docName));
                  String systemID = XSLTQueryResolver.URL_PREFIX + path;
                  src = new DOMSource(doc, systemID);
               }
               catch ( Exception e ) {
                  // This shouldn't happen
               }
            }
            else
               throw new CompilationException("Document '" + docName + "' not found");
         }
         else if ( hasXpath ) {
            ResultSet rs;
            if ( keys != null )
               rs = context.queryDocument(tx, XPathQueryResolver.STYLE_XPATH, xpath, nsMap, keys);
            else
               rs = context.queryCollection(tx, XPathQueryResolver.STYLE_XPATH, xpath, nsMap);

            Document doc = ResultSetWrapper.toDocument(rs);
            String path = context.getCanonicalName();
            String systemID = XSLTQueryResolver.URL_PREFIX+path+"/";
            src = new DOMSource(doc, systemID);
         }
         else {
            NodeList nl = sourceElem.getChildNodes();
            Element e = null;
            for ( int i = 0; i < nl.getLength(); i++ ) {
               Node n = nl.item(i);
               if ( n.getNodeType() == Node.ELEMENT_NODE ) {
                  if ( e == null )
                     e = (Element)n;
                  else
                     throw new CompilationException("Only a single Element is allowed for inline source");
               }
            }
            String path = context.getCanonicalName();
            String systemID = XSLTQueryResolver.URL_PREFIX+path+"/";
            src = new DOMSource(e, systemID);
         }
      }
      catch ( DBException e ) {
         try {
            tx.cancel();
         }
         catch ( DBException ex ) {
            ex.printStackTrace(System.err);
         }

         if ( e instanceof QueryException )
            throw (QueryException)e;
         else
            throw new CompilationException(e);
      }
      finally {
         if ( tx.getStatus() == Transaction.ACTIVE ) {
            try {
               tx.commit();
            }
            catch ( DBException e ) {
               e.printStackTrace(System.err);
            }
         }
      }
   }

   private void processParams(Element paramsElem) throws QueryException {
      NodeList nl = paramsElem.getChildNodes();
      for ( int i = 0; i < nl.getLength(); i++ ) {
         Node n = nl.item(i);
         if ( n.getNodeType() != Node.ELEMENT_NODE )
            continue;

         Element p = (Element)n;
         if ( p.getNamespaceURI().equals(Query.NSURI) ) {
            String tagName = p.getLocalName();
            if ( tagName.equals(PARAM) ) {
               String name = p.getAttribute(NAME);
               String value = p.getAttribute(VALUE);
               params.put(name, value);
            }
            else
               throw new CompilationException("Unknown Tag '" + tagName + "'");
         }
         else
            throw new CompilationException("Invalid Namespace '" + p.getNamespaceURI() + "'");
      }
   }

   private void processStyles(Element stylesElem) throws QueryException {
      String docName = stylesElem.getAttribute(DOCUMENT);
      if ( docName != null && docName.length() > 0 ) {
         if ( docName.indexOf('/') == -1 )
            docName = context.getCanonicalName()+"/"+docName;
         transformer = resolver.getTransformer(docName);
      }
      else {
         NodeList nl = stylesElem.getChildNodes();
         Element elem = null;
         for ( int i = 0; i < nl.getLength(); i++ ) {
            Node n = nl.item(i);
            if ( n.getNodeType() == Node.ELEMENT_NODE ) {
               if ( elem == null )
                  elem = (Element)n;
               else
                  throw new CompilationException("Only a single Element is allowed for inline styles");
            }
         }

         if ( elem != null ) {
            String path = context.getCanonicalName();
            String systemID = XSLTQueryResolver.URL_PREFIX+path+"/";
            Source ds = new DOMSource(elem, systemID);
            transformer = resolver.getTransformer(ds);
         }
         else
            throw new CompilationException("'document' attribute or inline styles required");
      }
   }

   public String getQueryStyle() {
      return XSLTQueryResolver.STYLE_XSLT;
   }

   public ResultSet execute(Transaction tx) throws QueryException {
      try {
         DOMResult res = new DOMResult();
         transformer.transform(src, res);

         return new XSLTResultSet(tx, context, this, res.getNode());
      }
      catch ( TransformerException e ) {
         throw new ProcessingException(e);
      }
   }
}
