Package com.dbxml.db.client.xmldb

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

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

import com.dbxml.xml.dom.DOMHelper;
import com.dbxml.xml.dom.TextWriter;
import java.io.StringReader;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.w3c.dom.Node;
import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import org.xml.sax.XMLReader;
import org.xmldb.api.base.Collection;
import org.xmldb.api.base.ErrorCodes;
import org.xmldb.api.base.XMLDBException;
import org.xmldb.api.modules.XMLResource;
import org.xmldb.api.sdk.modules.SetContentHandler;

/**
* XMLResourceImpl
*/

public final class XMLResourceImpl implements XMLResource {
   public static final String TYPE = "XMLResource";

  private SAXParserFactory spf;
   private CollectionImpl parent;
   private String docID;
   private String id;
   private String xml;

   public XMLResourceImpl(CollectionImpl parent, String docID, String id, String xml) {
      this.parent = parent;
      this.docID = docID;
      this.id = id;
      this.xml = xml;
   }

   public XMLResourceImpl(CollectionImpl parent) {
      this.parent = parent;
   }

   public String getDocumentId() throws XMLDBException {
      return docID;
   }

   public Collection getParentCollection() throws XMLDBException {
      return parent;
   }

   public Node getContentAsDOM() throws XMLDBException {
      try {
         return DOMHelper.parseText(xml);
      }
      catch ( Exception e ) {
         throw new XMLDBException(ErrorCodes.INVALID_RESOURCE, e.getMessage());
      }
   }

   public String getId() throws XMLDBException {
      return id;
   }

   public void setContentAsDOM(Node content) throws XMLDBException {
      xml = TextWriter.toString(content);
   }

   public String getResourceType() throws XMLDBException {
      return TYPE;
   }

  private void checkSAXFactory() throws SAXNotRecognizedException, SAXNotSupportedException {
    if ( spf == null ) {
      try {
        spf = SAXParserFactory.newInstance();
        spf.setNamespaceAware(true);
        spf.setValidating(false);
        spf.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
      }
      catch ( ParserConfigurationException e ) {
        throw new SAXNotSupportedException(e.getMessage());
      }
    }
  }
 
  public void setSAXFeature(String name, boolean value) throws SAXNotRecognizedException, SAXNotSupportedException {
    checkSAXFactory();
    try {
      spf.setFeature(name, value);
    }
    catch ( ParserConfigurationException e ) {
      throw new SAXNotRecognizedException(e.getMessage());
    }
  }
 
  public boolean getSAXFeature(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
    checkSAXFactory();
    try {
      return spf.getFeature(name);
    }
    catch ( ParserConfigurationException e ) {
      throw new SAXNotRecognizedException(e.getMessage());
    }
  }
 
  public void getContentAsSAX(ContentHandler handler) throws XMLDBException {
    if ( xml == null )
      throw new XMLDBException(ErrorCodes.INVALID_RESOURCE, "No content has been set");
   
    try {
      checkSAXFactory();
      SAXParser sp = spf.newSAXParser();
     
      XMLReader xr = sp.getXMLReader();
      xr.setContentHandler(handler);
     
      InputSource is = new InputSource(new StringReader(xml));
     
      xr.parse(is);
    }
    catch ( Exception e ) {
      throw new XMLDBException(ErrorCodes.UNKNOWN_ERROR, e.getMessage());
    }
   }

   public ContentHandler setContentAsSAX() throws XMLDBException {
    return new SetContentHandler(this);
   }

   public Object getContent() throws XMLDBException {
      return xml;
   }
 
   public void setContent(Object value) throws XMLDBException {
      if ( value instanceof Node )
         setContentAsDOM((Node)value);
      else if ( value instanceof String )
         xml = (String)value;
      else if ( value != null )
         throw new XMLDBException(ErrorCodes.WRONG_CONTENT_TYPE, "Setting content as '" + value.getClass().getName() + "' is not supported");
      else
         throw new XMLDBException(ErrorCodes.WRONG_CONTENT_TYPE, "Setting content as null is not supported");
   }
}
TOP

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

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.