home *** CD-ROM | disk | FTP | other *** search
/ Popular Software (Premium Edition) / mycd.iso / INTERNET / NETSCAP4.06 / CP32E406.EXE / netcast.z / ncjava10.jar / netscape / palomar / sgml / SGMLStreamB.class (.txt) < prev    next >
Encoding:
Java Class File  |  1998-02-26  |  5.0 KB  |  256 lines

  1. package netscape.palomar.sgml;
  2.  
  3. import java.io.InputStream;
  4. import java.util.Date;
  5. import netscape.palomar.util.CascadedException;
  6.  
  7. public class SGMLStreamB extends SGMLStream {
  8.    InputStream _inputStreamB;
  9.    byte[] _mainBuffer;
  10.    int _fillMark;
  11.  
  12.    public SGMLStreamB(InputStream nf, SGMLParser parser) throws CascadedException {
  13.       super(parser);
  14.       this._inputStreamB = nf;
  15.       this.loadBuffer();
  16.    }
  17.  
  18.    private boolean loadBuffer() throws CascadedException {
  19.       try {
  20.          if (super._endOfFile) {
  21.             return true;
  22.          } else {
  23.             int addSize = this._inputStreamB.available();
  24.             if (addSize > 0) {
  25.                if (this._mainBuffer == null) {
  26.                   this._mainBuffer = new byte[addSize + 100];
  27.                } else if (this._fillMark + addSize > this._mainBuffer.length) {
  28.                   byte[] oldBuffer = this._mainBuffer;
  29.                   this._mainBuffer = new byte[this._fillMark + addSize + 1000];
  30.                   System.arraycopy(oldBuffer, 0, this._mainBuffer, 0, this._fillMark);
  31.                }
  32.  
  33.                int actualRead = this._inputStreamB.read(this._mainBuffer, this._fillMark, addSize);
  34.                this._fillMark += actualRead;
  35.                return false;
  36.             } else {
  37.                int waitedByte = this._inputStreamB.read();
  38.                if (waitedByte == -1) {
  39.                   return super._endOfFile = true;
  40.                } else {
  41.                   addSize = this._inputStreamB.available();
  42.                   if (this._mainBuffer == null) {
  43.                      this._mainBuffer = new byte[addSize + 100];
  44.                   } else if (this._fillMark + addSize + 1 > this._mainBuffer.length) {
  45.                      byte[] oldBuffer = this._mainBuffer;
  46.                      this._mainBuffer = new byte[this._fillMark + addSize + this._fillMark / 3 + 1000];
  47.                      System.arraycopy(oldBuffer, 0, this._mainBuffer, 0, this._fillMark);
  48.                   }
  49.  
  50.                   this._mainBuffer[this._fillMark++] = (byte)waitedByte;
  51.                   int actualRead = this._inputStreamB.read(this._mainBuffer, this._fillMark, addSize);
  52.                   this._fillMark += actualRead;
  53.                   return false;
  54.                }
  55.             }
  56.          }
  57.       } catch (Exception e) {
  58.          throw new SGMLException(205, e);
  59.       }
  60.    }
  61.  
  62.    private final int indexOf(byte b, int offset) throws CascadedException {
  63.       while(offset < this._fillMark || !this.loadBuffer()) {
  64.          if (this._mainBuffer[offset] == b) {
  65.             return offset;
  66.          }
  67.  
  68.          ++offset;
  69.       }
  70.  
  71.       return -1;
  72.    }
  73.  
  74.    private final int indexOf(byte[] b, int offset) throws CascadedException {
  75.       while(true) {
  76.          int second = 0;
  77.  
  78.          while(this._mainBuffer[offset + second] == b[second]) {
  79.             ++second;
  80.             if (second > b.length) {
  81.                return offset;
  82.             }
  83.  
  84.             if (offset + second >= this._fillMark && this.loadBuffer()) {
  85.                return -1;
  86.             }
  87.          }
  88.  
  89.          ++offset;
  90.       }
  91.    }
  92.  
  93.    private String readComment() throws CascadedException {
  94.       try {
  95.          int scanner = super._linePos + 3;
  96.  
  97.          do {
  98.             scanner = this.indexOf((byte)45, scanner);
  99.             if (scanner < 0) {
  100.                return new String(this._mainBuffer, 0, super._linePos, this._fillMark - super._linePos) + "-->";
  101.             }
  102.  
  103.             scanner += 2;
  104.          } while(this._mainBuffer[scanner - 1] == 45);
  105.  
  106.          scanner = this.indexOf((byte)62, scanner);
  107.          if (scanner < 0) {
  108.             return new String(this._mainBuffer, 0, super._linePos, this._fillMark - super._linePos) + ">";
  109.          } else {
  110.             super._isText = true;
  111.             ++scanner;
  112.             String retval = new String(this._mainBuffer, 0, super._linePos, scanner - super._linePos);
  113.             super._linePos = scanner;
  114.             return retval;
  115.          }
  116.       } catch (Exception e) {
  117.          SGMLException ce = new SGMLException(206, e);
  118.          ((CascadedException)ce).addToken("line", Integer.toString(super._linePos));
  119.          throw ce;
  120.       }
  121.    }
  122.  
  123.    public String readTag() throws CascadedException {
  124.       try {
  125.          if (super._endOfFile) {
  126.             return null;
  127.          } else if (super._isText) {
  128.             SGMLException ce = new SGMLException(207);
  129.             throw ce;
  130.          } else {
  131.             if (this._fillMark < super._linePos + 4) {
  132.                this.loadBuffer();
  133.             }
  134.  
  135.             if (this._fillMark > super._linePos + 3 && this._mainBuffer[super._linePos + 1] == 33 && this._mainBuffer[super._linePos + 1] == 45 && this._mainBuffer[super._linePos + 1] == 45) {
  136.                return this.readComment();
  137.             } else {
  138.                ++super._tagNumber;
  139.                int indexPos = this.indexOf((byte)62, super._linePos);
  140.                if (indexPos < 0) {
  141.                   return null;
  142.                } else {
  143.                   super._isText = true;
  144.                   String retval = new String(this._mainBuffer, 0, super._linePos, indexPos + 1 - super._linePos);
  145.                   super._linePos = indexPos + 1;
  146.                   return retval;
  147.                }
  148.             }
  149.          }
  150.       } catch (Exception e) {
  151.          SGMLException ce = new SGMLException(208, e);
  152.          ((CascadedException)ce).addToken("line", Integer.toString(super._lineNumber));
  153.          throw ce;
  154.       }
  155.    }
  156.  
  157.    public String readText() throws CascadedException {
  158.       try {
  159.          if (super._endOfFile) {
  160.             return null;
  161.          } else if (!super._isText) {
  162.             SGMLException ce = new SGMLException(209);
  163.             throw ce;
  164.          } else {
  165.             int indexPos = this.indexOf((byte)60, super._linePos);
  166.             if (indexPos < 0) {
  167.                indexPos = this._fillMark;
  168.             }
  169.  
  170.             super._isText = false;
  171.             String retVal = new String(this._mainBuffer, 0, super._linePos, indexPos - super._linePos);
  172.             super._linePos = indexPos;
  173.             return retVal;
  174.          }
  175.       } catch (Exception e) {
  176.          SGMLException ce = new SGMLException(210, e);
  177.          ((CascadedException)ce).addToken("line", Integer.toString(super._lineNumber));
  178.          throw ce;
  179.       }
  180.    }
  181.  
  182.    private final int indexOfTag(String upperCaseEndToken) throws CascadedException {
  183.       byte[] angleBracketSlash = new byte[]{60, 47};
  184.       int pos = super._linePos;
  185.       int tokenLen = upperCaseEndToken.length();
  186.  
  187.       while(true) {
  188.          pos = this.indexOf(angleBracketSlash, pos);
  189.          if (pos < 0) {
  190.             return -1;
  191.          }
  192.  
  193.          boolean found = true;
  194.          int scanbase = pos + 1;
  195.  
  196.          for(int j = 1; j < tokenLen; ++j) {
  197.             if (Character.toUpperCase((char)this._mainBuffer[scanbase + j]) != upperCaseEndToken.charAt(j)) {
  198.                found = false;
  199.                break;
  200.             }
  201.          }
  202.  
  203.          if (found) {
  204.             byte followOnByte = this._mainBuffer[scanbase + tokenLen];
  205.             if (followOnByte == 62) {
  206.                return pos;
  207.             }
  208.  
  209.             if (followOnByte == 32) {
  210.                return pos;
  211.             }
  212.  
  213.             if (followOnByte == 9) {
  214.                return pos;
  215.             }
  216.  
  217.             if (followOnByte == 10) {
  218.                return pos;
  219.             }
  220.          }
  221.  
  222.          ++pos;
  223.       }
  224.    }
  225.  
  226.    public String getTextTilToken(String endToken) throws CascadedException {
  227.       try {
  228.          String upperCaseEnd = endToken.toUpperCase();
  229.          int indexPos = this.indexOfTag(upperCaseEnd);
  230.          if (indexPos < 0) {
  231.             indexPos = this._fillMark;
  232.          }
  233.  
  234.          String retVal = new String(this._mainBuffer, 0, super._linePos, indexPos - super._linePos);
  235.          super._linePos = indexPos;
  236.          super._isText = false;
  237.          return retVal;
  238.       } catch (Exception e) {
  239.          SGMLException ce = new SGMLException(211, e);
  240.          ((CascadedException)ce).addToken("tag", endToken);
  241.          throw ce;
  242.       }
  243.    }
  244.  
  245.    public void close() throws CascadedException {
  246.       super._closeTime = new Date();
  247.  
  248.       try {
  249.          this._inputStreamB.close();
  250.       } catch (Exception e) {
  251.          SGMLException ce = new SGMLException(212, e);
  252.          throw ce;
  253.       }
  254.    }
  255. }
  256.