Package com.dbxml.db.client.xmldb

Source Code of com.dbxml.db.client.xmldb.XSLTransformServiceImpl

package com.dbxml.db.client.xmldb;

/*
* 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: XSLTransformServiceImpl.java,v 1.4 2006/02/02 18:53:47 bradford Exp $
*/

import com.dbxml.db.client.CollectionClient;
import com.dbxml.db.client.ResultSetClient;
import com.dbxml.db.common.xslt.XSLTQueryResolver;
import com.dbxml.db.core.query.Query;
import com.dbxml.util.dbXMLException;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import org.xmldb.api.base.Collection;
import org.xmldb.api.base.ErrorCodes;
import org.xmldb.api.base.Resource;
import org.xmldb.api.base.XMLDBException;
import org.xmldb.api.modules.XMLResource;

/**
* XSLTransformServiceImpl
*/

public final class XSLTransformServiceImpl extends QueryServiceImpl implements XSLTransformService {
   private static final String NAME = "XSLTransformService";
   private static final String STYLE = XSLTQueryResolver.STYLE_XSLT;
   private static final String VERSION = "1.0";

   private Map params = Collections.synchronizedMap(new LinkedHashMap());
   private XMLResource templates;

   public XSLTransformServiceImpl(CollectionImpl col) {
      super(col, STYLE);
   }

   public String getName() throws XMLDBException {
      return NAME;
   }

   public String getVersion() throws XMLDBException {
      return VERSION;
   }

   public void setTemplates(XMLResource templates) throws XMLDBException {
      this.templates = templates;
   }

   public void setParameter(String name, String value) throws XMLDBException {
      params.put(name, value);
   }

   public String getParameter(String name) throws XMLDBException {
      return (String)params.get(name);
   }

   public void removeParameter(String name) throws XMLDBException {
      params.remove(name);
   }

   public void clearParameters() throws XMLDBException {
      params.clear();
   }

   private String getQuery(String xpath, String id) throws XMLDBException {
      try {
         StringBuffer sb = new StringBuffer();
         sb.append("<dbxml:xslt xmlns:dbxml=\""+Query.NSURI+"\">\n");
         sb.append("   <dbxml:source");
         if ( xpath != null )
            sb.append(" xpath=\"\"");
         if ( id != null )
            sb.append(" document=\"\"");
         sb.append("/>\n");
         if ( params.size() > 0 ) {
            sb.append("   <dbxml:params>\n");
            Iterator iter = params.entrySet().iterator();
            while ( iter.hasNext() ) {
               Map.Entry entry = (Map.Entry)iter.next();
               String key = (String)entry.getKey();
               String value = (String)entry.getValue();
               sb.append("      <param name=\""+key+"\" value=\""+value+"\">\n");
            }
            sb.append("   </dbxml:params>\n");
         }

         if ( templates != null ) {
            Collection parent = templates.getParentCollection();
            if ( parent instanceof CollectionImpl ) {
               CollectionImpl impl = (CollectionImpl)parent;
               CollectionClient cli = impl.getCollectionClient();
               String path = cli.getCanonicalName()+"/"+templates.getId();
               sb.append("   <dbxml:stylesheet document=\""+path+"\"/>\n");
            }
            else {
               sb.append("   <dbxml:stylesheet>\n");
               sb.append((String)templates.getContent());
               sb.append("   </dbxml:stylesheet>\n");
            }
         }
         sb.append("</dbxml:xslt>");
         return sb.toString();
      }
      catch ( dbXMLException e ) {
         throw new XMLDBException(ErrorCodes.VENDOR_ERROR, e.getMessage());
      }
   }

   private Resource getResource(ResultSetClient rs) throws XMLDBException {
      try {
         Resource resource = null;
         if ( rs.next() ) {
            String docID = rs.getResultKey();
            String id = Integer.toString(new Object().hashCode());
            String xml = rs.getResultAsText();
            resource = new XMLResourceImpl(col, docID, id, xml);
         }
         rs.close();
         return resource;
      }
      catch ( dbXMLException e ) {
         throw new XMLDBException(ErrorCodes.VENDOR_ERROR, e.getMessage());
      }
   }

   public Resource transform(String xpath) throws XMLDBException {
      return getResource(performQuery(getQuery(xpath, null)));
   }

   public Resource transformResource(String id) throws XMLDBException {
      return getResource(performQueryResource(getQuery(null, id), id));
   }
}
TOP

Related Classes of com.dbxml.db.client.xmldb.XSLTransformServiceImpl

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.