You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by ov...@apache.org on 2002/08/17 03:22:50 UTC

cvs commit: xml-cocoon2/src/java/org/apache/cocoon/components/flow/javascript ListInputStream.java

ovidiu      2002/08/16 18:22:50

  Added:       src/java/org/apache/cocoon/components/flow/javascript
                        ListInputStream.java
  Log:
  Aggregator InputStream to handle all the input streams corresponding
  to the script files. This class keeps track of the number of lines
  each file has, and makes that available to JSErrorReporter, which uses
  it to report errors in the real files.
  
  Revision  Changes    Path
  1.1                  xml-cocoon2/src/java/org/apache/cocoon/components/flow/javascript/ListInputStream.java
  
  Index: ListInputStream.java
  ===================================================================
  package org.apache.cocoon.components.flow.javascript;
  
  import java.io.IOException;
  import java.io.InputStream;
  import java.util.ArrayList;
  import java.util.Enumeration;
  import java.util.List;
  import org.apache.avalon.excalibur.collections.ArrayEnumeration;
  import org.apache.cocoon.ProcessingException;
  import org.apache.cocoon.environment.Source;
  
  /**
   * Maintains a list of {@link org.apache.cocoon.environment.Source}
   * objects to read the input from. This class keeps track of the
   * number of lines in each <code>Source</code> object, so that in case
   * of errors the location is correctly reported.
   *
   * @author <a href="mailto:ovidiu@cup.hp.com">Ovidiu Predescu</a>
   * @since August 15, 2002
   */
  public class ListInputStream extends InputStream
  {
    /**
     * The <code>List</code> of {@link
     * org.apache.cocoon.environment.Source} objects.
     */
    List sourcesList;
  
    /**
     * Location information on each <code>Source</code> object part of
     * <code>sourcesList</code>.
     */
    List sourcesInfo;
  
    /**
     * Indicates whether we read all the streams.
     */
    boolean eof = false;
  
    /**
     * The total number of elements in the <code>sourcesList</code> list.
     */
    private int size;
  
    /**
     * What is the current <code>Source</code> object being read from.
     */
    private int currentIndex;
  
    /**
     * The current <code>Source</code> object being read from.
     */
    protected SourceInfo currentSourceInfo;
  
    /**
     * The current <code>InputStream</code> object being read from.
     */
    private InputStream currentStream;
  
    /**
     * Given a <code>List</code> of {@link
     * org.apache.cocoon.environment.Source} objects, creates a new
     * <code>ListInputStream</code> instance.
     *
     * @param sources a <code>List</code> of {@link
     * org.apache.cocoon.environment.Source}
     */
    public ListInputStream(List sources)
      throws ProcessingException, IOException
    {
      sourcesList = sources;
      currentIndex = 0;
      size = sources.size();
  
      sourcesInfo = new ArrayList(size);
      for (int i = 0; i < size; i++)
        sourcesInfo.add(new SourceInfo((Source)sources.get(i)));
  
      currentSourceInfo = (SourceInfo)sourcesInfo.get(0);
      currentStream = ((Source)sources.get(0)).getInputStream();
    }
  
    /**
     * Simplistic implementation: return the number of number of bytes
     * that can be read only from the current stream, without bothering
     * to check the remaining streams.
     *
     * @return an <code>int</code> value
     */
    public int available()
      throws IOException
    {
      return currentStream.available();
    }
  
    public void close()
      throws IOException
    {
      try {
        for (int i = 0; i < size; i++)
          ((Source)sourcesList.get(i)).getInputStream().close();
      }
      catch (ProcessingException ex) {
        throw new IOException(ex.toString());
      }
      finally {
        currentStream = null;
        eof = true;
      }
    }
  
    public int read()
      throws IOException
    {
      if (eof)
        return -1;
  
      // Read a character from the current stream
      int ch = currentStream.read();
  
      // If we reached the end of the stream, try moving to the next one
      if (ch == -1) {
        currentIndex++;
  
        // If there are no more streams to read, indicate that to our caller
        if (currentIndex == size) {
          eof = true;
          return -1;
        }
  
        currentSourceInfo = (SourceInfo)sourcesInfo.get(currentIndex);
        try {
          currentStream
            = ((Source)sourcesList.get(currentIndex)).getInputStream();
        }
        catch (ProcessingException ex) {
          throw new IOException(ex.toString());
        }
      }
  
      // FIXME: I18N
      if (ch == '\n')
        currentSourceInfo.lineNumbers++;
  
      return ch;
    }
  
    public int read(byte[] array)
      throws IOException
    {
      return read(array, 0, array.length);
    }
  
    public int read(byte[] array, int offset, int len)
      throws IOException
    {
      if (eof)
        return -1;
  
      if (len == 0)
        return 0;
  
      int numberOfCharsRead = 0;
  
      for (int i = offset; numberOfCharsRead < len; numberOfCharsRead++, i++) {
        int ch = read();
        if (ch == -1)
          return numberOfCharsRead;
  
        array[i] = (byte)ch;
      }
  
      return numberOfCharsRead;
    }
  
    public boolean markSupported()
    {
      return false;
    }
  
    /**
     * Return a <code>List</code> of {@link SourceInfo} objects which
     * maintains the line number information of a {@linke
     * org.apache.cocoon.environment.Source} object.
     *
     * @return a <code>List</code> value
     */
    public List getSourceInfo()
    {
      return sourcesInfo;
    }
  }
  
  
  
  

----------------------------------------------------------------------
In case of troubles, e-mail:     webmaster@xml.apache.org
To unsubscribe, e-mail:          cocoon-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: cocoon-cvs-help@xml.apache.org