Package com.dbxml.db.common.xslt

Source Code of com.dbxml.db.common.xslt.XSLTQuery

package com.dbxml.db.common.xslt;

/*
* dbXML - Native XML Database
* Copyright (c) 1999-2006 The dbXML Group, L.L.C.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* $Id: XSLTQuery.java,v 1.5 2006/02/02 18:53:52 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);
      }
   }
}
TOP

Related Classes of com.dbxml.db.common.xslt.XSLTQuery

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.