home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / modules / edtplug / classes / netscape / plugin / composer / io / SelectedHTMLReader.java < prev    next >
Encoding:
Java Source  |  1998-04-08  |  5.3 KB  |  152 lines

  1. /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. package netscape.plugin.composer.io;
  20.  
  21. import java.io.*;
  22.  
  23. /** A filter that filters out the unselected text from an HTML stream. Use this
  24.  * stream to speeds up applications that only operate on the selected text.
  25.  */
  26.  
  27. public class SelectedHTMLReader extends Reader {
  28.     /** Create a stream that is used for read-only operations.
  29.      * (This method just calls the two-argument constructor with a null
  30.      * output stream.)
  31.      * @param in the stream to read from.
  32.      */
  33.  
  34.     public SelectedHTMLReader(Reader in ) throws IOException {
  35.         this(in, null);
  36.     }
  37.  
  38.     /** Create a stream that is used for read-write operations.
  39.      * If the out parameter is supplied, all the text outside of the
  40.      * HTML selection comments will be automaticly copied to the output stream.
  41.      * <p>
  42.      * Before the constructor returns, all the HTML text before the selection
  43.      * start comment will be copied to the output stream.
  44.      * @param in the stream to read from.
  45.      * @param out the stream to write to. (Can be null).
  46.      * @throws java.io.IOException if there is a problem reading or writing the
  47.      * streams, or if there are no HTML selection comments.
  48.      */
  49.  
  50.     public SelectedHTMLReader(Reader in, Writer out)
  51.         throws IOException
  52.     {
  53.         this.in = in;
  54.         this.out = out;
  55.         // Crude but effective. We copy the input stream into a buffer.
  56.         StringBuffer buffer = new StringBuffer(1024);
  57.         int len;
  58.         char[] temp = new char[1024];
  59.         while((len = in.read(temp)) >= 0){
  60.             buffer.append(temp,0,len);
  61.         }
  62.         // Now search for the beginning comment
  63.         string = new String(buffer);
  64.         String startComment = Comment.createSelectionStart().toString();
  65.         String endComment = Comment.createSelectionEnd().toString();
  66.         int start = string.indexOf(startComment);
  67.         if ( start < 0 ) {
  68.             // See if it's a sticky-after comment
  69.             start = string.indexOf(Comment.createSelectionStart(true).toString());
  70.             if ( start < 0 ) {
  71.                 System.err.println(string);
  72.                 throw new IOException("No start comment.");
  73.             }
  74.         }
  75.         int end = string.indexOf(endComment, start);
  76.         if ( end < 0 ) {
  77.             // See if it's a sticky-after comment
  78.             String stickyEnd = Comment.createSelectionEnd(true).toString();
  79.             end = string.indexOf(stickyEnd, start);
  80.             if ( end < 0 ) {
  81.                 System.err.println(string);
  82.                 throw new IOException("No end comment");
  83.             }
  84.             else {
  85.                 end += stickyEnd.length();
  86.             }
  87.         }
  88.         else {
  89.             end += endComment.length();
  90.         }
  91.  
  92.         this.index = start;
  93.         this.length = end;
  94.  
  95.         if ( out != null ) {
  96.             // Copy the text before the start to the output
  97.             out.write(string, 0, index);
  98.         }
  99.     }
  100.  
  101.     /** Closes the input stream.
  102.      * If an output stream was supplied to the constructor,
  103.      * any HTML text remaining in the input stream will be flushed to the
  104.      * output stream. The output stream will not be closed.
  105.      */
  106.  
  107.     public void close() throws IOException{
  108.         in.close();
  109.         if ( out != null ) {
  110.             // Have to copy any remaining text
  111.             int toWrite = string.length() - index;
  112.             out.write(string, index, toWrite);
  113.             index = string.length();
  114.         }
  115.     }
  116.     public int read() throws IOException {
  117.         if ( index >= length ) {
  118.             return -1;
  119.         }
  120.         return string.charAt(index++);
  121.     }
  122.  
  123.     public int read(char[] buf)  throws  IOException {
  124.         return read(buf, 0, buf.length);
  125.     }
  126.  
  127.     public int read(char[] buf, int start, int length)  throws  IOException {
  128.         if ( length <= 0 ) {
  129.             return 0;
  130.         }
  131.         int trueLength = Math.min(length, this.length - index);
  132.         if ( trueLength <= 0 ){
  133.             return -1;
  134.         }
  135.         string.getChars(index, index + trueLength, buf, start);
  136.         index += trueLength;
  137.         return trueLength;
  138.     }
  139.  
  140.     public long skip(long count) throws  IOException {
  141.         long trueSkip = Math.max(0, Math.min(count, (long) length - index));
  142.         index += (int) trueSkip;
  143.         return trueSkip;
  144.     }
  145.  
  146.     private Reader in;
  147.     private Writer out;
  148.     private int index; // Current position of stream
  149.     private int length; // Index of first character of buffer to not return.
  150.     private String string; // The whole contents of the input stream.
  151. }
  152.