Logo
Distributed Genetic Programming Framework
print print

File org.sfc.xml.ReferenceCountedInputSource.java

Here you can find all the information about the file org.sfc.xml.ReferenceCountedInputSource.java. You may explore it here or download it onto your local disk.
/*
 * Copyright (c) 2005 Thomas Weise
 * 
 * E-Mail           : tweise@gmx.de
 * Creation Date    : 2005-07-20 15:36:15
 * Original Filename: org.sfc.xml.ReferenceCountedInputSource.java
 * Version          : 1.0.2
 * Last modification: 2006-03-07
 *                by: Thomas Weise
 * 
 * License          : GNU LESSER GENERAL PUBLIC LICENSE
 *                    Version 2.1, February 1999
 *                    You should have received a copy of this license along
 *                    with this library; if not, write to the Free Software
 *                    Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *                    MA 02111-1307, USA or download the license under
 *                    http://www.gnu.org/copyleft/lesser.html.
 *                    
 * Warranty         : This software is provided "as is" without any
 *                    warranty; without even the implied warranty of
 *                    merchantability or fitness for a particular purpose.
 *                    See the Gnu Lesser General Public License for more
 *                    details.
 */

 
package org.sfc.xml;

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;

import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;

import org.xml.sax.InputSource;

import org.sfc.io.IO;
import org.sfc.io.ReferenceCountedInputStream;
import org.sfc.io.ReferenceCountedReader;
import org.sfc.scoped.AlreadyDisposedException;
import org.sfc.scoped.IReferenceCounted;


/**
 * This class represents an input source that can be released when done
 * with it.
 *
 * @author Thomas Weise
 */

public  final class ReferenceCountedInputSource extends     InputSource
                                                implements  IReferenceCounted,
                                                            Source,
                                                            Result
  {
/**
 * The internal reference counter.
 */

  private   int       m_ref_count ;
/**
 * <code>true</code> as long as this object is alive, <code>false</code> if
 * it died. 
 */

  private   boolean   m_living    ;
  
/**
 * Create a new input source with a system identifier.
 */

  ReferenceCountedInputSource ()
    {
    super();
    this.m_living     = true;
    this.m_ref_count  = 1;
    }
    
    
    
/**
 * Create a new closeable input source that is a copy of an existing one.
 * 
 * @param p_is  The <code>InputSource</code> to be copied.
 */

  public  final void  set_input_source  (final  InputSource  p_is)
    { 
    InputStream l_x;
    Reader      l_y;
    
    l_x = p_is.getByteStream();
    l_y = p_is.getCharacterStream();
    
    if(p_is instanceof ReferenceCountedInputSource)
      {
      if(l_x != null)
        {
        ((ReferenceCountedInputStream)l_x).add_ref();
        }
      if(l_y != null)
        {
        ((ReferenceCountedReader)l_y).add_ref();
        }
      }
    
    this.setByteStream(l_x);
    this.setCharacterStream(l_y);
    this.setEncoding(p_is.getEncoding());
    this.setPublicId(p_is.getPublicId());
    this.setSystemId(p_is.getSystemId());
    }
    
/**
 * Create a new closeable input source that is a copy of an existing
 * stream source.
 * 
 * @param p_is  The <code>StreamSource</code> to be copied.
 */

  public  final void  set_stream_source  (final  StreamSource  p_is)
    {
    InputStream l_x;
    Reader      l_y;
    
    l_x = p_is.getInputStream();
    l_y = p_is.getReader();
    
    if(p_is instanceof ReferenceCountedStreamSource)
      {
      if(l_x != null)
        {
        ((ReferenceCountedInputStream)l_x).add_ref();
        }
      if(l_y != null)
        {
        ((ReferenceCountedReader)l_y).add_ref();
        }
      }
    
    this.setByteStream(l_x);
    this.setCharacterStream(l_y);
    this.setPublicId(p_is.getPublicId());
    this.setSystemId(p_is.getSystemId());
    }
      
/**
 * Set the byte stream for this input source.
 *
 * <p>The SAX parser will ignore this if there is also a character
 * stream specified, but it will use a byte stream in preference
 * to opening a URI connection itself.</p>
 *
 * <p>If the application knows the character encoding of the
 * byte stream, it should set it with the setEncoding method.</p>
 *
 * @param p_byte_stream A byte stream containing an XML document or
 *        other entity.
 * @see java.io.InputStream
 */

  @Override
  public final  void setByteStream (final InputStream p_byte_stream)
    {
    ReferenceCountedInputStream l_x;
    
    l_x = ((ReferenceCountedInputStream)(this.getByteStream()));
    if(l_x != null) l_x.release();
    
    super.setByteStream(IO.get_input_stream(p_byte_stream));  
    }
    
    
    
/**
 * Set the character stream for this input source.
 *
 * <p>If there is a character stream specified, the SAX parser
 * will ignore any byte stream and will not attempt to open
 * a URI connection to the system identifier.</p>
 *
 * @param p_character_stream The character stream containing the
 *        XML document or other entity.
 * @see java.io.Reader
 */

  @Override
  public final  void setCharacterStream (final  Reader p_character_stream)
    {    
    ReferenceCountedReader l_x;
    
    l_x = ((ReferenceCountedReader)(this.getCharacterStream()));
    if(l_x != null) l_x.release();
    
    super.setCharacterStream(IO.get_reader(p_character_stream));
    }
  
/**
 * Increase the reference counter of this object by one.
 * You must call <code>add_ref</code> before you assign a variable to point
 * on this object (except when calling the constructor and storing the
 * result to exactly one variable).
 * @throws AlreadyDisposedException If the object has already been disposed.
 */

  public  final void  add_ref () throws AlreadyDisposedException
    {
    synchronized(this)
      {
      if(this.m_living)
        {
        this.m_ref_count++;
        }
      else
        {
        throw new AlreadyDisposedException();
        }
      }
    }
  
  
/**
 * Decrease the reference counter of this object by one.
 * If the reference count reaches zero, the object will automatically
 * perform cleanup operations. You must call <code>release</code> whenever
 * you don't need a variable pointing to this object anymore.
 * @throws AlreadyDisposedException If the object has already been disposed.
 */

  public  final void  release () throws AlreadyDisposedException
    {
    this.do_release();
    }
  
  
/**
 * Decrease the reference counter of this object by one.
 * If the reference count reaches zero, the object will automatically
 * perform cleanup operations. You must call <code>release</code> whenever
 * you don't need a variable pointing to this object anymore.
 * @throws AlreadyDisposedException If the object has already been disposed.
 * @return  The <code>IOException</code> that was thrown during closing of
 *          the object, or <code>null</code> if everything was ok. 
 */

  final IOException do_release () throws AlreadyDisposedException
    {
    InputStream l_is;
    Reader      l_r;
    IOException l_ioe;
    
    synchronized(this)
      {
      if(this.m_living)
        {        
        if( (--this.m_ref_count) <= 0)
          {
          this.m_living = false;
              
          l_ioe = null;    
          l_is  = getByteStream();
          if(l_is != null)
            {
            try
              {
              l_is.close();        
              }
            catch(IOException l_ioe2)
              {
              l_ioe = l_ioe2;
              }
            finally
              {
              super.setByteStream(null);
              }      
            }
          
          l_r = getCharacterStream();
          if(l_r != null)
            {
            try
              {
              l_r.close();        
              }
            catch(IOException l_ioe2)
              {
              l_ioe = l_ioe2;
              }
            finally
              {
              super.setCharacterStream(null);
              }
            }
          
          return l_ioe;
          }
        }
      else
        {
        throw new AlreadyDisposedException();
        }
      }
    
    return null;
    }
    

/**
 * Cleanup this input source.
 * @throws  Throwable From the super implementation.
 */

    @Override
  protected final void  finalize  ()  throws Throwable
    {
    synchronized(this)
      {
      if(this.m_living)
        {
        this.m_ref_count = 1;
        this.do_release();
        this.m_living    = false;        
        }
      
      this.m_ref_count = 0;
      }
    super.finalize();
    }
  }

File Information:

file name:ReferenceCountedInputSource.java
package:org.sfc.xml
qualified name:org.sfc.xml.ReferenceCountedInputSource.java
file type:Java Source File
download location:download http://dgpf.sourceforge.net/source/org/sfc/xml/ReferenceCountedInputSource.java
size:8.763 KB (8974 B)
uploaded: 2015-07-22 04:11:13 GMT+0000
last update: 2006-03-07 04:20:28 GMT+0000
last access: 2018-01-24 01:24:55 GMT+0000

statistics online since 2006-01-02.   RSS Feed
Contact us by sending an email to tweise@gmx.de to receive further information, to report errors, or to join our project.
All content on this site (http://dgpf.sourceforge.net/) is LGPL-licensed.
http://dgpf.sourceforge.net/scripts/source/source.php last modified at 2015-07-22 04:10:53 GMT+0000 served at 2018-01-24 01:24:55 GMT+0000.
Valid CSS Valid XHTML 1.1
Valid RSS SourceForge.net Logo