Package com.dbxml.db.common.adapters

Source Code of com.dbxml.db.common.adapters.SAXAdapter

package com.dbxml.db.common.adapters;

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

import com.dbxml.db.core.Collection;
import com.dbxml.db.core.DBException;
import com.dbxml.db.core.FaultCodes;
import com.dbxml.db.core.adapter.SimpleAdapter;
import com.dbxml.db.core.data.Key;
import com.dbxml.db.core.transaction.Transaction;
import com.dbxml.xml.SymbolTable;
import com.dbxml.xml.dtsm.DTSMException;
import com.dbxml.xml.dtsm.DTSMHelper;
import com.dbxml.xml.dtsm.DocumentTable;
import com.dbxml.xml.dtsm.TableBuilder;
import com.dbxml.xml.sax.DTSMContentHandler;
import org.xml.sax.ContentHandler;

/**
* SAXAdapter adapts a dbXML Collection to support the management of
* Documents using the SAX API.
*/

public final class SAXAdapter extends SimpleAdapter {
   public SAXAdapter(Collection col) {
      super(col);
   }

   private ContentHandler putDocument(Transaction tx, Object key) throws DBException {
      SymbolTable symbols = col.getSymbols();
      TableBuilder builder = new TableBuilder(symbols);
      return new AdapterContentHandler(tx, key, builder);
   }

   /**
    * setDocument sets a Document using SAX events that are fed to the
    * returned ContentHandler.  The document is stored in the Collection
    * using the provided Key.
    *
    * @param tx The controlling Transaction
    * @param key The Key to use
    * @return a ContentHandler for feeding SAX events
    * @throws DBException If a Database Exception occurs
    */
   public ContentHandler setDocument(Transaction tx, Object key) throws DBException {
      return putDocument(tx, key);
   }

   /**
    * setDocument sets a Document using SAX events that are fed to the
    * returned ContentHandler.  The document is stored in the Collection
    * using a newly generated Key.
    *
    * @param tx The controlling Transaction
    * @return a ContentHandler for feeding SAX events
    * @throws DBException If a Database Exception occurs
    */
   public ContentHandler insertDocument(Transaction tx) throws DBException {
      Key key = col.createNewOID();
      return putDocument(tx, key);
   }

   /**
    * insertDocument sets a Document using SAX events that are fed to the
    * returned ContentHandler.  The document is stored in the Collection
    * using the provided Key.
    *
    * @param tx The controlling Transaction
    * @param key The Key to use
    * @return a ContentHandler for feeding SAX events
    * @throws DBException If a Database Exception occurs
    */
   public ContentHandler insertDocument(Transaction tx, Object key) throws DBException {
      return putDocument(tx, key);
   }

   /**
    * getDocument instantiates and returns a DOM Document based on the
    * provided Key.
    *
    * @param tx The controlling Transaction
    * @param key The Document Key
    * @return a DOM Document
    * @throws DBException If a Database Exception occurs
    */
   public void getDocument(Transaction tx, Object key, ContentHandler handler) throws DBException {
      try {
         DocumentTable dt = col.getDocument(tx, key);
         if ( dt != null )
            DTSMHelper.tableToSAX(dt, handler);
      }
      catch ( DTSMException e ) {
         throw new DBException(FaultCodes.COL_CANNOT_RETRIEVE, "Error retrieving object '" + key + "'", e);
      }
   }


   /**
    * AdapterContentHandler
    */

   private class AdapterContentHandler extends DTSMContentHandler {
      public Transaction tx;
      public Object key;

      public AdapterContentHandler(Transaction tx, Object key, TableBuilder builder) {
         super(builder);
         this.tx = tx;
         this.key = key;
      }

      public void complete() {
         try {
            DocumentTable dt = getTableBuilder().buildDocumentTable();
            col.setDocument(tx, key, dt);
         }
         catch ( DBException e ) {
            /** @todo Uh-oh... What do we do? */
            e.printStackTrace(System.err);
         }
      }
   }
}
TOP

Related Classes of com.dbxml.db.common.adapters.SAXAdapter

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.