You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by cz...@apache.org on 2002/01/03 14:03:31 UTC

cvs commit: jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/source ResourceSource.java ResourceSourceFactory.java Source.java URLSource.java

cziegeler    02/01/03 05:03:31

  Modified:    src/scratchpad/org/apache/avalon/excalibur/source
                        Source.java URLSource.java
  Added:       src/scratchpad/org/apache/avalon/excalibur/source
                        ResourceSource.java ResourceSourceFactory.java
  Log:
  Added resource protocol, cleaned up Source interface
  
  Revision  Changes    Path
  1.8       +1 -18     jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/source/Source.java
  
  Index: Source.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/source/Source.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- Source.java	17 Dec 2001 10:02:21 -0000	1.7
  +++ Source.java	3 Jan 2002 13:03:31 -0000	1.8
  @@ -7,7 +7,6 @@
    */
   package org.apache.avalon.excalibur.source;
   
  -import org.xml.sax.InputSource;
   import java.io.IOException;
   import java.io.InputStream;
   
  @@ -20,30 +19,14 @@
    *
    * @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
    * @author <a href="mailto:ovidiu@cup.hp.com">Ovidiu Predescu</a>
  - * @version CVS $Revision: 1.7 $ $Date: 2001/12/17 10:02:21 $
  + * @version CVS $Revision: 1.8 $ $Date: 2002/01/03 13:03:31 $
    */
   public interface Source {
   
       /**
  -     * Get the content length of the source or -1 if it
  -     * is not possible to determine the length.
  -     */
  -    long getContentLength();
  -
  -    /**
        * Return an <code>InputStream</code> object to read from the source.
        */
       InputStream getInputStream()
  -    throws IOException;
  -
  -    /**
  -     * Return an <code>InputSource</code> object to read the XML
  -     * content.
  -     *
  -     * @return an <code>InputSource</code> value
  -     * @exception IOException if an error occurs
  -     */
  -    InputSource getInputSource()
       throws IOException;
   
       /**
  
  
  
  1.9       +4 -4      jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/source/URLSource.java
  
  Index: URLSource.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/source/URLSource.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- URLSource.java	17 Dec 2001 10:02:21 -0000	1.8
  +++ URLSource.java	3 Jan 2002 13:03:31 -0000	1.9
  @@ -31,11 +31,11 @@
    * Description of a source which is described by an URL.
    *
    * @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
  - * @version CVS $Revision: 1.8 $ $Date: 2001/12/17 10:02:21 $
  + * @version CVS $Revision: 1.9 $ $Date: 2002/01/03 13:03:31 $
    */
   
  -public final class URLSource 
  -implements ModifiableSource, XMLizable, Monitorable {
  +public final class URLSource
  +implements Composable, ModifiableSource, XMLizable, Monitorable {
   
       /** Identifier for file urls */
       private final String FILE = "file:";
  @@ -124,7 +124,7 @@
       /**
        *  Get the corresponding Resource object for monitoring.
        */
  -    public Resource getResource() 
  +    public Resource getResource()
       throws Exception {
           this.getInfos();
           if (this.isFile == true) {
  
  
  
  1.1                  jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/source/ResourceSource.java
  
  Index: ResourceSource.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.avalon.excalibur.source;
  
  import org.apache.avalon.framework.component.Composable;
  import org.apache.avalon.framework.component.ComponentManager;
  import org.apache.avalon.excalibur.source.validity.TimeStampValidity;
  import org.apache.avalon.excalibur.xml.Parser;
  import org.apache.avalon.excalibur.xml.XMLConsumer;
  import org.apache.avalon.excalibur.xml.XMLizable;
  
  import org.xml.sax.ContentHandler;
  import org.xml.sax.InputSource;
  import org.xml.sax.SAXException;
  
  import java.io.InputStream;
  import java.io.IOException;
  
  /**
   * Description of a source which is described by the resource protocol
   * which gets a resource from the cloassload
   *
   * @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
   * @version CVS $Revision: 1.1 $ $Date: 2002/01/03 13:03:31 $
   */
  
  public final class ResourceSource
  implements Composable, Source, XMLizable
  {
      /** Instantiation time of this class for validity. The resources do not
       *  change until the jars do not change. (Is this correct?) */
      private static SourceValidity initTime = new TimeStampValidity( System.currentTimeMillis() );
  
      /** The system identifier */
      private String systemId;
  
      /** Location of the resource */
      private String location;
  
      /** The ComponentManager needed for streaming */
      private ComponentManager manager;
  
      /**
       * Constructor
       */
      public ResourceSource(String systemId)
      {
          this.systemId = systemId;
          final int pos = systemId.indexOf("://");
          this.location = systemId.substring(pos+3);
      }
  
      public void compose(ComponentManager manager )
      {
          this.manager = manager;
      }
  
      /**
       * Return an <code>InputStream</code> object to read from the source.
       */
      public InputStream getInputStream()
      throws IOException
      {
          ClassLoader loader = Thread.currentThread().getContextClassLoader();
          if (loader == null)
          {
              loader = this.getClass().getClassLoader();
          }
          return loader.getResourceAsStream( this.location );
      }
  
      /**
       * Return an <code>InputSource</code> object to read the XML
       * content.
       *
       * @return an <code>InputSource</code> value
       * @exception IOException if an error occurs
       */
      public InputSource getInputSource()
      throws IOException
      {
          InputSource newObject = new InputSource(this.getInputStream());
          newObject.setSystemId(this.systemId);
          return newObject;
      }
  
      /**
       * Stream content to a content handler or to an XMLConsumer.
       *
       * @throws ResourceNotFoundException if file not found or
       *         HTTP location does not exist.
       * @throws SAXException if failed to parse source document.
       */
      public void toSAX(ContentHandler handler)
      throws SAXException
      {
          Parser parser = null;
          try {
              parser = (Parser)this.manager.lookup(Parser.ROLE);
  
              parser.parse(this.getInputSource(), handler);
          }
          catch (SAXException e)
          {
              // Preserve original exception
              throw e;
          } catch (Exception e)
          {
              throw new SAXException("Exception during processing of "
                                            + this.systemId, e);
          } finally
          {
              if (parser != null) this.manager.release(parser);
          }
      }
  
      /**
       * Return the unique identifer for this source
       */
      public String getSystemId()
      {
          return this.systemId;
      }
  
      /**
       *  Get the Validity object. This can either wrap the last modification
       *  date or the expires information or...
       *  If it is currently not possible to calculate such an information
       *  <code>null</code> is returned.
       */
      public SourceValidity getValidity()
      {
          return initTime;
      }
  
  }
  
  
  
  1.1                  jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/source/ResourceSourceFactory.java
  
  Index: ResourceSourceFactory.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.avalon.excalibur.source;
  
  import org.apache.avalon.framework.component.Component;
  
  import java.io.IOException;
  import java.net.MalformedURLException;
  
  /**
   * A factory for the Resource protocol
   * @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
   * @version $Id: ResourceSourceFactory.java,v 1.1 2002/01/03 13:03:31 cziegeler Exp $
   */
  public class ResourceSourceFactory
  implements SourceFactory {
  
      /**
       * Get a <code>Source</code> object.
       * @param parameters This is optional.
       */
      public Source getSource(String location, SourceParameters parameters)
      throws MalformedURLException, IOException {
          return new ResourceSource(location);
      }
  
  }
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>